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"
28 /* $ModDesc: Provides HTTP serving facilities to modules */
34 static ModuleHttp* HttpModule;
40 HTTP_SERVE_WAIT_REQUEST = 1,
41 HTTP_SERVE_SEND_DATA = 2
44 class HttpSocket : public InspSocket
47 HttpState InternalState;
48 std::stringstream headers;
52 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)
54 SI->Log(DEBUG,"HttpSocket constructor");
55 InternalState = HTTP_LISTEN;
58 HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind)
60 InternalState = HTTP_SERVE_WAIT_REQUEST;
63 virtual int OnIncomingConnection(int newsock, char* ip)
65 if (InternalState == HTTP_LISTEN)
67 HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
68 this->Instance->AddSocket(s);
73 virtual void OnClose()
77 std::string Response(int response)
84 return "SWITCHING PROTOCOLS";
92 return "NON-AUTHORITATIVE INFORMATION";
96 return "RESET CONTENT";
98 return "PARTIAL CONTENT";
100 return "MULTIPLE CHOICES";
102 return "MOVED PERMENANTLY";
108 return "NOT MODIFIED";
112 return "TEMPORARY REDIRECT";
114 return "BAD REQUEST";
116 return "UNAUTHORIZED";
118 return "PAYMENT REQUIRED";
124 return "METHOD NOT ALLOWED";
126 return "NOT ACCEPTABLE";
128 return "PROXY AUTHENTICATION REQUIRED";
130 return "REQUEST TIMEOUT";
136 return "LENGTH REQUIRED";
138 return "PRECONDITION FAILED";
140 return "REQUEST ENTITY TOO LARGE";
142 return "REQUEST-URI TOO LONG";
144 return "UNSUPPORTED MEDIA TYPE";
146 return "REQUESTED RANGE NOT SATISFIABLE";
148 return "EXPECTATION FAILED";
150 return "INTERNAL SERVER ERROR";
152 return "NOT IMPLEMENTED";
154 return "BAD GATEWAY";
156 return "SERVICE UNAVAILABLE";
158 return "GATEWAY TIMEOUT";
160 return "HTTP VERSION NOT SUPPORTED";
168 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
170 time_t local = this->Instance->Time();
171 struct tm *timeinfo = localtime(&local);
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");
223 e.Send(this->Instance);
227 SendHeaders(0, 404, "");
228 Instance->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 Instance->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;
269 ConfigReader c(ServerInstance);
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);
280 index = new FileReader(ServerInstance, this->indexfile);
283 void CreateListener()
285 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, index);
286 if ((http) && (http->GetState() == I_LISTENING))
288 ServerInstance->AddSocket(http);
292 ModuleHttp(InspIRCd* Me) : Module::Module(Me)
299 void OnEvent(Event* event)
303 char* OnRequest(Request* request)
305 ServerInstance->Log(DEBUG,"Got HTTPDocument object");
307 HTTPDocument* doc = (HTTPDocument*)request->GetData();
308 HttpSocket* sock = (HttpSocket*)doc->sock;
309 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
313 void Implements(char* List)
315 List[I_OnEvent] = List[I_OnRequest] = 1;
318 virtual ~ModuleHttp()
320 ServerInstance->DelSocket(http);
323 virtual Version GetVersion()
325 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
330 class ModuleHttpFactory : public ModuleFactory
341 virtual Module * CreateModule(InspIRCd* Me)
343 HttpModule = new ModuleHttp(Me);
349 extern "C" void * init_module( void )
351 return new ModuleHttpFactory;