Bitcoin Core  27.99.0
P2P Digital Currency
Public Types | Public Member Functions | Private Attributes | List of all members
HTTPRequest Class Reference

In-flight HTTP request. More...

#include <httpserver.h>

Collaboration diagram for HTTPRequest:
[legend]

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, const std::string &strReply="")
 Write HTTP reply. More...
 

Private Attributes

struct evhttp_request * req
 
const util::SignalInterruptm_interrupt
 
bool replySent
 

Detailed Description

In-flight HTTP request.

Thin C++ wrapper around evhttp_request.

Definition at line 60 of file httpserver.h.

Member Enumeration Documentation

◆ RequestMethod

Enumerator
UNKNOWN 
GET 
POST 
HEAD 
PUT 

Definition at line 71 of file httpserver.h.

Constructor & Destructor Documentation

◆ HTTPRequest()

HTTPRequest::HTTPRequest ( struct evhttp_request *  req,
const util::SignalInterrupt interrupt,
bool  replySent = false 
)
explicit

Definition at line 581 of file httpserver.cpp.

◆ ~HTTPRequest()

HTTPRequest::~HTTPRequest ( )

Definition at line 586 of file httpserver.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ GetHeader()

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 596 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetPeer()

CService HTTPRequest::GetPeer ( ) const

Get CService (address:ip) for the origin of the http request.

Definition at line 669 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetQueryParameter()

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.

Parameters
[in]keyrepresents the query parameter of which the value is returned

Definition at line 710 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetRequestMethod()

HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod ( ) const

Get request method.

Definition at line 694 of file httpserver.cpp.

Here is the caller graph for this function:

◆ GetURI()

std::string HTTPRequest::GetURI ( ) const

Get requested URI.

Definition at line 689 of file httpserver.cpp.

Here is the caller graph for this function:

◆ ReadBody()

std::string HTTPRequest::ReadBody ( )

Read request body.

Note
As this consumes the underlying buffer, call this only once. Repeated calls will return an empty string.

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 607 of file httpserver.cpp.

Here is the caller graph for this function:

◆ WriteHeader()

void HTTPRequest::WriteHeader ( const std::string &  hdr,
const std::string &  value 
)

Write output header.

Note
call this before calling WriteErrorReply or Reply.

Definition at line 627 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WriteReply()

void HTTPRequest::WriteReply ( int  nStatus,
const std::string &  strReply = "" 
)

Write HTTP reply.

Closure sent to main thread to request a reply to be sent to a HTTP request.

nStatus is the HTTP status code to send. strReply is the body of the reply. Keep it empty to send a standard message.

Note
Can be called only once. As this will give the request back to the main thread, do not call any other HTTPRequest methods after calling this.

Replies must be sent in the main loop in the main http thread, this cannot be done from worker threads.

Definition at line 639 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_interrupt

const util::SignalInterrupt& HTTPRequest::m_interrupt
private

Definition at line 64 of file httpserver.h.

◆ replySent

bool HTTPRequest::replySent
private

Definition at line 65 of file httpserver.h.

◆ req

struct evhttp_request* HTTPRequest::req
private

Definition at line 63 of file httpserver.h.


The documentation for this class was generated from the following files: