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) : InspSocket(newfd, ip)
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);
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_http.so/1.1\r\nContent-Length: "+ConvToStr(index->FileSize())+"\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n");
79 virtual bool OnDataReady()
81 char* data = this->Read();
82 /* Check that the data read is a valid pointer and it has some content */
87 if (headers.str().find("\r\n\r\n"))
89 /* Headers are complete */
90 InternalState = HTTP_SERVE_SEND_DATA;
93 this->Write(index->Contents());
95 /* This clever hax makes InspSocket think its
96 * in a connecting state, and time out 2 seconds
97 * from now. Most apache servers do this if the
98 * client doesnt close the connection as its
101 this->timeout_end = TIME + 2;
102 this->SetState(I_CONNECTING);
108 /* Bastard client closed the socket on us!
109 * Oh wait, theyre SUPPOSED to do that!
116 class ModuleHttp : public Module
121 std::string indexfile;
132 this->host = c.ReadValue("http", "host", 0);
133 this->bindip = c.ReadValue("http", "ip", 0);
134 this->port = c.ReadInteger("http", "port", 0, true);
135 this->indexfile = c.ReadValue("http", "index", 0);
137 index.LoadFile(this->indexfile);
140 void CreateListener()
142 http = new HttpSocket(this->bindip, this->port, true, 0, &index);
143 if ((http) && (http->GetState() == I_LISTENING))
145 Srv->AddSocket(http);
149 ModuleHttp(Server* Me) : Module::Module(Me)
156 void Implements(char* List)
158 List[I_OnEvent] = List[I_OnRequest] = 1;
161 virtual ~ModuleHttp()
163 Srv->DelSocket(http);
166 virtual Version GetVersion()
168 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
173 class ModuleHttpFactory : public ModuleFactory
184 virtual Module * CreateModule(Server* Me)
186 return new ModuleHttp(Me);
191 extern "C" void * init_module( void )
193 return new ModuleHttpFactory;