1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
23 #include "inspsocket.h"
24 #include "helperfuncs.h"
26 /* $ModDesc: Provides HTTP serving facilities to modules */
34 HTTP_SERVE_WAIT_REQUEST = 1,
35 HTTP_SERVE_SEND_DATA = 2
38 class HttpSocket : public InspSocket
41 HttpState InternalState;
42 std::stringstream headers;
46 HttpSocket(std::string host, int port, bool listening, unsigned long maxtime, FileReader* index_page) : InspSocket(host, port, listening, maxtime), index(index_page)
48 log(DEBUG,"HttpSocket constructor");
49 InternalState = HTTP_LISTEN;
52 HttpSocket(int newfd, char* ip, FileReader* ind) : InspSocket(newfd, ip), index(ind)
54 InternalState = HTTP_SERVE_WAIT_REQUEST;
57 virtual int OnIncomingConnection(int newsock, char* ip)
59 if (InternalState == HTTP_LISTEN)
61 HttpSocket* s = new HttpSocket(newsock, ip, index);
67 virtual void OnClose()
73 struct tm *timeinfo = localtime(&TIME);
74 this->Write("HTTP/1.1 200 OK\r\nDate: ");
75 this->Write(asctime(timeinfo));
76 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(index->ContentSize())+
77 "\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n");
80 virtual bool OnDataReady()
82 char* data = this->Read();
83 /* Check that the data read is a valid pointer and it has some content */
88 if (headers.str().find("\r\n\r\n") != std::string::npos)
90 /* Headers are complete */
91 InternalState = HTTP_SERVE_SEND_DATA;
94 this->Write(index->Contents());
102 /* Bastard client closed the socket on us!
103 * Oh wait, theyre SUPPOSED to do that!
110 class ModuleHttp : public Module
115 std::string indexfile;
126 this->host = c.ReadValue("http", "host", 0);
127 this->bindip = c.ReadValue("http", "ip", 0);
128 this->port = c.ReadInteger("http", "port", 0, true);
129 this->indexfile = c.ReadValue("http", "index", 0);
131 index.LoadFile(this->indexfile);
134 void CreateListener()
136 http = new HttpSocket(this->bindip, this->port, true, 0, &index);
137 if ((http) && (http->GetState() == I_LISTENING))
139 Srv->AddSocket(http);
143 ModuleHttp(Server* Me) : Module::Module(Me)
150 void Implements(char* List)
152 List[I_OnEvent] = List[I_OnRequest] = 1;
155 virtual ~ModuleHttp()
157 Srv->DelSocket(http);
160 virtual Version GetVersion()
162 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
167 class ModuleHttpFactory : public ModuleFactory
178 virtual Module * CreateModule(Server* Me)
180 return new ModuleHttp(Me);
185 extern "C" void * init_module( void )
187 return new ModuleHttpFactory;