summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/modules/extra/m_httpclienttest.cpp86
-rw-r--r--src/modules/m_http_client.cpp48
2 files changed, 117 insertions, 17 deletions
diff --git a/src/modules/extra/m_httpclienttest.cpp b/src/modules/extra/m_httpclienttest.cpp
new file mode 100644
index 000000000..800cd63f5
--- /dev/null
+++ b/src/modules/extra/m_httpclienttest.cpp
@@ -0,0 +1,86 @@
+#include "users.h"
+#include "channels.h"
+#include "modules.h"
+#include "inspircd.h"
+#include "httpclient.h"
+
+/* $ModDep: httpclient.h */
+
+class MyModule : public Module
+{
+public:
+ MyModule(InspIRCd* Me)
+ : Module::Module(Me)
+ {
+ }
+
+ virtual ~MyModule()
+ {
+ }
+
+ virtual void Implements(char* List)
+ {
+ List[I_OnUserJoin] = List[I_OnUserPart] = 1;
+ }
+
+ virtual Version GetVersion()
+ {
+ return Version(1,0,0,1,0,API_VERSION);
+ }
+
+ virtual void OnUserJoin(userrec* user, chanrec* channel)
+ {
+ // method called when a user joins a channel
+
+ std::string chan = channel->name;
+ std::string nick = user->nick;
+// ServerInstance->Log(DEBUG,"User " + nick + " joined " + chan);
+
+ Module* target = ServerInstance->FindModule("m_http_client");
+ if(target) {
+ HTTPClientRequest req(ServerInstance, this, target, "http://znc.in/~psychon");
+ req.Send();
+ }
+ else
+ ServerInstance->Log(DEBUG,"module not found, load it!!");
+ }
+
+ char* OnRequest(Request* req)
+ {
+ HTTPClientResponse* resp = (HTTPClientResponse*)req;
+ if(!strcmp(resp->GetId(), HTTP_CLIENT_RESPONSE))
+ {
+ ServerInstance->Log(DEBUG, resp->GetData());
+ }
+ return NULL;
+ }
+
+ virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
+ {
+ }
+
+};
+
+class MyModuleFactory : public ModuleFactory
+{
+public:
+ MyModuleFactory()
+ {
+ }
+
+ ~MyModuleFactory()
+ {
+ }
+
+ virtual Module * CreateModule(InspIRCd* Me)
+ {
+ return new MyModule(Me);
+ }
+
+};
+
+extern "C" void * init_module( void )
+{
+ return new MyModuleFactory;
+}
+
diff --git a/src/modules/m_http_client.cpp b/src/modules/m_http_client.cpp
index b0fcccd47..0034aa246 100644
--- a/src/modules/m_http_client.cpp
+++ b/src/modules/m_http_client.cpp
@@ -39,6 +39,7 @@ class HTTPSocket : public InspSocket
URL url;
enum { HTTP_CLOSED, HTTP_REQSENT, HTTP_HEADERS, HTTP_DATA } status;
std::string data;
+ std::string buffer;
public:
HTTPSocket(InspIRCd *Instance, class ModuleHTTPClient *Mod);
@@ -266,52 +267,65 @@ bool HTTPSocket::OnDataReady()
this->Close();
return false;
}
-
- // Needs buffering for incomplete reads..
- char *lend;
-
+
if (this->status < HTTP_DATA)
{
- while ((lend = strstr(data, "\r\n")) != NULL)
+ std::string line;
+ std::string::size_type pos;
+
+ this->buffer += data;
+ while ((pos = buffer.find("\r\n")) != std::string::npos)
{
- if (strncmp(data, "\r\n", 2) == 0)
+ line = buffer.substr(0, pos);
+ buffer = buffer.substr(pos + 2);
+ if (line.empty())
{
this->status = HTTP_DATA;
+ this->data += this->buffer;
+ this->buffer = "";
break;
}
-
- *lend = '\0';
+// 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);
this->status = HTTP_HEADERS;
continue;
}
- char *hdata = strchr(data, ':');
+ if ((pos = line.find(':')) != std::string::npos)
+ {
+
+// char *hdata = strchr(data, ':');
- if (!hdata)
- continue;
+// if (!hdata)
+// continue;
- *hdata = '\0';
+// *hdata = '\0';
- response->AddHeader(data, hdata + 2);
+// response->AddHeader(data, hdata + 2);
+ response->AddHeader(line.substr(0, pos), line.substr(pos + 1));
- data = lend + 2;
+// data = lend + 2;
+ } else
+ continue;
}
+ } else {
+ this->data += data;
}
-
- this->data += data;
return true;
}
void HTTPSocket::OnClose()
{
if (data.empty())
- return;
+ return; // notification that request failed?
response->data = data;
response->Send();