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 * ---------------------------------------------------
24 /* $ModDesc: Provides HTTP serving facilities to modules */
28 static ModuleHttp* HttpModule;
31 /** HTTP socket states
36 HTTP_SERVE_WAIT_REQUEST = 1,
37 HTTP_SERVE_RECV_POSTDATA = 2,
38 HTTP_SERVE_SEND_DATA = 3
43 /** This class is used to handle HTTP socket timeouts
45 class HTTPTimeout : public InspTimer
48 /** HttpSocket we are attached to
51 /** Socketengine the file descriptor is in
55 /** Attach timeout to HttpSocket
57 HTTPTimeout(HttpSocket* sock, SocketEngine* engine);
60 void Tick(time_t TIME);
63 /** A socket used for HTTP transport
65 class HttpSocket : public InspSocket
68 HttpState InternalState;
69 std::stringstream headers;
71 std::string request_type;
73 std::string http_version;
74 unsigned int postsize;
80 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)
82 SI->Log(DEBUG,"HttpSocket constructor");
83 InternalState = HTTP_LISTEN;
87 HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind), postsize(0)
89 InternalState = HTTP_SERVE_WAIT_REQUEST;
90 Timeout = new HTTPTimeout(this, Instance->SE);
91 Instance->Timers->AddTimer(Timeout);
96 if (Instance->Time() < Timeout->GetTimer())
99 Instance->Timers->DelTimer(Timeout);
104 virtual int OnIncomingConnection(int newsock, char* ip)
106 if (InternalState == HTTP_LISTEN)
108 HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
109 s = s; /* Stop GCC whining */
114 virtual void OnClose()
118 std::string Response(int response)
125 return "SWITCHING PROTOCOLS";
133 return "NON-AUTHORITATIVE INFORMATION";
137 return "RESET CONTENT";
139 return "PARTIAL CONTENT";
141 return "MULTIPLE CHOICES";
143 return "MOVED PERMENANTLY";
149 return "NOT MODIFIED";
153 return "TEMPORARY REDIRECT";
155 return "BAD REQUEST";
157 return "UNAUTHORIZED";
159 return "PAYMENT REQUIRED";
165 return "METHOD NOT ALLOWED";
167 return "NOT ACCEPTABLE";
169 return "PROXY AUTHENTICATION REQUIRED";
171 return "REQUEST TIMEOUT";
177 return "LENGTH REQUIRED";
179 return "PRECONDITION FAILED";
181 return "REQUEST ENTITY TOO LARGE";
183 return "REQUEST-URI TOO LONG";
185 return "UNSUPPORTED MEDIA TYPE";
187 return "REQUESTED RANGE NOT SATISFIABLE";
189 return "EXPECTATION FAILED";
191 return "INTERNAL SERVER ERROR";
193 return "NOT IMPLEMENTED";
195 return "BAD GATEWAY";
197 return "SERVICE UNAVAILABLE";
199 return "GATEWAY TIMEOUT";
201 return "HTTP VERSION NOT SUPPORTED";
209 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
211 time_t local = this->Instance->Time();
212 struct tm *timeinfo = gmtime(&local);
213 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
214 this->Write(asctime(timeinfo));
215 if (extraheaders.empty())
217 this->Write("Content-Type: text/html\r\n");
221 this->Write(extraheaders);
223 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
224 "\r\nConnection: close\r\n\r\n");
227 virtual bool OnDataReady()
229 char* data = this->Read();
231 /* Check that the data read is a valid pointer and it has some content */
236 if (headers.str().find("\r\n\r\n") != std::string::npos)
238 if (request_type == "")
240 headers >> request_type;
242 headers >> http_version;
244 std::transform(request_type.begin(), request_type.end(), request_type.begin(), ::toupper);
245 std::transform(http_version.begin(), http_version.end(), http_version.begin(), ::toupper);
248 if ((InternalState == HTTP_SERVE_WAIT_REQUEST) && (request_type == "POST"))
250 /* Do we need to fetch postdata? */
253 InternalState = HTTP_SERVE_RECV_POSTDATA;
254 std::string header_item;
255 while (headers >> header_item)
257 if (header_item == "Content-Length:")
259 headers >> header_item;
260 postsize = atoi(header_item.c_str());
265 InternalState = HTTP_SERVE_SEND_DATA;
266 SendHeaders(0, 400, "");
267 Timeout = new HTTPTimeout(this, Instance->SE);
268 Instance->Timers->AddTimer(Timeout);
272 Instance->Log(DEBUG,"%d bytes to read for POST",postsize);
273 std::string::size_type x = headers.str().find("\r\n\r\n");
274 postdata = headers.str().substr(x+4, headers.str().length());
275 /* Get content length and store */
276 Instance->Log(DEBUG,"Initial postdata: '%s'", postdata.c_str());
277 if (postdata.length() >= postsize)
281 else if (InternalState == HTTP_SERVE_RECV_POSTDATA)
283 /* Add postdata, once we have it all, send the event */
284 amount += strlen(data);
285 postdata.append(data);
286 if (amount >= postsize)
305 /* Headers are complete */
306 InternalState = HTTP_SERVE_SEND_DATA;
308 Instance->Timers->DelTimer(Timeout);
311 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
313 SendHeaders(0, 505, "");
317 if ((request_type == "GET") && (uri == "/"))
319 SendHeaders(index->ContentSize(), 200, "");
320 this->Write(index->Contents());
325 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP(),postdata);
326 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
327 e.Send(this->Instance);
330 SendHeaders(0, 404, "");
331 Instance->Log(DEBUG,"Page not claimed, 404");
335 Timeout = new HTTPTimeout(this, Instance->SE);
336 Instance->Timers->AddTimer(Timeout);
339 void Page(std::stringstream* n, int response, std::string& extraheaders)
341 Instance->Log(DEBUG,"Sending page");
342 SendHeaders(n->str().length(), response, extraheaders);
343 this->Write(n->str());
347 HTTPTimeout::HTTPTimeout(HttpSocket* sock, SocketEngine* engine) : InspTimer(60, time(NULL)), s(sock), SE(engine)
351 void HTTPTimeout::Tick(time_t TIME)
358 class ModuleHttp : public Module
363 std::string indexfile;
373 ConfigReader c(ServerInstance);
374 this->host = c.ReadValue("http", "host", 0);
375 this->bindip = c.ReadValue("http", "ip", 0);
376 this->port = c.ReadInteger("http", "port", 0, true);
377 this->indexfile = c.ReadValue("http", "index", 0);
384 index = new FileReader(ServerInstance, this->indexfile);
387 void CreateListener()
389 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, index);
392 ModuleHttp(InspIRCd* Me) : Module::Module(Me)
399 void OnEvent(Event* event)
403 char* OnRequest(Request* request)
405 ServerInstance->Log(DEBUG,"Got HTTPDocument object");
407 HTTPDocument* doc = (HTTPDocument*)request->GetData();
408 HttpSocket* sock = (HttpSocket*)doc->sock;
409 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
413 void Implements(char* List)
415 List[I_OnEvent] = List[I_OnRequest] = 1;
418 virtual ~ModuleHttp()
420 ServerInstance->SE->DelFd(http);
423 virtual Version GetVersion()
425 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
430 class ModuleHttpFactory : public ModuleFactory
441 virtual Module * CreateModule(InspIRCd* Me)
443 HttpModule = new ModuleHttp(Me);
449 extern "C" void * init_module( void )
451 return new ModuleHttpFactory;