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