]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd.cpp
978a60ab76e8a959fca18f52a76f4190deb719ea
[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                         Close();
99                         return false;
100                 }
101
102                 return true;
103         }
104
105         template<int (HttpServerSocket::*f)()>
106         static int Callback(http_parser* p)
107         {
108                 HttpServerSocket* sock = static_cast<HttpServerSocket*>(p->data);
109                 return (sock->*f)();
110         }
111
112         template<int (HttpServerSocket::*f)(const char*, size_t)>
113         static int DataCallback(http_parser* p, const char* buf, size_t len)
114         {
115                 HttpServerSocket* sock = static_cast<HttpServerSocket*>(p->data);
116                 return (sock->*f)(buf, len);
117         }
118
119         static void ConfigureParser()
120         {
121                 http_parser_settings_init(&parser_settings);
122                 parser_settings.on_message_begin = Callback<&HttpServerSocket::OnMessageBegin>;
123                 parser_settings.on_url = DataCallback<&HttpServerSocket::OnUrl>;
124                 parser_settings.on_header_field = DataCallback<&HttpServerSocket::OnHeaderField>;
125                 parser_settings.on_body = DataCallback<&HttpServerSocket::OnBody>;
126                 parser_settings.on_message_complete = Callback<&HttpServerSocket::OnMessageComplete>;
127         }
128
129         int OnMessageBegin()
130         {
131                 uri.clear();
132                 header_state = HEADER_NONE;
133                 body.clear();
134                 total_buffers = 0;
135                 return 0;
136         }
137
138         bool AcceptData(size_t len)
139         {
140                 total_buffers += len;
141                 return total_buffers < 8192;
142         }
143
144         int OnUrl(const char* buf, size_t len)
145         {
146                 if (!AcceptData(len))
147                 {
148                         status_code = HTTP_STATUS_URI_TOO_LONG;
149                         return -1;
150                 }
151                 uri.append(buf, len);
152                 return 0;
153         }
154
155         enum { HEADER_NONE, HEADER_FIELD, HEADER_VALUE } header_state;
156         std::string header_field;
157         std::string header_value;
158
159         void OnHeaderComplete()
160         {
161                 headers.SetHeader(header_field, header_value);
162                 header_field.clear();
163                 header_value.clear();
164         }
165
166         int OnHeaderField(const char* buf, size_t len)
167         {
168                 if (header_state == HEADER_VALUE)
169                         OnHeaderComplete();
170                 header_state = HEADER_FIELD;
171                 if (!AcceptData(len))
172                 {
173                         status_code = HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE;
174                         return -1;
175                 }
176                 header_field.append(buf, len);
177                 return 0;
178         }
179
180         int OnHeaderValue(const char* buf, size_t len)
181         {
182                 header_state = HEADER_VALUE;
183                 if (!AcceptData(len))
184                 {
185                         status_code = HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE;
186                         return -1;
187                 }
188                 header_value.append(buf, len);
189                 return 0;
190         }
191
192         int OnHeadersComplete()
193         {
194                 if (header_state != HEADER_NONE)
195                         OnHeaderComplete();
196                 return 0;
197         }
198
199         int OnBody(const char* buf, size_t len)
200         {
201                 if (!AcceptData(len))
202                 {
203                         status_code = HTTP_STATUS_PAYLOAD_TOO_LARGE;
204                         return -1;
205                 }
206                 body.append(buf, len);
207                 return 0;
208         }
209
210         int OnMessageComplete()
211         {
212                 messagecomplete = true;
213                 ServeData();
214                 return 0;
215         }
216
217  public:
218         HttpServerSocket(int newfd, const std::string& IP, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server, unsigned int timeoutsec)
219                 : BufferedSocket(newfd)
220                 , Timer(timeoutsec)
221                 , ip(IP)
222                 , status_code(0)
223                 , waitingcull(false)
224                 , messagecomplete(false)
225         {
226                 if ((!via->iohookprovs.empty()) && (via->iohookprovs.back()))
227                 {
228                         via->iohookprovs.back()->OnAccept(this, client, server);
229                         // IOHook may have errored
230                         if (!getError().empty())
231                         {
232                                 Close();
233                                 return;
234                         }
235                 }
236
237                 parser.data = this;
238                 http_parser_init(&parser, HTTP_REQUEST);
239                 ServerInstance->Timers.AddTimer(this);
240         }
241
242         ~HttpServerSocket()
243         {
244                 sockets.erase(this);
245         }
246
247         void Close() CXX11_OVERRIDE
248         {
249                 if (waitingcull || !HasFd())
250                         return;
251
252                 waitingcull = true;
253                 BufferedSocket::Close();
254                 ServerInstance->GlobalCulls.AddItem(this);
255         }
256
257         void OnError(BufferedSocketError) CXX11_OVERRIDE
258         {
259                 Close();
260         }
261
262         void SendHTTPError(unsigned int response)
263         {
264                 static HTTPHeaders empty;
265                 std::string data = InspIRCd::Format(
266                         "<html><head></head><body style='font-family: sans-serif; text-align: center'>"
267                         "<h1 style='font-size: 48pt'>Error %u</h1><h2 style='font-size: 24pt'>%s</h2><hr>"
268                         "<small>Powered by <a href='https://www.inspircd.org'>InspIRCd</a></small></body></html>",
269                         response, http_status_str((http_status)response));
270
271                 Page(data, response, &empty);
272         }
273
274         void SendHeaders(unsigned long size, unsigned int response, HTTPHeaders &rheaders)
275         {
276                 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)));
277
278                 rheaders.CreateHeader("Date", InspIRCd::TimeString(ServerInstance->Time(), "%a, %d %b %Y %H:%M:%S GMT", true));
279                 rheaders.CreateHeader("Server", INSPIRCD_BRANCH);
280                 rheaders.SetHeader("Content-Length", ConvToStr(size));
281
282                 if (size)
283                         rheaders.CreateHeader("Content-Type", "text/html");
284                 else
285                         rheaders.RemoveHeader("Content-Type");
286
287                 /* Supporting Connection: keep-alive causes a whole world of hurt syncronizing timeouts,
288                  * so remove it, its not essential for what we need.
289                  */
290                 rheaders.SetHeader("Connection", "Close");
291
292                 WriteData(rheaders.GetFormattedHeaders());
293                 WriteData("\r\n");
294         }
295
296         void OnDataReady() CXX11_OVERRIDE
297         {
298                 if (parser.upgrade || HTTP_PARSER_ERRNO(&parser))
299                         return;
300                 http_parser_execute(&parser, &parser_settings, recvq.data(), recvq.size());
301                 if (parser.upgrade || HTTP_PARSER_ERRNO(&parser))
302                         SendHTTPError(status_code ? status_code : 400);
303         }
304
305         void ServeData()
306         {
307                 ModResult MOD_RESULT;
308                 std::string method = http_method_str(static_cast<http_method>(parser.method));
309                 HTTPRequestURI parsed;
310                 ParseURI(uri, parsed);
311                 HTTPRequest acl(method, parsed, &headers, this, ip, body);
312                 FIRST_MOD_RESULT_CUSTOM(*aclevprov, HTTPACLEventListener, OnHTTPACLCheck, MOD_RESULT, (acl));
313                 if (MOD_RESULT != MOD_RES_DENY)
314                 {
315                         HTTPRequest request(method, parsed, &headers, this, ip, body);
316                         FIRST_MOD_RESULT_CUSTOM(*reqevprov, HTTPRequestEventListener, OnHTTPRequest, MOD_RESULT, (request));
317                         if (MOD_RESULT == MOD_RES_PASSTHRU)
318                         {
319                                 SendHTTPError(404);
320                         }
321                 }
322         }
323
324         void Page(const std::string& s, unsigned int response, HTTPHeaders* hheaders)
325         {
326                 SendHeaders(s.length(), response, *hheaders);
327                 WriteData(s);
328                 BufferedSocket::Close(true);
329         }
330
331         void Page(std::stringstream* n, unsigned int response, HTTPHeaders* hheaders)
332         {
333                 Page(n->str(), response, hheaders);
334         }
335
336         bool ParseURI(const std::string& uristr, HTTPRequestURI& out)
337         {
338                 http_parser_url_init(&url);
339                 if (http_parser_parse_url(uristr.c_str(), uristr.size(), 0, &url) != 0)
340                         return false;
341
342                 if (url.field_set & (1 << UF_PATH))
343                         out.path = uri.substr(url.field_data[UF_PATH].off, url.field_data[UF_PATH].len);
344
345                 if (url.field_set & (1 << UF_FRAGMENT))
346                         out.fragment = uri.substr(url.field_data[UF_FRAGMENT].off, url.field_data[UF_FRAGMENT].len);
347
348                 std::string param_str;
349                 if (url.field_set & (1 << UF_QUERY))
350                         param_str = uri.substr(url.field_data[UF_QUERY].off, url.field_data[UF_QUERY].len);
351
352                 irc::sepstream param_stream(param_str, '&');
353                 std::string token;
354                 std::string::size_type eq_pos;
355                 while (param_stream.GetToken(token))
356                 {
357                         eq_pos = token.find('=');
358                         if (eq_pos == std::string::npos)
359                         {
360                                 out.query_params.insert(std::make_pair(token, ""));
361                         }
362                         else
363                         {
364                                 out.query_params.insert(std::make_pair(token.substr(0, eq_pos), token.substr(eq_pos + 1)));
365                         }
366                 }
367                 return true;
368         }
369 };
370
371 class HTTPdAPIImpl : public HTTPdAPIBase
372 {
373  public:
374         HTTPdAPIImpl(Module* parent)
375                 : HTTPdAPIBase(parent)
376         {
377         }
378
379         void SendResponse(HTTPDocumentResponse& resp) CXX11_OVERRIDE
380         {
381                 resp.src.sock->Page(resp.document, resp.responsecode, &resp.headers);
382         }
383 };
384
385 class ModuleHttpServer : public Module
386 {
387         HTTPdAPIImpl APIImpl;
388         unsigned int timeoutsec;
389         Events::ModuleEventProvider acleventprov;
390         Events::ModuleEventProvider reqeventprov;
391
392  public:
393         ModuleHttpServer()
394                 : APIImpl(this)
395                 , acleventprov(this, "event/http-acl")
396                 , reqeventprov(this, "event/http-request")
397         {
398                 aclevprov = &acleventprov;
399                 reqevprov = &reqeventprov;
400                 HttpServerSocket::ConfigureParser();
401         }
402
403         void init() CXX11_OVERRIDE
404         {
405                 HttpModule = this;
406         }
407
408         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
409         {
410                 ConfigTag* tag = ServerInstance->Config->ConfValue("httpd");
411                 timeoutsec = tag->getDuration("timeout", 10, 1);
412         }
413
414         ModResult OnAcceptConnection(int nfd, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE
415         {
416                 if (!stdalgo::string::equalsci(from->bind_tag->getString("type"), "httpd"))
417                         return MOD_RES_PASSTHRU;
418
419                 sockets.push_front(new HttpServerSocket(nfd, client->addr(), from, client, server, timeoutsec));
420                 return MOD_RES_ALLOW;
421         }
422
423         void OnUnloadModule(Module* mod) CXX11_OVERRIDE
424         {
425                 for (insp::intrusive_list<HttpServerSocket>::const_iterator i = sockets.begin(); i != sockets.end(); )
426                 {
427                         HttpServerSocket* sock = *i;
428                         ++i;
429                         if (sock->GetModHook(mod))
430                         {
431                                 sock->cull();
432                                 delete sock;
433                         }
434                 }
435         }
436
437         CullResult cull() CXX11_OVERRIDE
438         {
439                 for (insp::intrusive_list<HttpServerSocket>::const_iterator i = sockets.begin(); i != sockets.end(); ++i)
440                 {
441                         HttpServerSocket* sock = *i;
442                         sock->Close();
443                 }
444                 return Module::cull();
445         }
446
447         Version GetVersion() CXX11_OVERRIDE
448         {
449                 return Version("Provides HTTP serving facilities to modules", VF_VENDOR);
450         }
451 };
452
453 MODULE_INIT(ModuleHttpServer)