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