]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_http_client.cpp
m_http_client is crashy. will fix.
[user/henk/code/inspircd.git] / src / modules / m_http_client.cpp
index 2cb890a1e08a77f259cb7e680a2eb65b9e595d64..d7722caffad1e1be0ec2b7621dca2be587adef28 100644 (file)
@@ -24,7 +24,7 @@ class URL
        int port;
 };
 
-class HTTPSocket : public InspSocket
+class HTTPSocket : public BufferedSocket
 {
  private:
        InspIRCd *Server;
@@ -54,16 +54,20 @@ class HTTPResolver : public Resolver
  public:
        HTTPResolver(HTTPSocket *socket, InspIRCd *Instance, const string &hostname, bool &cached, Module* me) : Resolver(Instance, hostname, DNS_QUERY_FORWARD, cached, me), socket(socket)
        {
+               ServerInstance->Log(DEBUG,"HTTPResolver::HTTPResolver");
        }
        
-       void OnLookupComplete(const string &result, unsigned int ttl, bool cached)
+       void OnLookupComplete(const string &result, unsigned int ttl, bool cached, int resultnum = 0)
        {
-               socket->Connect(result);
+               if (!resultnum)
+                       socket->Connect(result);
        }
        
        void OnError(ResolverError e, const string &errmsg)
        {
-               delete socket;
+               ServerInstance->Log(DEBUG,"HTTPResolver::OnError");
+               /*if (ServerInstance->SocketCull.find(socket) == ServerInstance->SocketCull.end())
+                       ServerInstance->SocketCull[socket] = socket;*/
        }
 };
 
@@ -77,6 +81,8 @@ class ModuleHTTPClient : public Module
        ModuleHTTPClient(InspIRCd *Me)
                : Module(Me)
        {
+               Implementation eventlist[] = { I_OnRequest };
+               ServerInstance->Modules->Attach(eventlist, this, 1);
        }
        
        virtual ~ModuleHTTPClient()
@@ -90,10 +96,6 @@ class ModuleHTTPClient : public Module
                return Version(1, 0, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
        }
 
-       void Implements(char* List)
-       {
-               List[I_OnRequest] = 1;
-       }
 
        char* OnRequest(Request *req)
        {
@@ -109,9 +111,9 @@ class ModuleHTTPClient : public Module
 };
 
 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;
 }
 
@@ -130,6 +132,7 @@ 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
@@ -166,6 +169,7 @@ bool HTTPSocket::DoRequest(HTTPClientRequest *req)
 
 bool HTTPSocket::ParseURL(const std::string &iurl)
 {
+       Instance->Log(DEBUG,"HTTPSocket::ParseURL");
        url.url = iurl;
        url.port = 80;
        url.protocol = "http";
@@ -174,10 +178,10 @@ bool HTTPSocket::ParseURL(const std::string &iurl)
        
        for (int p = 0;; p++)
        {
-               std::string part = tokenizer.GetToken();
-               if (part.empty() && tokenizer.StreamEnd())
+               std::string part;
+               if (!tokenizer.GetToken(part))
                        break;
-               
+
                if ((p == 0) && (part[part.length() - 1] == ':'))
                {
                        // Protocol ('http:')
@@ -248,6 +252,7 @@ bool HTTPSocket::ParseURL(const std::string &iurl)
 
 void HTTPSocket::Connect(const string &ip)
 {
+       Instance->Log(DEBUG,"HTTPSocket::Connect");
        strlcpy(this->IP, ip.c_str(), MAXBUF);
        
        if (!this->DoConnect())
@@ -280,6 +285,7 @@ bool HTTPSocket::OnConnected()
 
 bool HTTPSocket::OnDataReady()
 {
+       Instance->Log(DEBUG,"HTTPSocket::OnDataReady()");
        char *data = this->Read();
 
        if (!data)
@@ -302,7 +308,7 @@ bool HTTPSocket::OnDataReady()
                        {
                                this->status = HTTP_DATA;
                                this->data += this->buffer;
-                               this->buffer = "";
+                               this->buffer.clear();
                                break;
                        }
 
@@ -335,6 +341,7 @@ bool HTTPSocket::OnDataReady()
 
 void HTTPSocket::OnClose()
 {
+       Instance->Log(DEBUG,"HTTPSocket::OnClose");
        if (data.empty())
                return; // notification that request failed?
 
@@ -343,24 +350,5 @@ void HTTPSocket::OnClose()
        delete response;
 }
 
-class ModuleHTTPClientFactory : public ModuleFactory
-{
- public:
-       ModuleHTTPClientFactory()
-       {
-       }
-       
-       ~ModuleHTTPClientFactory()
-       {
-       }
-       
-       Module *CreateModule(InspIRCd* Me)
-       {
-               return new ModuleHTTPClient(Me);
-       }
-};
+MODULE_INIT(ModuleHTTPClient)
 
-extern "C" DllExport void *init_module(void)
-{
-       return new ModuleHTTPClientFactory;
-}