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)
62 ServerInstance->Log(DEBUG,"Resolving "+hostname);
65 void OnLookupComplete(const string &result, unsigned int ttl, bool cached)
67 ServerInstance->Log(DEBUG,"Resolver done");
68 socket->Connect(result);
71 void OnError(ResolverError e, const string &errmsg)
73 ServerInstance->Log(DEBUG,"Resolver error");
78 typedef vector<HTTPSocket*> HTTPList;
80 class ModuleHTTPClient : public Module
85 ModuleHTTPClient(InspIRCd *Me)
90 virtual ~ModuleHTTPClient()
92 for (HTTPList::iterator i = sockets.begin(); i != sockets.end(); i++)
96 virtual Version GetVersion()
98 return Version(1, 0, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
101 void Implements(char* List)
103 List[I_OnRequest] = 1;
106 char* OnRequest(Request *req)
108 HTTPClientRequest *httpreq = (HTTPClientRequest *)req;
109 if (!strcmp(httpreq->GetId(), HTTP_CLIENT_REQUEST))
111 HTTPSocket *sock = new HTTPSocket(ServerInstance, this);
112 sock->DoRequest(httpreq);
119 HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
120 : InspSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
122 this->ClosePending = false;
126 HTTPSocket::~HTTPSocket()
129 for (HTTPList::iterator i = Mod->sockets.begin(); i != Mod->sockets.end(); i++)
133 Mod->sockets.erase(i);
139 bool HTTPSocket::DoRequest(HTTPClientRequest *req)
141 /* Tweak by brain - we take a copy of this,
142 * so that the caller doesnt need to leave
143 * pointers knocking around, less chance of
148 Instance->Log(DEBUG,"Request in progress");
150 if (!ParseURL(this->req.GetURL()))
152 Instance->Log(DEBUG,"Parse failed");
156 this->port = url.port;
157 strlcpy(this->host, url.domain.c_str(), MAXBUF);
162 if ((inet_aton(this->host, &addy1) > 0) || (inet_pton(AF_INET6, this->host, &addy2) > 0))
164 if (inet_aton(this->host, &addy1) > 0)
168 HTTPResolver* r = new HTTPResolver(this, Server, url.domain, cached, (Module*)Mod);
169 Instance->AddResolver(r, cached);
174 this->Connect(url.domain);
180 bool HTTPSocket::ParseURL(const std::string &iurl)
184 url.protocol = "http";
186 irc::sepstream tokenizer(iurl, '/');
188 for (int p = 0;; p++)
190 std::string part = tokenizer.GetToken();
191 if (part.empty() && tokenizer.StreamEnd())
194 if ((p == 0) && (part[part.length() - 1] == ':'))
196 // Protocol ('http:')
197 url.protocol = part.substr(0, part.length() - 1);
199 else if ((p == 1) && (part.empty()))
203 else if (url.domain.empty())
205 // Domain part: [user[:pass]@]domain[:port]
206 std::string::size_type usrpos = part.find('@');
207 if (usrpos != std::string::npos)
209 // Have a user (and possibly password) part
210 std::string::size_type ppos = part.find(':');
211 if ((ppos != std::string::npos) && (ppos < usrpos))
214 url.password = part.substr(ppos + 1, usrpos - ppos - 1);
215 url.username = part.substr(0, ppos);
219 url.username = part.substr(0, usrpos);
222 part = part.substr(usrpos + 1);
225 std::string::size_type popos = part.rfind(':');
226 if (popos != std::string::npos)
228 url.port = atoi(part.substr(popos + 1).c_str());
229 url.domain = part.substr(0, popos);
238 // Request (part of it)..
239 url.request.append("/");
240 url.request.append(part);
244 if (url.request.empty())
247 if ((url.domain.empty()) || (!url.port) || (url.protocol.empty()))
249 Instance->Log(DEFAULT, "Invalid URL (%s): Missing required value", iurl.c_str());
253 if (url.protocol != "http")
255 Instance->Log(DEFAULT, "Invalid URL (%s): Unsupported protocol '%s'", iurl.c_str(), url.protocol.c_str());
262 void HTTPSocket::Connect(const string &ip)
264 strlcpy(this->IP, ip.c_str(), MAXBUF);
266 if (!this->DoConnect())
272 bool HTTPSocket::OnConnected()
274 std::string request = "GET " + url.request + " HTTP/1.1\r\n";
276 // Dump headers into the request
277 HeaderMap headers = req.GetHeaders();
279 for (HeaderMap::iterator i = headers.begin(); i != headers.end(); i++)
280 request += i->first + ": " + i->second + "\r\n";
282 // The Host header is required for HTTP 1.1 and isn't known when the request is created; if they didn't overload it
283 // manually, add it here
284 if (headers.find("Host") == headers.end())
285 request += "Host: " + url.domain + "\r\n";
289 this->status = HTTP_REQSENT;
291 return this->Write(request);
294 bool HTTPSocket::OnDataReady()
296 char *data = this->Read();
304 if (this->status < HTTP_DATA)
307 std::string::size_type pos;
309 this->buffer += data;
310 while ((pos = buffer.find("\r\n")) != std::string::npos)
312 line = buffer.substr(0, pos);
313 buffer = buffer.substr(pos + 2);
316 this->status = HTTP_DATA;
317 this->data += this->buffer;
321 // while ((line = buffer.sstrstr(data, "\r\n")) != NULL)
323 // if (strncmp(data, "\r\n", 2) == 0)
325 if (this->status == HTTP_REQSENT)
327 // HTTP reply (HTTP/1.1 200 msg)
328 char const* data = line.c_str();
330 response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, atoi(data), data + 4);
331 this->status = HTTP_HEADERS;
335 if ((pos = line.find(':')) != std::string::npos)
338 // char *hdata = strchr(data, ':');
345 // response->AddHeader(data, hdata + 2);
346 response->AddHeader(line.substr(0, pos), line.substr(pos + 1));
358 void HTTPSocket::OnClose()
361 return; // notification that request failed?
363 response->data = data;
368 class ModuleHTTPClientFactory : public ModuleFactory
371 ModuleHTTPClientFactory()
375 ~ModuleHTTPClientFactory()
379 Module *CreateModule(InspIRCd* Me)
381 return new ModuleHTTPClient(Me);
385 extern "C" void *init_module(void)
387 return new ModuleHTTPClientFactory;