]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_http_client.cpp
7e579196539d9f20c2f1fdf05f84fef41c27d6b8
[user/henk/code/inspircd.git] / src / modules / m_http_client.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 /* Written by Special (john@yarbbles.com) */
18
19 #include "inspircd.h"
20 #include "http.h"
21
22 /* $ModDesc: HTTP client service provider */
23
24 class URL
25 {
26  public:
27         std::string url;
28         std::string protocol, username, password, domain, request;
29         int port;
30 };
31
32 class HTTPSocket : public InspSocket
33 {
34  private:
35         InspIRCd *Server;
36         class ModuleHTTPClient *Mod;
37         HTTPClientRequest *req;
38         HTTPClientResponse *response;
39         URL url;
40         enum { HTTP_CLOSED, HTTP_REQSENT, HTTP_HEADERS, HTTP_DATA } status;
41         std::string data;
42
43  public:
44         HTTPSocket(InspIRCd *Instance, class ModuleHTTPClient *Mod);
45         virtual ~HTTPSocket();
46         virtual bool DoRequest(HTTPClientRequest *req);
47         virtual bool ParseURL(const std::string &url);
48         virtual void Connect(const std::string &ip);
49         virtual bool OnConnected();
50         virtual bool OnDataReady();
51         virtual void OnClose();
52 };
53
54 class HTTPResolver : public Resolver
55 {
56  private:
57         HTTPSocket *socket;
58  public:
59         HTTPResolver(HTTPSocket *socket, InspIRCd *Instance, const string &hostname) : Resolver(Instance, hostname, DNS_QUERY_FORWARD), socket(socket)
60         {
61         }
62         
63         void OnLookupComplete(const string &result)
64         {
65                 socket->Connect(result);
66         }
67         
68         void OnError(ResolverError e, const string &errmsg)
69         {
70                 delete socket;
71         }
72 };
73
74 typedef vector<HTTPSocket*> HTTPList;
75
76 class ModuleHTTPClient : public Module
77 {
78  public:
79         HTTPList sockets;
80
81         ModuleHTTPClient(InspIRCd *Me)
82                 : Module::Module(Me)
83         {
84         }
85         
86         virtual ~ModuleHTTPClient()
87         {
88                 for (HTTPList::iterator i = sockets.begin(); i != sockets.end(); i++)
89                         delete *i;
90         }
91         
92         virtual Version GetVersion()
93         {
94                 return Version(1, 0, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
95         }
96
97         void Implements(char* List)
98         {
99                 List[I_OnRequest] = 1;
100         }
101         
102         char *OnRequest(Request *req)
103         {
104                 HTTPClientRequest *httpreq = (HTTPClientRequest *) req->GetData();
105                 HTTPSocket *sock = new HTTPSocket(ServerInstance, this);
106                 sock->DoRequest(httpreq);
107                 // No return value
108                 return NULL;
109         }
110         
111         void SendReply(Module *to, HTTPClientResponse *data)
112         {
113                 Request req((char *) data, this, to);
114                 req.Send();
115         }
116 };
117
118 HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
119                 : InspSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
120 {
121         this->ClosePending = false;
122         this->port = 80;
123 }
124
125 HTTPSocket::~HTTPSocket()
126 {
127         Close();
128         for (HTTPList::iterator i = Mod->sockets.begin(); i != Mod->sockets.end(); i++)
129         {
130                 if (*i == this)
131                 {
132                         Mod->sockets.erase(i);
133                         break;
134                 }
135         }
136 }
137
138 bool HTTPSocket::DoRequest(HTTPClientRequest *req)
139 {
140         this->req = req;
141         
142         if (!ParseURL(req->GetURL()))
143                 return false;
144         
145         this->port = url.port;
146         strlcpy(this->host, url.domain.c_str(), MAXBUF);
147
148         if (!inet_aton(this->host, &this->addy))
149         {
150                 new HTTPResolver(this, Server, url.domain);
151                 return true;
152         }
153         else
154         {
155                 this->Connect(url.domain);
156         }
157         
158         return true;
159 }
160
161 bool HTTPSocket::ParseURL(const std::string &iurl)
162 {
163         url.url = iurl;
164         url.port = 80;
165         
166         // Tokenize by slashes (protocol:, blank, domain, request..)
167         int pos = 0, pstart = 0, pend = 0;
168         
169         for (; ; pend = url.url.find('/', pstart))
170         {
171                 string part = url.url.substr(pstart, pend);
172
173                 switch (pos)
174                 {
175                         case 0:
176                                 // Protocol
177                                 if (part[part.length()-1] != ':')
178                                         return false;
179                                 url.protocol = part.substr(0, part.length() - 1);
180                                 break;
181                         case 1:
182                                 // Empty, skip
183                                 break;
184                         case 2:
185                                 // User and password (user:pass@)
186                                 string::size_type aend = part.find('@', 0);
187                                 if (aend != string::npos)
188                                 {
189                                         // Technically, it is valid to not have a password (username@domain)
190                                         string::size_type usrend = part.find(':', 0);
191                                         
192                                         if ((usrend != string::npos) && (usrend < aend))
193                                                 url.password = part.substr(usrend + 1, aend);
194                                         else
195                                                 usrend = aend;
196                                         
197                                         url.username = part.substr(0, usrend);
198                                 }
199                                 else
200                                         aend = 0;
201                                 
202                                 // Port (:port)
203                                 string::size_type dend = part.find(':', aend);
204                                 if (dend != string::npos)
205                                         url.port = atoi(part.substr(dend + 1).c_str());
206
207                                 // Domain
208                                 url.domain = part.substr(aend + 1, dend);
209                                 
210                                 // The rest of the string is the request
211                                 url.request = url.url.substr(pend);
212                                 break;
213                 }
214                 
215                 if (pos++ == 2)
216                         break;
217
218                 pstart = pend + 1;
219         }
220         
221         return true;
222 }
223
224 void HTTPSocket::Connect(const string &ip)
225 {
226         strlcpy(this->IP, ip.c_str(), MAXBUF);
227         
228         if (!this->DoConnect())
229         {
230                 Server->Log(DEBUG, "Unable to connect HTTPSocket to %s", this->host);
231                 delete this;
232         }
233 }
234
235 bool HTTPSocket::OnConnected()
236 {
237         std::string request = "GET " + url.request + " HTTP/1.1\r\n";
238
239         // Dump headers into the request
240         HeaderMap headers = req->GetHeaders();
241         
242         for (HeaderMap::iterator i = headers.begin(); i != headers.end(); i++)
243                 request += i->first + ": " + i->second + "\r\n";
244
245         // The Host header is required for HTTP 1.1 and isn't known when the request is created; if they didn't overload it
246         // manually, add it here
247         if (headers.find("Host") == headers.end())
248                 request += "Host: " + url.domain + "\r\n"; 
249         
250         request += "\r\n";
251         
252         this->status = HTTP_REQSENT;
253         
254         return this->Write(request);
255 }
256
257 bool HTTPSocket::OnDataReady()
258 {
259         char *data = this->Read();
260
261         if (!data)
262         {
263                 this->Close();
264                 return false;
265         }
266         
267         // Needs buffering for incomplete reads..
268         char *lend;
269         
270         if (this->status < HTTP_DATA)
271         {
272                 while ((lend = strstr(data, "\r\n")) != NULL)
273                 {
274                         if (strncmp(data, "\r\n", 2) == 0)
275                         {
276                                 this->status = HTTP_DATA;
277                                 break;
278                         }
279                         
280                         *lend = '\0';
281                         
282                         if (this->status == HTTP_REQSENT)
283                         {
284                                 // HTTP reply (HTTP/1.1 200 msg)
285                                 data += 9;
286                                 response = new HTTPClientResponse(url.url, atoi(data), data + 4);
287                                 this->status = HTTP_HEADERS;
288                                 continue;
289                         }
290                         
291                         char *hdata = strchr(data, ':');
292                         
293                         if (!hdata)
294                                 continue;
295                         
296                         *hdata = '\0';
297                         
298                         response->AddHeader(data, hdata + 2);
299                         
300                         data = lend + 2;
301                 }
302         }
303         
304         this->data += data;
305         return true;
306 }
307
308 void HTTPSocket::OnClose()
309 {
310         if (!data.length())
311         {
312                 Server->Log(DEBUG, "HTTP socket closed unexpectedly (no content recieved)");
313                 return;
314         }
315         Server->Log(DEBUG, "Got file from HTTP successfully");
316         response->data = data;
317         Mod->SendReply(req->GetSrc(), response);
318 }
319
320 class ModuleHTTPClientFactory : public ModuleFactory
321 {
322  public:
323         ModuleHTTPClientFactory()
324         {
325         }
326         
327         ~ModuleHTTPClientFactory()
328         {
329         }
330         
331         Module *CreateModule(InspIRCd* Me)
332         {
333                 return new ModuleHTTPClient(Me);
334         }
335 };
336
337 extern "C" void *init_module(void)
338 {
339         return new ModuleHTTPClientFactory;
340 }
341
342