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