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;
55 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)
57 SI->Log(DEBUG,"HttpSocket constructor");
58 InternalState = HTTP_LISTEN;
61 HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind), postsize(0)
63 InternalState = HTTP_SERVE_WAIT_REQUEST;
66 virtual int OnIncomingConnection(int newsock, char* ip)
68 if (InternalState == HTTP_LISTEN)
70 HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
71 s = s; /* Stop GCC whining */
76 virtual void OnClose()
80 std::string Response(int response)
87 return "SWITCHING PROTOCOLS";
95 return "NON-AUTHORITATIVE INFORMATION";
99 return "RESET CONTENT";
101 return "PARTIAL CONTENT";
103 return "MULTIPLE CHOICES";
105 return "MOVED PERMENANTLY";
111 return "NOT MODIFIED";
115 return "TEMPORARY REDIRECT";
117 return "BAD REQUEST";
119 return "UNAUTHORIZED";
121 return "PAYMENT REQUIRED";
127 return "METHOD NOT ALLOWED";
129 return "NOT ACCEPTABLE";
131 return "PROXY AUTHENTICATION REQUIRED";
133 return "REQUEST TIMEOUT";
139 return "LENGTH REQUIRED";
141 return "PRECONDITION FAILED";
143 return "REQUEST ENTITY TOO LARGE";
145 return "REQUEST-URI TOO LONG";
147 return "UNSUPPORTED MEDIA TYPE";
149 return "REQUESTED RANGE NOT SATISFIABLE";
151 return "EXPECTATION FAILED";
153 return "INTERNAL SERVER ERROR";
155 return "NOT IMPLEMENTED";
157 return "BAD GATEWAY";
159 return "SERVICE UNAVAILABLE";
161 return "GATEWAY TIMEOUT";
163 return "HTTP VERSION NOT SUPPORTED";
171 void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
173 time_t local = this->Instance->Time();
174 struct tm *timeinfo = localtime(&local);
175 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
176 this->Write(asctime(timeinfo));
177 if (extraheaders.empty())
179 this->Write("Content-Type: text/html\r\n");
183 this->Write(extraheaders);
185 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
186 "\r\nConnection: close\r\n\r\n");
189 virtual bool OnDataReady()
191 char* data = this->Read();
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 >> request_type;
202 headers >> http_version;
204 if ((InternalState == HTTP_SERVE_WAIT_REQUEST) && (request_type == "POST"))
206 /* Do we need to fetch postdata? */
208 InternalState = HTTP_SERVE_RECV_POSTDATA;
209 /* TODO: Get content length and store */
211 else if (InternalState == HTTP_SERVE_RECV_POSTDATA)
213 /* Add postdata, once we have it all, send the event */
217 /* Headers are complete */
218 InternalState = HTTP_SERVE_SEND_DATA;
220 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
222 SendHeaders(0, 505, "");
226 if ((request_type == "GET") && (uri == "/"))
228 SendHeaders(index->ContentSize(), 200, "");
229 this->Write(index->Contents());
234 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP(),postdata);
235 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
236 e.Send(this->Instance);
240 SendHeaders(0, 404, "");
241 Instance->Log(DEBUG,"Page not claimed, 404");
253 /* Bastard client closed the socket on us!
254 * Oh wait, theyre SUPPOSED to do that!
260 void Page(std::stringstream* n, int response, std::string& extraheaders)
262 Instance->Log(DEBUG,"Sending page");
263 SendHeaders(n->str().length(), response, extraheaders);
264 this->Write(n->str());
268 class ModuleHttp : public Module
273 std::string indexfile;
283 ConfigReader c(ServerInstance);
284 this->host = c.ReadValue("http", "host", 0);
285 this->bindip = c.ReadValue("http", "ip", 0);
286 this->port = c.ReadInteger("http", "port", 0, true);
287 this->indexfile = c.ReadValue("http", "index", 0);
294 index = new FileReader(ServerInstance, this->indexfile);
297 void CreateListener()
299 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, index);
302 ModuleHttp(InspIRCd* Me) : Module::Module(Me)
309 void OnEvent(Event* event)
313 char* OnRequest(Request* request)
315 ServerInstance->Log(DEBUG,"Got HTTPDocument object");
317 HTTPDocument* doc = (HTTPDocument*)request->GetData();
318 HttpSocket* sock = (HttpSocket*)doc->sock;
319 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
323 void Implements(char* List)
325 List[I_OnEvent] = List[I_OnRequest] = 1;
328 virtual ~ModuleHttp()
330 ServerInstance->SE->DelFd(http);
333 virtual Version GetVersion()
335 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
340 class ModuleHttpFactory : public ModuleFactory
351 virtual Module * CreateModule(InspIRCd* Me)
353 HttpModule = new ModuleHttp(Me);
359 extern "C" void * init_module( void )
361 return new ModuleHttpFactory;