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