]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_http.cpp
Try to make this serve some static content
[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
24 /* $ModDesc: Provides HTTP serving facilities to modules */
25
26 static Server *Srv;
27
28 enum HttpState
29 {
30         HTTP_LISTEN = 0,
31         HTTP_SERVE_WAIT_REQUEST = 1,
32         HTTP_SERVE_SEND_DATA = 2
33 };
34
35 class HttpSocket : public InspSocket
36 {
37         FileReader* index;
38         HttpState InternalState;
39         std::stringstream headers;
40
41  public:
42
43         HttpSocket(std::string host, int port, bool listening, unsigned long maxtime, FileReader* index_page) : InspSocket(host, port, listening, maxtime), index(index_page)
44         {
45                 InternalState = HTTP_LISTEN;
46         }
47
48         HttpSocket(int newfd, char* ip) : InspSocket(newfd, ip)
49         {
50                 InternalState = HTTP_SERVE_WAIT_REQUEST;
51         }
52
53         virtual int OnIncomingConnection(int newsock, char* ip)
54         {
55                 if (InternalState == HTTP_LISTEN)
56                 {
57                         HttpSocket* s = new HttpSocket(newsock, ip);
58                         Srv->AddSocket(s);
59                 }
60         }
61
62         virtual void OnClose()
63         {
64         }
65
66         virtual bool OnDataReady()
67         {
68                 char* data = this->Read();
69                 /* Check that the data read is a valid pointer and it has some content */
70                 if (data && *data)
71                 {
72                         headers << data;
73
74                         if (headers.str().find("\r\n\r\n"))
75                         {
76                                 /* Headers are complete */
77                                 InternalState = HTTP_SERVE_SEND_DATA;
78                                 this->Write("<HTML><H1>COWS.</H1></HTML>");
79                         }
80                 }
81         }
82 };
83
84 class ModuleHttp : public Module
85 {
86         int port;
87         std::string host;
88         std::string bindip;
89         std::string indexfile;
90
91         FileReader index;
92
93         HttpSocket* http;
94
95  public:
96
97         void ReadConfig()
98         {
99                 ConfigReader c;
100                 this->host = c.ReadValue("http", "host", 0);
101                 this->bindip = c.ReadValue("http", "ip", 0);
102                 this->port = c.ReadInteger("http", "port", 0, true);
103                 this->indexfile = c.ReadValue("http", "index", 0);
104
105                 index.LoadFile(this->indexfile);
106         }
107
108         void CreateListener()
109         {
110                 http = new HttpSocket(this->host, this->port, true, 0, &index);
111                 Srv->AddSocket(http);
112         }
113
114         ModuleHttp(Server* Me) : Module::Module(Me)
115         {
116                 ReadConfig();
117                 CreateListener();
118         }
119
120         void Implements(char* List)
121         {
122                 List[I_OnEvent] = List[I_OnRequest] = 1;
123         }
124
125         virtual ~ModuleHttp()
126         {
127                 Srv->DelSocket(http);
128         }
129
130         virtual Version GetVersion()
131         {
132                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
133         }
134 };
135
136
137 class ModuleHttpFactory : public ModuleFactory
138 {
139  public:
140         ModuleHttpFactory()
141         {
142         }
143         
144         ~ModuleHttpFactory()
145         {
146         }
147         
148         virtual Module * CreateModule(Server* Me)
149         {
150                 return new ModuleHttp(Me);
151         }
152 };
153
154
155 extern "C" void * init_module( void )
156 {
157         return new ModuleHttpFactory;
158 }