]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_httpd.cpp
Fix some confusing logic in sanick.
[user/henk/code/inspircd.git] / src / modules / m_httpd.cpp
index c4b5dc1f29be996fc2ba658b76b57a27efd31b94..330e98c6a9739877f85589f211d097db65feb8ab 100644 (file)
@@ -1,12 +1,17 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2018 edef <edef@edef.eu>
+ *   Copyright (C) 2013-2014, 2017-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
- *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
- *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
- *   Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
- *   Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
+ *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007 John Brooks <special@inspircd.org>
  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
  *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
  * redistribute it and/or modify it under the terms of the GNU General Public
 #include "iohook.h"
 #include "modules/httpd.h"
 
+#ifdef __GNUC__
+# pragma GCC diagnostic push
+#endif
+
 // Fix warnings about the use of commas at end of enumerator lists and long long
 // on C++03.
 #if defined __clang__
 
 // Fix warnings about shadowing in http_parser.
 #ifdef __GNUC__
-//# pragma GCC diagnostic ignored "-Wshadow"
+# pragma GCC diagnostic ignored "-Wshadow"
 #endif
 
 #include <http_parser.c>
 
+#ifdef __GNUC__
+# pragma GCC diagnostic pop
+#endif
+
 class ModuleHttpServer;
 
 static ModuleHttpServer* HttpModule;
@@ -82,7 +95,8 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
        {
                if (!messagecomplete)
                {
-                       AddToCull();
+                       ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP socket %d timed out", GetFd());
+                       Close();
                        return false;
                }
 
@@ -216,7 +230,9 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                        // IOHook may have errored
                        if (!getError().empty())
                        {
-                               AddToCull();
+                               ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP socket %d encountered a hook error: %s",
+                                       GetFd(), getError().c_str());
+                               Close();
                                return;
                        }
                }
@@ -231,17 +247,31 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                sockets.erase(this);
        }
 
-       void OnError(BufferedSocketError) CXX11_OVERRIDE
+       void Close() CXX11_OVERRIDE
+       {
+               if (waitingcull || !HasFd())
+                       return;
+
+               waitingcull = true;
+               BufferedSocket::Close();
+               ServerInstance->GlobalCulls.AddItem(this);
+       }
+
+       void OnError(BufferedSocketError err) CXX11_OVERRIDE
        {
-               AddToCull();
+               ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP socket %d encountered an error: %d - %s",
+                       GetFd(), err, getError().c_str());
+               Close();
        }
 
        void SendHTTPError(unsigned int response)
        {
-               HTTPHeaders empty;
+               static HTTPHeaders empty;
                std::string data = InspIRCd::Format(
-                       "<html><head></head><body>Server error %u: %s<br>"
-                       "<small>Powered by <a href='https://www.inspircd.org'>InspIRCd</a></small></body></html>", response, http_status_str((http_status)response));
+                       "<html><head></head><body style='font-family: sans-serif; text-align: center'>"
+                       "<h1 style='font-size: 48pt'>Error %u</h1><h2 style='font-size: 24pt'>%s</h2><hr>"
+                       "<small>Powered by <a href='https://www.inspircd.org'>InspIRCd</a></small></body></html>",
+                       response, http_status_str((http_status)response));
 
                Page(data, response, &empty);
        }
@@ -259,7 +289,7 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                else
                        rheaders.RemoveHeader("Content-Type");
 
-               /* Supporting Connection: keep-alive causes a whole world of hurt syncronizing timeouts,
+               /* Supporting Connection: keep-alive causes a whole world of hurt synchronizing timeouts,
                 * so remove it, its not essential for what we need.
                 */
                rheaders.SetHeader("Connection", "Close");
@@ -300,7 +330,7 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
        {
                SendHeaders(s.length(), response, *hheaders);
                WriteData(s);
-               Close(true);
+               BufferedSocket::Close(true);
        }
 
        void Page(std::stringstream* n, unsigned int response, HTTPHeaders* hheaders)
@@ -308,16 +338,6 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                Page(n->str(), response, hheaders);
        }
 
-       void AddToCull()
-       {
-               if (waitingcull)
-                       return;
-
-               waitingcull = true;
-               Close();
-               ServerInstance->GlobalCulls.AddItem(this);
-       }
-
        bool ParseURI(const std::string& uristr, HTTPRequestURI& out)
        {
                http_parser_url_init(&url);
@@ -424,14 +444,14 @@ class ModuleHttpServer : public Module
                for (insp::intrusive_list<HttpServerSocket>::const_iterator i = sockets.begin(); i != sockets.end(); ++i)
                {
                        HttpServerSocket* sock = *i;
-                       sock->AddToCull();
+                       sock->Close();
                }
                return Module::cull();
        }
 
        Version GetVersion() CXX11_OVERRIDE
        {
-               return Version("Provides HTTP serving facilities to modules", VF_VENDOR);
+               return Version("Allows the server administrator to serve various useful resources over HTTP.", VF_VENDOR);
        }
 };