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
44 /** A socket used for HTTP transport
46 class HttpSocket : public InspSocket
49 HttpState InternalState;
50 std::stringstream headers;
52 std::string request_type;
54 std::string http_version;
55 unsigned int postsize;
60 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)
62 SI->Log(DEBUG,"HttpSocket constructor");
63 InternalState = HTTP_LISTEN;
66 HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind), postsize(0)
68 InternalState = HTTP_SERVE_WAIT_REQUEST;
71 virtual int OnIncomingConnection(int newsock, char* ip)
73 if (InternalState == HTTP_LISTEN)
75 HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
76 s = s; /* Stop GCC whining */
81 virtual void OnClose()
85 std::string Response(int response)
92 return "SWITCHING PROTOCOLS";
100 return "NON-AUTHORITATIVE INFORMATION";
104 return "RESET CONTENT";
106 return "PARTIAL CONTENT";
108 return "MULTIPLE CHOICES";
110 return "MOVED PERMENANTLY";
116 return "NOT MODIFIED";
120 return "TEMPORARY REDIRECT";
122 return "BAD REQUEST";
124 return "UNAUTHORIZED";
126 return "PAYMENT REQUIRED";
132 return "METHOD NOT ALLOWED";
134 return "NOT ACCEPTABLE";
136 return "PROXY AUTHENTICATION REQUIRED";
138 return "REQUEST TIMEOUT";
144 return "LENGTH REQUIRED";
146 return "PRECONDITION FAILED";
148 return "REQUEST ENTITY TOO LARGE";
150 return "REQUEST-URI TOO LONG";
152 return "UNSUPPORTED MEDIA TYPE";
154 return "REQUESTED RANGE NOT SATISFIABLE";
156 return "EXPECTATION FAILED";
158 return "INTERNAL SERVER ERROR";
160 return "NOT IMPLEMENTED";
162 return "BAD GATEWAY";
164 return "SERVICE UNAVAILABLE";
166 return "GATEWAY TIMEOUT";
168 return "HTTP VERSION NOT SUPPORTED";
176 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
178 time_t local = this->Instance->Time();
179 struct tm *timeinfo = gmtime(&local);
180 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
181 this->Write(asctime(timeinfo));
182 if (extraheaders.empty())
184 this->Write("Content-Type: text/html\r\n");
188 this->Write(extraheaders);
190 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
191 "\r\nConnection: close\r\n\r\n");
194 virtual bool OnDataReady()
196 char* data = this->Read();
198 /* Check that the data read is a valid pointer and it has some content */
203 if (headers.str().find("\r\n\r\n") != std::string::npos)
205 if (request_type == "")
207 headers >> request_type;
209 headers >> http_version;
212 if ((InternalState == HTTP_SERVE_WAIT_REQUEST) && (request_type == "POST"))
214 /* Do we need to fetch postdata? */
217 InternalState = HTTP_SERVE_RECV_POSTDATA;
218 std::string header_item;
219 while (headers >> header_item)
221 if (header_item == "Content-Length:")
223 headers >> header_item;
224 postsize = atoi(header_item.c_str());
229 InternalState = HTTP_SERVE_SEND_DATA;
230 SendHeaders(0, 400, "");
234 Instance->Log(DEBUG,"%d bytes to read for POST",postsize);
235 std::string::size_type x = headers.str().find("\r\n\r\n");
236 postdata = headers.str().substr(x+4, headers.str().length());
237 /* Get content length and store */
238 Instance->Log(DEBUG,"Initial postdata: '%s'", postdata.c_str());
239 if (postdata.length() >= postsize)
243 else if (InternalState == HTTP_SERVE_RECV_POSTDATA)
245 /* Add postdata, once we have it all, send the event */
246 amount += strlen(data);
247 postdata.append(data);
248 if (amount >= postsize)
267 /* Headers are complete */
268 InternalState = HTTP_SERVE_SEND_DATA;
270 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
272 SendHeaders(0, 505, "");
276 if ((request_type == "GET") && (uri == "/"))
278 SendHeaders(index->ContentSize(), 200, "");
279 this->Write(index->Contents());
284 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP(),postdata);
285 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
286 e.Send(this->Instance);
289 SendHeaders(0, 404, "");
290 Instance->Log(DEBUG,"Page not claimed, 404");
296 void Page(std::stringstream* n, int response, std::string& extraheaders)
298 Instance->Log(DEBUG,"Sending page");
299 SendHeaders(n->str().length(), response, extraheaders);
300 this->Write(n->str());
304 class ModuleHttp : public Module
309 std::string indexfile;
319 ConfigReader c(ServerInstance);
320 this->host = c.ReadValue("http", "host", 0);
321 this->bindip = c.ReadValue("http", "ip", 0);
322 this->port = c.ReadInteger("http", "port", 0, true);
323 this->indexfile = c.ReadValue("http", "index", 0);
330 index = new FileReader(ServerInstance, this->indexfile);
333 void CreateListener()
335 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, index);
338 ModuleHttp(InspIRCd* Me) : Module::Module(Me)
345 void OnEvent(Event* event)
349 char* OnRequest(Request* request)
351 ServerInstance->Log(DEBUG,"Got HTTPDocument object");
353 HTTPDocument* doc = (HTTPDocument*)request->GetData();
354 HttpSocket* sock = (HttpSocket*)doc->sock;
355 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
359 void Implements(char* List)
361 List[I_OnEvent] = List[I_OnRequest] = 1;
364 virtual ~ModuleHttp()
366 ServerInstance->SE->DelFd(http);
369 virtual Version GetVersion()
371 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
376 class ModuleHttpFactory : public ModuleFactory
387 virtual Module * CreateModule(InspIRCd* Me)
389 HttpModule = new ModuleHttp(Me);
395 extern "C" void * init_module( void )
397 return new ModuleHttpFactory;