]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd.cpp
4699d06966e6adc490412fa2f001eeb9390290e5
[user/henk/code/inspircd.git] / src / modules / m_httpd.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                     E-mail:
7  *              <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *          the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23 #include "inspsocket.h"
24 #include "helperfuncs.h"
25 #include "inspircd.h"
26 #include "httpd.h"
27
28 /* $ModDesc: Provides HTTP serving facilities to modules */
29
30 class ModuleHttp;
31
32 extern InspIRCd* ServerInstance;
33
34 static ModuleHttp* HttpModule;
35 extern time_t TIME;
36 static bool claimed;
37
38 enum HttpState
39 {
40         HTTP_LISTEN = 0,
41         HTTP_SERVE_WAIT_REQUEST = 1,
42         HTTP_SERVE_SEND_DATA = 2
43 };
44
45 class HttpSocket : public InspSocket
46 {
47         FileReader* index;
48         HttpState InternalState;
49         std::stringstream headers;
50
51  public:
52
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)
54         {
55                 log(DEBUG,"HttpSocket constructor");
56                 InternalState = HTTP_LISTEN;
57         }
58
59         HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind)
60         {
61                 InternalState = HTTP_SERVE_WAIT_REQUEST;
62         }
63
64         virtual int OnIncomingConnection(int newsock, char* ip)
65         {
66                 if (InternalState == HTTP_LISTEN)
67                 {
68                         HttpSocket* s = new HttpSocket(this->Instance, newsock, ip, index);
69                         ServerInstance->AddSocket(s);
70                 }
71                 return true;
72         }
73
74         virtual void OnClose()
75         {
76         }
77
78         std::string Response(int response)
79         {
80                 switch (response)
81                 {
82                         case 100:
83                                 return "CONTINUE";
84                         case 101:
85                                 return "SWITCHING PROTOCOLS";
86                         case 200:
87                                 return "OK";
88                         case 201:
89                                 return "CREATED";
90                         case 202:
91                                 return "ACCEPTED";
92                         case 203:
93                                 return "NON-AUTHORITATIVE INFORMATION";
94                         case 204:
95                                 return "NO CONTENT";
96                         case 205:
97                                 return "RESET CONTENT";
98                         case 206:
99                                 return "PARTIAL CONTENT";
100                         case 300:
101                                 return "MULTIPLE CHOICES";
102                         case 301:
103                                 return "MOVED PERMENANTLY";
104                         case 302:
105                                 return "FOUND";
106                         case 303:
107                                 return "SEE OTHER";
108                         case 304:
109                                 return "NOT MODIFIED";
110                         case 305:
111                                 return "USE PROXY";
112                         case 307:
113                                 return "TEMPORARY REDIRECT";
114                         case 400:
115                                 return "BAD REQUEST";
116                         case 401:
117                                 return "UNAUTHORIZED";
118                         case 402:
119                                 return "PAYMENT REQUIRED";
120                         case 403:
121                                 return "FORBIDDEN";
122                         case 404:
123                                 return "NOT FOUND";
124                         case 405:
125                                 return "METHOD NOT ALLOWED";
126                         case 406:
127                                 return "NOT ACCEPTABLE";
128                         case 407:
129                                 return "PROXY AUTHENTICATION REQUIRED";
130                         case 408:
131                                 return "REQUEST TIMEOUT";
132                         case 409:
133                                 return "CONFLICT";
134                         case 410:
135                                 return "GONE";
136                         case 411:
137                                 return "LENGTH REQUIRED";
138                         case 412:
139                                 return "PRECONDITION FAILED";
140                         case 413:
141                                 return "REQUEST ENTITY TOO LARGE";
142                         case 414:
143                                 return "REQUEST-URI TOO LONG";
144                         case 415:
145                                 return "UNSUPPORTED MEDIA TYPE";
146                         case 416:
147                                 return "REQUESTED RANGE NOT SATISFIABLE";
148                         case 417:
149                                 return "EXPECTATION FAILED";
150                         case 500:
151                                 return "INTERNAL SERVER ERROR";
152                         case 501:
153                                 return "NOT IMPLEMENTED";
154                         case 502:
155                                 return "BAD GATEWAY";
156                         case 503:
157                                 return "SERVICE UNAVAILABLE";
158                         case 504:
159                                 return "GATEWAY TIMEOUT";
160                         case 505:
161                                 return "HTTP VERSION NOT SUPPORTED";
162                         default:
163                                 return "WTF";
164                         break;
165                                 
166                 }
167         }
168
169         void SendHeaders(unsigned long size, int response, const std::string &extraheaders)
170         {
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())
175                 {
176                         this->Write("Content-Type: text/html\r\n");
177                 }
178                 else
179                 {
180                         this->Write(extraheaders);
181                 }
182                 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
183                                 "\r\nConnection: close\r\n\r\n");
184         }
185
186         virtual bool OnDataReady()
187         {
188                 char* data = this->Read();
189                 std::string request_type;
190                 std::string uri;
191                 std::string http_version;
192
193                 /* Check that the data read is a valid pointer and it has some content */
194                 if (data && *data)
195                 {
196                         headers << data;
197
198                         if (headers.str().find("\r\n\r\n") != std::string::npos)
199                         {
200                                 /* Headers are complete */
201                                 InternalState = HTTP_SERVE_SEND_DATA;
202
203                                 headers >> request_type;
204                                 headers >> uri;
205                                 headers >> http_version;
206
207                                 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
208                                 {
209                                         SendHeaders(0, 505, "");
210                                 }
211                                 else
212                                 {
213                                         if ((request_type == "GET") && (uri == "/"))
214                                         {
215                                                 SendHeaders(index->ContentSize(), 200, "");
216                                                 this->Write(index->Contents());
217                                         }
218                                         else
219                                         {
220                                                 claimed = false;
221                                                 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP());
222                                                 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
223                                                 e.Send();
224
225                                                 if (!claimed)
226                                                 {
227                                                         SendHeaders(0, 404, "");
228                                                         log(DEBUG,"Page not claimed, 404");
229                                                 }
230                                         }
231                                 }
232
233                                 return false;
234                         }
235                         return true;
236                 }
237                 else
238                 {
239                         /* Bastard client closed the socket on us!
240                          * Oh wait, theyre SUPPOSED to do that!
241                          */
242                         return false;
243                 }
244         }
245
246         void Page(std::stringstream* n, int response, std::string& extraheaders)
247         {
248                 log(DEBUG,"Sending page");
249                 SendHeaders(n->str().length(), response, extraheaders);
250                 this->Write(n->str());
251         }
252 };
253
254 class ModuleHttp : public Module
255 {
256         int port;
257         std::string host;
258         std::string bindip;
259         std::string indexfile;
260
261         FileReader index;
262
263         HttpSocket* http;
264
265  public:
266
267         void ReadConfig()
268         {
269                 ConfigReader c;
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);
274
275                 index.LoadFile(this->indexfile);
276         }
277
278         void CreateListener()
279         {
280                 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, &index);
281                 if ((http) && (http->GetState() == I_LISTENING))
282                 {
283                         ServerInstance->AddSocket(http);
284                 }
285         }
286
287         ModuleHttp(InspIRCd* Me) : Module::Module(Me)
288         {
289                 
290                 ReadConfig();
291                 CreateListener();
292         }
293
294         void OnEvent(Event* event)
295         {
296         }
297
298         char* OnRequest(Request* request)
299         {
300                 log(DEBUG,"Got HTTPDocument object");
301                 claimed = true;
302                 HTTPDocument* doc = (HTTPDocument*)request->GetData();
303                 HttpSocket* sock = (HttpSocket*)doc->sock;
304                 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
305                 return NULL;
306         }
307
308         void Implements(char* List)
309         {
310                 List[I_OnEvent] = List[I_OnRequest] = 1;
311         }
312
313         virtual ~ModuleHttp()
314         {
315                 ServerInstance->DelSocket(http);
316         }
317
318         virtual Version GetVersion()
319         {
320                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
321         }
322 };
323
324
325 class ModuleHttpFactory : public ModuleFactory
326 {
327  public:
328         ModuleHttpFactory()
329         {
330         }
331         
332         ~ModuleHttpFactory()
333         {
334         }
335         
336         virtual Module * CreateModule(InspIRCd* Me)
337         {
338                 HttpModule = new ModuleHttp(Me);
339                 return HttpModule;
340         }
341 };
342
343
344 extern "C" void * init_module( void )
345 {
346         return new ModuleHttpFactory;
347 }