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;
54 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)
56 SI->Log(DEBUG,"HttpSocket constructor");
57 InternalState = HTTP_LISTEN;
60 HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind)
62 InternalState = HTTP_SERVE_WAIT_REQUEST;
65 virtual int OnIncomingConnection(int newsock, char* ip)
67 if (InternalState == HTTP_LISTEN)
69 HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
70 s = s; /* Stop GCC whining */
75 virtual void OnClose()
79 std::string Response(int response)
86 return "SWITCHING PROTOCOLS";
94 return "NON-AUTHORITATIVE INFORMATION";
98 return "RESET CONTENT";
100 return "PARTIAL CONTENT";
102 return "MULTIPLE CHOICES";
104 return "MOVED PERMENANTLY";
110 return "NOT MODIFIED";
114 return "TEMPORARY REDIRECT";
116 return "BAD REQUEST";
118 return "UNAUTHORIZED";
120 return "PAYMENT REQUIRED";
126 return "METHOD NOT ALLOWED";
128 return "NOT ACCEPTABLE";
130 return "PROXY AUTHENTICATION REQUIRED";
132 return "REQUEST TIMEOUT";
138 return "LENGTH REQUIRED";
140 return "PRECONDITION FAILED";
142 return "REQUEST ENTITY TOO LARGE";
144 return "REQUEST-URI TOO LONG";
146 return "UNSUPPORTED MEDIA TYPE";
148 return "REQUESTED RANGE NOT SATISFIABLE";
150 return "EXPECTATION FAILED";
152 return "INTERNAL SERVER ERROR";
154 return "NOT IMPLEMENTED";
156 return "BAD GATEWAY";
158 return "SERVICE UNAVAILABLE";
160 return "GATEWAY TIMEOUT";
162 return "HTTP VERSION NOT SUPPORTED";
170 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
172 time_t local = this->Instance->Time();
173 struct tm *timeinfo = localtime(&local);
174 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
175 this->Write(asctime(timeinfo));
176 if (extraheaders.empty())
178 this->Write("Content-Type: text/html\r\n");
182 this->Write(extraheaders);
184 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
185 "\r\nConnection: close\r\n\r\n");
188 virtual bool OnDataReady()
190 char* data = this->Read();
192 /* Check that the data read is a valid pointer and it has some content */
197 if (headers.str().find("\r\n\r\n") != std::string::npos)
199 headers >> request_type;
201 headers >> http_version;
203 if ((InternalState == HTTP_SERVE_WAIT_REQUEST) && (request_type == "POST"))
205 /* Do we need to fetch postdata? */
207 InternalState = HTTP_SERVE_RECV_POSTDATA;
208 /* TODO: Get content length and store */
210 else if (InternalState == HTTP_SERVE_RECV_POSTDATA)
212 /* Add postdata, once we have it all, send the event */
216 /* Headers are complete */
217 InternalState = HTTP_SERVE_SEND_DATA;
219 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
221 SendHeaders(0, 505, "");
225 if ((request_type == "GET") && (uri == "/"))
227 SendHeaders(index->ContentSize(), 200, "");
228 this->Write(index->Contents());
233 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP(),postdata);
234 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
235 e.Send(this->Instance);
239 SendHeaders(0, 404, "");
240 Instance->Log(DEBUG,"Page not claimed, 404");
252 /* Bastard client closed the socket on us!
253 * Oh wait, theyre SUPPOSED to do that!
259 void Page(std::stringstream* n, int response, std::string& extraheaders)
261 Instance->Log(DEBUG,"Sending page");
262 SendHeaders(n->str().length(), response, extraheaders);
263 this->Write(n->str());
267 class ModuleHttp : public Module
272 std::string indexfile;
282 ConfigReader c(ServerInstance);
283 this->host = c.ReadValue("http", "host", 0);
284 this->bindip = c.ReadValue("http", "ip", 0);
285 this->port = c.ReadInteger("http", "port", 0, true);
286 this->indexfile = c.ReadValue("http", "index", 0);
293 index = new FileReader(ServerInstance, this->indexfile);
296 void CreateListener()
298 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, index);
301 ModuleHttp(InspIRCd* Me) : Module::Module(Me)
308 void OnEvent(Event* event)
312 char* OnRequest(Request* request)
314 ServerInstance->Log(DEBUG,"Got HTTPDocument object");
316 HTTPDocument* doc = (HTTPDocument*)request->GetData();
317 HttpSocket* sock = (HttpSocket*)doc->sock;
318 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
322 void Implements(char* List)
324 List[I_OnEvent] = List[I_OnRequest] = 1;
327 virtual ~ModuleHttp()
329 ServerInstance->SE->DelFd(http);
332 virtual Version GetVersion()
334 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
339 class ModuleHttpFactory : public ModuleFactory
350 virtual Module * CreateModule(InspIRCd* Me)
352 HttpModule = new ModuleHttp(Me);
358 extern "C" void * init_module( void )
360 return new ModuleHttpFactory;