]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_http_client.cpp
NOTE: our stuff for parsing multiple dns replies for dnsbl with an 'A record reply...
[user/henk/code/inspircd.git] / src / modules / m_http_client.cpp
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 "inspircd.h"
15 #include "httpclient.h"
16
17 /* $ModDesc: HTTP client service provider */
18
19 class URL
20 {
21  public:
22         std::string url;
23         std::string protocol, username, password, domain, request;
24         int port;
25 };
26
27 class HTTPSocket : public BufferedSocket
28 {
29  private:
30         InspIRCd *Server;
31         class ModuleHTTPClient *Mod;
32         HTTPClientRequest req;
33         HTTPClientResponse *response;
34         URL url;
35         enum { HTTP_CLOSED, HTTP_REQSENT, HTTP_HEADERS, HTTP_DATA } status;
36         std::string data;
37         std::string buffer;
38         bool closed;
39
40  public:
41         HTTPSocket(InspIRCd *Instance, class ModuleHTTPClient *Mod);
42         virtual ~HTTPSocket();
43         virtual bool DoRequest(HTTPClientRequest *req);
44         virtual bool ParseURL(const std::string &url);
45         virtual void Connect(const std::string &ip);
46         virtual bool OnConnected();
47         virtual bool OnDataReady();
48         virtual void OnClose();
49 };
50
51 class HTTPResolver : public Resolver
52 {
53  private:
54         HTTPSocket *socket;
55         std::string orig;
56  public:
57         HTTPResolver(HTTPSocket *s, InspIRCd *Instance, const string &hostname, bool &cached, Module* me) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), socket(s)
58         {
59                 ServerInstance->Log(DEBUG,">>>>>>>>>>>>>>>>>> HTTPResolver::HTTPResolver <<<<<<<<<<<<<<<");
60                 orig = hostname;
61         }
62         
63         void OnLookupComplete(const string &result, unsigned int ttl, bool cached, int resultnum = 0)
64         {
65                 ServerInstance->Log(DEBUG,"************* HTTPResolver::OnLookupComplete ***************");
66                 if (!resultnum)
67                         socket->Connect(result);
68                 else
69                         socket->OnClose();
70         }
71         
72         void OnError(ResolverError e, const string &errmsg)
73         {
74                 ServerInstance->Log(DEBUG,"!!!!!!!!!!!!!!!! HTTPResolver::OnError: %s", errmsg.c_str());
75                 socket->OnClose();
76         }
77 };
78
79 typedef vector<HTTPSocket*> HTTPList;
80
81 class ModuleHTTPClient : public Module
82 {
83  public:
84         HTTPList sockets;
85
86         ModuleHTTPClient(InspIRCd *Me)
87                 : Module(Me)
88         {
89                 Implementation eventlist[] = { I_OnRequest };
90                 ServerInstance->Modules->Attach(eventlist, this, 1);
91         }
92         
93         virtual ~ModuleHTTPClient()
94         {
95                 for (HTTPList::iterator i = sockets.begin(); i != sockets.end(); i++)
96                         (*i)->Close();
97                 ServerInstance->BufferedSocketCull();
98         }
99         
100         virtual Version GetVersion()
101         {
102                 return Version(1, 0, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
103         }
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                 : BufferedSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
121 {
122         Instance->Log(DEBUG,"HTTPSocket::HTTPSocket");
123         this->port = 80;
124         response = NULL;
125         closed = false;
126 }
127
128 HTTPSocket::~HTTPSocket()
129 {
130         Close();
131         for (HTTPList::iterator i = Mod->sockets.begin(); i != Mod->sockets.end(); i++)
132         {
133                 if (*i == this)
134                 {
135                         Mod->sockets.erase(i);
136                         break;
137                 }
138         }
139 }
140
141 bool HTTPSocket::DoRequest(HTTPClientRequest *req)
142 {
143         Instance->Log(DEBUG,"HTTPSocket::DoRequest");
144         /* Tweak by brain - we take a copy of this,
145          * so that the caller doesnt need to leave
146          * pointers knocking around, less chance of
147          * a memory leak.
148          */
149         this->req = *req;
150
151         if (!ParseURL(this->req.GetURL()))
152                 return false;
153         
154         this->port = url.port;
155         strlcpy(this->host, url.domain.c_str(), MAXBUF);
156
157         Instance->Log(DEBUG,"Doing request for %s", url.url.c_str());
158
159         in6_addr s6;
160         in_addr s4;
161         /* Doesnt look like an ipv4 or an ipv6 address */
162         if ((inet_pton(AF_INET6, url.domain.c_str(), &s6) < 1) && (inet_pton(AF_INET, url.domain.c_str(), &s4) < 1))
163         {
164                 bool cached;
165                 HTTPResolver* r = new HTTPResolver(this, Server, url.domain, cached, (Module*)Mod);
166                 Instance->AddResolver(r, cached);
167                 Instance->Log(DEBUG,"Resolver added, cached=%d", cached);
168         }
169         else
170                 Connect(url.domain);
171         
172         return true;
173 }
174
175 bool HTTPSocket::ParseURL(const std::string &iurl)
176 {
177         Instance->Log(DEBUG,"HTTPSocket::ParseURL %s", iurl.c_str());
178         url.url = iurl;
179         url.port = 80;
180         url.protocol = "http";
181
182         irc::sepstream tokenizer(iurl, '/');
183         
184         for (int p = 0;; p++)
185         {
186                 std::string part;
187                 if (!tokenizer.GetToken(part))
188                         break;
189
190                 if ((p == 0) && (part[part.length() - 1] == ':'))
191                 {
192                         // Protocol ('http:')
193                         url.protocol = part.substr(0, part.length() - 1);
194                 }
195                 else if ((p == 1) && (part.empty()))
196                 {
197                         continue;
198                 }
199                 else if (url.domain.empty())
200                 {
201                         // Domain part: [user[:pass]@]domain[:port]
202                         std::string::size_type usrpos = part.find('@');
203                         if (usrpos != std::string::npos)
204                         {
205                                 // Have a user (and possibly password) part
206                                 std::string::size_type ppos = part.find(':');
207                                 if ((ppos != std::string::npos) && (ppos < usrpos))
208                                 {
209                                         // Have password too
210                                         url.password = part.substr(ppos + 1, usrpos - ppos - 1);
211                                         url.username = part.substr(0, ppos);
212                                 }
213                                 else
214                                 {
215                                         url.username = part.substr(0, usrpos);
216                                 }
217                                 
218                                 part = part.substr(usrpos + 1);
219                         }
220                         
221                         std::string::size_type popos = part.rfind(':');
222                         if (popos != std::string::npos)
223                         {
224                                 url.port = atoi(part.substr(popos + 1).c_str());
225                                 url.domain = part.substr(0, popos);
226                         }
227                         else
228                         {
229                                 url.domain = part;
230                         }
231                 }
232                 else
233                 {
234                         // Request (part of it)..
235                         url.request.append("/");
236                         url.request.append(part);
237                 }
238         }
239         
240         if (url.request.empty())
241                 url.request = "/";
242
243         if ((url.domain.empty()) || (!url.port) || (url.protocol.empty()))
244         {
245                 Instance->Log(DEFAULT, "Invalid URL (%s): Missing required value", iurl.c_str());
246                 return false;
247         }
248         
249         if (url.protocol != "http")
250         {
251                 Instance->Log(DEFAULT, "Invalid URL (%s): Unsupported protocol '%s'", iurl.c_str(), url.protocol.c_str());
252                 return false;
253         }
254         
255         return true;
256 }
257
258 void HTTPSocket::Connect(const string &ip)
259 {
260         this->response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, 0, "");
261
262         Instance->Log(DEBUG,"HTTPSocket::Connect(%s) response=%08lx", ip.c_str(), response);
263         strlcpy(this->IP, ip.c_str(), MAXBUF);
264         strlcpy(this->host, ip.c_str(), MAXBUF);
265
266         if (!this->DoConnect())
267         {
268                 Instance->Log(DEBUG,"DoConnect failed, bailing");
269                 this->Close();
270         }
271 }
272
273 bool HTTPSocket::OnConnected()
274 {
275         Instance->Log(DEBUG,"HTTPSocket::OnConnected");
276
277         std::string request = "GET " + url.request + " HTTP/1.1\r\n";
278
279         // Dump headers into the request
280         HeaderMap headers = req.GetHeaders();
281         
282         for (HeaderMap::iterator i = headers.begin(); i != headers.end(); i++)
283                 request += i->first + ": " + i->second + "\r\n";
284
285         // The Host header is required for HTTP 1.1 and isn't known when the request is created; if they didn't overload it
286         // manually, add it here
287         if (headers.find("Host") == headers.end())
288                 request += "Host: " + url.domain + "\r\n"; 
289         
290         request += "\r\n";
291         
292         this->status = HTTP_REQSENT;
293         
294         return this->Write(request);
295 }
296
297 bool HTTPSocket::OnDataReady()
298 {
299         Instance->Log(DEBUG,"HTTPSocket::OnDataReady()");
300         char *data = this->Read();
301
302         if (!data)
303                 return false;
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.clear();
320                                 break;
321                         }
322
323                         if (this->status == HTTP_REQSENT)
324                         {
325                                 // HTTP reply (HTTP/1.1 200 msg)
326                                 char const* data = line.c_str();
327                                 data += 9;
328                                 response->SetResponse(data);
329                                 response->SetData(data + 4);
330                                 this->status = HTTP_HEADERS;
331                                 continue;
332                         }
333                         
334                         if ((pos = line.find(':')) != std::string::npos)
335                         {
336                                 response->AddHeader(line.substr(0, pos), line.substr(pos + 1));
337                         }
338                         else
339                         {
340                                 continue;
341                         }
342                 }
343         }
344         else
345         {
346                 this->data += data;
347         }
348         return true;
349 }
350
351 void HTTPSocket::OnClose()
352 {
353         if (!closed)
354         {
355                 closed = true;
356                 Instance->Log(DEBUG,"HTTPSocket::OnClose response=%08lx", response);
357                 std::string e;
358                 if (data.empty())
359                         {
360                         Instance->Log(DEBUG,"Send error");
361                         HTTPClientError* err = new HTTPClientError((Module*)Mod, req.GetSource(), req.GetURL(), 0);
362                         err->Send();
363                         delete err;
364                         return;
365                 }
366
367                 Instance->Log(DEBUG,"Set data and send, %s", response->GetURL().c_str());
368                 response->SetData(data);
369                 response->Send();
370                 delete response;
371         }
372 }
373
374 MODULE_INIT(ModuleHTTPClient)
375