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;
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();
54 class HTTPResolver : public Resolver
59 HTTPResolver(HTTPSocket *socket, InspIRCd *Instance, const string &hostname, bool &cached, Module* me) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), socket(socket)
63 void OnLookupComplete(const string &result, unsigned int ttl, bool cached)
65 socket->Connect(result);
68 void OnError(ResolverError e, const string &errmsg)
74 typedef vector<HTTPSocket*> HTTPList;
76 class ModuleHTTPClient : public Module
81 ModuleHTTPClient(InspIRCd *Me)
86 virtual ~ModuleHTTPClient()
88 for (HTTPList::iterator i = sockets.begin(); i != sockets.end(); i++)
92 virtual Version GetVersion()
94 return Version(1, 0, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
97 void Implements(char* List)
99 List[I_OnRequest] = 1;
102 char* OnRequest(Request *req)
104 HTTPClientRequest *httpreq = (HTTPClientRequest *)req;
105 if (!strcmp(httpreq->GetId(), HTTP_CLIENT_REQUEST))
107 HTTPSocket *sock = new HTTPSocket(ServerInstance, this);
108 sock->DoRequest(httpreq);
115 HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
116 : InspSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
118 this->ClosePending = false;
122 HTTPSocket::~HTTPSocket()
125 for (HTTPList::iterator i = Mod->sockets.begin(); i != Mod->sockets.end(); i++)
129 Mod->sockets.erase(i);
135 bool HTTPSocket::DoRequest(HTTPClientRequest *req)
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
144 if (!ParseURL(this->req.GetURL()))
147 this->port = url.port;
148 strlcpy(this->host, url.domain.c_str(), MAXBUF);
150 if (!insp_aton(this->host, &this->addy))
153 HTTPResolver* r = new HTTPResolver(this, Server, url.domain, cached, (Module*)Mod);
154 Instance->AddResolver(r, cached);
159 this->Connect(url.domain);
165 bool HTTPSocket::ParseURL(const std::string &iurl)
170 // Tokenize by slashes (protocol:, blank, domain, request..)
171 int pos = 0, pstart = 0, pend = 0;
173 for (; ; pend = url.url.find('/', pstart))
175 string part = url.url.substr(pstart, pend);
181 if (part[part.length()-1] != ':')
183 url.protocol = part.substr(0, part.length() - 1);
189 // User and password (user:pass@)
190 string::size_type aend = part.find('@', 0);
191 if (aend != string::npos)
193 // Technically, it is valid to not have a password (username@domain)
194 string::size_type usrend = part.find(':', 0);
196 if ((usrend != string::npos) && (usrend < aend))
197 url.password = part.substr(usrend + 1, aend);
201 url.username = part.substr(0, usrend);
207 string::size_type dend = part.find(':', aend);
208 if (dend != string::npos)
209 url.port = atoi(part.substr(dend + 1).c_str());
212 url.domain = part.substr(aend + 1, dend);
214 // The rest of the string is the request
215 url.request = url.url.substr(pend);
228 void HTTPSocket::Connect(const string &ip)
230 strlcpy(this->IP, ip.c_str(), MAXBUF);
232 if (!this->DoConnect())
234 Server->Log(DEBUG, "Unable to connect HTTPSocket to %s", this->host);
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 // Needs buffering for incomplete reads..
274 if (this->status < HTTP_DATA)
276 while ((lend = strstr(data, "\r\n")) != NULL)
278 if (strncmp(data, "\r\n", 2) == 0)
280 this->status = HTTP_DATA;
286 if (this->status == HTTP_REQSENT)
288 // HTTP reply (HTTP/1.1 200 msg)
290 response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, atoi(data), data + 4);
291 this->status = HTTP_HEADERS;
295 char *hdata = strchr(data, ':');
302 response->AddHeader(data, hdata + 2);
312 void HTTPSocket::OnClose()
316 Server->Log(DEBUG, "HTTP socket closed unexpectedly (no content recieved)");
319 Server->Log(DEBUG, "Got file from HTTP successfully");
320 response->data = data;
325 class ModuleHTTPClientFactory : public ModuleFactory
328 ModuleHTTPClientFactory()
332 ~ModuleHTTPClientFactory()
336 Module *CreateModule(InspIRCd* Me)
338 return new ModuleHTTPClient(Me);
342 extern "C" void *init_module(void)
344 return new ModuleHTTPClientFactory;