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;
37 HTTP_SERVE_WAIT_REQUEST = 1,
38 HTTP_SERVE_RECV_POSTDATA = 2,
39 HTTP_SERVE_SEND_DATA = 3
42 class HttpSocket : public InspSocket
45 HttpState InternalState;
46 std::stringstream headers;
48 std::string request_type;
50 std::string http_version;
51 unsigned int postsize;
56 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)
58 SI->Log(DEBUG,"HttpSocket constructor");
59 InternalState = HTTP_LISTEN;
62 HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind), postsize(0)
64 InternalState = HTTP_SERVE_WAIT_REQUEST;
67 virtual int OnIncomingConnection(int newsock, char* ip)
69 if (InternalState == HTTP_LISTEN)
71 HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
72 s = s; /* Stop GCC whining */
77 virtual void OnClose()
81 std::string Response(int response)
88 return "SWITCHING PROTOCOLS";
96 return "NON-AUTHORITATIVE INFORMATION";
100 return "RESET CONTENT";
102 return "PARTIAL CONTENT";
104 return "MULTIPLE CHOICES";
106 return "MOVED PERMENANTLY";
112 return "NOT MODIFIED";
116 return "TEMPORARY REDIRECT";
118 return "BAD REQUEST";
120 return "UNAUTHORIZED";
122 return "PAYMENT REQUIRED";
128 return "METHOD NOT ALLOWED";
130 return "NOT ACCEPTABLE";
132 return "PROXY AUTHENTICATION REQUIRED";
134 return "REQUEST TIMEOUT";
140 return "LENGTH REQUIRED";
142 return "PRECONDITION FAILED";
144 return "REQUEST ENTITY TOO LARGE";
146 return "REQUEST-URI TOO LONG";
148 return "UNSUPPORTED MEDIA TYPE";
150 return "REQUESTED RANGE NOT SATISFIABLE";
152 return "EXPECTATION FAILED";
154 return "INTERNAL SERVER ERROR";
156 return "NOT IMPLEMENTED";
158 return "BAD GATEWAY";
160 return "SERVICE UNAVAILABLE";
162 return "GATEWAY TIMEOUT";
164 return "HTTP VERSION NOT SUPPORTED";
172 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
174 time_t local = this->Instance->Time();
175 struct tm *timeinfo = localtime(&local);
176 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
177 this->Write(asctime(timeinfo));
178 if (extraheaders.empty())
180 this->Write("Content-Type: text/html\r\n");
184 this->Write(extraheaders);
186 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
187 "\r\nConnection: close\r\n\r\n");
190 virtual bool OnDataReady()
192 char* data = this->Read();
194 /* Check that the data read is a valid pointer and it has some content */
199 if (headers.str().find("\r\n\r\n") != std::string::npos)
201 if (request_type == "")
203 headers >> request_type;
205 headers >> http_version;
208 if ((InternalState == HTTP_SERVE_WAIT_REQUEST) && (request_type == "POST"))
210 /* Do we need to fetch postdata? */
213 InternalState = HTTP_SERVE_RECV_POSTDATA;
214 std::string header_item;
215 while (headers >> header_item)
217 if (header_item == "Content-Length:")
219 headers >> header_item;
220 postsize = atoi(header_item.c_str());
225 InternalState = HTTP_SERVE_SEND_DATA;
226 SendHeaders(0, 400, "");
230 Instance->Log(DEBUG,"%d bytes to read for POST",postsize);
231 std::string::size_type x = headers.str().find("\r\n\r\n");
232 postdata = headers.str().substr(x+5, headers.str().length());
233 /* Get content length and store */
234 if (postdata.length() >= postsize)
238 else if (InternalState == HTTP_SERVE_RECV_POSTDATA)
240 /* Add postdata, once we have it all, send the event */
241 amount += strlen(data);
242 postdata.append(data);
243 if (amount >= postsize)
262 /* Headers are complete */
263 InternalState = HTTP_SERVE_SEND_DATA;
265 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
267 SendHeaders(0, 505, "");
271 if ((request_type == "GET") && (uri == "/"))
273 SendHeaders(index->ContentSize(), 200, "");
274 this->Write(index->Contents());
279 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP(),postdata);
280 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
281 e.Send(this->Instance);
284 SendHeaders(0, 404, "");
285 Instance->Log(DEBUG,"Page not claimed, 404");
291 void Page(std::stringstream* n, int response, std::string& extraheaders)
293 Instance->Log(DEBUG,"Sending page");
294 SendHeaders(n->str().length(), response, extraheaders);
295 this->Write(n->str());
299 class ModuleHttp : public Module
304 std::string indexfile;
314 ConfigReader c(ServerInstance);
315 this->host = c.ReadValue("http", "host", 0);
316 this->bindip = c.ReadValue("http", "ip", 0);
317 this->port = c.ReadInteger("http", "port", 0, true);
318 this->indexfile = c.ReadValue("http", "index", 0);
325 index = new FileReader(ServerInstance, this->indexfile);
328 void CreateListener()
330 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, index);
333 ModuleHttp(InspIRCd* Me) : Module::Module(Me)
340 void OnEvent(Event* event)
344 char* OnRequest(Request* request)
346 ServerInstance->Log(DEBUG,"Got HTTPDocument object");
348 HTTPDocument* doc = (HTTPDocument*)request->GetData();
349 HttpSocket* sock = (HttpSocket*)doc->sock;
350 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
354 void Implements(char* List)
356 List[I_OnEvent] = List[I_OnRequest] = 1;
359 virtual ~ModuleHttp()
361 ServerInstance->SE->DelFd(http);
364 virtual Version GetVersion()
366 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
371 class ModuleHttpFactory : public ModuleFactory
382 virtual Module * CreateModule(InspIRCd* Me)
384 HttpModule = new ModuleHttp(Me);
390 extern "C" void * init_module( void )
392 return new ModuleHttpFactory;