]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd.cpp
761ea2d9cf345425f703ceeea70f3b57af72a031
[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
33
34 static ModuleHttp* HttpModule;
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(InspIRCd* SI, std::string host, int port, bool listening, unsigned long maxtime, FileReader* index_page) : InspSocket(SI, host, port, listening, maxtime), index(index_page)
53         {
54                 ilog(SI,DEBUG,"HttpSocket constructor");
55                 InternalState = HTTP_LISTEN;
56         }
57
58         HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, 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(this->Instance, newsock, ip, index);
68                         this->Instance->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                 time_t local = this->Instance->Time();
171                 struct tm *timeinfo = localtime(&local);
172                 this->Write("HTTP/1.1 "+ConvToStr(response)+" "+Response(response)+"\r\nDate: ");
173                 this->Write(asctime(timeinfo));
174                 if (extraheaders.empty())
175                 {
176                         this->Write("Content-Type: text/html\r\n");
177                 }
178                 else
179                 {
180                         this->Write(extraheaders);
181                 }
182                 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
183                                 "\r\nConnection: close\r\n\r\n");
184         }
185
186         virtual bool OnDataReady()
187         {
188                 char* data = this->Read();
189                 std::string request_type;
190                 std::string uri;
191                 std::string http_version;
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 are complete */
201                                 InternalState = HTTP_SERVE_SEND_DATA;
202
203                                 headers >> request_type;
204                                 headers >> uri;
205                                 headers >> http_version;
206
207                                 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
208                                 {
209                                         SendHeaders(0, 505, "");
210                                 }
211                                 else
212                                 {
213                                         if ((request_type == "GET") && (uri == "/"))
214                                         {
215                                                 SendHeaders(index->ContentSize(), 200, "");
216                                                 this->Write(index->Contents());
217                                         }
218                                         else
219                                         {
220                                                 claimed = false;
221                                                 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP());
222                                                 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
223                                                 e.Send(this->Instance);
224
225                                                 if (!claimed)
226                                                 {
227                                                         SendHeaders(0, 404, "");
228                                                         ilog(Instance,DEBUG,"Page not claimed, 404");
229                                                 }
230                                         }
231                                 }
232
233                                 return false;
234                         }
235                         return true;
236                 }
237                 else
238                 {
239                         /* Bastard client closed the socket on us!
240                          * Oh wait, theyre SUPPOSED to do that!
241                          */
242                         return false;
243                 }
244         }
245
246         void Page(std::stringstream* n, int response, std::string& extraheaders)
247         {
248                 ilog(Instance,DEBUG,"Sending page");
249                 SendHeaders(n->str().length(), response, extraheaders);
250                 this->Write(n->str());
251         }
252 };
253
254 class ModuleHttp : public Module
255 {
256         int port;
257         std::string host;
258         std::string bindip;
259         std::string indexfile;
260
261         FileReader* index;
262
263         HttpSocket* http;
264
265  public:
266
267         void ReadConfig()
268         {
269                 ConfigReader c(ServerInstance);
270                 this->host = c.ReadValue("http", "host", 0);
271                 this->bindip = c.ReadValue("http", "ip", 0);
272                 this->port = c.ReadInteger("http", "port", 0, true);
273                 this->indexfile = c.ReadValue("http", "index", 0);
274
275                 if (index)
276                 {
277                         delete index;
278                         index = NULL;
279                 }
280                 index = new FileReader(ServerInstance, this->indexfile);
281         }
282
283         void CreateListener()
284         {
285                 http = new HttpSocket(ServerInstance, this->bindip, this->port, true, 0, index);
286                 if ((http) && (http->GetState() == I_LISTENING))
287                 {
288                         ServerInstance->AddSocket(http);
289                 }
290         }
291
292         ModuleHttp(InspIRCd* Me) : Module::Module(Me)
293         {
294                 index = NULL;
295                 ReadConfig();
296                 CreateListener();
297         }
298
299         void OnEvent(Event* event)
300         {
301         }
302
303         char* OnRequest(Request* request)
304         {
305                 log(DEBUG,"Got HTTPDocument object");
306                 claimed = true;
307                 HTTPDocument* doc = (HTTPDocument*)request->GetData();
308                 HttpSocket* sock = (HttpSocket*)doc->sock;
309                 sock->Page(doc->GetDocument(), doc->GetResponseCode(), doc->GetExtraHeaders());
310                 return NULL;
311         }
312
313         void Implements(char* List)
314         {
315                 List[I_OnEvent] = List[I_OnRequest] = 1;
316         }
317
318         virtual ~ModuleHttp()
319         {
320                 ServerInstance->DelSocket(http);
321         }
322
323         virtual Version GetVersion()
324         {
325                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
326         }
327 };
328
329
330 class ModuleHttpFactory : public ModuleFactory
331 {
332  public:
333         ModuleHttpFactory()
334         {
335         }
336         
337         ~ModuleHttpFactory()
338         {
339         }
340         
341         virtual Module * CreateModule(InspIRCd* Me)
342         {
343                 HttpModule = new ModuleHttp(Me);
344                 return HttpModule;
345         }
346 };
347
348
349 extern "C" void * init_module( void )
350 {
351         return new ModuleHttpFactory;
352 }