* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
- * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- * E-mail:
- * <brain@chatspike.net>
- * <Craig@chatspike.net>
- *
- * Written by Craig Edwards, Craig McLure, and others.
+ * InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
* This program is free but copyrighted software; see
* the file COPYING for details.
*
* ---------------------------------------------------
*/
-/* Written by Special (john@yarbbles.com) */
-
#include "inspircd.h"
#include "httpclient.h"
int port;
};
-class HTTPSocket : public InspSocket
+class HTTPSocket : public BufferedSocket
{
private:
InspIRCd *Server;
enum { HTTP_CLOSED, HTTP_REQSENT, HTTP_HEADERS, HTTP_DATA } status;
std::string data;
std::string buffer;
+ bool closed;
public:
HTTPSocket(InspIRCd *Instance, class ModuleHTTPClient *Mod);
{
private:
HTTPSocket *socket;
+ std::string orig;
public:
- HTTPResolver(HTTPSocket *socket, InspIRCd *Instance, const string &hostname, bool &cached, Module* me) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), socket(socket)
+ HTTPResolver(HTTPSocket *s, InspIRCd *Instance, const std::string &hostname, bool &cached, Module* me) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), socket(s)
{
- ServerInstance->Log(DEBUG,"Resolving "+hostname);
+ ServerInstance->Log(DEBUG,">>>>>>>>>>>>>>>>>> HTTPResolver::HTTPResolver <<<<<<<<<<<<<<<");
+ orig = hostname;
}
- void OnLookupComplete(const string &result, unsigned int ttl, bool cached)
+ void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
{
- ServerInstance->Log(DEBUG,"Resolver done");
- socket->Connect(result);
+ ServerInstance->Log(DEBUG,"************* HTTPResolver::OnLookupComplete ***************");
+ if (!resultnum)
+ socket->Connect(result);
+ else
+ socket->OnClose();
}
- void OnError(ResolverError e, const string &errmsg)
+ void OnError(ResolverError e, const std::string &errmsg)
{
- ServerInstance->Log(DEBUG,"Resolver error");
- delete socket;
+ ServerInstance->Log(DEBUG,"!!!!!!!!!!!!!!!! HTTPResolver::OnError: %s", errmsg.c_str());
+ socket->OnClose();
}
};
-typedef vector<HTTPSocket*> HTTPList;
+typedef std::vector<HTTPSocket*> HTTPList;
class ModuleHTTPClient : public Module
{
HTTPList sockets;
ModuleHTTPClient(InspIRCd *Me)
- : Module::Module(Me)
+ : Module(Me)
{
+ Implementation eventlist[] = { I_OnRequest };
+ ServerInstance->Modules->Attach(eventlist, this, 1);
}
virtual ~ModuleHTTPClient()
{
for (HTTPList::iterator i = sockets.begin(); i != sockets.end(); i++)
- delete *i;
+ (*i)->Close();
+ ServerInstance->BufferedSocketCull();
}
virtual Version GetVersion()
return Version(1, 0, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
}
- void Implements(char* List)
- {
- List[I_OnRequest] = 1;
- }
char* OnRequest(Request *req)
{
};
HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
- : InspSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
+ : BufferedSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
{
- this->ClosePending = false;
+ Instance->Log(DEBUG,"HTTPSocket::HTTPSocket");
this->port = 80;
+ response = NULL;
+ closed = false;
+ timeout_val = 10;
}
HTTPSocket::~HTTPSocket()
bool HTTPSocket::DoRequest(HTTPClientRequest *req)
{
+ Instance->Log(DEBUG,"HTTPSocket::DoRequest");
/* Tweak by brain - we take a copy of this,
* so that the caller doesnt need to leave
* pointers knocking around, less chance of
*/
this->req = *req;
- Instance->Log(DEBUG,"Request in progress");
-
if (!ParseURL(this->req.GetURL()))
- {
- Instance->Log(DEBUG,"Parse failed");
return false;
- }
this->port = url.port;
strlcpy(this->host, url.domain.c_str(), MAXBUF);
- if (insp_aton(this->host, &this->addy) < 1)
+ Instance->Log(DEBUG,"Doing request for %s", url.url.c_str());
+
+ in6_addr s6;
+ in_addr s4;
+ /* Doesnt look like an ipv4 or an ipv6 address */
+ if ((inet_pton(AF_INET6, url.domain.c_str(), &s6) < 1) && (inet_pton(AF_INET, url.domain.c_str(), &s4) < 1))
{
bool cached;
HTTPResolver* r = new HTTPResolver(this, Server, url.domain, cached, (Module*)Mod);
Instance->AddResolver(r, cached);
- return true;
+ Instance->Log(DEBUG,"Resolver added, cached=%d", cached);
}
else
- {
- this->Connect(url.domain);
- }
+ Connect(url.domain);
return true;
}
bool HTTPSocket::ParseURL(const std::string &iurl)
{
+ Instance->Log(DEBUG,"HTTPSocket::ParseURL %s", iurl.c_str());
url.url = iurl;
url.port = 80;
+ url.protocol = "http";
- Instance->Log(DEBUG,"Parse: "+iurl);
+ irc::sepstream tokenizer(iurl, '/');
- // Tokenize by slashes (protocol:, blank, domain, request..)
- int pos = 0, pstart = 0, pend = 0;
-
- for (;;)
+ for (int p = 0;; p++)
{
- pend = url.url.find('/', pstart);
- string part = url.url.substr(pstart, pend);
+ std::string part;
+ if (!tokenizer.GetToken(part))
+ break;
- switch (pos)
+ if ((p == 0) && (part[part.length() - 1] == ':'))
{
- case 0:
- Instance->Log(DEBUG,"PART 0: "+part);
- // Protocol
- if (part[part.length()-1] != ':')
- return false;
- url.protocol = part.substr(0, part.length() - 1);
- break;
- case 1:
- // Empty, skip
- break;
- case 2:
- Instance->Log(DEBUG,"PART 2: "+part);
- // User and password (user:pass@)
- string::size_type aend = part.find('@', 0);
- if (aend != string::npos)
+ // Protocol ('http:')
+ url.protocol = part.substr(0, part.length() - 1);
+ }
+ else if ((p == 1) && (part.empty()))
+ {
+ continue;
+ }
+ else if (url.domain.empty())
+ {
+ // Domain part: [user[:pass]@]domain[:port]
+ std::string::size_type usrpos = part.find('@');
+ if (usrpos != std::string::npos)
+ {
+ // Have a user (and possibly password) part
+ std::string::size_type ppos = part.find(':');
+ if ((ppos != std::string::npos) && (ppos < usrpos))
{
- // Technically, it is valid to not have a password (username@domain)
- string::size_type usrend = part.find(':', 0);
-
- if ((usrend != string::npos) && (usrend < aend))
- url.password = part.substr(usrend + 1, aend);
- else
- usrend = aend;
-
- url.username = part.substr(0, usrend);
+ // Have password too
+ url.password = part.substr(ppos + 1, usrpos - ppos - 1);
+ url.username = part.substr(0, ppos);
}
else
- aend = 0;
-
- // Port (:port)
- string::size_type dend = part.find(':', aend);
- if (dend != string::npos)
- url.port = atoi(part.substr(dend + 1).c_str());
-
- // Domain
- url.domain = part.substr(aend + 1, dend);
+ {
+ url.username = part.substr(0, usrpos);
+ }
- // The rest of the string is the request
- url.request = url.url.substr(pend);
- break;
+ part = part.substr(usrpos + 1);
+ }
+
+ std::string::size_type popos = part.rfind(':');
+ if (popos != std::string::npos)
+ {
+ url.port = atoi(part.substr(popos + 1).c_str());
+ url.domain = part.substr(0, popos);
+ }
+ else
+ {
+ url.domain = part;
+ }
}
-
- if (pos++ == 2)
- break;
+ else
+ {
+ // Request (part of it)..
+ url.request.append("/");
+ url.request.append(part);
+ }
+ }
+
+ if (url.request.empty())
+ url.request = "/";
- pstart = pend + 1;
+ if ((url.domain.empty()) || (!url.port) || (url.protocol.empty()))
+ {
+ Instance->Log(DEFAULT, "Invalid URL (%s): Missing required value", iurl.c_str());
+ return false;
+ }
+
+ if (url.protocol != "http")
+ {
+ Instance->Log(DEFAULT, "Invalid URL (%s): Unsupported protocol '%s'", iurl.c_str(), url.protocol.c_str());
+ return false;
}
return true;
}
-void HTTPSocket::Connect(const string &ip)
+void HTTPSocket::Connect(const std::string &ip)
{
+ this->response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, 0, "");
+
+ Instance->Log(DEBUG,"HTTPSocket::Connect(%s) response=%08lx", ip.c_str(), response);
strlcpy(this->IP, ip.c_str(), MAXBUF);
-
+ strlcpy(this->host, ip.c_str(), MAXBUF);
+
if (!this->DoConnect())
{
- delete this;
+ Instance->Log(DEBUG,"DoConnect failed, bailing");
+ this->Close();
}
}
bool HTTPSocket::OnConnected()
{
+ Instance->Log(DEBUG,"HTTPSocket::OnConnected");
+
std::string request = "GET " + url.request + " HTTP/1.1\r\n";
// Dump headers into the request
bool HTTPSocket::OnDataReady()
{
+ Instance->Log(DEBUG,"HTTPSocket::OnDataReady() for %s", url.url.c_str());
char *data = this->Read();
if (!data)
- {
- this->Close();
return false;
- }
if (this->status < HTTP_DATA)
{
{
this->status = HTTP_DATA;
this->data += this->buffer;
- this->buffer = "";
+ this->buffer.clear();
break;
}
-// while ((line = buffer.sstrstr(data, "\r\n")) != NULL)
-// {
-// if (strncmp(data, "\r\n", 2) == 0)
-
+
if (this->status == HTTP_REQSENT)
{
// HTTP reply (HTTP/1.1 200 msg)
char const* data = line.c_str();
data += 9;
- response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, atoi(data), data + 4);
+ response->SetResponse(data);
+ response->SetData(data + 4);
this->status = HTTP_HEADERS;
continue;
}
if ((pos = line.find(':')) != std::string::npos)
{
-
-// char *hdata = strchr(data, ':');
-
-// if (!hdata)
-// continue;
-
-// *hdata = '\0';
-
-// response->AddHeader(data, hdata + 2);
response->AddHeader(line.substr(0, pos), line.substr(pos + 1));
-
-// data = lend + 2;
- } else
+ }
+ else
+ {
continue;
+ }
}
- } else {
+ }
+ else
+ {
this->data += data;
}
return true;
void HTTPSocket::OnClose()
{
- if (data.empty())
- return; // notification that request failed?
+ if (!closed)
+ {
+ closed = true;
+ Instance->Log(DEBUG,"HTTPSocket::OnClose response=%08lx", response);
+ std::string e;
+ if (data.empty())
+ {
+ Instance->Log(DEBUG,"Send error");
+ HTTPClientError* err = new HTTPClientError((Module*)Mod, req.GetSource(), req.GetURL(), 0);
+ err->Send();
+ delete err;
+ return;
+ }
- response->data = data;
- response->Send();
- delete response;
+ Instance->Log(DEBUG,"Set data and send, %s", response->GetURL().c_str());
+ response->SetData(data);
+ response->Send();
+ delete response;
+ }
}
-class ModuleHTTPClientFactory : public ModuleFactory
-{
- public:
- ModuleHTTPClientFactory()
- {
- }
-
- ~ModuleHTTPClientFactory()
- {
- }
-
- Module *CreateModule(InspIRCd* Me)
- {
- return new ModuleHTTPClient(Me);
- }
-};
+MODULE_INIT(ModuleHTTPClient)
-extern "C" void *init_module(void)
-{
- return new ModuleHTTPClientFactory;
-}