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