1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
23 #include "inspsocket.h"
24 #include "helperfuncs.h"
27 /* $ModDesc: Provides HTTP serving facilities to modules */
32 ModuleHttp* HttpModule;
38 HTTP_SERVE_WAIT_REQUEST = 1,
39 HTTP_SERVE_SEND_DATA = 2
42 class HttpSocket : public InspSocket
45 HttpState InternalState;
46 std::stringstream headers;
50 HttpSocket(std::string host, int port, bool listening, unsigned long maxtime, FileReader* index_page) : InspSocket(host, port, listening, maxtime), index(index_page)
52 log(DEBUG,"HttpSocket constructor");
53 InternalState = HTTP_LISTEN;
56 HttpSocket(int newfd, char* ip, FileReader* ind) : InspSocket(newfd, ip), index(ind)
58 InternalState = HTTP_SERVE_WAIT_REQUEST;
61 virtual int OnIncomingConnection(int newsock, char* ip)
63 if (InternalState == HTTP_LISTEN)
65 HttpSocket* s = new HttpSocket(newsock, ip, index);
71 virtual void OnClose()
75 void SendHeaders(unsigned long size)
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");
84 virtual bool OnDataReady()
86 char* data = this->Read();
87 std::string request_type;
90 /* Check that the data read is a valid pointer and it has some content */
95 if (headers.str().find("\r\n\r\n") != std::string::npos)
97 /* Headers are complete */
98 InternalState = HTTP_SERVE_SEND_DATA;
100 headers >> request_type;
103 if ((request_type == "GET") && (uri == "/"))
105 SendHeaders(index->ContentSize());
106 this->Write(index->Contents());
110 HTTPRequest httpr(request_type,uri,&headers,this,this->GetIP());
111 Event e((char*)&httpr, (Module*)HttpModule, "httpd_url");
120 /* Bastard client closed the socket on us!
121 * Oh wait, theyre SUPPOSED to do that!
128 class ModuleHttp : public Module
133 std::string indexfile;
144 this->host = c.ReadValue("http", "host", 0);
145 this->bindip = c.ReadValue("http", "ip", 0);
146 this->port = c.ReadInteger("http", "port", 0, true);
147 this->indexfile = c.ReadValue("http", "index", 0);
149 index.LoadFile(this->indexfile);
152 void CreateListener()
154 http = new HttpSocket(this->bindip, this->port, true, 0, &index);
155 if ((http) && (http->GetState() == I_LISTENING))
157 Srv->AddSocket(http);
161 ModuleHttp(Server* Me) : Module::Module(Me)
168 void OnEvent(Event* event)
172 char* OnRequest(Request* request)
177 void Implements(char* List)
179 List[I_OnEvent] = List[I_OnRequest] = 1;
182 virtual ~ModuleHttp()
184 Srv->DelSocket(http);
187 virtual Version GetVersion()
189 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
194 class ModuleHttpFactory : public ModuleFactory
205 virtual Module * CreateModule(Server* Me)
207 HttpModule = new ModuleHttp(Me);
213 extern "C" void * init_module( void )
215 return new ModuleHttpFactory;