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