1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 /* Written by Special (john@yarbbles.com) */
20 #include "httpclient.h"
22 /* $ModDesc: HTTP client service provider */
28 std::string protocol, username, password, domain, request;
32 class HTTPSocket : public InspSocket
36 class ModuleHTTPClient *Mod;
37 HTTPClientRequest req;
38 HTTPClientResponse *response;
40 enum { HTTP_CLOSED, HTTP_REQSENT, HTTP_HEADERS, HTTP_DATA } status;
45 HTTPSocket(InspIRCd *Instance, class ModuleHTTPClient *Mod);
46 virtual ~HTTPSocket();
47 virtual bool DoRequest(HTTPClientRequest *req);
48 virtual bool ParseURL(const std::string &url);
49 virtual void Connect(const std::string &ip);
50 virtual bool OnConnected();
51 virtual bool OnDataReady();
52 virtual void OnClose();
55 class HTTPResolver : public Resolver
60 HTTPResolver(HTTPSocket *socket, InspIRCd *Instance, const string &hostname, bool &cached, Module* me) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), socket(socket)
64 void OnLookupComplete(const string &result, unsigned int ttl, bool cached)
66 socket->Connect(result);
69 void OnError(ResolverError e, const string &errmsg)
75 typedef vector<HTTPSocket*> HTTPList;
77 class ModuleHTTPClient : public Module
82 ModuleHTTPClient(InspIRCd *Me)
87 virtual ~ModuleHTTPClient()
89 for (HTTPList::iterator i = sockets.begin(); i != sockets.end(); i++)
93 virtual Version GetVersion()
95 return Version(1, 0, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
98 void Implements(char* List)
100 List[I_OnRequest] = 1;
103 char* OnRequest(Request *req)
105 HTTPClientRequest *httpreq = (HTTPClientRequest *)req;
106 if (!strcmp(httpreq->GetId(), HTTP_CLIENT_REQUEST))
108 HTTPSocket *sock = new HTTPSocket(ServerInstance, this);
109 sock->DoRequest(httpreq);
116 HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
117 : InspSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
119 this->ClosePending = false;
123 HTTPSocket::~HTTPSocket()
126 for (HTTPList::iterator i = Mod->sockets.begin(); i != Mod->sockets.end(); i++)
130 Mod->sockets.erase(i);
136 bool HTTPSocket::DoRequest(HTTPClientRequest *req)
138 /* Tweak by brain - we take a copy of this,
139 * so that the caller doesnt need to leave
140 * pointers knocking around, less chance of
145 if (!ParseURL(this->req.GetURL()))
148 this->port = url.port;
149 strlcpy(this->host, url.domain.c_str(), MAXBUF);
151 if (!insp_aton(this->host, &this->addy))
154 HTTPResolver* r = new HTTPResolver(this, Server, url.domain, cached, (Module*)Mod);
155 Instance->AddResolver(r, cached);
160 this->Connect(url.domain);
166 bool HTTPSocket::ParseURL(const std::string &iurl)
171 // Tokenize by slashes (protocol:, blank, domain, request..)
172 int pos = 0, pstart = 0, pend = 0;
174 for (; ; pend = url.url.find('/', pstart))
176 string part = url.url.substr(pstart, pend);
182 if (part[part.length()-1] != ':')
184 url.protocol = part.substr(0, part.length() - 1);
190 // User and password (user:pass@)
191 string::size_type aend = part.find('@', 0);
192 if (aend != string::npos)
194 // Technically, it is valid to not have a password (username@domain)
195 string::size_type usrend = part.find(':', 0);
197 if ((usrend != string::npos) && (usrend < aend))
198 url.password = part.substr(usrend + 1, aend);
202 url.username = part.substr(0, usrend);
208 string::size_type dend = part.find(':', aend);
209 if (dend != string::npos)
210 url.port = atoi(part.substr(dend + 1).c_str());
213 url.domain = part.substr(aend + 1, dend);
215 // The rest of the string is the request
216 url.request = url.url.substr(pend);
229 void HTTPSocket::Connect(const string &ip)
231 strlcpy(this->IP, ip.c_str(), MAXBUF);
233 if (!this->DoConnect())
239 bool HTTPSocket::OnConnected()
241 std::string request = "GET " + url.request + " HTTP/1.1\r\n";
243 // Dump headers into the request
244 HeaderMap headers = req.GetHeaders();
246 for (HeaderMap::iterator i = headers.begin(); i != headers.end(); i++)
247 request += i->first + ": " + i->second + "\r\n";
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";
256 this->status = HTTP_REQSENT;
258 return this->Write(request);
261 bool HTTPSocket::OnDataReady()
263 char *data = this->Read();
271 if (this->status < HTTP_DATA)
274 std::string::size_type pos;
276 this->buffer += data;
277 while ((pos = buffer.find("\r\n")) != std::string::npos)
279 line = buffer.substr(0, pos);
280 buffer = buffer.substr(pos + 2);
283 this->status = HTTP_DATA;
284 this->data += this->buffer;
288 // while ((line = buffer.sstrstr(data, "\r\n")) != NULL)
290 // if (strncmp(data, "\r\n", 2) == 0)
292 if (this->status == HTTP_REQSENT)
294 // HTTP reply (HTTP/1.1 200 msg)
295 char const* data = line.c_str();
297 response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, atoi(data), data + 4);
298 this->status = HTTP_HEADERS;
302 if ((pos = line.find(':')) != std::string::npos)
305 // char *hdata = strchr(data, ':');
312 // response->AddHeader(data, hdata + 2);
313 response->AddHeader(line.substr(0, pos), line.substr(pos + 1));
325 void HTTPSocket::OnClose()
328 return; // notification that request failed?
330 response->data = data;
335 class ModuleHTTPClientFactory : public ModuleFactory
338 ModuleHTTPClientFactory()
342 ~ModuleHTTPClientFactory()
346 Module *CreateModule(InspIRCd* Me)
348 return new ModuleHTTPClient(Me);
352 extern "C" void *init_module(void)
354 return new ModuleHTTPClientFactory;