X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_http_client.cpp;h=065cf40556325fb24f0bac57000bdadb72d58224;hb=4798f98adad084cf9e207cc7b32d7e4370e9cc90;hp=2010e3e4772ddd291dfc9bed49841cd399a3fe4d;hpb=d41820f19f704687b8ef15c47f38c558e6bf3cee;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_http_client.cpp b/src/modules/m_http_client.cpp index 2010e3e47..065cf4055 100644 --- a/src/modules/m_http_client.cpp +++ b/src/modules/m_http_client.cpp @@ -2,7 +2,7 @@ * | 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 @@ -35,6 +35,7 @@ class HTTPSocket : public BufferedSocket 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); @@ -53,29 +54,29 @@ class HTTPResolver : public Resolver 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->Log(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->Log(DEBUG,"HTTPResolver::OnLookupComplete"); + 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,"HTTPResolver::OnError"); + ServerInstance->Log(DEBUG,"!!!!!!!!!!!!!!!! HTTPResolver::OnError: %s", errmsg.c_str()); socket->OnClose(); } }; -typedef vector HTTPList; +typedef std::vector HTTPList; class ModuleHTTPClient : public Module { @@ -115,11 +116,14 @@ class ModuleHTTPClient : public Module } }; -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"); this->port = 80; + response = NULL; + closed = false; + timeout_val = 10; } HTTPSocket::~HTTPSocket() @@ -135,7 +139,7 @@ HTTPSocket::~HTTPSocket() } } -bool HTTPSocket::DoRequest(HTTPClientRequest *req) +bool HTTPSocket::DoRequest(HTTPClientRequest *request) { Instance->Log(DEBUG,"HTTPSocket::DoRequest"); /* Tweak by brain - we take a copy of this, @@ -143,7 +147,7 @@ bool HTTPSocket::DoRequest(HTTPClientRequest *req) * pointers knocking around, less chance of * a memory leak. */ - this->req = *req; + this->req = *request; if (!ParseURL(this->req.GetURL())) return false; @@ -151,6 +155,8 @@ bool HTTPSocket::DoRequest(HTTPClientRequest *req) this->port = url.port; strlcpy(this->host, url.domain.c_str(), MAXBUF); + 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 */ @@ -159,6 +165,7 @@ bool HTTPSocket::DoRequest(HTTPClientRequest *req) bool cached; HTTPResolver* r = new HTTPResolver(this, Server, url.domain, cached, (Module*)Mod); Instance->AddResolver(r, cached); + Instance->Log(DEBUG,"Resolver added, cached=%d", cached); } else Connect(url.domain); @@ -168,7 +175,7 @@ bool HTTPSocket::DoRequest(HTTPClientRequest *req) bool HTTPSocket::ParseURL(const std::string &iurl) { - Instance->Log(DEBUG,"HTTPSocket::ParseURL"); + Instance->Log(DEBUG,"HTTPSocket::ParseURL %s", iurl.c_str()); url.url = iurl; url.port = 80; url.protocol = "http"; @@ -249,18 +256,25 @@ bool HTTPSocket::ParseURL(const std::string &iurl) return true; } -void HTTPSocket::Connect(const string &ip) +void HTTPSocket::Connect(const std::string &ip) { - Instance->Log(DEBUG,"HTTPSocket::Connect(%s)", ip.c_str()); + 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()) + { + 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 @@ -283,10 +297,10 @@ bool HTTPSocket::OnConnected() bool HTTPSocket::OnDataReady() { - Instance->Log(DEBUG,"HTTPSocket::OnDataReady()"); - char *data = this->Read(); + Instance->Log(DEBUG,"HTTPSocket::OnDataReady() for %s", url.url.c_str()); + char *sdata = this->Read(); - if (!data || !*data) + if (!sdata) return false; if (this->status < HTTP_DATA) @@ -294,7 +308,7 @@ bool HTTPSocket::OnDataReady() 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); @@ -310,9 +324,10 @@ bool HTTPSocket::OnDataReady() 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; } @@ -336,19 +351,25 @@ bool HTTPSocket::OnDataReady() void HTTPSocket::OnClose() { - Instance->Log(DEBUG,"HTTPSocket::OnClose"); - if (data.empty()) + if (!closed) { - HTTPClientError* err = new HTTPClientError((Module*)Mod, req.GetSource(), req.GetURL(), 0); - err->Send(); - delete err; - return; - } + 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; + } - Instance->Log(DEBUG,"Set data and send"); - 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; + } } MODULE_INIT(ModuleHTTPClient)