]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/httpclient.h
a89de47852040ea0c86d2033fc7e7d38e46be961
[user/henk/code/inspircd.git] / src / modules / httpclient.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "base.h"
15
16 #ifndef HTTPCLIENT_H__
17 #define HTTPCLIENT_H__
18
19 #include <string>
20 #include <map>
21
22 typedef std::map<std::string,std::string> HeaderMap;
23
24 const char* HTTP_CLIENT_RESPONSE = "HTTPCLIENT_RESPONSE";
25 const char* HTTP_CLIENT_REQUEST = "HTTPCLIENT_REQUEST";
26 const char* HTTP_CLIENT_ERROR = "HTTPCLIENT_ERROR";
27
28 /** This class represents an outgoing HTTP request
29  */
30 class HTTPClientRequest : public Request
31 {
32  protected:
33         std::string url;
34         InspIRCd *Instance;
35         Module *src;
36         HeaderMap Headers;
37  public:
38         HTTPClientRequest(InspIRCd *Instance, Module *src, Module* target, const std::string &url)
39                 : Request(src, target, HTTP_CLIENT_REQUEST), url(url), Instance(Instance), src(src)
40         {
41                 Headers["User-Agent"] = "InspIRCd (m_http_client.so)";
42                 Headers["Connection"] = "Close";
43                 Headers["Accept"] = "*/*";
44         }
45
46         HTTPClientRequest() : Request(NULL, NULL, HTTP_CLIENT_REQUEST)
47         {
48         }
49
50         const std::string &GetURL()
51         {
52                 return url;
53         }
54
55         void AddHeader(std::string &header, std::string &data)
56         {
57                 Headers[header] = data;
58         }
59         
60         void DeleteHeader(std::string &header)
61         {
62                 Headers.erase(header);
63         }
64
65         HeaderMap GetHeaders()
66         {
67                 return Headers;
68         }
69 };
70
71 class HTTPClientError : public Request
72 {
73  protected:
74         friend class HTTPSocket;
75         std::string url;
76         int response;
77         std::string responsestr;
78         HeaderMap Headers;
79  public:
80         HTTPClientError(Module* src, Module* target, const std::string &url, int response)
81                 : Request(src, target, HTTP_CLIENT_ERROR), url(url), response(response)
82         {
83         }
84
85         const std::string &GetURL()
86         {
87                 return url;
88         }
89 };
90
91 class HTTPClientResponse : public Request
92 {
93  protected:
94         friend class HTTPSocket;
95         
96         std::string url;
97         std::string data;
98         int response;
99         std::string responsestr;
100         HeaderMap Headers;
101  public:
102         HTTPClientResponse(Module* src, Module* target, std::string &url, int response, std::string responsestr)
103                 : Request(src, target, HTTP_CLIENT_RESPONSE), url(url), response(response), responsestr(responsestr)
104         {
105         }
106
107         HTTPClientResponse() : Request(NULL, NULL, HTTP_CLIENT_RESPONSE)
108         {
109         }
110
111         void SetData(const std::string &ndata)
112         {
113                 data = ndata;
114         }
115         
116         void AddHeader(const std::string &header, const std::string &data)
117         {
118                 Headers[header] = data;
119         }
120         
121         const std::string &GetURL()
122         {
123                 return url;
124         }
125         
126         const std::string &GetData()
127         {
128                 return data;
129         }
130         
131         int GetResponse(std::string &str)
132         {
133                 str = responsestr;
134                 return response;
135         }
136         
137         std::string GetHeader(const std::string &header)
138         {
139                 HeaderMap::iterator i = Headers.find(header);
140                 
141                 if (i != Headers.end())
142                         return i->second;
143                 else
144                         return "";
145         }
146 };
147
148 #endif