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