Bitcoin
Public Types | Public Member Functions | Private Attributes | List of all members
HTTPRequest Class Reference

#include <httpserver.h>

Public Types

enum  RequestMethod {
  UNKNOWN, GET, POST, HEAD,
  PUT
}
 

Public Member Functions

 HTTPRequest (struct evhttp_request *req)
 
 ~HTTPRequest ()
 
std::string GetURI () const
 
CService GetPeer () const
 
RequestMethod GetRequestMethod () const
 
std::pair< bool, std::string > GetHeader (const std::string &hdr) const
 
std::string ReadBody ()
 
void WriteHeader (const std::string &hdr, const std::string &value)
 
void WriteReply (int nStatus, const std::string &strReply="")
 

Private Attributes

struct evhttp_request * req
 
bool replySent
 

Detailed Description

In-flight HTTP request. Thin C++ wrapper around evhttp_request.

Member Enumeration Documentation

◆ RequestMethod

Enumerator
UNKNOWN 
GET 
POST 
HEAD 
PUT 

Constructor & Destructor Documentation

◆ HTTPRequest()

HTTPRequest::HTTPRequest ( struct evhttp_request *  req)
explicit

◆ ~HTTPRequest()

HTTPRequest::~HTTPRequest ( )

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).

◆ GetPeer()

CService HTTPRequest::GetPeer ( ) const

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

◆ GetRequestMethod()

HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod ( ) const

Get request method.

◆ GetURI()

std::string HTTPRequest::GetURI ( ) const

Get requested URI.

◆ 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.

◆ WriteHeader()

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

Write output header.

Note
call this before calling WriteErrorReply or Reply.

◆ WriteReply()

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

Write HTTP reply. 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.

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.

Member Data Documentation

◆ replySent

bool HTTPRequest::replySent
private

◆ req

struct evhttp_request* HTTPRequest::req
private

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