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"
27 /* $ModDesc: Provides HTTP serving facilities to modules */
31 static ModuleHttp* HttpModule;
34 /** HTTP socket states
39 HTTP_SERVE_WAIT_REQUEST = 1,
40 HTTP_SERVE_RECV_POSTDATA = 2,
41 HTTP_SERVE_SEND_DATA = 3
46 class HTTPTimeout : public InspTimer
52 HTTPTimeout(HttpSocket* sock, SocketEngine* engine);
53 void Tick(time_t TIME);
56 /** A socket used for HTTP transport
58 class HttpSocket : public InspSocket
61 HttpState InternalState;
62 std::stringstream headers;
64 std::string request_type;
66 std::string http_version;
67 unsigned int postsize;
73 HttpSocket(InspIRCd* SI, std::string host, int port, bool listening, unsigned long maxtime, FileReader* index_page) : InspSocket(SI, host, port, listening, maxtime), index(index_page), postsize(0)
75 SI->Log(DEBUG,"HttpSocket constructor");
76 InternalState = HTTP_LISTEN;
80 HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind), postsize(0)
82 InternalState = HTTP_SERVE_WAIT_REQUEST;
83 Timeout = new HTTPTimeout(this, Instance->SE);
84 Instance->Timers->AddTimer(Timeout);
89 if (Instance->Time() < Timeout->GetTimer())
92 Instance->Timers->DelTimer(Timeout);
97 virtual int OnIncomingConnection(int newsock, char* ip)
99 if (InternalState == HTTP_LISTEN)
101 HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
102 s = s; /* Stop GCC whining */
107 virtual void OnClose()
111 std::string Response(int response)
118 return "SWITCHING PROTOCOLS";
126 return "NON-AUTHORITATIVE INFORMATION";
130 return "RESET CONTENT";
132 return "PARTIAL CONTENT";
134 return "MULTIPLE CHOICES";
136 return "MOVED PERMENANTLY";
142 return "NOT MODIFIED";
146 return "TEMPORARY REDIRECT";
148 return "BAD REQUEST";
150 return "UNAUTHORIZED";
152 return "PAYMENT REQUIRED";
158 return "METHOD NOT ALLOWED";
160 return "NOT ACCEPTABLE";
162 return "PROXY AUTHENTICATION REQUIRED";
164 return "REQUEST TIMEOUT";
170 return "LENGTH REQUIRED";
172 return "PRECONDITION FAILED";
174 return "REQUEST ENTITY TOO LARGE";
176 return "REQUEST-URI TOO LONG";
178 return "UNSUPPORTED MEDIA TYPE";
180 return "REQUESTED RANGE NOT SATISFIABLE";
182 return "EXPECTATION FAILED";
184 return "INTERNAL SERVER ERROR";
186 return "NOT IMPLEMENTED";
188 return "BAD GATEWAY";
190 return "SERVICE UNAVAILABLE";
192 return "GATEWAY TIMEOUT";
194 return "HTTP VERSION NOT SUPPORTED";
202 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
204 time_t local = this->Instance->Time();
205 struct tm *timeinfo = gmtime(&local);
206 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
207 this->Write(asctime(timeinfo));
208 if (extraheaders.empty())
210 this->Write("Content-Type: text/html\r\n");
214 this->Write(extraheaders);
216 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
217 "\r\nConnection: close\r\n\r\n");
220 virtual bool OnDataReady()
222 char* data = this->Read();
224 /* Check that the data read is a valid pointer and it has some content */
229 if (headers.str().find("\r\n\r\n") != std::string::npos)
231 if (request_type == "")
233 headers >> request_type;
235 headers >> http_version;
238 if ((InternalState == HTTP_SERVE_WAIT_REQUEST) && (request_type == "POST"))
240 /* Do we need to fetch postdata? */
243 InternalState = HTTP_SERVE_RECV_POSTDATA;
244 std::string header_item;
245 while (headers >> header_item)
247 if (header_item == "Content-Length:")
249 headers >> header_item;
250 postsize = atoi(header_item.c_str());
255 InternalState = HTTP_SERVE_SEND_DATA;
256 SendHeaders(0, 400, "");
257 Timeout = new HTTPTimeout(this, Instance->SE);
258 Instance->Timers->AddTimer(Timeout);
262 Instance->Log(DEBUG,"%d bytes to read for POST",postsize);
263 std::string::size_type x = headers.str().find("\r\n\r\n");
264 postdata = headers.str().substr(x+4, headers.str().length());
265 /* Get content length and store */
266 Instance->Log(DEBUG,"Initial postdata: '%s'", postdata.c_str());
267 if (postdata.length() >= postsize)
271 else if (InternalState == HTTP_SERVE_RECV_POSTDATA)
273 /* Add postdata, once we have it all, send the event */
274 amount += strlen(data);
275 postdata.append(data);
276 if (amount >= postsize)
295 /* Headers are complete */
296 InternalState = HTTP_SERVE_SEND_DATA;
298 Instance->Timers->DelTimer(Timeout);
301 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
303 SendHeaders(0, 505, "");
307 if ((request_type == "GET") && (uri == "/"))
309 SendHeaders(index->ContentSize(), 200, "");
310 this->Write(index->Contents());
315 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP(),postdata);
316 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
317 e.Send(this->Instance);
320 SendHeaders(0, 404, "");
321 Instance->Log(DEBUG,"Page not claimed, 404");
325 Timeout = new HTTPTimeout(this, Instance->SE);
326 Instance->Timers->AddTimer(Timeout);
329 void Page(std::stringstream* n, int response, std::string& extraheaders)
331 Instance->Log(DEBUG,"Sending page");
332 SendHeaders(n->str().length(), response, extraheaders);
333 this->Write(n->str());
337 HTTPTimeout::HTTPTimeout(HttpSocket* sock, SocketEngine* engine) : InspTimer(60, time(NULL)), s(sock), SE(engine)
341 void HTTPTimeout::Tick(time_t TIME)
348 class ModuleHttp : public Module
353 std::string indexfile;
363 ConfigReader c(ServerInstance);
364 this->host = c.ReadValue("http", "host", 0);
365 this->bindip = c.ReadValue("http", "ip", 0);
366 this->port = c.ReadInteger("http", "port", 0, true);
367 this->indexfile = c.ReadValue("http", "index", 0);
374 index = new FileReader(ServerInstance, this->indexfile);
377 void CreateListener()
379 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, index);
382 ModuleHttp(InspIRCd* Me) : Module::Module(Me)
389 void OnEvent(Event* event)
393 char* OnRequest(Request* request)
395 ServerInstance->Log(DEBUG,"Got HTTPDocument object");
397 HTTPDocument* doc = (HTTPDocument*)request->GetData();
398 HttpSocket* sock = (HttpSocket*)doc->sock;
399 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
403 void Implements(char* List)
405 List[I_OnEvent] = List[I_OnRequest] = 1;
408 virtual ~ModuleHttp()
410 ServerInstance->SE->DelFd(http);
413 virtual Version GetVersion()
415 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
420 class ModuleHttpFactory : public ModuleFactory
431 virtual Module * CreateModule(InspIRCd* Me)
433 HttpModule = new ModuleHttp(Me);
439 extern "C" void * init_module( void )
441 return new ModuleHttpFactory;