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