]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/modules/httpd.h
Update copyright headers.
[user/henk/code/inspircd.git] / include / modules / httpd.h
index d1746e862f99afc80fd598e68e54f6de40b3c4c6..f3402c8d84ab64e632cec4c050305e69e9e24ce1 100644 (file)
@@ -1,11 +1,14 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2013, 2021 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2013, 2015 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
- *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
- *   Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
+ *   Copyright (C) 2007 John Brooks <special@inspircd.org>
  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
- *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
+ *   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
 #pragma once
 
 #include "base.h"
+#include "event.h"
 
 #include <string>
 #include <sstream>
 #include <map>
 
+class HTTPQueryParameters : public insp::flat_multimap<std::string, std::string>
+{
+ public:
+       bool get(const std::string& key, std::string& value) const
+       {
+               const_iterator it = find(key);
+               if (it == end())
+                       return false;
+
+               value = it->second;
+               return true;
+       }
+
+       std::string getString(const std::string& key, const std::string& def = "") const
+       {
+               std::string value;
+               if (!get(key, value))
+                       return def;
+
+               return value;
+       }
+
+       template <typename T>
+       T getNum(const std::string& key, T def = 0) const
+       {
+               std::string value;
+               if (!get(key, value))
+                       return def;
+
+               return ConvToNum<T>(value);
+       }
+
+       unsigned long getDuration(const std::string& key, unsigned long def = 0) const
+       {
+               unsigned long value;
+               if (!InspIRCd::Duration(getString(key, "0"), value))
+                       return def;
+
+               return value;
+       }
+
+       bool getBool(const std::string& key, bool def = false) const
+       {
+               return getNum<bool>(key, def);
+       }
+};
+
+struct HTTPRequestURI
+{
+       std::string path;
+       HTTPQueryParameters query_params;
+       std::string fragment;
+};
+
 /** A modifyable list of HTTP header fields
  */
 class HTTPHeaders
@@ -107,13 +165,13 @@ class HttpServerSocket;
 
 /** This class represents a HTTP request.
  */
-class HTTPRequest : public Event
+class HTTPRequest
 {
  protected:
        std::string type;
-       std::string document;
        std::string ipaddr;
        std::string postdata;
+       HTTPRequestURI parseduri;
 
  public:
 
@@ -128,15 +186,20 @@ class HTTPRequest : public Event
        /** Initialize HTTPRequest.
         * This constructor is called by m_httpd.so to initialize the class.
         * @param request_type The request type, e.g. GET, POST, HEAD
-        * @param uri The URI, e.g. /page
+        * @param parsed_uri The URI which was requested by the client.
         * @param hdr The headers sent with the request
-        * @param opaque An opaque pointer used internally by m_httpd, which you must pass back to the module in your reply.
+        * @param socket The server socket which this request came in via.
         * @param ip The IP address making the web request.
         * @param pdata The post data (content after headers) received with the request, up to Content-Length in size
         */
-       HTTPRequest(Module* me, const std::string &eventid, const std::string &request_type, const std::string &uri,
-               HTTPHeaders* hdr, HttpServerSocket* socket, const std::string &ip, const std::string &pdata)
-               : Event(me, eventid), type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(socket)
+       HTTPRequest(const std::string& request_type, const HTTPRequestURI& parsed_uri, HTTPHeaders* hdr,
+                       HttpServerSocket* socket, const std::string& ip, const std::string& pdata)
+               : type(request_type)
+               , ipaddr(ip)
+               , postdata(pdata)
+               , parseduri(parsed_uri)
+               , headers(hdr)
+               , sock(socket)
        {
        }
 
@@ -158,13 +221,14 @@ class HTTPRequest : public Event
                return type;
        }
 
-       /** Get URI.
-        * The URI string (URL minus hostname and scheme) will be provided by this function.
-        * @return The URI being requested
-        */
-       std::string& GetURI()
+       HTTPRequestURI& GetParsedURI()
+       {
+               return parseduri;
+       }
+
+       std::string& GetPath()
        {
-               return document;
+               return GetParsedURI().path;
        }
 
        /** Get IP address of requester.
@@ -177,27 +241,85 @@ class HTTPRequest : public Event
        }
 };
 
-/** You must return a HTTPDocument to the httpd module by using the Request class.
- * When you initialize this class you may initialize it with all components required to
- * form a valid HTTP response, including document data, headers, and a response code.
+/** If you want to reply to HTTP requests, you must return a HTTPDocumentResponse to
+ * the httpd module via the HTTPdAPI.
+ * When you initialize this class you initialize it with all components required to
+ * form a valid HTTP response: the document data and a response code.
+ * You can add additional HTTP headers, if you want.
  */
-class HTTPDocumentResponse : public Request
+class HTTPDocumentResponse
 {
  public:
+       /** Module that generated this reply
+        */
+       Module* const module;
+
        std::stringstream* document;
-       int responsecode;
+       unsigned int responsecode;
+
+       /** Any extra headers to include with the defaults
+        */
        HTTPHeaders headers;
+
        HTTPRequest& src;
 
-       /** Initialize a HTTPRequest ready for sending to m_httpd.so.
-        * @param opaque The socket pointer you obtained from the HTTPRequest at an earlier time
+       /** Initialize a HTTPDocumentResponse ready for sending to the httpd module.
+        * @param mod A pointer to the module who responded to the request
+        * @param req The request you obtained from the HTTPRequest at an earlier time
         * @param doc A stringstream containing the document body
         * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
         * based upon the response code.
-        * @param extra Any extra headers to include with the defaults, seperated by carriage return and linefeed.
         */
-       HTTPDocumentResponse(Module* me, HTTPRequest& req, std::stringstream* doc, int response)
-               : Request(me, req.source, "HTTP-DOC"), document(doc), responsecode(response), src(req)
+       HTTPDocumentResponse(Module* mod, HTTPRequest& req, std::stringstream* doc, unsigned int response)
+               : module(mod), document(doc), responsecode(response), src(req)
+       {
+       }
+};
+
+class HTTPdAPIBase : public DataProvider
+{
+ public:
+       HTTPdAPIBase(Module* parent)
+               : DataProvider(parent, "m_httpd_api")
        {
        }
+
+       /** Answer an incoming HTTP request with the provided document
+        * @param response The response created by your module that will be sent to the client
+        */
+       virtual void SendResponse(HTTPDocumentResponse& response) = 0;
+};
+
+/** The API provided by the httpd module that allows other modules to respond to incoming
+ * HTTP requests
+ */
+class HTTPdAPI : public dynamic_reference<HTTPdAPIBase>
+{
+ public:
+       HTTPdAPI(Module* parent)
+               : dynamic_reference<HTTPdAPIBase>(parent, "m_httpd_api")
+       {
+       }
+};
+
+class HTTPACLEventListener : public Events::ModuleEventListener
+{
+ public:
+       HTTPACLEventListener(Module* mod)
+               : ModuleEventListener(mod, "event/http-acl")
+       {
+       }
+
+       virtual ModResult OnHTTPACLCheck(HTTPRequest& req) = 0;
+};
+
+class HTTPRequestEventListener : public Events::ModuleEventListener
+{
+ public:
+       HTTPRequestEventListener(Module* mod)
+               : ModuleEventListener(mod, "event/http-request")
+       {
+       }
+
+       virtual ModResult OnHTTPRequest(HTTPRequest& req) = 0;
 };