]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_http_client.cpp
Remove some debug (im on a crusade to make debug mode useful, but at the same time...
[user/henk/code/inspircd.git] / src / modules / m_http_client.cpp
index 7e579196539d9f20c2f1fdf05f84fef41c27d6b8..4b3edca55ef076e8e228d533585bfc9f9289e0e9 100644 (file)
@@ -17,7 +17,7 @@
 /* Written by Special (john@yarbbles.com) */
 
 #include "inspircd.h"
-#include "http.h"
+#include "httpclient.h"
 
 /* $ModDesc: HTTP client service provider */
 
@@ -34,7 +34,7 @@ class HTTPSocket : public InspSocket
  private:
        InspIRCd *Server;
        class ModuleHTTPClient *Mod;
-       HTTPClientRequest *req;
+       HTTPClientRequest req;
        HTTPClientResponse *response;
        URL url;
        enum { HTTP_CLOSED, HTTP_REQSENT, HTTP_HEADERS, HTTP_DATA } status;
@@ -56,11 +56,11 @@ class HTTPResolver : public Resolver
  private:
        HTTPSocket *socket;
  public:
-       HTTPResolver(HTTPSocket *socket, InspIRCd *Instance, const string &hostname) : Resolver(Instance, hostname, DNS_QUERY_FORWARD), socket(socket)
+       HTTPResolver(HTTPSocket *socket, InspIRCd *Instance, const string &hostname, bool &cached, Module* me) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), socket(socket)
        {
        }
        
-       void OnLookupComplete(const string &result)
+       void OnLookupComplete(const string &result, unsigned int ttl, bool cached)
        {
                socket->Connect(result);
        }
@@ -98,21 +98,18 @@ class ModuleHTTPClient : public Module
        {
                List[I_OnRequest] = 1;
        }
-       
-       char *OnRequest(Request *req)
+
+       charOnRequest(Request *req)
        {
-               HTTPClientRequest *httpreq = (HTTPClientRequest *) req->GetData();
-               HTTPSocket *sock = new HTTPSocket(ServerInstance, this);
-               sock->DoRequest(httpreq);
-               // No return value
+               HTTPClientRequest *httpreq = (HTTPClientRequest *)req;
+               if (!strcmp(httpreq->GetId(), HTTP_CLIENT_REQUEST))
+               {
+                       HTTPSocket *sock = new HTTPSocket(ServerInstance, this);
+                       sock->DoRequest(httpreq);
+                       // No return value
+               }
                return NULL;
        }
-       
-       void SendReply(Module *to, HTTPClientResponse *data)
-       {
-               Request req((char *) data, this, to);
-               req.Send();
-       }
 };
 
 HTTPSocket::HTTPSocket(InspIRCd *Instance, ModuleHTTPClient *Mod)
@@ -137,17 +134,24 @@ HTTPSocket::~HTTPSocket()
 
 bool HTTPSocket::DoRequest(HTTPClientRequest *req)
 {
-       this->req = req;
-       
-       if (!ParseURL(req->GetURL()))
+       /* 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;
+
+       if (!ParseURL(this->req.GetURL()))
                return false;
        
        this->port = url.port;
        strlcpy(this->host, url.domain.c_str(), MAXBUF);
 
-       if (!inet_aton(this->host, &this->addy))
+       if (!insp_aton(this->host, &this->addy))
        {
-               new HTTPResolver(this, Server, url.domain);
+               bool cached;
+               HTTPResolver* r = new HTTPResolver(this, Server, url.domain, cached, (Module*)Mod);
+               Instance->AddResolver(r, cached);
                return true;
        }
        else
@@ -237,7 +241,7 @@ bool HTTPSocket::OnConnected()
        std::string request = "GET " + url.request + " HTTP/1.1\r\n";
 
        // Dump headers into the request
-       HeaderMap headers = req->GetHeaders();
+       HeaderMap headers = req.GetHeaders();
        
        for (HeaderMap::iterator i = headers.begin(); i != headers.end(); i++)
                request += i->first + ": " + i->second + "\r\n";
@@ -283,7 +287,7 @@ bool HTTPSocket::OnDataReady()
                        {
                                // HTTP reply (HTTP/1.1 200 msg)
                                data += 9;
-                               response = new HTTPClientResponse(url.url, atoi(data), data + 4);
+                               response = new HTTPClientResponse((Module*)Mod, req.GetSource() , url.url, atoi(data), data + 4);
                                this->status = HTTP_HEADERS;
                                continue;
                        }
@@ -314,7 +318,8 @@ void HTTPSocket::OnClose()
        }
        Server->Log(DEBUG, "Got file from HTTP successfully");
        response->data = data;
-       Mod->SendReply(req->GetSrc(), response);
+       response->Send();
+       delete response;
 }
 
 class ModuleHTTPClientFactory : public ModuleFactory
@@ -338,5 +343,3 @@ extern "C" void *init_module(void)
 {
        return new ModuleHTTPClientFactory;
 }
-
-