2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
36 class HTTPQueryParameters : public insp::flat_multimap<std::string, std::string>
39 bool get(const std::string& key, std::string& value) const
41 const_iterator it = find(key);
49 std::string getString(const std::string& key, const std::string& def = "") const
59 T getNum(const std::string& key, T def = 0) const
65 return ConvToNum<T>(value);
68 unsigned long getDuration(const std::string& key, unsigned long def = 0) const
71 if (!InspIRCd::Duration(getString(key, "0"), value))
77 bool getBool(const std::string& key, bool def = false) const
79 return getNum<bool>(key, def);
86 HTTPQueryParameters query_params;
90 /** A modifyable list of HTTP header fields
95 std::map<std::string,std::string> headers;
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
101 void SetHeader(const std::string &name, const std::string &data)
103 headers[name] = data;
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
109 void CreateHeader(const std::string &name, const std::string &data)
112 SetHeader(name, data);
115 /** Remove the named header
117 void RemoveHeader(const std::string &name)
122 /** Remove all headers
129 /** Get the value of a header
130 * @return The value of the header, or an empty string
132 std::string GetHeader(const std::string &name)
134 std::map<std::string,std::string>::iterator it = headers.find(name);
135 if (it == headers.end())
136 return std::string();
141 /** Check if the given header is specified
142 * @return true if the header is specified
144 bool IsSet(const std::string &name)
146 std::map<std::string,std::string>::iterator it = headers.find(name);
147 return (it != headers.end());
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
153 std::string GetFormattedHeaders()
157 for (std::map<std::string,std::string>::iterator i = headers.begin(); i != headers.end(); i++)
158 re += i->first + ": " + i->second + "\r\n";
164 class HttpServerSocket;
166 /** This class represents a HTTP request.
173 std::string postdata;
174 HTTPRequestURI parseduri;
178 HTTPHeaders *headers;
181 /** A socket pointer, which you must return in your HTTPDocument class
182 * if you reply to this request.
184 HttpServerSocket* sock;
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
195 HTTPRequest(const std::string& request_type, const HTTPRequestURI& parsed_uri, HTTPHeaders* hdr,
196 HttpServerSocket* socket, const std::string& ip, const std::string& pdata)
200 , parseduri(parsed_uri)
206 /** Get the post data (request content).
207 * All post data will be returned, including carriage returns and linefeeds.
208 * @return The postdata
210 std::string& GetPostData()
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
219 std::string& GetType()
224 HTTPRequestURI& GetParsedURI()
229 std::string& GetPath()
231 return GetParsedURI().path;
234 /** Get IP address of requester.
235 * The requesting system's ip address will be returned.
236 * @return The IP address as a string
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.
250 class HTTPDocumentResponse
253 /** Module that generated this reply
255 Module* const module;
257 std::stringstream* document;
258 unsigned int responsecode;
260 /** Any extra headers to include with the defaults
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.
273 HTTPDocumentResponse(Module* mod, HTTPRequest& req, std::stringstream* doc, unsigned int response)
274 : module(mod), document(doc), responsecode(response), src(req)
279 class HTTPdAPIBase : public DataProvider
282 HTTPdAPIBase(Module* parent)
283 : DataProvider(parent, "m_httpd_api")
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
290 virtual void SendResponse(HTTPDocumentResponse& response) = 0;
293 /** The API provided by the httpd module that allows other modules to respond to incoming
296 class HTTPdAPI : public dynamic_reference<HTTPdAPIBase>
299 HTTPdAPI(Module* parent)
300 : dynamic_reference<HTTPdAPIBase>(parent, "m_httpd_api")
305 class HTTPACLEventListener : public Events::ModuleEventListener
308 HTTPACLEventListener(Module* mod)
309 : ModuleEventListener(mod, "event/http-acl")
313 virtual ModResult OnHTTPACLCheck(HTTPRequest& req) = 0;
316 class HTTPRequestEventListener : public Events::ModuleEventListener
319 HTTPRequestEventListener(Module* mod)
320 : ModuleEventListener(mod, "event/http-request")
324 virtual ModResult OnHTTPRequest(HTTPRequest& req) = 0;