]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/httpclient.h
181c58b6be8c61689670c00ffc2d3989e656e87f
[user/henk/code/inspircd.git] / src / modules / httpclient.h
1 #include "base.h"
2
3 #ifndef HTTPCLIENT_H__
4 #define HTTPCLIENT_H__
5
6 #include <string>
7 #include <map>
8
9 typedef std::map<std::string,std::string> HeaderMap;
10
11 const char* HTTP_CLIENT_RESPONSE = "HTTPCLIENT_RESPONSE";
12 const char* HTTP_CLIENT_REQUEST = "HTTPCLIENT_REQUEST";
13
14 /** This class represents an outgoing HTTP request
15  */
16 class HTTPClientRequest : public Request
17 {
18  protected:
19         std::string url;
20         InspIRCd *Instance;
21         Module *src;
22         HeaderMap Headers;
23  public:
24         HTTPClientRequest(InspIRCd *Instance, Module *src, Module* target, const std::string &url)
25                 : Request(src, target, HTTP_CLIENT_REQUEST), url(url), Instance(Instance), src(src)
26         {
27                 Headers["User-Agent"] = "InspIRCd (m_http_client.so)";
28                 Headers["Connection"] = "Close";
29                 Headers["Accept"] = "*/*";
30         }
31
32         const std::string &GetURL()
33         {
34                 return url;
35         }
36
37         void AddHeader(std::string &header, std::string &data)
38         {
39                 Headers[header] = data;
40         }
41         
42         void DeleteHeader(std::string &header)
43         {
44                 Headers.erase(header);
45         }
46
47         HeaderMap GetHeaders()
48         {
49                 return Headers;
50         }
51 };
52
53 class HTTPClientResponse : public Request
54 {
55  protected:
56         friend class HTTPSocket;
57         
58         std::string url;
59         std::string data;
60         int response;
61         std::string responsestr;
62         HeaderMap Headers;
63  public:
64         HTTPClientResponse(Module* src, Module* target, std::string &url, int response, std::string responsestr)
65                 : Request(src, target, HTTP_CLIENT_RESPONSE), url(url), response(response), responsestr(responsestr)
66         {
67         }
68         
69         void SetData(const std::string &ndata)
70         {
71                 data = ndata;
72         }
73         
74         void AddHeader(const std::string &header, const std::string &data)
75         {
76                 Headers[header] = data;
77         }
78         
79         const std::string &GetURL()
80         {
81                 return url;
82         }
83         
84         const std::string &GetData()
85         {
86                 return data;
87         }
88         
89         int GetResponse(std::string &str)
90         {
91                 str = responsestr;
92                 return response;
93         }
94         
95         std::string GetHeader(const std::string &header)
96         {
97                 HeaderMap::iterator i = Headers.find(header);
98                 
99                 if (i != Headers.end())
100                         return i->second;
101                 else
102                         return "";
103         }
104 };
105
106 #endif