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