mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-09-12 13:42:10 +02:00
http: Implement HTTPHeaders class
see: https://www.rfc-editor.org/rfc/rfc2616#section-4.2 https://www.rfc-editor.org/rfc/rfc7231#section-5 https://www.rfc-editor.org/rfc/rfc7231#section-7 https://httpwg.org/specs/rfc9111.html#header.field.definitions
This commit is contained in:
@@ -10,6 +10,9 @@
|
||||
#include <span>
|
||||
#include <string>
|
||||
|
||||
#include <util/strencodings.h>
|
||||
#include <util/string.h>
|
||||
|
||||
namespace util {
|
||||
class SignalInterrupt;
|
||||
} // namespace util
|
||||
@@ -187,4 +190,50 @@ private:
|
||||
struct event* ev;
|
||||
};
|
||||
|
||||
namespace http_bitcoin {
|
||||
|
||||
//! Maximum size of each headers line in an HTTP request,
|
||||
//! also the maximum size of all headers total.
|
||||
//! See https://github.com/bitcoin/bitcoin/pull/6859
|
||||
//! And libevent http.c evhttp_parse_headers_()
|
||||
constexpr size_t MAX_HEADERS_SIZE{8192};
|
||||
|
||||
class HTTPHeaders
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @param[in] key The field-name of the header to search for
|
||||
* @returns The value of the first header that matches the provided key
|
||||
* nullopt if key is not found
|
||||
*/
|
||||
std::optional<std::string> FindFirst(std::string_view key) const;
|
||||
/**
|
||||
* @param[in] key The field-name of the header to search for
|
||||
* @returns Views into all values matching the provided key (valid while this object is alive)
|
||||
*/
|
||||
std::vector<std::string_view> FindAll(std::string_view key) const;
|
||||
void Write(std::string&& key, std::string&& value);
|
||||
/**
|
||||
* @param[in] key The field-name of the header to search for and delete
|
||||
*/
|
||||
void RemoveAll(std::string_view key);
|
||||
/**
|
||||
* @returns false if LineReader hits the end of the buffer before reading an
|
||||
* \n, meaning that we are still waiting on more data from the client.
|
||||
* true after reading an entire HTTP headers section, terminated
|
||||
* by an empty line and \n.
|
||||
* @throws on exceeded read limit and on bad headers syntax (e.g. no ":" in a line)
|
||||
*/
|
||||
bool Read(util::LineReader& reader);
|
||||
std::string Stringify() const;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Headers can have duplicate field names, so we use a vector of key-value pairs instead of a map.
|
||||
* https://httpwg.org/specs/rfc9110.html#rfc.section.5.2
|
||||
*/
|
||||
std::vector<std::pair<std::string, std::string>> m_headers;
|
||||
};
|
||||
} // namespace http_bitcoin
|
||||
|
||||
#endif // BITCOIN_HTTPSERVER_H
|
||||
|
||||
Reference in New Issue
Block a user