]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/httpd.h
Remove more classbase
[user/henk/code/inspircd.git] / src / modules / httpd.h
index 565648659e7268e36f07cd5203008a70161718df..97148d413cab69ba2a51cf28a3fe748920cc5e18 100644 (file)
@@ -1,3 +1,16 @@
+/*       +------------------------------------+
+ *       | Inspire Internet Relay Chat Daemon |
+ *       +------------------------------------+
+ *
+ *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ * See: http://wiki.inspircd.org/Credits
+ *
+ * This program is free but copyrighted software; see
+ *            the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
 #include "base.h"
 
 #ifndef __HTTPD_H__
 
 #include <string>
 #include <sstream>
+#include <map>
 
-/** This class represents a HTTP request.
- * It will be sent to all modules as the data section of
- * an Event.
+/** A modifyable list of HTTP header fields
  */
-class HTTPRequest : public classbase
+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::stringstream* headers;
+       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.
         */
-       void* sock;
+       HttpServerSocket* sock;
 
        /** Initialize HTTPRequest.
         * This constructor is called by m_httpd.so to initialize the class.
@@ -33,19 +123,21 @@ class HTTPRequest : public classbase
         * @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(const std::string &request_type, const std::string &uri, std::stringstream* hdr, void* opaque, const std::string &ip)
-               : type(request_type), document(uri), ipaddr(ip), headers(hdr), sock(opaque)
+       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 headers.
-        * All the headers for the web request are returned, as a pointer to a stringstream.
-        * @return The header information
+       /** Get the post data (request content).
+        * All post data will be returned, including carriage returns and linefeeds.
+        * @return The postdata
         */
-       std::stringstream* GetHeaders()
+       std::string& GetPostData()
        {
-               return headers;
+               return postdata;
        }
 
        /** Get the request type.
@@ -80,19 +172,13 @@ class HTTPRequest : public classbase
  * 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 HTTPDocument : public classbase
+class HTTPDocumentResponse : public Request
 {
- protected:
-
+ public:
        std::stringstream* document;
        int responsecode;
-       std::string extraheaders;
-
- public:
-
-       /** The socket pointer from an earlier HTTPRequest
-        */
-       void* sock;
+       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
@@ -101,40 +187,9 @@ class HTTPDocument : public classbase
         * based upon the response code.
         * @param extra Any extra headers to include with the defaults, seperated by carriage return and linefeed.
         */
-       HTTPDocument(void* opaque, std::stringstream* doc, int response, const std::string &extra) : document(doc), responsecode(response), extraheaders(extra), sock(opaque)
-       {
-       }
-
-       /** Get the document text.
-        * @return The document text
-        */
-       std::stringstream* GetDocument()
-       {
-               return this->document;
-       }
-
-       /** Get the document size.
-        * @return the size of the document text in bytes
-        */
-       unsigned long GetDocumentSize()
-       {
-               return this->document->str().length();
-       }
-
-       /** Get the response code.
-        * @return The response code
-        */
-       int GetResponseCode()
-       {
-               return this->responsecode;
-       }
-
-       /** Get the headers.
-        * @return The header text, headers seperated by carriage return and linefeed.
-        */
-       std::string& GetExtraHeaders()
+       HTTPDocumentResponse(Module* me, HTTPRequest& req, std::stringstream* doc, int response)
+               : Request(me, req.source, "HTTP-DOC"), document(doc), responsecode(response), src(req)
        {
-               return this->extraheaders;
        }
 };