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) */
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) : Resolver(Instance, hostname, DNS_QUERY_FORWARD), socket(socket)
63 void OnLookupComplete(const string &result)
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->GetData();
105 HTTPSocket *sock = new HTTPSocket(ServerInstance, this);
106 sock->DoRequest(httpreq);
111 void SendReply(Module *to, HTTPClientResponse *data)
113 Request req((char *) data, this, to);
118 HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
119 : InspSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
121 this->ClosePending = false;
125 HTTPSocket::~HTTPSocket()
128 for (HTTPList::iterator i = Mod->sockets.begin(); i != Mod->sockets.end(); i++)
132 Mod->sockets.erase(i);
138 bool HTTPSocket::DoRequest(HTTPClientRequest *req)
142 if (!ParseURL(req->GetURL()))
145 this->port = url.port;
146 strlcpy(this->host, url.domain.c_str(), MAXBUF);
148 if (!inet_aton(this->host, &this->addy))
150 new HTTPResolver(this, Server, url.domain);
155 this->Connect(url.domain);
161 bool HTTPSocket::ParseURL(const std::string &iurl)
166 // Tokenize by slashes (protocol:, blank, domain, request..)
167 int pos = 0, pstart = 0, pend = 0;
169 for (; ; pend = url.url.find('/', pstart))
171 string part = url.url.substr(pstart, pend);
177 if (part[part.length()-1] != ':')
179 url.protocol = part.substr(0, part.length() - 1);
185 // User and password (user:pass@)
186 string::size_type aend = part.find('@', 0);
187 if (aend != string::npos)
189 // Technically, it is valid to not have a password (username@domain)
190 string::size_type usrend = part.find(':', 0);
192 if ((usrend != string::npos) && (usrend < aend))
193 url.password = part.substr(usrend + 1, aend);
197 url.username = part.substr(0, usrend);
203 string::size_type dend = part.find(':', aend);
204 if (dend != string::npos)
205 url.port = atoi(part.substr(dend + 1).c_str());
208 url.domain = part.substr(aend + 1, dend);
210 // The rest of the string is the request
211 url.request = url.url.substr(pend);
224 void HTTPSocket::Connect(const string &ip)
226 strlcpy(this->IP, ip.c_str(), MAXBUF);
228 if (!this->DoConnect())
230 Server->Log(DEBUG, "Unable to connect HTTPSocket to %s", this->host);
235 bool HTTPSocket::OnConnected()
237 std::string request = "GET " + url.request + " HTTP/1.1\r\n";
239 // Dump headers into the request
240 HeaderMap headers = req->GetHeaders();
242 for (HeaderMap::iterator i = headers.begin(); i != headers.end(); i++)
243 request += i->first + ": " + i->second + "\r\n";
245 // The Host header is required for HTTP 1.1 and isn't known when the request is created; if they didn't overload it
246 // manually, add it here
247 if (headers.find("Host") == headers.end())
248 request += "Host: " + url.domain + "\r\n";
252 this->status = HTTP_REQSENT;
254 return this->Write(request);
257 bool HTTPSocket::OnDataReady()
259 char *data = this->Read();
267 // Needs buffering for incomplete reads..
270 if (this->status < HTTP_DATA)
272 while ((lend = strstr(data, "\r\n")) != NULL)
274 if (strncmp(data, "\r\n", 2) == 0)
276 this->status = HTTP_DATA;
282 if (this->status == HTTP_REQSENT)
284 // HTTP reply (HTTP/1.1 200 msg)
286 response = new HTTPClientResponse(url.url, atoi(data), data + 4);
287 this->status = HTTP_HEADERS;
291 char *hdata = strchr(data, ':');
298 response->AddHeader(data, hdata + 2);
308 void HTTPSocket::OnClose()
312 Server->Log(DEBUG, "HTTP socket closed unexpectedly (no content recieved)");
315 Server->Log(DEBUG, "Got file from HTTP successfully");
316 response->data = data;
317 Mod->SendReply(req->GetSrc(), response);
320 class ModuleHTTPClientFactory : public ModuleFactory
323 ModuleHTTPClientFactory()
327 ~ModuleHTTPClientFactory()
331 Module *CreateModule(InspIRCd* Me)
333 return new ModuleHTTPClient(Me);
337 extern "C" void *init_module(void)
339 return new ModuleHTTPClientFactory;