]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_http_client.cpp
58ceb9e093c963fe44d4343738c326dacf8f7fbd
[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         std::string orig;
55  public:
56         HTTPResolver(HTTPSocket *socket, InspIRCd *Instance, const string &hostname, bool &cached, Module* me) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), socket(socket)
57         {
58                 ServerInstance->Log(DEBUG,"HTTPResolver::HTTPResolver");
59                 orig = hostname;
60         }
61         
62         void OnLookupComplete(const string &result, unsigned int ttl, bool cached, int resultnum = 0)
63         {
64                 ServerInstance->Log(DEBUG,"HTTPResolver::OnLookupComplete");
65                 if (!resultnum)
66                         socket->Connect(result);
67                 else
68                         socket->OnClose();
69         }
70         
71         void OnError(ResolverError e, const string &errmsg)
72         {
73                 ServerInstance->Log(DEBUG,"HTTPResolver::OnError");
74                 socket->OnClose();
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(Me)
87         {
88                 Implementation eventlist[] = { I_OnRequest };
89                 ServerInstance->Modules->Attach(eventlist, this, 1);
90         }
91         
92         virtual ~ModuleHTTPClient()
93         {
94                 for (HTTPList::iterator i = sockets.begin(); i != sockets.end(); i++)
95                         (*i)->Close();
96                 ServerInstance->BufferedSocketCull();
97         }
98         
99         virtual Version GetVersion()
100         {
101                 return Version(1, 0, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
102         }
103
104
105         char* OnRequest(Request *req)
106         {
107                 HTTPClientRequest *httpreq = (HTTPClientRequest *)req;
108                 if (!strcmp(httpreq->GetId(), HTTP_CLIENT_REQUEST))
109                 {
110                         HTTPSocket *sock = new HTTPSocket(ServerInstance, this);
111                         sock->DoRequest(httpreq);
112                         // No return value
113                 }
114                 return NULL;
115         }
116 };
117
118 HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
119                 : BufferedSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
120 {
121         Instance->Log(DEBUG,"HTTPSocket::HTTPSocket");
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         Instance->Log(DEBUG,"HTTPSocket::DoRequest");
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         if (!ParseURL(this->req.GetURL()))
149                 return false;
150         
151         this->port = url.port;
152         strlcpy(this->host, url.domain.c_str(), MAXBUF);
153
154         Instance->Log(DEBUG,"Doing request for %s", url.url.c_str());
155
156         in6_addr s6;
157         in_addr s4;
158         /* Doesnt look like an ipv4 or an ipv6 address */
159         if ((inet_pton(AF_INET6, url.domain.c_str(), &s6) < 1) && (inet_pton(AF_INET, url.domain.c_str(), &s4) < 1))
160         {
161                 bool cached;
162                 HTTPResolver* r = new HTTPResolver(this, Server, url.domain, cached, (Module*)Mod);
163                 Instance->AddResolver(r, cached);
164         }
165         else
166                 Connect(url.domain);
167         
168         return true;
169 }
170
171 bool HTTPSocket::ParseURL(const std::string &iurl)
172 {
173         Instance->Log(DEBUG,"HTTPSocket::ParseURL");
174         url.url = iurl;
175         url.port = 80;
176         url.protocol = "http";
177
178         irc::sepstream tokenizer(iurl, '/');
179         
180         for (int p = 0;; p++)
181         {
182                 std::string part;
183                 if (!tokenizer.GetToken(part))
184                         break;
185
186                 if ((p == 0) && (part[part.length() - 1] == ':'))
187                 {
188                         // Protocol ('http:')
189                         url.protocol = part.substr(0, part.length() - 1);
190                 }
191                 else if ((p == 1) && (part.empty()))
192                 {
193                         continue;
194                 }
195                 else if (url.domain.empty())
196                 {
197                         // Domain part: [user[:pass]@]domain[:port]
198                         std::string::size_type usrpos = part.find('@');
199                         if (usrpos != std::string::npos)
200                         {
201                                 // Have a user (and possibly password) part
202                                 std::string::size_type ppos = part.find(':');
203                                 if ((ppos != std::string::npos) && (ppos < usrpos))
204                                 {
205                                         // Have password too
206                                         url.password = part.substr(ppos + 1, usrpos - ppos - 1);
207                                         url.username = part.substr(0, ppos);
208                                 }
209                                 else
210                                 {
211                                         url.username = part.substr(0, usrpos);
212                                 }
213                                 
214                                 part = part.substr(usrpos + 1);
215                         }
216                         
217                         std::string::size_type popos = part.rfind(':');
218                         if (popos != std::string::npos)
219                         {
220                                 url.port = atoi(part.substr(popos + 1).c_str());
221                                 url.domain = part.substr(0, popos);
222                         }
223                         else
224                         {
225                                 url.domain = part;
226                         }
227                 }
228                 else
229                 {
230                         // Request (part of it)..
231                         url.request.append("/");
232                         url.request.append(part);
233                 }
234         }
235         
236         if (url.request.empty())
237                 url.request = "/";
238
239         if ((url.domain.empty()) || (!url.port) || (url.protocol.empty()))
240         {
241                 Instance->Log(DEFAULT, "Invalid URL (%s): Missing required value", iurl.c_str());
242                 return false;
243         }
244         
245         if (url.protocol != "http")
246         {
247                 Instance->Log(DEFAULT, "Invalid URL (%s): Unsupported protocol '%s'", iurl.c_str(), url.protocol.c_str());
248                 return false;
249         }
250         
251         return true;
252 }
253
254 void HTTPSocket::Connect(const string &ip)
255 {
256         Instance->Log(DEBUG,"HTTPSocket::Connect(%s)", ip.c_str());
257         strlcpy(this->IP, ip.c_str(), MAXBUF);
258         strlcpy(this->host, ip.c_str(), MAXBUF);
259
260         if (!this->DoConnect())
261                 this->Close();
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 || !*data)
292                 return false;
293
294         if (this->status < HTTP_DATA)
295         {
296                 std::string line;
297                 std::string::size_type pos;
298
299                 this->buffer += data;
300                 while ((pos = buffer.find("\r\n")) != std::string::npos)
301                 {
302                         line = buffer.substr(0, pos);
303                         buffer = buffer.substr(pos + 2);
304                         if (line.empty())
305                         {
306                                 this->status = HTTP_DATA;
307                                 this->data += this->buffer;
308                                 this->buffer.clear();
309                                 break;
310                         }
311
312                         if (this->status == HTTP_REQSENT)
313                         {
314                                 // HTTP reply (HTTP/1.1 200 msg)
315                                 char const* data = line.c_str();
316                                 data += 9;
317                                 response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, atoi(data), data + 4);
318                                 this->status = HTTP_HEADERS;
319                                 continue;
320                         }
321                         
322                         if ((pos = line.find(':')) != std::string::npos)
323                         {
324                                 response->AddHeader(line.substr(0, pos), line.substr(pos + 1));
325                         }
326                         else
327                         {
328                                 continue;
329                         }
330                 }
331         }
332         else
333         {
334                 this->data += data;
335         }
336         return true;
337 }
338
339 void HTTPSocket::OnClose()
340 {
341         Instance->Log(DEBUG,"HTTPSocket::OnClose");
342         if (data.empty())
343         {
344                 HTTPClientError* err = new HTTPClientError((Module*)Mod, req.GetSource(), req.GetURL(), 0);
345                 err->Send();
346                 delete err;
347                 return;
348         }
349
350         Instance->Log(DEBUG,"Set data and send");
351         response->data = data;
352         response->Send();
353         delete response;
354 }
355
356 MODULE_INIT(ModuleHTTPClient)
357