]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/httpd.h
Document the anticaps exemption.
[user/henk/code/inspircd.git] / include / modules / httpd.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6  *   Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #pragma once
25
26 #include "base.h"
27 #include "event.h"
28
29 #include <string>
30 #include <sstream>
31 #include <map>
32
33 class HTTPQueryParameters : public insp::flat_multimap<std::string, std::string>
34 {
35  public:
36         bool get(const std::string& key, std::string& value) const
37         {
38                 const_iterator it = find(key);
39                 if (it == end())
40                         return false;
41
42                 value = it->second;
43                 return true;
44         }
45
46         std::string getString(const std::string& key, const std::string& def = "") const
47         {
48                 std::string value;
49                 if (!get(key, value))
50                         return def;
51
52                 return value;
53         }
54
55         template <typename T>
56         T getNum(const std::string& key, T def = 0) const
57         {
58                 std::string value;
59                 if (!get(key, value))
60                         return def;
61
62                 return ConvToNum<T>(value);
63         }
64
65         unsigned long getDuration(const std::string& key, unsigned long def = 0) const
66         {
67                 unsigned long value;
68                 if (!InspIRCd::Duration(getString(key, "0"), value))
69                         return def;
70
71                 return value;
72         }
73
74         bool getBool(const std::string& key, bool def = false) const
75         {
76                 return getNum<bool>(key, def);
77         }
78 };
79
80 struct HTTPRequestURI
81 {
82         std::string path;
83         HTTPQueryParameters query_params;
84         std::string fragment;
85 };
86
87 /** A modifyable list of HTTP header fields
88  */
89 class HTTPHeaders
90 {
91  protected:
92         std::map<std::string,std::string> headers;
93  public:
94
95         /** Set the value of a header
96          * Sets the value of the named header. If the header is already present, it will be replaced
97          */
98         void SetHeader(const std::string &name, const std::string &data)
99         {
100                 headers[name] = data;
101         }
102
103         /** Set the value of a header, only if it doesn't exist already
104          * Sets the value of the named header. If the header is already present, it will NOT be updated
105          */
106         void CreateHeader(const std::string &name, const std::string &data)
107         {
108                 if (!IsSet(name))
109                         SetHeader(name, data);
110         }
111
112         /** Remove the named header
113          */
114         void RemoveHeader(const std::string &name)
115         {
116                 headers.erase(name);
117         }
118
119         /** Remove all headers
120          */
121         void Clear()
122         {
123                 headers.clear();
124         }
125
126         /** Get the value of a header
127          * @return The value of the header, or an empty string
128          */
129         std::string GetHeader(const std::string &name)
130         {
131                 std::map<std::string,std::string>::iterator it = headers.find(name);
132                 if (it == headers.end())
133                         return std::string();
134
135                 return it->second;
136         }
137
138         /** Check if the given header is specified
139          * @return true if the header is specified
140          */
141         bool IsSet(const std::string &name)
142         {
143                 std::map<std::string,std::string>::iterator it = headers.find(name);
144                 return (it != headers.end());
145         }
146
147         /** Get all headers, formatted by the HTTP protocol
148          * @return Returns all headers, formatted according to the HTTP protocol. There is no request terminator at the end
149          */
150         std::string GetFormattedHeaders()
151         {
152                 std::string re;
153
154                 for (std::map<std::string,std::string>::iterator i = headers.begin(); i != headers.end(); i++)
155                         re += i->first + ": " + i->second + "\r\n";
156
157                 return re;
158         }
159 };
160
161 class HttpServerSocket;
162
163 /** This class represents a HTTP request.
164  */
165 class HTTPRequest
166 {
167  protected:
168         std::string type;
169         std::string ipaddr;
170         std::string postdata;
171         HTTPRequestURI parseduri;
172
173  public:
174
175         HTTPHeaders *headers;
176         int errorcode;
177
178         /** A socket pointer, which you must return in your HTTPDocument class
179          * if you reply to this request.
180          */
181         HttpServerSocket* sock;
182
183         /** Initialize HTTPRequest.
184          * This constructor is called by m_httpd.so to initialize the class.
185          * @param request_type The request type, e.g. GET, POST, HEAD
186          * @param hdr The headers sent with the request
187          * @param opaque An opaque pointer used internally by m_httpd, which you must pass back to the module in your reply.
188          * @param ip The IP address making the web request.
189          * @param pdata The post data (content after headers) received with the request, up to Content-Length in size
190          */
191         HTTPRequest(const std::string& request_type, const HTTPRequestURI& Parseduri,
192                 HTTPHeaders* hdr, HttpServerSocket* socket, const std::string &ip, const std::string &pdata)
193                 : type(request_type)
194                 , ipaddr(ip)
195                 , postdata(pdata)
196                 , parseduri(Parseduri)
197                 , headers(hdr)
198                 , sock(socket)
199         {
200         }
201
202         /** Get the post data (request content).
203          * All post data will be returned, including carriage returns and linefeeds.
204          * @return The postdata
205          */
206         std::string& GetPostData()
207         {
208                 return postdata;
209         }
210
211         /** Get the request type.
212          * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.
213          * @return The request type, e.g. GET, POST, HEAD
214          */
215         std::string& GetType()
216         {
217                 return type;
218         }
219
220         HTTPRequestURI& GetParsedURI()
221         {
222                 return parseduri;
223         }
224
225         std::string& GetPath()
226         {
227                 return GetParsedURI().path;
228         }
229
230         /** Get IP address of requester.
231          * The requesting system's ip address will be returned.
232          * @return The IP address as a string
233          */
234         std::string& GetIP()
235         {
236                 return ipaddr;
237         }
238 };
239
240 /** If you want to reply to HTTP requests, you must return a HTTPDocumentResponse to
241  * the httpd module via the HTTPdAPI.
242  * When you initialize this class you initialize it with all components required to
243  * form a valid HTTP response: the document data and a response code.
244  * You can add additional HTTP headers, if you want.
245  */
246 class HTTPDocumentResponse
247 {
248  public:
249         /** Module that generated this reply
250          */
251         Module* const module;
252
253         std::stringstream* document;
254         unsigned int responsecode;
255
256         /** Any extra headers to include with the defaults
257          */
258         HTTPHeaders headers;
259
260         HTTPRequest& src;
261
262         /** Initialize a HTTPDocumentResponse ready for sending to the httpd module.
263          * @param mod A pointer to the module who responded to the request
264          * @param req The request you obtained from the HTTPRequest at an earlier time
265          * @param doc A stringstream containing the document body
266          * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
267          * based upon the response code.
268          */
269         HTTPDocumentResponse(Module* mod, HTTPRequest& req, std::stringstream* doc, unsigned int response)
270                 : module(mod), document(doc), responsecode(response), src(req)
271         {
272         }
273 };
274
275 class HTTPdAPIBase : public DataProvider
276 {
277  public:
278         HTTPdAPIBase(Module* parent)
279                 : DataProvider(parent, "m_httpd_api")
280         {
281         }
282
283         /** Answer an incoming HTTP request with the provided document
284          * @param response The response created by your module that will be sent to the client
285          */
286         virtual void SendResponse(HTTPDocumentResponse& response) = 0;
287 };
288
289 /** The API provided by the httpd module that allows other modules to respond to incoming
290  * HTTP requests
291  */
292 class HTTPdAPI : public dynamic_reference<HTTPdAPIBase>
293 {
294  public:
295         HTTPdAPI(Module* parent)
296                 : dynamic_reference<HTTPdAPIBase>(parent, "m_httpd_api")
297         {
298         }
299 };
300
301 class HTTPACLEventListener : public Events::ModuleEventListener
302 {
303  public:
304         HTTPACLEventListener(Module* mod)
305                 : ModuleEventListener(mod, "event/http-acl")
306         {
307         }
308
309         virtual ModResult OnHTTPACLCheck(HTTPRequest& req) = 0;
310 };
311
312 class HTTPRequestEventListener : public Events::ModuleEventListener
313 {
314  public:
315         HTTPRequestEventListener(Module* mod)
316                 : ModuleEventListener(mod, "event/http-request")
317         {
318         }
319
320         virtual ModResult OnHTTPRequest(HTTPRequest& req) = 0;
321 };