2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
33 class HTTPQueryParameters : public insp::flat_multimap<std::string, std::string>
36 bool get(const std::string& key, std::string& value) const
38 const_iterator it = find(key);
46 std::string getString(const std::string& key, const std::string& def = "") const
56 T getNum(const std::string& key, T def = 0) const
62 return ConvToNum<T>(value);
65 unsigned long getDuration(const std::string& key, unsigned long def = 0) const
68 if (!InspIRCd::Duration(getString(key, "0"), value))
74 bool getBool(const std::string& key, bool def = false) const
76 return getNum<bool>(key, def);
83 HTTPQueryParameters query_params;
87 /** A modifyable list of HTTP header fields
92 std::map<std::string,std::string> headers;
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
98 void SetHeader(const std::string &name, const std::string &data)
100 headers[name] = data;
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
106 void CreateHeader(const std::string &name, const std::string &data)
109 SetHeader(name, data);
112 /** Remove the named header
114 void RemoveHeader(const std::string &name)
119 /** Remove all headers
126 /** Get the value of a header
127 * @return The value of the header, or an empty string
129 std::string GetHeader(const std::string &name)
131 std::map<std::string,std::string>::iterator it = headers.find(name);
132 if (it == headers.end())
133 return std::string();
138 /** Check if the given header is specified
139 * @return true if the header is specified
141 bool IsSet(const std::string &name)
143 std::map<std::string,std::string>::iterator it = headers.find(name);
144 return (it != headers.end());
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
150 std::string GetFormattedHeaders()
154 for (std::map<std::string,std::string>::iterator i = headers.begin(); i != headers.end(); i++)
155 re += i->first + ": " + i->second + "\r\n";
161 class HttpServerSocket;
163 /** This class represents a HTTP request.
170 std::string postdata;
171 HTTPRequestURI parseduri;
175 HTTPHeaders *headers;
178 /** A socket pointer, which you must return in your HTTPDocument class
179 * if you reply to this request.
181 HttpServerSocket* sock;
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
191 HTTPRequest(const std::string& request_type, const HTTPRequestURI& Parseduri,
192 HTTPHeaders* hdr, HttpServerSocket* socket, const std::string &ip, const std::string &pdata)
196 , parseduri(Parseduri)
202 /** Get the post data (request content).
203 * All post data will be returned, including carriage returns and linefeeds.
204 * @return The postdata
206 std::string& GetPostData()
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
215 std::string& GetType()
220 HTTPRequestURI& GetParsedURI()
225 std::string& GetPath()
227 return GetParsedURI().path;
230 /** Get IP address of requester.
231 * The requesting system's ip address will be returned.
232 * @return The IP address as a string
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.
246 class HTTPDocumentResponse
249 /** Module that generated this reply
251 Module* const module;
253 std::stringstream* document;
254 unsigned int responsecode;
256 /** Any extra headers to include with the defaults
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.
269 HTTPDocumentResponse(Module* mod, HTTPRequest& req, std::stringstream* doc, unsigned int response)
270 : module(mod), document(doc), responsecode(response), src(req)
275 class HTTPdAPIBase : public DataProvider
278 HTTPdAPIBase(Module* parent)
279 : DataProvider(parent, "m_httpd_api")
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
286 virtual void SendResponse(HTTPDocumentResponse& response) = 0;
289 /** The API provided by the httpd module that allows other modules to respond to incoming
292 class HTTPdAPI : public dynamic_reference<HTTPdAPIBase>
295 HTTPdAPI(Module* parent)
296 : dynamic_reference<HTTPdAPIBase>(parent, "m_httpd_api")
301 class HTTPACLEventListener : public Events::ModuleEventListener
304 HTTPACLEventListener(Module* mod)
305 : ModuleEventListener(mod, "event/http-acl")
309 virtual ModResult OnHTTPACLCheck(HTTPRequest& req) = 0;
312 class HTTPRequestEventListener : public Events::ModuleEventListener
315 HTTPRequestEventListener(Module* mod)
316 : ModuleEventListener(mod, "event/http-request")
320 virtual ModResult OnHTTPRequest(HTTPRequest& req) = 0;