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