]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/httpd.h
Update copyright headers.
[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, 2021 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2013, 2015 Attila Molnar <attilamolnar@hush.com>
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 parsed_uri The URI which was requested by the client.
190          * @param hdr The headers sent with the request
191          * @param socket The server socket which this request came in via.
192          * @param ip The IP address making the web request.
193          * @param pdata The post data (content after headers) received with the request, up to Content-Length in size
194          */
195         HTTPRequest(const std::string& request_type, const HTTPRequestURI& parsed_uri, HTTPHeaders* hdr,
196                         HttpServerSocket* socket, const std::string& ip, const std::string& pdata)
197                 : type(request_type)
198                 , ipaddr(ip)
199                 , postdata(pdata)
200                 , parseduri(parsed_uri)
201                 , headers(hdr)
202                 , sock(socket)
203         {
204         }
205
206         /** Get the post data (request content).
207          * All post data will be returned, including carriage returns and linefeeds.
208          * @return The postdata
209          */
210         std::string& GetPostData()
211         {
212                 return postdata;
213         }
214
215         /** Get the request type.
216          * Any request type can be intercepted, even ones which are invalid in the HTTP/1.1 spec.
217          * @return The request type, e.g. GET, POST, HEAD
218          */
219         std::string& GetType()
220         {
221                 return type;
222         }
223
224         HTTPRequestURI& GetParsedURI()
225         {
226                 return parseduri;
227         }
228
229         std::string& GetPath()
230         {
231                 return GetParsedURI().path;
232         }
233
234         /** Get IP address of requester.
235          * The requesting system's ip address will be returned.
236          * @return The IP address as a string
237          */
238         std::string& GetIP()
239         {
240                 return ipaddr;
241         }
242 };
243
244 /** If you want to reply to HTTP requests, you must return a HTTPDocumentResponse to
245  * the httpd module via the HTTPdAPI.
246  * When you initialize this class you initialize it with all components required to
247  * form a valid HTTP response: the document data and a response code.
248  * You can add additional HTTP headers, if you want.
249  */
250 class HTTPDocumentResponse
251 {
252  public:
253         /** Module that generated this reply
254          */
255         Module* const module;
256
257         std::stringstream* document;
258         unsigned int responsecode;
259
260         /** Any extra headers to include with the defaults
261          */
262         HTTPHeaders headers;
263
264         HTTPRequest& src;
265
266         /** Initialize a HTTPDocumentResponse ready for sending to the httpd module.
267          * @param mod A pointer to the module who responded to the request
268          * @param req The request you obtained from the HTTPRequest at an earlier time
269          * @param doc A stringstream containing the document body
270          * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
271          * based upon the response code.
272          */
273         HTTPDocumentResponse(Module* mod, HTTPRequest& req, std::stringstream* doc, unsigned int response)
274                 : module(mod), document(doc), responsecode(response), src(req)
275         {
276         }
277 };
278
279 class HTTPdAPIBase : public DataProvider
280 {
281  public:
282         HTTPdAPIBase(Module* parent)
283                 : DataProvider(parent, "m_httpd_api")
284         {
285         }
286
287         /** Answer an incoming HTTP request with the provided document
288          * @param response The response created by your module that will be sent to the client
289          */
290         virtual void SendResponse(HTTPDocumentResponse& response) = 0;
291 };
292
293 /** The API provided by the httpd module that allows other modules to respond to incoming
294  * HTTP requests
295  */
296 class HTTPdAPI : public dynamic_reference<HTTPdAPIBase>
297 {
298  public:
299         HTTPdAPI(Module* parent)
300                 : dynamic_reference<HTTPdAPIBase>(parent, "m_httpd_api")
301         {
302         }
303 };
304
305 class HTTPACLEventListener : public Events::ModuleEventListener
306 {
307  public:
308         HTTPACLEventListener(Module* mod)
309                 : ModuleEventListener(mod, "event/http-acl")
310         {
311         }
312
313         virtual ModResult OnHTTPACLCheck(HTTPRequest& req) = 0;
314 };
315
316 class HTTPRequestEventListener : public Events::ModuleEventListener
317 {
318  public:
319         HTTPRequestEventListener(Module* mod)
320                 : ModuleEventListener(mod, "event/http-request")
321         {
322         }
323
324         virtual ModResult OnHTTPRequest(HTTPRequest& req) = 0;
325 };