]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_http_client.cpp
Fix for bug #205 reported by nenolod (modules that erroneously check remote users...
[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 "httpclient.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         std::string buffer;
43
44  public:
45         HTTPSocket(InspIRCd *Instance, class ModuleHTTPClient *Mod);
46         virtual ~HTTPSocket();
47         virtual bool DoRequest(HTTPClientRequest *req);
48         virtual bool ParseURL(const std::string &url);
49         virtual void Connect(const std::string &ip);
50         virtual bool OnConnected();
51         virtual bool OnDataReady();
52         virtual void OnClose();
53 };
54
55 class HTTPResolver : public Resolver
56 {
57  private:
58         HTTPSocket *socket;
59  public:
60         HTTPResolver(HTTPSocket *socket, InspIRCd *Instance, const string &hostname, bool &cached, Module* me) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), socket(socket)
61         {
62                 ServerInstance->Log(DEBUG,"Resolving "+hostname);
63         }
64         
65         void OnLookupComplete(const string &result, unsigned int ttl, bool cached)
66         {
67                 ServerInstance->Log(DEBUG,"Resolver done");
68                 socket->Connect(result);
69         }
70         
71         void OnError(ResolverError e, const string &errmsg)
72         {
73                 ServerInstance->Log(DEBUG,"Resolver error");
74                 delete socket;
75         }
76 };
77
78 typedef vector<HTTPSocket*> HTTPList;
79
80 class ModuleHTTPClient : public Module
81 {
82  public:
83         HTTPList sockets;
84
85         ModuleHTTPClient(InspIRCd *Me)
86                 : Module::Module(Me)
87         {
88         }
89         
90         virtual ~ModuleHTTPClient()
91         {
92                 for (HTTPList::iterator i = sockets.begin(); i != sockets.end(); i++)
93                         delete *i;
94         }
95         
96         virtual Version GetVersion()
97         {
98                 return Version(1, 0, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
99         }
100
101         void Implements(char* List)
102         {
103                 List[I_OnRequest] = 1;
104         }
105
106         char* OnRequest(Request *req)
107         {
108                 HTTPClientRequest *httpreq = (HTTPClientRequest *)req;
109                 if (!strcmp(httpreq->GetId(), HTTP_CLIENT_REQUEST))
110                 {
111                         HTTPSocket *sock = new HTTPSocket(ServerInstance, this);
112                         sock->DoRequest(httpreq);
113                         // No return value
114                 }
115                 return NULL;
116         }
117 };
118
119 HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
120                 : InspSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
121 {
122         this->ClosePending = false;
123         this->port = 80;
124 }
125
126 HTTPSocket::~HTTPSocket()
127 {
128         Close();
129         for (HTTPList::iterator i = Mod->sockets.begin(); i != Mod->sockets.end(); i++)
130         {
131                 if (*i == this)
132                 {
133                         Mod->sockets.erase(i);
134                         break;
135                 }
136         }
137 }
138
139 bool HTTPSocket::DoRequest(HTTPClientRequest *req)
140 {
141         /* Tweak by brain - we take a copy of this,
142          * so that the caller doesnt need to leave
143          * pointers knocking around, less chance of
144          * a memory leak.
145          */
146         this->req = *req;
147
148         Instance->Log(DEBUG,"Request in progress");
149
150         if (!ParseURL(this->req.GetURL()))
151         {
152                 Instance->Log(DEBUG,"Parse failed");
153                 return false;
154         }
155         
156         this->port = url.port;
157         strlcpy(this->host, url.domain.c_str(), MAXBUF);
158
159         in_addr addy1;
160 #ifdef IPV6
161         in6_addr addy2;
162         char buf[MAXBUF];
163         if ((inet_aton(this->host, &addy1) > 0) || (inet_pton(AF_INET6, this->host, &addy2) > 0))
164 #else
165         if (inet_aton(this->host, &addy1) > 0)
166 #endif
167         {
168                 bool cached;
169                 HTTPResolver* r = new HTTPResolver(this, Server, url.domain, cached, (Module*)Mod);
170                 Instance->AddResolver(r, cached);
171                 return true;
172         }
173         else
174         {
175                 this->Connect(url.domain);
176         }
177         
178         return true;
179 }
180
181 bool HTTPSocket::ParseURL(const std::string &iurl)
182 {
183         url.url = iurl;
184         url.port = 80;
185         url.protocol = "http";
186
187         irc::sepstream tokenizer(iurl, '/');
188         
189         for (int p = 0;; p++)
190         {
191                 std::string part = tokenizer.GetToken();
192                 if (part.empty() && tokenizer.StreamEnd())
193                         break;
194                 
195                 if ((p == 0) && (part[part.length() - 1] == ':'))
196                 {
197                         // Protocol ('http:')
198                         url.protocol = part.substr(0, part.length() - 1);
199                 }
200                 else if ((p == 1) && (part.empty()))
201                 {
202                         continue;
203                 }
204                 else if (url.domain.empty())
205                 {
206                         // Domain part: [user[:pass]@]domain[:port]
207                         std::string::size_type usrpos = part.find('@');
208                         if (usrpos != std::string::npos)
209                         {
210                                 // Have a user (and possibly password) part
211                                 std::string::size_type ppos = part.find(':');
212                                 if ((ppos != std::string::npos) && (ppos < usrpos))
213                                 {
214                                         // Have password too
215                                         url.password = part.substr(ppos + 1, usrpos - ppos - 1);
216                                         url.username = part.substr(0, ppos);
217                                 }
218                                 else
219                                 {
220                                         url.username = part.substr(0, usrpos);
221                                 }
222                                 
223                                 part = part.substr(usrpos + 1);
224                         }
225                         
226                         std::string::size_type popos = part.rfind(':');
227                         if (popos != std::string::npos)
228                         {
229                                 url.port = atoi(part.substr(popos + 1).c_str());
230                                 url.domain = part.substr(0, popos);
231                         }
232                         else
233                         {
234                                 url.domain = part;
235                         }
236                 }
237                 else
238                 {
239                         // Request (part of it)..
240                         url.request.append("/");
241                         url.request.append(part);
242                 }
243         }
244         
245         if (url.request.empty())
246                 url.request = "/";
247
248         if ((url.domain.empty()) || (!url.port) || (url.protocol.empty()))
249         {
250                 Instance->Log(DEFAULT, "Invalid URL (%s): Missing required value", iurl.c_str());
251                 return false;
252         }
253         
254         if (url.protocol != "http")
255         {
256                 Instance->Log(DEFAULT, "Invalid URL (%s): Unsupported protocol '%s'", iurl.c_str(), url.protocol.c_str());
257                 return false;
258         }
259         
260         return true;
261 }
262
263 void HTTPSocket::Connect(const string &ip)
264 {
265         strlcpy(this->IP, ip.c_str(), MAXBUF);
266         
267         if (!this->DoConnect())
268         {
269                 delete this;
270         }
271 }
272
273 bool HTTPSocket::OnConnected()
274 {
275         std::string request = "GET " + url.request + " HTTP/1.1\r\n";
276
277         // Dump headers into the request
278         HeaderMap headers = req.GetHeaders();
279         
280         for (HeaderMap::iterator i = headers.begin(); i != headers.end(); i++)
281                 request += i->first + ": " + i->second + "\r\n";
282
283         // The Host header is required for HTTP 1.1 and isn't known when the request is created; if they didn't overload it
284         // manually, add it here
285         if (headers.find("Host") == headers.end())
286                 request += "Host: " + url.domain + "\r\n"; 
287         
288         request += "\r\n";
289         
290         this->status = HTTP_REQSENT;
291         
292         return this->Write(request);
293 }
294
295 bool HTTPSocket::OnDataReady()
296 {
297         char *data = this->Read();
298
299         if (!data)
300         {
301                 this->Close();
302                 return false;
303         }
304
305         if (this->status < HTTP_DATA)
306         {
307                 std::string line;
308                 std::string::size_type pos;
309
310                 this->buffer += data;
311                 while ((pos = buffer.find("\r\n")) != std::string::npos)
312                 {
313                         line = buffer.substr(0, pos);
314                         buffer = buffer.substr(pos + 2);
315                         if (line.empty())
316                         {
317                                 this->status = HTTP_DATA;
318                                 this->data += this->buffer;
319                                 this->buffer = "";
320                                 break;
321                         }
322 //              while ((line = buffer.sstrstr(data, "\r\n")) != NULL)
323 //              {
324 //                      if (strncmp(data, "\r\n", 2) == 0)
325                         
326                         if (this->status == HTTP_REQSENT)
327                         {
328                                 // HTTP reply (HTTP/1.1 200 msg)
329                                 char const* data = line.c_str();
330                                 data += 9;
331                                 response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, atoi(data), data + 4);
332                                 this->status = HTTP_HEADERS;
333                                 continue;
334                         }
335                         
336                         if ((pos = line.find(':')) != std::string::npos)
337                         {
338
339 //                      char *hdata = strchr(data, ':');
340                         
341 //                      if (!hdata)
342 //                              continue;
343                         
344 //                      *hdata = '\0';
345                         
346 //                      response->AddHeader(data, hdata + 2);
347                                 response->AddHeader(line.substr(0, pos), line.substr(pos + 1));
348                         
349 //                      data = lend + 2;
350                         } else
351                                 continue;
352                 }
353         } else {
354                 this->data += data;
355         }
356         return true;
357 }
358
359 void HTTPSocket::OnClose()
360 {
361         if (data.empty())
362                 return; // notification that request failed?
363
364         response->data = data;
365         response->Send();
366         delete response;
367 }
368
369 class ModuleHTTPClientFactory : public ModuleFactory
370 {
371  public:
372         ModuleHTTPClientFactory()
373         {
374         }
375         
376         ~ModuleHTTPClientFactory()
377         {
378         }
379         
380         Module *CreateModule(InspIRCd* Me)
381         {
382                 return new ModuleHTTPClient(Me);
383         }
384 };
385
386 extern "C" void *init_module(void)
387 {
388         return new ModuleHTTPClientFactory;
389 }