]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/httpclient.h
Rename all the classes in m_httpd to be HttpServer etc,
[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         HTTPClientRequest() : Request(NULL, NULL, HTTP_CLIENT_REQUEST)
33         {
34         }
35
36         const std::string &GetURL()
37         {
38                 return url;
39         }
40
41         void AddHeader(std::string &header, std::string &data)
42         {
43                 Headers[header] = data;
44         }
45         
46         void DeleteHeader(std::string &header)
47         {
48                 Headers.erase(header);
49         }
50
51         HeaderMap GetHeaders()
52         {
53                 return Headers;
54         }
55 };
56
57 class HTTPClientResponse : public Request
58 {
59  protected:
60         friend class HTTPSocket;
61         
62         std::string url;
63         std::string data;
64         int response;
65         std::string responsestr;
66         HeaderMap Headers;
67  public:
68         HTTPClientResponse(Module* src, Module* target, std::string &url, int response, std::string responsestr)
69                 : Request(src, target, HTTP_CLIENT_RESPONSE), url(url), response(response), responsestr(responsestr)
70         {
71         }
72
73         HTTPClientResponse() : Request(NULL, NULL, HTTP_CLIENT_RESPONSE)
74         {
75         }
76
77         void SetData(const std::string &ndata)
78         {
79                 data = ndata;
80         }
81         
82         void AddHeader(const std::string &header, const std::string &data)
83         {
84                 Headers[header] = data;
85         }
86         
87         const std::string &GetURL()
88         {
89                 return url;
90         }
91         
92         const std::string &GetData()
93         {
94                 return data;
95         }
96         
97         int GetResponse(std::string &str)
98         {
99                 str = responsestr;
100                 return response;
101         }
102         
103         std::string GetHeader(const std::string &header)
104         {
105                 HeaderMap::iterator i = Headers.find(header);
106                 
107                 if (i != Headers.end())
108                         return i->second;
109                 else
110                         return "";
111         }
112 };
113
114 #endif