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