]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/httpd.h
Remove InspIRCd::WriteOpers in favour of snomask O
[user/henk/code/inspircd.git] / src / modules / httpd.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 #include <map>
22
23 /** A modifyable list of HTTP header fields
24  */
25 class HTTPHeaders
26 {
27  protected:
28         std::map<std::string,std::string> headers;
29  public:
30         
31         /** Set the value of a header
32          * Sets the value of the named header. If the header is already present, it will be replaced
33          */
34         void SetHeader(const std::string &name, const std::string &data)
35         {
36                 headers[name] = data;
37         }
38         
39         /** Set the value of a header, only if it doesn't exist already
40          * Sets the value of the named header. If the header is already present, it will NOT be updated
41          */
42         void CreateHeader(const std::string &name, const std::string &data)
43         {
44                 if (!IsSet(name))
45                         SetHeader(name, data);
46         }
47         
48         /** Remove the named header
49          */
50         void RemoveHeader(const std::string &name)
51         {
52                 headers.erase(name);
53         }
54         
55         /** Remove all headers
56          */
57         void Clear()
58         {
59                 headers.clear();
60         }
61         
62         /** Get the value of a header
63          * @return The value of the header, or an empty string
64          */
65         std::string GetHeader(const std::string &name)
66         {
67                 std::map<std::string,std::string>::iterator it = headers.find(name);
68                 if (it == headers.end())
69                         return std::string();
70                 
71                 return it->second;
72         }
73         
74         /** Check if the given header is specified
75          * @return true if the header is specified
76          */
77         bool IsSet(const std::string &name)
78         {
79                 std::map<std::string,std::string>::iterator it = headers.find(name);
80                 return (it != headers.end());
81         }
82         
83         /** Get all headers, formatted by the HTTP protocol
84          * @return Returns all headers, formatted according to the HTTP protocol. There is no request terminator at the end
85          */
86         std::string GetFormattedHeaders()
87         {
88                 std::string re;
89                 
90                 for (std::map<std::string,std::string>::iterator i = headers.begin(); i != headers.end(); i++)
91                         re += i->first + ": " + i->second + "\r\n";
92                 
93                 return re;
94         }
95 };
96
97 /** This class represents a HTTP request.
98  * It will be sent to all modules as the data section of
99  * an Event.
100  */
101 class HTTPRequest : public classbase
102 {
103  protected:
104         std::string type;
105         std::string document;
106         std::string ipaddr;
107         std::string postdata;
108         
109  public:
110
111         HTTPHeaders *headers;
112         
113         /** A socket pointer, which you must return in your HTTPDocument class
114          * if you reply to this request.
115          */
116         void* sock;
117
118         /** Initialize HTTPRequest.
119          * This constructor is called by m_httpd.so to initialize the class.
120          * @param request_type The request type, e.g. GET, POST, HEAD
121          * @param uri The URI, e.g. /page
122          * @param hdr The headers sent with the request
123          * @param opaque An opaque pointer used internally by m_httpd, which you must pass back to the module in your reply.
124          * @param ip The IP address making the web request.
125          * @param pdata The post data (content after headers) received with the request, up to Content-Length in size
126          */
127         HTTPRequest(const std::string &request_type, const std::string &uri, HTTPHeaders* hdr, void* opaque, const std::string &ip, const std::string &pdata)
128                 : type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(opaque)
129         {
130         }
131
132         /** Get the post data (request content).
133          * All post data will be returned, including carriage returns and linefeeds.
134          * @return The postdata
135          */
136         std::string& GetPostData()
137         {
138                 return postdata;
139         }
140
141         /** Get the request type.
142          * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.
143          * @return The request type, e.g. GET, POST, HEAD
144          */
145         std::string& GetType()
146         {
147                 return type;
148         }
149
150         /** Get URI.
151          * The URI string (URL minus hostname and scheme) will be provided by this function.
152          * @return The URI being requested
153          */
154         std::string& GetURI()
155         {
156                 return document;
157         }
158
159         /** Get IP address of requester.
160          * The requesting system's ip address will be returned.
161          * @return The IP address as a string
162          */
163         std::string& GetIP()
164         {
165                 return ipaddr;
166         }
167 };
168
169 /** You must return a HTTPDocument to the httpd module by using the Request class.
170  * When you initialize this class you may initialize it with all components required to
171  * form a valid HTTP response, including document data, headers, and a response code.
172  */
173 class HTTPDocument : public classbase
174 {
175  protected:
176
177         std::stringstream* document;
178         int responsecode;
179
180  public:
181
182         HTTPHeaders headers;
183         
184         /** The socket pointer from an earlier HTTPRequest
185          */
186         void* sock;
187
188         /** Initialize a HTTPRequest ready for sending to m_httpd.so.
189          * @param opaque The socket pointer you obtained from the HTTPRequest at an earlier time
190          * @param doc A stringstream containing the document body
191          * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
192          * based upon the response code.
193          * @param extra Any extra headers to include with the defaults, seperated by carriage return and linefeed.
194          */
195         HTTPDocument(void* opaque, std::stringstream* doc, int response) : document(doc), responsecode(response), sock(opaque)
196         {
197         }
198
199         /** Get the document text.
200          * @return The document text
201          */
202         std::stringstream* GetDocument()
203         {
204                 return this->document;
205         }
206
207         /** Get the document size.
208          * @return the size of the document text in bytes
209          */
210         unsigned long GetDocumentSize()
211         {
212                 return this->document->str().length();
213         }
214
215         /** Get the response code.
216          * @return The response code
217          */
218         int GetResponseCode()
219         {
220                 return this->responsecode;
221         }
222 };
223
224 #endif
225