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