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);
90 Instance->Timers->DelTimer(Timeout);
94 virtual int OnIncomingConnection(int newsock, char* ip)
96 if (InternalState == HTTP_LISTEN)
98 HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
99 s = s; /* Stop GCC whining */
104 virtual void OnClose()
108 std::string Response(int response)
115 return "SWITCHING PROTOCOLS";
123 return "NON-AUTHORITATIVE INFORMATION";
127 return "RESET CONTENT";
129 return "PARTIAL CONTENT";
131 return "MULTIPLE CHOICES";
133 return "MOVED PERMENANTLY";
139 return "NOT MODIFIED";
143 return "TEMPORARY REDIRECT";
145 return "BAD REQUEST";
147 return "UNAUTHORIZED";
149 return "PAYMENT REQUIRED";
155 return "METHOD NOT ALLOWED";
157 return "NOT ACCEPTABLE";
159 return "PROXY AUTHENTICATION REQUIRED";
161 return "REQUEST TIMEOUT";
167 return "LENGTH REQUIRED";
169 return "PRECONDITION FAILED";
171 return "REQUEST ENTITY TOO LARGE";
173 return "REQUEST-URI TOO LONG";
175 return "UNSUPPORTED MEDIA TYPE";
177 return "REQUESTED RANGE NOT SATISFIABLE";
179 return "EXPECTATION FAILED";
181 return "INTERNAL SERVER ERROR";
183 return "NOT IMPLEMENTED";
185 return "BAD GATEWAY";
187 return "SERVICE UNAVAILABLE";
189 return "GATEWAY TIMEOUT";
191 return "HTTP VERSION NOT SUPPORTED";
199 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
201 time_t local = this->Instance->Time();
202 struct tm *timeinfo = gmtime(&local);
203 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
204 this->Write(asctime(timeinfo));
205 if (extraheaders.empty())
207 this->Write("Content-Type: text/html\r\n");
211 this->Write(extraheaders);
213 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
214 "\r\nConnection: close\r\n\r\n");
217 virtual bool OnDataReady()
219 char* data = this->Read();
221 /* Check that the data read is a valid pointer and it has some content */
226 if (headers.str().find("\r\n\r\n") != std::string::npos)
228 if (request_type == "")
230 headers >> request_type;
232 headers >> http_version;
235 if ((InternalState == HTTP_SERVE_WAIT_REQUEST) && (request_type == "POST"))
237 /* Do we need to fetch postdata? */
240 InternalState = HTTP_SERVE_RECV_POSTDATA;
241 std::string header_item;
242 while (headers >> header_item)
244 if (header_item == "Content-Length:")
246 headers >> header_item;
247 postsize = atoi(header_item.c_str());
252 InternalState = HTTP_SERVE_SEND_DATA;
253 SendHeaders(0, 400, "");
254 Timeout = new HTTPTimeout(this, Instance->SE);
255 Instance->Timers->AddTimer(Timeout);
259 Instance->Log(DEBUG,"%d bytes to read for POST",postsize);
260 std::string::size_type x = headers.str().find("\r\n\r\n");
261 postdata = headers.str().substr(x+4, headers.str().length());
262 /* Get content length and store */
263 Instance->Log(DEBUG,"Initial postdata: '%s'", postdata.c_str());
264 if (postdata.length() >= postsize)
268 else if (InternalState == HTTP_SERVE_RECV_POSTDATA)
270 /* Add postdata, once we have it all, send the event */
271 amount += strlen(data);
272 postdata.append(data);
273 if (amount >= postsize)
292 /* Headers are complete */
293 InternalState = HTTP_SERVE_SEND_DATA;
295 Instance->Timers->DelTimer(Timeout);
298 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
300 SendHeaders(0, 505, "");
304 if ((request_type == "GET") && (uri == "/"))
306 SendHeaders(index->ContentSize(), 200, "");
307 this->Write(index->Contents());
312 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP(),postdata);
313 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
314 e.Send(this->Instance);
317 SendHeaders(0, 404, "");
318 Instance->Log(DEBUG,"Page not claimed, 404");
322 Timeout = new HTTPTimeout(this, Instance->SE);
323 Instance->Timers->AddTimer(Timeout);
326 void Page(std::stringstream* n, int response, std::string& extraheaders)
328 Instance->Log(DEBUG,"Sending page");
329 SendHeaders(n->str().length(), response, extraheaders);
330 this->Write(n->str());
334 HTTPTimeout::HTTPTimeout(HttpSocket* sock, SocketEngine* engine) : InspTimer(60, time(NULL)), s(sock), SE(engine)
338 void HTTPTimeout::Tick(time_t TIME)
345 class ModuleHttp : public Module
350 std::string indexfile;
360 ConfigReader c(ServerInstance);
361 this->host = c.ReadValue("http", "host", 0);
362 this->bindip = c.ReadValue("http", "ip", 0);
363 this->port = c.ReadInteger("http", "port", 0, true);
364 this->indexfile = c.ReadValue("http", "index", 0);
371 index = new FileReader(ServerInstance, this->indexfile);
374 void CreateListener()
376 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, index);
379 ModuleHttp(InspIRCd* Me) : Module::Module(Me)
386 void OnEvent(Event* event)
390 char* OnRequest(Request* request)
392 ServerInstance->Log(DEBUG,"Got HTTPDocument object");
394 HTTPDocument* doc = (HTTPDocument*)request->GetData();
395 HttpSocket* sock = (HttpSocket*)doc->sock;
396 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
400 void Implements(char* List)
402 List[I_OnEvent] = List[I_OnRequest] = 1;
405 virtual ~ModuleHttp()
407 ServerInstance->SE->DelFd(http);
410 virtual Version GetVersion()
412 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
417 class ModuleHttpFactory : public ModuleFactory
428 virtual Module * CreateModule(InspIRCd* Me)
430 HttpModule = new ModuleHttp(Me);
436 extern "C" void * init_module( void )
438 return new ModuleHttpFactory;