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 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
194 HTTPRequest(const std::string& request_type, const HTTPRequestURI& Parseduri,
195 HTTPHeaders* hdr, HttpServerSocket* socket, const std::string &ip, const std::string &pdata)
199 , parseduri(Parseduri)
205 /** Get the post data (request content).
206 * All post data will be returned, including carriage returns and linefeeds.
207 * @return The postdata
209 std::string& GetPostData()
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
218 std::string& GetType()
223 HTTPRequestURI& GetParsedURI()
228 std::string& GetPath()
230 return GetParsedURI().path;
233 /** Get IP address of requester.
234 * The requesting system's ip address will be returned.
235 * @return The IP address as a string
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.
249 class HTTPDocumentResponse
252 /** Module that generated this reply
254 Module* const module;
256 std::stringstream* document;
257 unsigned int responsecode;
259 /** Any extra headers to include with the defaults
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.
272 HTTPDocumentResponse(Module* mod, HTTPRequest& req, std::stringstream* doc, unsigned int response)
273 : module(mod), document(doc), responsecode(response), src(req)
278 class HTTPdAPIBase : public DataProvider
281 HTTPdAPIBase(Module* parent)
282 : DataProvider(parent, "m_httpd_api")
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
289 virtual void SendResponse(HTTPDocumentResponse& response) = 0;
292 /** The API provided by the httpd module that allows other modules to respond to incoming
295 class HTTPdAPI : public dynamic_reference<HTTPdAPIBase>
298 HTTPdAPI(Module* parent)
299 : dynamic_reference<HTTPdAPIBase>(parent, "m_httpd_api")
304 class HTTPACLEventListener : public Events::ModuleEventListener
307 HTTPACLEventListener(Module* mod)
308 : ModuleEventListener(mod, "event/http-acl")
312 virtual ModResult OnHTTPACLCheck(HTTPRequest& req) = 0;
315 class HTTPRequestEventListener : public Events::ModuleEventListener
318 HTTPRequestEventListener(Module* mod)
319 : ModuleEventListener(mod, "event/http-request")
323 virtual ModResult OnHTTPRequest(HTTPRequest& req) = 0;