1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /* $ModDesc: Provides HTTP serving facilities to modules */
18 /* $ModDep: httpd.h */
20 class ModuleHttpServer;
22 static ModuleHttpServer* HttpModule;
25 /** HTTP socket states
29 HTTP_SERVE_WAIT_REQUEST = 0, /* Waiting for a full request */
30 HTTP_SERVE_RECV_POSTDATA = 1, /* Waiting to finish recieving POST data */
31 HTTP_SERVE_SEND_DATA = 2 /* Sending response */
34 /** A socket used for HTTP transport
36 class HttpServerSocket : public BufferedSocket
38 HttpState InternalState;
42 std::string reqbuffer;
44 unsigned int postsize;
45 std::string request_type;
47 std::string http_version;
51 HttpServerSocket(int newfd, const std::string& IP, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
52 : BufferedSocket(newfd), ip(IP), postsize(0)
54 InternalState = HTTP_SERVE_WAIT_REQUEST;
56 FOREACH_MOD(I_OnHookIO, OnHookIO(this, via));
58 GetIOHook()->OnStreamSocketAccept(this, client, server);
61 virtual void OnError(BufferedSocketError)
63 ServerInstance->GlobalCulls.AddItem(this);
66 std::string Response(int response)
73 return "SWITCHING PROTOCOLS";
81 return "NON-AUTHORITATIVE INFORMATION";
85 return "RESET CONTENT";
87 return "PARTIAL CONTENT";
89 return "MULTIPLE CHOICES";
91 return "MOVED PERMENANTLY";
97 return "NOT MODIFIED";
101 return "TEMPORARY REDIRECT";
103 return "BAD REQUEST";
105 return "UNAUTHORIZED";
107 return "PAYMENT REQUIRED";
113 return "METHOD NOT ALLOWED";
115 return "NOT ACCEPTABLE";
117 return "PROXY AUTHENTICATION REQUIRED";
119 return "REQUEST TIMEOUT";
125 return "LENGTH REQUIRED";
127 return "PRECONDITION FAILED";
129 return "REQUEST ENTITY TOO LARGE";
131 return "REQUEST-URI TOO LONG";
133 return "UNSUPPORTED MEDIA TYPE";
135 return "REQUESTED RANGE NOT SATISFIABLE";
137 return "EXPECTATION FAILED";
139 return "INTERNAL SERVER ERROR";
141 return "NOT IMPLEMENTED";
143 return "BAD GATEWAY";
145 return "SERVICE UNAVAILABLE";
147 return "GATEWAY TIMEOUT";
149 return "HTTP VERSION NOT SUPPORTED";
157 void SendHTTPError(int response)
160 std::string data = "<html><head></head><body>Server error "+ConvToStr(response)+": "+Response(response)+"<br>"+
161 "<small>Powered by <a href='http://www.inspircd.org'>InspIRCd</a></small></body></html>";
163 SendHeaders(data.length(), response, empty);
167 void SendHeaders(unsigned long size, int response, HTTPHeaders &rheaders)
170 WriteData(http_version + " "+ConvToStr(response)+" "+Response(response)+"\r\n");
172 time_t local = ServerInstance->Time();
173 struct tm *timeinfo = gmtime(&local);
174 char *date = asctime(timeinfo);
175 date[strlen(date) - 1] = '\0';
176 rheaders.CreateHeader("Date", date);
178 rheaders.CreateHeader("Server", "InspIRCd/m_httpd.so/1.2");
179 rheaders.SetHeader("Content-Length", ConvToStr(size));
182 rheaders.CreateHeader("Content-Type", "text/html");
184 rheaders.RemoveHeader("Content-Type");
186 /* Supporting Connection: keep-alive causes a whole world of hurt syncronizing timeouts,
187 * so remove it, its not essential for what we need.
189 rheaders.SetHeader("Connection", "Close");
191 WriteData(rheaders.GetFormattedHeaders());
197 if (InternalState == HTTP_SERVE_RECV_POSTDATA)
199 postdata.append(recvq);
200 if (postdata.length() >= postsize)
205 reqbuffer.append(recvq);
207 if (reqbuffer.length() >= 8192)
209 ServerInstance->Logs->Log("m_httpd",DEBUG, "m_httpd dropped connection due to an oversized request buffer");
214 if (InternalState == HTTP_SERVE_WAIT_REQUEST)
215 CheckRequestBuffer();
219 void CheckRequestBuffer()
221 std::string::size_type reqend = reqbuffer.find("\r\n\r\n");
222 if (reqend == std::string::npos)
225 // We have the headers; parse them all
226 std::string::size_type hbegin = 0, hend;
227 while ((hend = reqbuffer.find("\r\n", hbegin)) != std::string::npos)
232 if (request_type.empty())
234 std::istringstream cheader(std::string(reqbuffer, hbegin, hend - hbegin));
235 cheader >> request_type;
237 cheader >> http_version;
239 if (request_type.empty() || uri.empty() || http_version.empty())
249 std::string cheader = reqbuffer.substr(hbegin, hend - hbegin);
251 std::string::size_type fieldsep = cheader.find(':');
252 if ((fieldsep == std::string::npos) || (fieldsep == 0) || (fieldsep == cheader.length() - 1))
258 headers.SetHeader(cheader.substr(0, fieldsep), cheader.substr(fieldsep + 2));
263 reqbuffer.erase(0, reqend + 4);
265 std::transform(request_type.begin(), request_type.end(), request_type.begin(), ::toupper);
266 std::transform(http_version.begin(), http_version.end(), http_version.begin(), ::toupper);
268 if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
274 if (headers.IsSet("Content-Length") && (postsize = atoi(headers.GetHeader("Content-Length").c_str())) != 0)
276 InternalState = HTTP_SERVE_RECV_POSTDATA;
278 if (reqbuffer.length() >= postsize)
280 postdata = reqbuffer.substr(0, postsize);
281 reqbuffer.erase(0, postsize);
283 else if (!reqbuffer.empty())
285 postdata = reqbuffer;
289 if (postdata.length() >= postsize)
300 InternalState = HTTP_SERVE_SEND_DATA;
303 HTTPRequest acl((Module*)HttpModule, "httpd_acl", request_type, uri, &headers, this, ip, postdata);
307 HTTPRequest url((Module*)HttpModule, "httpd_url", request_type, uri, &headers, this, ip, postdata);
316 void Page(std::stringstream* n, int response, HTTPHeaders *hheaders)
318 SendHeaders(n->str().length(), response, *hheaders);
323 class ModuleHttpServer : public Module
325 std::vector<HttpServerSocket *> httpsocks;
330 ServerInstance->Modules->Attach(I_OnAcceptConnection, this);
333 void OnRequest(Request& request)
335 if (strcmp(request.id, "HTTP-DOC") != 0)
337 HTTPDocumentResponse& resp = static_cast<HTTPDocumentResponse&>(request);
339 resp.src.sock->Page(resp.document, resp.responsecode, &resp.headers);
342 ModResult OnAcceptConnection(int nfd, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
344 if (from->bind_tag->getString("type") != "httpd")
345 return MOD_RES_PASSTHRU;
347 std::string incomingip;
348 irc::sockets::satoap(*client, incomingip, port);
349 new HttpServerSocket(nfd, incomingip, from, client, server);
350 return MOD_RES_ALLOW;
354 virtual ~ModuleHttpServer()
356 for (size_t i = 0; i < httpsocks.size(); i++)
358 httpsocks[i]->cull();
363 virtual Version GetVersion()
365 return Version("Provides HTTP serving facilities to modules", VF_VENDOR);
369 MODULE_INIT(ModuleHttpServer)