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"
24 #include "helperfuncs.h"
27 /* $ModDesc: Provides HTTP serving facilities to modules */
32 static ModuleHttp* HttpModule;
39 HTTP_SERVE_WAIT_REQUEST = 1,
40 HTTP_SERVE_SEND_DATA = 2
43 class HttpSocket : public InspSocket
46 HttpState InternalState;
47 std::stringstream headers;
51 HttpSocket(std::string host, int port, bool listening, unsigned long maxtime, FileReader* index_page) : InspSocket(host, port, listening, maxtime), index(index_page)
53 log(DEBUG,"HttpSocket constructor");
54 InternalState = HTTP_LISTEN;
57 HttpSocket(int newfd, char* ip, FileReader* ind) : InspSocket(newfd, ip), index(ind)
59 InternalState = HTTP_SERVE_WAIT_REQUEST;
62 virtual int OnIncomingConnection(int newsock, char* ip)
64 if (InternalState == HTTP_LISTEN)
66 HttpSocket* s = new HttpSocket(newsock, ip, index);
72 virtual void OnClose()
76 std::string Response(int response)
83 return "SWITCHING PROTOCOLS";
91 return "NON-AUTHORITATIVE INFORMATION";
95 return "RESET CONTENT";
97 return "PARTIAL CONTENT";
99 return "MULTIPLE CHOICES";
101 return "MOVED PERMENANTLY";
107 return "NOT MODIFIED";
111 return "TEMPORARY REDIRECT";
113 return "BAD REQUEST";
115 return "UNAUTHORIZED";
117 return "PAYMENT REQUIRED";
123 return "METHOD NOT ALLOWED";
125 return "NOT ACCEPTABLE";
127 return "PROXY AUTHENTICATION REQUIRED";
129 return "REQUEST TIMEOUT";
135 return "LENGTH REQUIRED";
137 return "PRECONDITION FAILED";
139 return "REQUEST ENTITY TOO LARGE";
141 return "REQUEST-URI TOO LONG";
143 return "UNSUPPORTED MEDIA TYPE";
145 return "REQUESTED RANGE NOT SATISFIABLE";
147 return "EXPECTATION FAILED";
149 return "INTERNAL SERVER ERROR";
151 return "NOT IMPLEMENTED";
153 return "BAD GATEWAY";
155 return "SERVICE UNAVAILABLE";
157 return "GATEWAY TIMEOUT";
159 return "HTTP VERSION NOT SUPPORTED";
167 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
169 struct tm *timeinfo = localtime(&TIME);
170 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
171 this->Write(asctime(timeinfo));
172 if (extraheaders.empty())
174 this->Write("Content-Type: text/html\r\n");
178 this->Write(extraheaders);
180 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
181 "\r\nConnection: close\r\n\r\n");
184 virtual bool OnDataReady()
186 char* data = this->Read();
187 std::string request_type;
189 std::string http_version;
191 /* Check that the data read is a valid pointer and it has some content */
196 if (headers.str().find("\r\n\r\n") != std::string::npos)
198 /* Headers are complete */
199 InternalState = HTTP_SERVE_SEND_DATA;
201 headers >> request_type;
203 headers >> http_version;
205 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
207 SendHeaders(0, 505, "");
211 if ((request_type == "GET") && (uri == "/"))
213 SendHeaders(index->ContentSize(), 200, "");
214 this->Write(index->Contents());
219 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP());
220 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
225 SendHeaders(0, 404, "");
226 log(DEBUG,"Page not claimed, 404");
237 /* Bastard client closed the socket on us!
238 * Oh wait, theyre SUPPOSED to do that!
244 void Page(std::stringstream* n, int response, std::string& extraheaders)
246 log(DEBUG,"Sending page");
247 SendHeaders(n->str().length(), response, extraheaders);
248 this->Write(n->str());
252 class ModuleHttp : public Module
257 std::string indexfile;
268 this->host = c.ReadValue("http", "host", 0);
269 this->bindip = c.ReadValue("http", "ip", 0);
270 this->port = c.ReadInteger("http", "port", 0, true);
271 this->indexfile = c.ReadValue("http", "index", 0);
273 index.LoadFile(this->indexfile);
276 void CreateListener()
278 http = new HttpSocket(this->bindip, this->port, true, 0, &index);
279 if ((http) && (http->GetState() == I_LISTENING))
281 Srv->AddSocket(http);
285 ModuleHttp(Server* Me) : Module::Module(Me)
292 void OnEvent(Event* event)
296 char* OnRequest(Request* request)
298 log(DEBUG,"Got HTTPDocument object");
300 HTTPDocument* doc = (HTTPDocument*)request->GetData();
301 HttpSocket* sock = (HttpSocket*)doc->sock;
302 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
306 void Implements(char* List)
308 List[I_OnEvent] = List[I_OnRequest] = 1;
311 virtual ~ModuleHttp()
313 Srv->DelSocket(http);
316 virtual Version GetVersion()
318 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
323 class ModuleHttpFactory : public ModuleFactory
334 virtual Module * CreateModule(Server* Me)
336 HttpModule = new ModuleHttp(Me);
342 extern "C" void * init_module( void )
344 return new ModuleHttpFactory;