* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
- * InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * InspIRCd: (C) 2002-2008 InspIRCd Development Team
* See: http://www.inspircd.org/wiki/index.php/Credits
*
* This program is free but copyrighted software; see
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,"HTTPResolver::HTTPResolver");
+ ServerInstance->Logs->Log("m_http_client",DEBUG,">>>>>>>>>>>>>>>>>> HTTPResolver::HTTPResolver <<<<<<<<<<<<<<<");
+ orig = hostname;
}
- void OnLookupComplete(const string &result, unsigned int ttl, bool cached, int resultnum = 0)
+ void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
{
+ ServerInstance->Logs->Log("m_http_client",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,"HTTPResolver::OnError");
- /*if (ServerInstance->SocketCull.find(socket) == ServerInstance->SocketCull.end())
- ServerInstance->SocketCull[socket] = socket;*/
+ ServerInstance->Logs->Log("m_http_client",DEBUG,"!!!!!!!!!!!!!!!! HTTPResolver::OnError: %s", errmsg.c_str());
+ socket->OnClose();
}
};
-typedef vector<HTTPSocket*> HTTPList;
+typedef std::vector<HTTPSocket*> HTTPList;
class ModuleHTTPClient : public Module
{
virtual ~ModuleHTTPClient()
{
for (HTTPList::iterator i = sockets.begin(); i != sockets.end(); i++)
- delete *i;
+ (*i)->Close();
+ ServerInstance->BufferedSocketCull();
}
virtual Version GetVersion()
}
- char* OnRequest(Request *req)
+ virtual const char* OnRequest(Request *req)
{
HTTPClientRequest *httpreq = (HTTPClientRequest *)req;
if (!strcmp(httpreq->GetId(), HTTP_CLIENT_REQUEST))
}
};
-HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
- : BufferedSocket(Instance), Server(Instance), Mod(Mod), status(HTTP_CLOSED)
+HTTPSocket::HTTPSocket(InspIRCd *SI, ModuleHTTPClient *m)
+ : BufferedSocket(SI), Server(SI), Mod(m), status(HTTP_CLOSED)
{
- Instance->Log(DEBUG,"HTTPSocket::HTTPSocket");
+ Instance->Logs->Log("m_http_client",DEBUG,"HTTPSocket::HTTPSocket");
this->port = 80;
+ response = NULL;
+ closed = false;
+ timeout_val = 10;
}
HTTPSocket::~HTTPSocket()
}
}
-bool HTTPSocket::DoRequest(HTTPClientRequest *req)
+bool HTTPSocket::DoRequest(HTTPClientRequest *request)
{
- Instance->Log(DEBUG,"HTTPSocket::DoRequest");
+ Instance->Logs->Log("m_http_client",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
* a memory leak.
*/
- this->req = *req;
+ this->req = *request;
if (!ParseURL(this->req.GetURL()))
return false;
this->port = url.port;
strlcpy(this->host, url.domain.c_str(), MAXBUF);
- in_addr addy1;
-#ifdef IPV6
- in6_addr addy2;
- if ((inet_aton(this->host, &addy1) > 0) || (inet_pton(AF_INET6, this->host, &addy2) > 0))
-#else
- if (inet_aton(this->host, &addy1) > 0)
-#endif
+ Instance->Logs->Log("m_http_client",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->Logs->Log("m_http_client",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");
+ Instance->Logs->Log("m_http_client",DEBUG,"HTTPSocket::ParseURL %s", iurl.c_str());
url.url = iurl;
url.port = 80;
url.protocol = "http";
if ((url.domain.empty()) || (!url.port) || (url.protocol.empty()))
{
- Instance->Log(DEFAULT, "Invalid URL (%s): Missing required value", iurl.c_str());
+ Instance->Logs->Log("m_http_client",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());
+ Instance->Logs->Log("m_http_client",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)
{
- Instance->Log(DEBUG,"HTTPSocket::Connect");
+ this->response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, 0, "");
+
+ Instance->Logs->Log("m_http_client",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->Logs->Log("m_http_client",DEBUG,"DoConnect failed, bailing");
+ this->Close();
}
}
bool HTTPSocket::OnConnected()
{
+ Instance->Logs->Log("m_http_client",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()");
- char *data = this->Read();
+ Instance->Logs->Log("m_http_client",DEBUG,"HTTPSocket::OnDataReady() for %s", url.url.c_str());
+ const char *sdata = this->Read();
- if (!data)
- {
- this->Close();
+ if (!sdata)
return false;
- }
if (this->status < HTTP_DATA)
{
std::string line;
std::string::size_type pos;
- this->buffer += data;
+ this->buffer += sdata;
while ((pos = buffer.find("\r\n")) != std::string::npos)
{
line = buffer.substr(0, pos);
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);
+ char const* sdata2 = line.c_str();
+ sdata2 += 9;
+ response->SetResponse(sdata2);
+ response->SetData(sdata2 + 4);
this->status = HTTP_HEADERS;
continue;
}
void HTTPSocket::OnClose()
{
- Instance->Log(DEBUG,"HTTPSocket::OnClose");
- if (data.empty())
- return; // notification that request failed?
+ if (!closed)
+ {
+ closed = true;
+ Instance->Logs->Log("m_http_client",DEBUG,"HTTPSocket::OnClose response=%08lx", response);
+ std::string e;
+ if (data.empty())
+ {
+ Instance->Logs->Log("m_http_client",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->Logs->Log("m_http_client",DEBUG,"Set data and send, %s", response->GetURL().c_str());
+ response->SetData(data);
+ response->Send();
+ delete response;
+ }
}
MODULE_INIT(ModuleHTTPClient)