]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/httpd.h
More (last of extra)
[user/henk/code/inspircd.git] / src / modules / httpd.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "base.h"
15
16 #ifndef __HTTPD_H__
17 #define __HTTPD_H__
18
19 #include <string>
20 #include <sstream>
21
22 /** This class represents a HTTP request.
23  * It will be sent to all modules as the data section of
24  * an Event.
25  */
26 class HTTPRequest : public classbase
27 {
28  protected:
29
30         std::string type;
31         std::string document;
32         std::string ipaddr;
33         std::string postdata;
34         std::stringstream* headers;
35
36  public:
37
38         /** A socket pointer, which you must return in your HTTPDocument class
39          * if you reply to this request.
40          */
41         void* sock;
42
43         /** Initialize HTTPRequest.
44          * This constructor is called by m_httpd.so to initialize the class.
45          * @param request_type The request type, e.g. GET, POST, HEAD
46          * @param uri The URI, e.g. /page
47          * @param hdr The headers sent with the request
48          * @param opaque An opaque pointer used internally by m_httpd, which you must pass back to the module in your reply.
49          * @param ip The IP address making the web request.
50          * @param pdata The post data (content after headers) received with the request, up to Content-Length in size
51          */
52         HTTPRequest(const std::string &request_type, const std::string &uri, std::stringstream* hdr, void* opaque, const std::string &ip, const std::string &pdata)
53                 : type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(opaque)
54         {
55         }
56
57         /** Get headers.
58          * All the headers for the web request are returned, as a pointer to a stringstream.
59          * @return The header information
60          */
61         std::stringstream* GetHeaders()
62         {
63                 return headers;
64         }
65
66         /** Get the post data (request content).
67          * All post data will be returned, including carriage returns and linefeeds.
68          * @return The postdata
69          */
70         std::string& GetPostData()
71         {
72                 return postdata;
73         }
74
75         /** Get the request type.
76          * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.
77          * @return The request type, e.g. GET, POST, HEAD
78          */
79         std::string& GetType()
80         {
81                 return type;
82         }
83
84         /** Get URI.
85          * The URI string (URL minus hostname and scheme) will be provided by this function.
86          * @return The URI being requested
87          */
88         std::string& GetURI()
89         {
90                 return document;
91         }
92
93         /** Get IP address of requester.
94          * The requesting system's ip address will be returned.
95          * @return The IP address as a string
96          */
97         std::string& GetIP()
98         {
99                 return ipaddr;
100         }
101 };
102
103 /** You must return a HTTPDocument to the httpd module by using the Request class.
104  * When you initialize this class you may initialize it with all components required to
105  * form a valid HTTP response, including document data, headers, and a response code.
106  */
107 class HTTPDocument : public classbase
108 {
109  protected:
110
111         std::stringstream* document;
112         int responsecode;
113         std::string extraheaders;
114
115  public:
116
117         /** The socket pointer from an earlier HTTPRequest
118          */
119         void* sock;
120
121         /** Initialize a HTTPRequest ready for sending to m_httpd.so.
122          * @param opaque The socket pointer you obtained from the HTTPRequest at an earlier time
123          * @param doc A stringstream containing the document body
124          * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
125          * based upon the response code.
126          * @param extra Any extra headers to include with the defaults, seperated by carriage return and linefeed.
127          */
128         HTTPDocument(void* opaque, std::stringstream* doc, int response, const std::string &extra) : document(doc), responsecode(response), extraheaders(extra), sock(opaque)
129         {
130         }
131
132         /** Get the document text.
133          * @return The document text
134          */
135         std::stringstream* GetDocument()
136         {
137                 return this->document;
138         }
139
140         /** Get the document size.
141          * @return the size of the document text in bytes
142          */
143         unsigned long GetDocumentSize()
144         {
145                 return this->document->str().length();
146         }
147
148         /** Get the response code.
149          * @return The response code
150          */
151         int GetResponseCode()
152         {
153                 return this->responsecode;
154         }
155
156         /** Get the headers.
157          * @return The header text, headers seperated by carriage return and linefeed.
158          */
159         std::string& GetExtraHeaders()
160         {
161                 return this->extraheaders;
162         }
163 };
164
165 #endif
166