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