From 184b6220255ec89c221bff152a62450f1a137340 Mon Sep 17 00:00:00 2001 From: brain Date: Mon, 10 Jul 2006 21:23:21 +0000 Subject: Try to make this serve some static content git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4308 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/modules/m_http.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/modules/m_http.cpp b/src/modules/m_http.cpp index b4f77903a..59293e7a9 100644 --- a/src/modules/m_http.cpp +++ b/src/modules/m_http.cpp @@ -21,42 +21,100 @@ using namespace std; #include "channels.h" #include "modules.h" -/* $ModDesc: Povides a proof-of-concept test /WOOT command */ +/* $ModDesc: Provides HTTP serving facilities to modules */ static Server *Srv; enum HttpState { HTTP_LISTEN = 0, - HTTP_SERVE = 1 + HTTP_SERVE_WAIT_REQUEST = 1, + HTTP_SERVE_SEND_DATA = 2 }; class HttpSocket : public InspSocket { + FileReader* index; + HttpState InternalState; + std::stringstream headers; + public: - HttpSocket(std::string host, int port, bool listening, unsigned long maxtime) : InspSocket(host, port, listening, maxtime) + HttpSocket(std::string host, int port, bool listening, unsigned long maxtime, FileReader* index_page) : InspSocket(host, port, listening, maxtime), index(index_page) { + InternalState = HTTP_LISTEN; } HttpSocket(int newfd, char* ip) : InspSocket(newfd, ip) { + InternalState = HTTP_SERVE_WAIT_REQUEST; + } + + virtual int OnIncomingConnection(int newsock, char* ip) + { + if (InternalState == HTTP_LISTEN) + { + HttpSocket* s = new HttpSocket(newsock, ip); + Srv->AddSocket(s); + } + } + + virtual void OnClose() + { + } + + virtual bool OnDataReady() + { + char* data = this->Read(); + /* Check that the data read is a valid pointer and it has some content */ + if (data && *data) + { + headers << data; + + if (headers.str().find("\r\n\r\n")) + { + /* Headers are complete */ + InternalState = HTTP_SERVE_SEND_DATA; + this->Write("

COWS.

"); + } + } } }; class ModuleHttp : public Module { + int port; + std::string host; + std::string bindip; + std::string indexfile; + + FileReader index; + + HttpSocket* http; + public: void ReadConfig() { ConfigReader c; + this->host = c.ReadValue("http", "host", 0); + this->bindip = c.ReadValue("http", "ip", 0); + this->port = c.ReadInteger("http", "port", 0, true); + this->indexfile = c.ReadValue("http", "index", 0); + + index.LoadFile(this->indexfile); + } + + void CreateListener() + { + http = new HttpSocket(this->host, this->port, true, 0, &index); + Srv->AddSocket(http); } - ModuleHttp(Server* Me) - : Module::Module(Me) + ModuleHttp(Server* Me) : Module::Module(Me) { ReadConfig(); + CreateListener(); } void Implements(char* List) @@ -66,8 +124,9 @@ class ModuleHttp : public Module virtual ~ModuleHttp() { + Srv->DelSocket(http); } - + virtual Version GetVersion() { return Version(1,0,0,0,VF_STATIC|VF_VENDOR); @@ -90,7 +149,6 @@ class ModuleHttpFactory : public ModuleFactory { return new ModuleHttp(Me); } - }; @@ -98,4 +156,3 @@ extern "C" void * init_module( void ) { return new ModuleHttpFactory; } - -- cgit v1.2.3