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