]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/httpd.h
m_auditorium Handle NULL return from Channel::GetUser() in OnSendWhoLine()
[user/henk/code/inspircd.git] / src / modules / httpd.h
index 32bac757f12835d50d8db92e14346add3f8219c2..56fd22da03820a166f63b03ee6695426f2184168 100644 (file)
@@ -1 +1,207 @@
-/*       +------------------------------------+\r *       | Inspire Internet Relay Chat Daemon |\r *       +------------------------------------+\r *\r *  InspIRCd: (C) 2002-2007 InspIRCd Development Team\r * See: http://www.inspircd.org/wiki/index.php/Credits\r *\r * This program is free but copyrighted software; see\r *            the file COPYING for details.\r *\r * ---------------------------------------------------\r */\r\r#include "base.h"\r\r#ifndef __HTTPD_H__\r#define __HTTPD_H__\r\r#include <string>\r#include <sstream>\r\r/** This class represents a HTTP request.\r * It will be sent to all modules as the data section of\r * an Event.\r */\rclass HTTPRequest : public classbase\r{\r protected:\r\r std::string type;\r      std::string document;\r  std::string ipaddr;\r    std::string postdata;\r  std::stringstream* headers;\r\r public:\r\r /** A socket pointer, which you must return in your HTTPDocument class\r  * if you reply to this request.\r        */\r    void* sock;\r\r   /** Initialize HTTPRequest.\r     * This constructor is called by m_httpd.so to initialize the class.\r    * @param request_type The request type, e.g. GET, POST, HEAD\r   * @param uri The URI, e.g. /page\r       * @param hdr The headers sent with the request\r         * @param opaque An opaque pointer used internally by m_httpd, which you must pass back to the module in your reply.\r    * @param ip The IP address making the web request.\r     * @param pdata The post data (content after headers) received with the request, up to Content-Length in size\r   */\r    HTTPRequest(const std::string &request_type, const std::string &uri, std::stringstream* hdr, void* opaque, const std::string &ip, const std::string &pdata)\r            : type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(opaque)\r   {\r      }\r\r     /** Get headers.\r        * All the headers for the web request are returned, as a pointer to a stringstream.\r    * @return The header information\r       */\r    std::stringstream* GetHeaders()\r        {\r              return headers;\r        }\r\r     /** Get the post data (request content).\r        * All post data will be returned, including carriage returns and linefeeds.\r    * @return The postdata\r         */\r    std::string& GetPostData()\r     {\r              return postdata;\r       }\r\r     /** Get the request type.\r       * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.\r       * @return The request type, e.g. GET, POST, HEAD\r       */\r    std::string& GetType()\r {\r              return type;\r   }\r\r     /** Get URI.\r    * The URI string (URL minus hostname and scheme) will be provided by this function.\r    * @return The URI being requested\r      */\r    std::string& GetURI()\r  {\r              return document;\r       }\r\r     /** Get IP address of requester.\r        * The requesting system's ip address will be returned.\r         * @return The IP address as a string\r   */\r    std::string& GetIP()\r   {\r              return ipaddr;\r }\r};\r\r/** You must return a HTTPDocument to the httpd module by using the Request class.\r * When you initialize this class you may initialize it with all components required to\r * form a valid HTTP response, including document data, headers, and a response code.\r */\rclass HTTPDocument : public classbase\r{\r protected:\r\r        std::stringstream* document;\r   int responsecode;\r      std::string extraheaders;\r\r public:\r\r   /** The socket pointer from an earlier HTTPRequest\r      */\r    void* sock;\r\r   /** Initialize a HTTPRequest ready for sending to m_httpd.so.\r   * @param opaque The socket pointer you obtained from the HTTPRequest at an earlier time\r        * @param doc A stringstream containing the document body\r       * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you\r     * based upon the response code.\r        * @param extra Any extra headers to include with the defaults, seperated by carriage return and linefeed.\r      */\r    HTTPDocument(void* opaque, std::stringstream* doc, int response, const std::string &extra) : document(doc), responsecode(response), extraheaders(extra), sock(opaque)\r  {\r      }\r\r     /** Get the document text.\r      * @return The document text\r    */\r    std::stringstream* GetDocument()\r       {\r              return this->document;\r }\r\r     /** Get the document size.\r      * @return the size of the document text in bytes\r       */\r    unsigned long GetDocumentSize()\r        {\r              return this->document->str().length();\r }\r\r     /** Get the response code.\r      * @return The response code\r    */\r    int GetResponseCode()\r  {\r              return this->responsecode;\r     }\r\r     /** Get the headers.\r    * @return The header text, headers seperated by carriage return and linefeed.\r  */\r    std::string& GetExtraHeaders()\r {\r              return this->extraheaders;\r     }\r};\r\r#endif\r\r
\ No newline at end of file
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ *   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 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
+ *
+ * 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
+ * License as published by the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "base.h"
+
+#ifndef HTTPD_H
+#define HTTPD_H
+
+#include <string>
+#include <sstream>
+#include <map>
+
+/** A modifyable list of HTTP header fields
+ */
+class HTTPHeaders
+{
+ protected:
+       std::map<std::string,std::string> headers;
+ public:
+
+       /** Set the value of a header
+        * Sets the value of the named header. If the header is already present, it will be replaced
+        */
+       void SetHeader(const std::string &name, const std::string &data)
+       {
+               headers[name] = data;
+       }
+
+       /** Set the value of a header, only if it doesn't exist already
+        * Sets the value of the named header. If the header is already present, it will NOT be updated
+        */
+       void CreateHeader(const std::string &name, const std::string &data)
+       {
+               if (!IsSet(name))
+                       SetHeader(name, data);
+       }
+
+       /** Remove the named header
+        */
+       void RemoveHeader(const std::string &name)
+       {
+               headers.erase(name);
+       }
+
+       /** Remove all headers
+        */
+       void Clear()
+       {
+               headers.clear();
+       }
+
+       /** Get the value of a header
+        * @return The value of the header, or an empty string
+        */
+       std::string GetHeader(const std::string &name)
+       {
+               std::map<std::string,std::string>::iterator it = headers.find(name);
+               if (it == headers.end())
+                       return std::string();
+
+               return it->second;
+       }
+
+       /** Check if the given header is specified
+        * @return true if the header is specified
+        */
+       bool IsSet(const std::string &name)
+       {
+               std::map<std::string,std::string>::iterator it = headers.find(name);
+               return (it != headers.end());
+       }
+
+       /** Get all headers, formatted by the HTTP protocol
+        * @return Returns all headers, formatted according to the HTTP protocol. There is no request terminator at the end
+        */
+       std::string GetFormattedHeaders()
+       {
+               std::string re;
+
+               for (std::map<std::string,std::string>::iterator i = headers.begin(); i != headers.end(); i++)
+                       re += i->first + ": " + i->second + "\r\n";
+
+               return re;
+       }
+};
+
+class HttpServerSocket;
+
+/** This class represents a HTTP request.
+ */
+class HTTPRequest : public Event
+{
+ protected:
+       std::string type;
+       std::string document;
+       std::string ipaddr;
+       std::string postdata;
+
+ public:
+
+       HTTPHeaders *headers;
+       int errorcode;
+
+       /** A socket pointer, which you must return in your HTTPDocument class
+        * if you reply to this request.
+        */
+       HttpServerSocket* sock;
+
+       /** 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 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 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)
+       {
+       }
+
+       /** Get the post data (request content).
+        * All post data will be returned, including carriage returns and linefeeds.
+        * @return The postdata
+        */
+       std::string& GetPostData()
+       {
+               return postdata;
+       }
+
+       /** Get the request type.
+        * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.
+        * @return The request type, e.g. GET, POST, HEAD
+        */
+       std::string& GetType()
+       {
+               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()
+       {
+               return document;
+       }
+
+       /** Get IP address of requester.
+        * The requesting system's ip address will be returned.
+        * @return The IP address as a string
+        */
+       std::string& GetIP()
+       {
+               return ipaddr;
+       }
+};
+
+/** 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.
+ */
+class HTTPDocumentResponse : public Request
+{
+ public:
+       std::stringstream* document;
+       int responsecode;
+       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
+        * @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)
+       {
+       }
+};
+
+#endif
+