]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd.cpp
dc32c02278fecf779d2e321c02cdc77226ca17a5
[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 "httpd.h"
26
27 /* $ModDesc: Provides HTTP serving facilities to modules */
28
29 class ModuleHttp;
30
31 static Server *Srv;
32 ModuleHttp* HttpModule;
33 extern time_t TIME;
34
35 enum HttpState
36 {
37         HTTP_LISTEN = 0,
38         HTTP_SERVE_WAIT_REQUEST = 1,
39         HTTP_SERVE_SEND_DATA = 2
40 };
41
42 class HttpSocket : public InspSocket
43 {
44         FileReader* index;
45         HttpState InternalState;
46         std::stringstream headers;
47
48  public:
49
50         HttpSocket(std::string host, int port, bool listening, unsigned long maxtime, FileReader* index_page) : InspSocket(host, port, listening, maxtime), index(index_page)
51         {
52                 log(DEBUG,"HttpSocket constructor");
53                 InternalState = HTTP_LISTEN;
54         }
55
56         HttpSocket(int newfd, char* ip, FileReader* ind) : InspSocket(newfd, ip), index(ind)
57         {
58                 InternalState = HTTP_SERVE_WAIT_REQUEST;
59         }
60
61         virtual int OnIncomingConnection(int newsock, char* ip)
62         {
63                 if (InternalState == HTTP_LISTEN)
64                 {
65                         HttpSocket* s = new HttpSocket(newsock, ip, index);
66                         Srv->AddSocket(s);
67                 }
68                 return true;
69         }
70
71         virtual void OnClose()
72         {
73         }
74
75         void SendHeaders(unsigned long size)
76         {
77                 struct tm *timeinfo = localtime(&TIME);
78                 this->Write("HTTP/1.1 200 OK\r\nDate: ");
79                 this->Write(asctime(timeinfo));
80                 this->Write("Server: InspIRCd/m_httpd.so/1.1\r\nContent-Length: "+ConvToStr(size)+
81                                 "\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n");
82         }
83
84         virtual bool OnDataReady()
85         {
86                 char* data = this->Read();
87                 std::string request_type;
88                 std::string uri;
89
90                 /* Check that the data read is a valid pointer and it has some content */
91                 if (data && *data)
92                 {
93                         headers << data;
94
95                         if (headers.str().find("\r\n\r\n") != std::string::npos)
96                         {
97                                 /* Headers are complete */
98                                 InternalState = HTTP_SERVE_SEND_DATA;
99
100                                 headers >> request_type;
101                                 headers >> uri;
102
103                                 if ((request_type == "GET") && (uri == "/"))
104                                 {
105                                         SendHeaders(index->ContentSize());
106                                         this->Write(index->Contents());
107                                 }
108                                 else
109                                 {
110                                         HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP());
111                                         Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
112                                         e.Send();
113                                 }
114
115                                 return false;
116                         }
117                         return true;
118                 }
119                 else
120                 {
121                         /* Bastard client closed the socket on us!
122                          * Oh wait, theyre SUPPOSED to do that!
123                          */
124                         return false;
125                 }
126         }
127 };
128
129 class ModuleHttp : public Module
130 {
131         int port;
132         std::string host;
133         std::string bindip;
134         std::string indexfile;
135
136         FileReader index;
137
138         HttpSocket* http;
139
140  public:
141
142         void ReadConfig()
143         {
144                 ConfigReader c;
145                 this->host = c.ReadValue("http", "host", 0);
146                 this->bindip = c.ReadValue("http", "ip", 0);
147                 this->port = c.ReadInteger("http", "port", 0, true);
148                 this->indexfile = c.ReadValue("http", "index", 0);
149
150                 index.LoadFile(this->indexfile);
151         }
152
153         void CreateListener()
154         {
155                 http = new HttpSocket(this->bindip, this->port, true, 0, &index);
156                 if ((http) && (http->GetState() == I_LISTENING))
157                 {
158                         Srv->AddSocket(http);
159                 }
160         }
161
162         ModuleHttp(Server* Me) : Module::Module(Me)
163         {
164                 Srv = Me;
165                 ReadConfig();
166                 CreateListener();
167         }
168
169         void OnEvent(Event* event)
170         {
171         }
172
173         char* OnRequest(Request* request)
174         {
175                 return NULL;
176         }
177
178         void Implements(char* List)
179         {
180                 List[I_OnEvent] = List[I_OnRequest] = 1;
181         }
182
183         virtual ~ModuleHttp()
184         {
185                 Srv->DelSocket(http);
186         }
187
188         virtual Version GetVersion()
189         {
190                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
191         }
192 };
193
194
195 class ModuleHttpFactory : public ModuleFactory
196 {
197  public:
198         ModuleHttpFactory()
199         {
200         }
201         
202         ~ModuleHttpFactory()
203         {
204         }
205         
206         virtual Module * CreateModule(Server* Me)
207         {
208                 HttpModule = new ModuleHttp(Me);
209                 return HttpModule;
210         }
211 };
212
213
214 extern "C" void * init_module( void )
215 {
216         return new ModuleHttpFactory;
217 }