$darkmode
Kourier 1.0.0
Kourier::HttpRequest Class Reference

The HttpRequest class represents an HTTP request. More...

Public Types

enum class  BodyType {
  NoBody = 0 ,
  NotChunked ,
  Chunked
}
 This enum describes the request's body type. More...
 
enum class  Method {
  GET = 0 ,
  PUT ,
  POST ,
  PATCH ,
  DELETE ,
  HEAD ,
  OPTIONS
}
 This enum describes the HTTP request method. More...
 

Public Member Functions

 ~HttpRequest ()
 
std::string_view body () const
 
BodyType bodyType () const
 
bool chunked () const
 
bool hasBody () const
 
bool hasHeader (std::string_view name) const
 
std::string_view header (std::string_view name, int pos=1) const
 
size_t headerCount (std::string_view name) const
 
size_t headersCount () const
 
bool isComplete () const
 
Method method () const
 
std::string_view peerAddress () const
 
uint16_t peerPort () const
 
size_t pendingBodySize () const
 
size_t requestBodySize () const
 
std::string_view targetPath () const
 
std::string_view targetQuery () const
 

Detailed Description

The HttpRequest class represents an HTTP request.

HttpRequest cannot be created by you. It is created by HttpServer and passed as an argument to the mapped handler. You can call HttpServer::addRoute to map handlers to HTTP methods and paths.

HttpServer calls the mapped handler right after it parses the request header block. If the request has a body that is not chunked, HttpServer processes all body data that were available when the header block was fully parsed. You can call isComplete() to know if HttpRequest represents a complete request.

HttpRequest is only valid inside the called handler. You cannot call its methods outside of the handler function. You can use the HttpBroker instance HttpServer passes as an argument to the handler to receive the pending body data.

Member Enumeration Documentation

◆ BodyType

enum class Kourier::HttpRequest::BodyType
strong

This enum describes the request's body type.

Enumerator
NoBody 

The request does not have a body.

NotChunked 

The request contains a body of a known size that is given right after the header block and the body is not chunked.

Chunked 

The request contains a body of an unknown size, which is given in chunks.

◆ Method

enum class Kourier::HttpRequest::Method
strong

This enum describes the HTTP request method.

Enumerator
GET 

HTTP GET request.

PUT 

HTTP PUT request.

POST 

HTTP POST request.

PATCH 

HTTP PATCH request.

DELETE 

HTTP DELETE request.

HEAD 

HTTP HEAD request.

OPTIONS 

HTTP OPTIONS request.

Constructor & Destructor Documentation

◆ ~HttpRequest()

Kourier::HttpRequest::~HttpRequest ( )

Destroys the object.

Member Function Documentation

◆ body()

std::string_view Kourier::HttpRequest::body ( ) const

This method returns an empty string view if the request has no body or if it has a chunked body. Otherwise, this method returns the available body data when HttpServer parses the headers block. In this case, you can call requestBodySize() to know the size of the request body and pendingBodySize() to know how much body data is still pending to be processed. Both requestBodySize() and pendingBodySize() methods return zero if the body is chunked.

◆ bodyType()

HttpRequest::BodyType Kourier::HttpRequest::bodyType ( ) const

Returns the request body type.

◆ chunked()

bool Kourier::HttpRequest::chunked ( ) const

Returns true if the request has a body given in chunks. If the request has no body or if the body is not given in chunks, this method returns false.

◆ hasBody()

bool Kourier::HttpRequest::hasBody ( ) const

Returns true if the request has a body. You can call chunked() or bodyType() to know whether the request body is chunked. If the request body is not chunked, you can call requestBodySize() to get the size of the body and body() to fetch the body data that was available at the time HttpServer finished parsing the header block and created this instance before passing it to the mapped handler.

◆ hasHeader()

bool Kourier::HttpRequest::hasHeader ( std::string_view  name) const

Returns true if the header block contains at least one field line with the given name.

◆ header()

std::string_view Kourier::HttpRequest::header ( std::string_view  name,
int  pos = 1 
) const

Returns the field line's field value with the given name at position pos in the header block. Position is relative to field lines having the same name.

◆ headerCount()

size_t Kourier::HttpRequest::headerCount ( std::string_view  name) const

Returns the number of field lines with the given name in the header block.

◆ headersCount()

size_t Kourier::HttpRequest::headersCount ( ) const

Returns the number of field lines in the header block.

◆ isComplete()

bool Kourier::HttpRequest::isComplete ( ) const

Returns true if the request represented by this instance is complete. A request can only be complete when HttpServer calls the mapped handler if the request doesn't have a body or if the body is not chunked and all of its data is available when the request header block is parsed.

◆ method()

HttpRequest::Method Kourier::HttpRequest::method ( ) const

Returns the request method.

◆ peerAddress()

std::string_view Kourier::HttpRequest::peerAddress ( ) const

Returns the requester's IP.

◆ peerPort()

uint16_t Kourier::HttpRequest::peerPort ( ) const

Returns the requester's port.

◆ pendingBodySize()

size_t Kourier::HttpRequest::pendingBodySize ( ) const

Returns the number of bytes pending for the request body to be fully received. If the request has no body or if it has a chunked body, this method returns 0.

◆ requestBodySize()

size_t Kourier::HttpRequest::requestBodySize ( ) const

Returns the size of the body in the request. If the request has no body or if it has a chunked body, this method returns 0.

◆ targetPath()

std::string_view Kourier::HttpRequest::targetPath ( ) const

Returns the request path.

◆ targetQuery()

std::string_view Kourier::HttpRequest::targetQuery ( ) const

Returns the request query. Returns an empty string view if the request has no query.