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 * ---------------------------------------------------
24 /* $ModDesc: Provides HTTP serving facilities to modules */
31 HTTP_SERVE_WAIT_REQUEST = 1,
32 HTTP_SERVE_SEND_DATA = 2
35 class HttpSocket : public InspSocket
38 HttpState InternalState;
39 std::stringstream headers;
43 HttpSocket(std::string host, int port, bool listening, unsigned long maxtime, FileReader* index_page) : InspSocket(host, port, listening, maxtime), index(index_page)
45 InternalState = HTTP_LISTEN;
48 HttpSocket(int newfd, char* ip) : InspSocket(newfd, ip)
50 InternalState = HTTP_SERVE_WAIT_REQUEST;
53 virtual int OnIncomingConnection(int newsock, char* ip)
55 if (InternalState == HTTP_LISTEN)
57 HttpSocket* s = new HttpSocket(newsock, ip);
62 virtual void OnClose()
66 virtual bool OnDataReady()
68 char* data = this->Read();
69 /* Check that the data read is a valid pointer and it has some content */
74 if (headers.str().find("\r\n\r\n"))
76 /* Headers are complete */
77 InternalState = HTTP_SERVE_SEND_DATA;
78 this->Write("<HTML><H1>COWS.</H1></HTML>");
84 class ModuleHttp : public Module
89 std::string indexfile;
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);
105 index.LoadFile(this->indexfile);
108 void CreateListener()
110 http = new HttpSocket(this->host, this->port, true, 0, &index);
111 Srv->AddSocket(http);
114 ModuleHttp(Server* Me) : Module::Module(Me)
120 void Implements(char* List)
122 List[I_OnEvent] = List[I_OnRequest] = 1;
125 virtual ~ModuleHttp()
127 Srv->DelSocket(http);
130 virtual Version GetVersion()
132 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
137 class ModuleHttpFactory : public ModuleFactory
148 virtual Module * CreateModule(Server* Me)
150 return new ModuleHttp(Me);
155 extern "C" void * init_module( void )
157 return new ModuleHttpFactory;