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"
28 /* $ModDesc: Provides HTTP serving facilities to modules */
34 static ModuleHttp* HttpModule;
41 HTTP_SERVE_WAIT_REQUEST = 1,
42 HTTP_SERVE_SEND_DATA = 2
45 class HttpSocket : public InspSocket
48 HttpState InternalState;
49 std::stringstream headers;
53 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)
55 log(DEBUG,"HttpSocket constructor");
56 InternalState = HTTP_LISTEN;
59 HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind)
61 InternalState = HTTP_SERVE_WAIT_REQUEST;
64 virtual int OnIncomingConnection(int newsock, char* ip)
66 if (InternalState == HTTP_LISTEN)
68 HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
69 this->Instance->AddSocket(s);
74 virtual void OnClose()
78 std::string Response(int response)
85 return "SWITCHING PROTOCOLS";
93 return "NON-AUTHORITATIVE INFORMATION";
97 return "RESET CONTENT";
99 return "PARTIAL CONTENT";
101 return "MULTIPLE CHOICES";
103 return "MOVED PERMENANTLY";
109 return "NOT MODIFIED";
113 return "TEMPORARY REDIRECT";
115 return "BAD REQUEST";
117 return "UNAUTHORIZED";
119 return "PAYMENT REQUIRED";
125 return "METHOD NOT ALLOWED";
127 return "NOT ACCEPTABLE";
129 return "PROXY AUTHENTICATION REQUIRED";
131 return "REQUEST TIMEOUT";
137 return "LENGTH REQUIRED";
139 return "PRECONDITION FAILED";
141 return "REQUEST ENTITY TOO LARGE";
143 return "REQUEST-URI TOO LONG";
145 return "UNSUPPORTED MEDIA TYPE";
147 return "REQUESTED RANGE NOT SATISFIABLE";
149 return "EXPECTATION FAILED";
151 return "INTERNAL SERVER ERROR";
153 return "NOT IMPLEMENTED";
155 return "BAD GATEWAY";
157 return "SERVICE UNAVAILABLE";
159 return "GATEWAY TIMEOUT";
161 return "HTTP VERSION NOT SUPPORTED";
169 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
171 struct tm *timeinfo = localtime(&TIME);
172 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
173 this->Write(asctime(timeinfo));
174 if (extraheaders.empty())
176 this->Write("Content-Type: text/html\r\n");
180 this->Write(extraheaders);
182 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
183 "\r\nConnection: close\r\n\r\n");
186 virtual bool OnDataReady()
188 char* data = this->Read();
189 std::string request_type;
191 std::string http_version;
193 /* Check that the data read is a valid pointer and it has some content */
198 if (headers.str().find("\r\n\r\n") != std::string::npos)
200 /* Headers are complete */
201 InternalState = HTTP_SERVE_SEND_DATA;
203 headers >> request_type;
205 headers >> http_version;
207 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
209 SendHeaders(0, 505, "");
213 if ((request_type == "GET") && (uri == "/"))
215 SendHeaders(index->ContentSize(), 200, "");
216 this->Write(index->Contents());
221 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP());
222 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
227 SendHeaders(0, 404, "");
228 log(DEBUG,"Page not claimed, 404");
239 /* Bastard client closed the socket on us!
240 * Oh wait, theyre SUPPOSED to do that!
246 void Page(std::stringstream* n, int response, std::string& extraheaders)
248 log(DEBUG,"Sending page");
249 SendHeaders(n->str().length(), response, extraheaders);
250 this->Write(n->str());
254 class ModuleHttp : public Module
259 std::string indexfile;
270 this->host = c.ReadValue("http", "host", 0);
271 this->bindip = c.ReadValue("http", "ip", 0);
272 this->port = c.ReadInteger("http", "port", 0, true);
273 this->indexfile = c.ReadValue("http", "index", 0);
275 index.LoadFile(this->indexfile);
278 void CreateListener()
280 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, &index);
281 if ((http) && (http->GetState() == I_LISTENING))
283 ServerInstance->AddSocket(http);
287 ModuleHttp(InspIRCd* Me) : Module::Module(Me)
294 void OnEvent(Event* event)
298 char* OnRequest(Request* request)
300 log(DEBUG,"Got HTTPDocument object");
302 HTTPDocument* doc = (HTTPDocument*)request->GetData();
303 HttpSocket* sock = (HttpSocket*)doc->sock;
304 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
308 void Implements(char* List)
310 List[I_OnEvent] = List[I_OnRequest] = 1;
313 virtual ~ModuleHttp()
315 ServerInstance->DelSocket(http);
318 virtual Version GetVersion()
320 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
325 class ModuleHttpFactory : public ModuleFactory
336 virtual Module * CreateModule(InspIRCd* Me)
338 HttpModule = new ModuleHttp(Me);
344 extern "C" void * init_module( void )
346 return new ModuleHttpFactory;