]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_http.cpp
Add FileReader::ContentSize and FileReader::Contents
[user/henk/code/inspircd.git] / src / modules / m_http.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) : InspSocket(newfd, ip)
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);
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_http.so/1.1\r\nContent-Length: "+ConvToStr(index->FileSize())+"\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n");
77         }
78
79         virtual bool OnDataReady()
80         {
81                 char* data = this->Read();
82                 /* Check that the data read is a valid pointer and it has some content */
83                 if (data && *data)
84                 {
85                         headers << data;
86
87                         if (headers.str().find("\r\n\r\n"))
88                         {
89                                 /* Headers are complete */
90                                 InternalState = HTTP_SERVE_SEND_DATA;
91                                 SendHeaders();
92
93                                 this->Write(index->Contents());
94
95                                 /* This clever hax makes InspSocket think its
96                                  * in a connecting state, and time out 2 seconds
97                                  * from now. Most apache servers do this if the
98                                  * client doesnt close the connection as its
99                                  * supposed to.
100                                  */
101                                 this->timeout_end = TIME + 2;
102                                 this->SetState(I_CONNECTING);
103                         }
104                         return true;
105                 }
106                 else
107                 {
108                         /* Bastard client closed the socket on us!
109                          * Oh wait, theyre SUPPOSED to do that!
110                          */
111                         return false;
112                 }
113         }
114 };
115
116 class ModuleHttp : public Module
117 {
118         int port;
119         std::string host;
120         std::string bindip;
121         std::string indexfile;
122
123         FileReader index;
124
125         HttpSocket* http;
126
127  public:
128
129         void ReadConfig()
130         {
131                 ConfigReader c;
132                 this->host = c.ReadValue("http", "host", 0);
133                 this->bindip = c.ReadValue("http", "ip", 0);
134                 this->port = c.ReadInteger("http", "port", 0, true);
135                 this->indexfile = c.ReadValue("http", "index", 0);
136
137                 index.LoadFile(this->indexfile);
138         }
139
140         void CreateListener()
141         {
142                 http = new HttpSocket(this->bindip, this->port, true, 0, &index);
143                 if ((http) && (http->GetState() == I_LISTENING))
144                 {
145                         Srv->AddSocket(http);
146                 }
147         }
148
149         ModuleHttp(Server* Me) : Module::Module(Me)
150         {
151                 Srv = Me;
152                 ReadConfig();
153                 CreateListener();
154         }
155
156         void Implements(char* List)
157         {
158                 List[I_OnEvent] = List[I_OnRequest] = 1;
159         }
160
161         virtual ~ModuleHttp()
162         {
163                 Srv->DelSocket(http);
164         }
165
166         virtual Version GetVersion()
167         {
168                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
169         }
170 };
171
172
173 class ModuleHttpFactory : public ModuleFactory
174 {
175  public:
176         ModuleHttpFactory()
177         {
178         }
179         
180         ~ModuleHttpFactory()
181         {
182         }
183         
184         virtual Module * CreateModule(Server* Me)
185         {
186                 return new ModuleHttp(Me);
187         }
188 };
189
190
191 extern "C" void * init_module( void )
192 {
193         return new ModuleHttpFactory;
194 }