Bitcoin Core 28.99.0
P2P Digital Currency
|
In-flight HTTP request. More...
#include <httpserver.h>
Public Types | |
enum | RequestMethod { UNKNOWN , GET , POST , HEAD , PUT } |
Public Member Functions | |
HTTPRequest (struct evhttp_request *req, const util::SignalInterrupt &interrupt, bool replySent=false) | |
~HTTPRequest () | |
std::string | GetURI () const |
Get requested URI. More... | |
CService | GetPeer () const |
Get CService (address:ip) for the origin of the http request. More... | |
RequestMethod | GetRequestMethod () const |
Get request method. More... | |
std::optional< std::string > | GetQueryParameter (const std::string &key) const |
Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not found. More... | |
std::pair< bool, std::string > | GetHeader (const std::string &hdr) const |
Get the request header specified by hdr, or an empty string. More... | |
std::string | ReadBody () |
Read request body. More... | |
void | WriteHeader (const std::string &hdr, const std::string &value) |
Write output header. More... | |
void | WriteReply (int nStatus, std::string_view reply="") |
Write HTTP reply. More... | |
void | WriteReply (int nStatus, std::span< const std::byte > reply) |
Closure sent to main thread to request a reply to be sent to a HTTP request. More... | |
Private Attributes | |
struct evhttp_request * | req |
const util::SignalInterrupt & | m_interrupt |
bool | replySent |
In-flight HTTP request.
Thin C++ wrapper around evhttp_request.
Definition at line 61 of file httpserver.h.
Enumerator | |
---|---|
UNKNOWN | |
GET | |
POST | |
HEAD | |
PUT |
Definition at line 72 of file httpserver.h.
|
explicit |
Definition at line 590 of file httpserver.cpp.
HTTPRequest::~HTTPRequest | ( | ) |
std::pair< bool, std::string > HTTPRequest::GetHeader | ( | const std::string & | hdr | ) | const |
Get the request header specified by hdr, or an empty string.
Return a pair (isPresent,string).
Definition at line 605 of file httpserver.cpp.
CService HTTPRequest::GetPeer | ( | ) | const |
Get CService (address:ip) for the origin of the http request.
Definition at line 678 of file httpserver.cpp.
std::optional< std::string > HTTPRequest::GetQueryParameter | ( | const std::string & | key | ) | const |
Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not found.
If the query string contains duplicate keys, the first value is returned. Many web frameworks would instead parse this as an array of values, but this is not (yet) implemented as it is currently not needed in any of the endpoints.
[in] | key | represents the query parameter of which the value is returned |
Definition at line 719 of file httpserver.cpp.
HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod | ( | ) | const |
Get request method.
Definition at line 703 of file httpserver.cpp.
std::string HTTPRequest::GetURI | ( | ) | const |
Get requested URI.
Definition at line 698 of file httpserver.cpp.
std::string HTTPRequest::ReadBody | ( | ) |
Read request body.
Trivial implementation: if this is ever a performance bottleneck, internal copying can be avoided in multi-segment buffers by using evbuffer_peek and an awkward loop. Though in that case, it'd be even better to not copy into an intermediate string but use a stream abstraction to consume the evbuffer on the fly in the parsing algorithm.
Definition at line 616 of file httpserver.cpp.
void HTTPRequest::WriteHeader | ( | const std::string & | hdr, |
const std::string & | value | ||
) |
Write output header.
Definition at line 636 of file httpserver.cpp.
void HTTPRequest::WriteReply | ( | int | nStatus, |
std::span< const std::byte > | reply | ||
) |
Closure sent to main thread to request a reply to be sent to a HTTP request.
Replies must be sent in the main loop in the main http thread, this cannot be done from worker threads.
Definition at line 648 of file httpserver.cpp.
|
inline |
Write HTTP reply.
nStatus is the HTTP status code to send. reply is the body of the reply. Keep it empty to send a standard message.
Definition at line 132 of file httpserver.h.
|
private |
Definition at line 65 of file httpserver.h.
|
private |
Definition at line 66 of file httpserver.h.
|
private |
Definition at line 64 of file httpserver.h.