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