]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd.cpp
8eb44a03ba79f2f12c7455f67e3cb7f507100b26
[user/henk/code/inspircd.git] / src / modules / m_httpd.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5  *   Copyright (C) 2018 edef <edef@edef.eu>
6  *   Copyright (C) 2013-2014, 2017-2019 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
8  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
11  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
12  *   Copyright (C) 2007 John Brooks <special@inspircd.org>
13  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
14  *   Copyright (C) 2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
15  *
16  * This file is part of InspIRCd.  InspIRCd is free software: you can
17  * redistribute it and/or modify it under the terms of the GNU General Public
18  * License as published by the Free Software Foundation, version 2.
19  *
20  * This program is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  */
28
29 /// $CompilerFlags: -Ivendor_directory("http_parser")
30
31
32 #include "inspircd.h"
33 #include "iohook.h"
34 #include "modules/httpd.h"
35
36 #ifdef __GNUC__
37 # pragma GCC diagnostic push
38 #endif
39
40 // Fix warnings about the use of commas at end of enumerator lists and long long
41 // on C++03.
42 #if defined __clang__
43 # pragma clang diagnostic ignored "-Wc++11-extensions"
44 # pragma clang diagnostic ignored "-Wc++11-long-long"
45 #elif defined __GNUC__
46 # pragma GCC diagnostic ignored "-Wlong-long"
47 # if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8))
48 #  pragma GCC diagnostic ignored "-Wpedantic"
49 # else
50 #  pragma GCC diagnostic ignored "-pedantic"
51 # endif
52 #endif
53
54 // Fix warnings about shadowing in http_parser.
55 #ifdef __GNUC__
56 # pragma GCC diagnostic ignored "-Wshadow"
57 #endif
58
59 #include <http_parser.c>
60
61 #ifdef __GNUC__
62 # pragma GCC diagnostic pop
63 #endif
64
65 class ModuleHttpServer;
66
67 static ModuleHttpServer* HttpModule;
68 static insp::intrusive_list<HttpServerSocket> sockets;
69 static Events::ModuleEventProvider* aclevprov;
70 static Events::ModuleEventProvider* reqevprov;
71 static http_parser_settings parser_settings;
72
73 /** A socket used for HTTP transport
74  */
75 class HttpServerSocket : public BufferedSocket, public Timer, public insp::intrusive_list_node<HttpServerSocket>
76 {
77  private:
78         friend class ModuleHttpServer;
79
80         http_parser parser;
81         http_parser_url url;
82         std::string ip;
83         std::string uri;
84         HTTPHeaders headers;
85         std::string body;
86         size_t total_buffers;
87         int status_code;
88
89         /** True if this object is in the cull list
90          */
91         bool waitingcull;
92         bool messagecomplete;
93
94         bool Tick(time_t currtime) CXX11_OVERRIDE
95         {
96                 if (!messagecomplete)
97                 {
98                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP socket %d timed out", GetFd());
99                         Close();
100                         return false;
101                 }
102
103                 return true;
104         }
105
106         template<int (HttpServerSocket::*f)()>
107         static int Callback(http_parser* p)
108         {
109                 HttpServerSocket* sock = static_cast<HttpServerSocket*>(p->data);
110                 return (sock->*f)();
111         }
112
113         template<int (HttpServerSocket::*f)(const char*, size_t)>
114         static int DataCallback(http_parser* p, const char* buf, size_t len)
115         {
116                 HttpServerSocket* sock = static_cast<HttpServerSocket*>(p->data);
117                 return (sock->*f)(buf, len);
118         }
119
120         static void ConfigureParser()
121         {
122                 http_parser_settings_init(&parser_settings);
123                 parser_settings.on_message_begin = Callback<&HttpServerSocket::OnMessageBegin>;
124                 parser_settings.on_url = DataCallback<&HttpServerSocket::OnUrl>;
125                 parser_settings.on_header_field = DataCallback<&HttpServerSocket::OnHeaderField>;
126                 parser_settings.on_body = DataCallback<&HttpServerSocket::OnBody>;
127                 parser_settings.on_message_complete = Callback<&HttpServerSocket::OnMessageComplete>;
128         }
129
130         int OnMessageBegin()
131         {
132                 uri.clear();
133                 header_state = HEADER_NONE;
134                 body.clear();
135                 total_buffers = 0;
136                 return 0;
137         }
138
139         bool AcceptData(size_t len)
140         {
141                 total_buffers += len;
142                 return total_buffers < 8192;
143         }
144
145         int OnUrl(const char* buf, size_t len)
146         {
147                 if (!AcceptData(len))
148                 {
149                         status_code = HTTP_STATUS_URI_TOO_LONG;
150                         return -1;
151                 }
152                 uri.append(buf, len);
153                 return 0;
154         }
155
156         enum { HEADER_NONE, HEADER_FIELD, HEADER_VALUE } header_state;
157         std::string header_field;
158         std::string header_value;
159
160         void OnHeaderComplete()
161         {
162                 headers.SetHeader(header_field, header_value);
163                 header_field.clear();
164                 header_value.clear();
165         }
166
167         int OnHeaderField(const char* buf, size_t len)
168         {
169                 if (header_state == HEADER_VALUE)
170                         OnHeaderComplete();
171                 header_state = HEADER_FIELD;
172                 if (!AcceptData(len))
173                 {
174                         status_code = HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE;
175                         return -1;
176                 }
177                 header_field.append(buf, len);
178                 return 0;
179         }
180
181         int OnHeaderValue(const char* buf, size_t len)
182         {
183                 header_state = HEADER_VALUE;
184                 if (!AcceptData(len))
185                 {
186                         status_code = HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE;
187                         return -1;
188                 }
189                 header_value.append(buf, len);
190                 return 0;
191         }
192
193         int OnHeadersComplete()
194         {
195                 if (header_state != HEADER_NONE)
196                         OnHeaderComplete();
197                 return 0;
198         }
199
200         int OnBody(const char* buf, size_t len)
201         {
202                 if (!AcceptData(len))
203                 {
204                         status_code = HTTP_STATUS_PAYLOAD_TOO_LARGE;
205                         return -1;
206                 }
207                 body.append(buf, len);
208                 return 0;
209         }
210
211         int OnMessageComplete()
212         {
213                 messagecomplete = true;
214                 ServeData();
215                 return 0;
216         }
217
218  public:
219         HttpServerSocket(int newfd, const std::string& IP, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server, unsigned int timeoutsec)
220                 : BufferedSocket(newfd)
221                 , Timer(timeoutsec)
222                 , ip(IP)
223                 , status_code(0)
224                 , waitingcull(false)
225                 , messagecomplete(false)
226         {
227                 if ((!via->iohookprovs.empty()) && (via->iohookprovs.back()))
228                 {
229                         via->iohookprovs.back()->OnAccept(this, client, server);
230                         // IOHook may have errored
231                         if (!getError().empty())
232                         {
233                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP socket %d encountered a hook error: %s",
234                                         GetFd(), getError().c_str());
235                                 Close();
236                                 return;
237                         }
238                 }
239
240                 parser.data = this;
241                 http_parser_init(&parser, HTTP_REQUEST);
242                 ServerInstance->Timers.AddTimer(this);
243         }
244
245         ~HttpServerSocket()
246         {
247                 sockets.erase(this);
248         }
249
250         void Close() CXX11_OVERRIDE
251         {
252                 if (waitingcull || !HasFd())
253                         return;
254
255                 waitingcull = true;
256                 BufferedSocket::Close();
257                 ServerInstance->GlobalCulls.AddItem(this);
258         }
259
260         void OnError(BufferedSocketError err) CXX11_OVERRIDE
261         {
262                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP socket %d encountered an error: %d - %s",
263                         GetFd(), err, getError().c_str());
264                 Close();
265         }
266
267         void SendHTTPError(unsigned int response)
268         {
269                 static HTTPHeaders empty;
270                 std::string data = InspIRCd::Format(
271                         "<html><head></head><body style='font-family: sans-serif; text-align: center'>"
272                         "<h1 style='font-size: 48pt'>Error %u</h1><h2 style='font-size: 24pt'>%s</h2><hr>"
273                         "<small>Powered by <a href='https://www.inspircd.org'>InspIRCd</a></small></body></html>",
274                         response, http_status_str((http_status)response));
275
276                 Page(data, response, &empty);
277         }
278
279         void SendHeaders(unsigned long size, unsigned int response, HTTPHeaders &rheaders)
280         {
281                 WriteData(InspIRCd::Format("HTTP/%u.%u %u %s\r\n", parser.http_major ? parser.http_major : 1, parser.http_major ? parser.http_minor : 1, response, http_status_str((http_status)response)));
282
283                 rheaders.CreateHeader("Date", InspIRCd::TimeString(ServerInstance->Time(), "%a, %d %b %Y %H:%M:%S GMT", true));
284                 rheaders.CreateHeader("Server", INSPIRCD_BRANCH);
285                 rheaders.SetHeader("Content-Length", ConvToStr(size));
286
287                 if (size)
288                         rheaders.CreateHeader("Content-Type", "text/html");
289                 else
290                         rheaders.RemoveHeader("Content-Type");
291
292                 /* Supporting Connection: keep-alive causes a whole world of hurt syncronizing timeouts,
293                  * so remove it, its not essential for what we need.
294                  */
295                 rheaders.SetHeader("Connection", "Close");
296
297                 WriteData(rheaders.GetFormattedHeaders());
298                 WriteData("\r\n");
299         }
300
301         void OnDataReady() CXX11_OVERRIDE
302         {
303                 if (parser.upgrade || HTTP_PARSER_ERRNO(&parser))
304                         return;
305                 http_parser_execute(&parser, &parser_settings, recvq.data(), recvq.size());
306                 if (parser.upgrade || HTTP_PARSER_ERRNO(&parser))
307                         SendHTTPError(status_code ? status_code : 400);
308         }
309
310         void ServeData()
311         {
312                 ModResult MOD_RESULT;
313                 std::string method = http_method_str(static_cast<http_method>(parser.method));
314                 HTTPRequestURI parsed;
315                 ParseURI(uri, parsed);
316                 HTTPRequest acl(method, parsed, &headers, this, ip, body);
317                 FIRST_MOD_RESULT_CUSTOM(*aclevprov, HTTPACLEventListener, OnHTTPACLCheck, MOD_RESULT, (acl));
318                 if (MOD_RESULT != MOD_RES_DENY)
319                 {
320                         HTTPRequest request(method, parsed, &headers, this, ip, body);
321                         FIRST_MOD_RESULT_CUSTOM(*reqevprov, HTTPRequestEventListener, OnHTTPRequest, MOD_RESULT, (request));
322                         if (MOD_RESULT == MOD_RES_PASSTHRU)
323                         {
324                                 SendHTTPError(404);
325                         }
326                 }
327         }
328
329         void Page(const std::string& s, unsigned int response, HTTPHeaders* hheaders)
330         {
331                 SendHeaders(s.length(), response, *hheaders);
332                 WriteData(s);
333                 BufferedSocket::Close(true);
334         }
335
336         void Page(std::stringstream* n, unsigned int response, HTTPHeaders* hheaders)
337         {
338                 Page(n->str(), response, hheaders);
339         }
340
341         bool ParseURI(const std::string& uristr, HTTPRequestURI& out)
342         {
343                 http_parser_url_init(&url);
344                 if (http_parser_parse_url(uristr.c_str(), uristr.size(), 0, &url) != 0)
345                         return false;
346
347                 if (url.field_set & (1 << UF_PATH))
348                         out.path = uri.substr(url.field_data[UF_PATH].off, url.field_data[UF_PATH].len);
349
350                 if (url.field_set & (1 << UF_FRAGMENT))
351                         out.fragment = uri.substr(url.field_data[UF_FRAGMENT].off, url.field_data[UF_FRAGMENT].len);
352
353                 std::string param_str;
354                 if (url.field_set & (1 << UF_QUERY))
355                         param_str = uri.substr(url.field_data[UF_QUERY].off, url.field_data[UF_QUERY].len);
356
357                 irc::sepstream param_stream(param_str, '&');
358                 std::string token;
359                 std::string::size_type eq_pos;
360                 while (param_stream.GetToken(token))
361                 {
362                         eq_pos = token.find('=');
363                         if (eq_pos == std::string::npos)
364                         {
365                                 out.query_params.insert(std::make_pair(token, ""));
366                         }
367                         else
368                         {
369                                 out.query_params.insert(std::make_pair(token.substr(0, eq_pos), token.substr(eq_pos + 1)));
370                         }
371                 }
372                 return true;
373         }
374 };
375
376 class HTTPdAPIImpl : public HTTPdAPIBase
377 {
378  public:
379         HTTPdAPIImpl(Module* parent)
380                 : HTTPdAPIBase(parent)
381         {
382         }
383
384         void SendResponse(HTTPDocumentResponse& resp) CXX11_OVERRIDE
385         {
386                 resp.src.sock->Page(resp.document, resp.responsecode, &resp.headers);
387         }
388 };
389
390 class ModuleHttpServer : public Module
391 {
392         HTTPdAPIImpl APIImpl;
393         unsigned int timeoutsec;
394         Events::ModuleEventProvider acleventprov;
395         Events::ModuleEventProvider reqeventprov;
396
397  public:
398         ModuleHttpServer()
399                 : APIImpl(this)
400                 , acleventprov(this, "event/http-acl")
401                 , reqeventprov(this, "event/http-request")
402         {
403                 aclevprov = &acleventprov;
404                 reqevprov = &reqeventprov;
405                 HttpServerSocket::ConfigureParser();
406         }
407
408         void init() CXX11_OVERRIDE
409         {
410                 HttpModule = this;
411         }
412
413         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
414         {
415                 ConfigTag* tag = ServerInstance->Config->ConfValue("httpd");
416                 timeoutsec = tag->getDuration("timeout", 10, 1);
417         }
418
419         ModResult OnAcceptConnection(int nfd, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE
420         {
421                 if (!stdalgo::string::equalsci(from->bind_tag->getString("type"), "httpd"))
422                         return MOD_RES_PASSTHRU;
423
424                 sockets.push_front(new HttpServerSocket(nfd, client->addr(), from, client, server, timeoutsec));
425                 return MOD_RES_ALLOW;
426         }
427
428         void OnUnloadModule(Module* mod) CXX11_OVERRIDE
429         {
430                 for (insp::intrusive_list<HttpServerSocket>::const_iterator i = sockets.begin(); i != sockets.end(); )
431                 {
432                         HttpServerSocket* sock = *i;
433                         ++i;
434                         if (sock->GetModHook(mod))
435                         {
436                                 sock->cull();
437                                 delete sock;
438                         }
439                 }
440         }
441
442         CullResult cull() CXX11_OVERRIDE
443         {
444                 for (insp::intrusive_list<HttpServerSocket>::const_iterator i = sockets.begin(); i != sockets.end(); ++i)
445                 {
446                         HttpServerSocket* sock = *i;
447                         sock->Close();
448                 }
449                 return Module::cull();
450         }
451
452         Version GetVersion() CXX11_OVERRIDE
453         {
454                 return Version("Allows the server administrator to serve various useful resources over HTTP.", VF_VENDOR);
455         }
456 };
457
458 MODULE_INIT(ModuleHttpServer)