httpxx 0.1
Streaming HTTP Parser for C++

http::Request Class Reference

Parser for HTTP requests. More...

#include <Request.hpp>

Inheritance diagram for http::Request:
http::Message

List of all members.

Public Member Functions

 Request ()
 Build a fresh, independant HTTP request parser.
void clear ()
 Empty all request content, but keep allocated buffers.
const Method method () const
 Obtain the HTTP method used by the client for the request.
bool upgrade () const
 Check if the client requested a connection upgrade.
const std::string & url () const
 Get the request URL.
const std::string & path () const
 Get the request path (subset of the URL).
const std::string & fragment () const
 Get the request fragment (subset of the URL).
std::size_t feed (const void *data,::size_t size)
 Feed data to the parser.
std::size_t feed (const char *data,::size_t size)
 Feed data to the parser.
bool complete () const
 Check if the request has been completely parsed.
bool headerscomplete () const
 Check if the request headers have been completely parsed.
int majorversion () const
 Obtain the HTTP version used by the remote peer.
int minorversion () const
 Obtain the HTTP revision used by the remote peer.
const Flags flags () const
 Obtain parser flags.
bool hasheader (const std::string &field) const
 Check if the request included a header named field.
std::string header (const std::string &field) const
 Obtain the value of the header named field.
const std::string & body () const
 Obtain the entire request body.

Protected Attributes

::http_parser_settings mySettings
::http_parser myParser
std::string myCurrentField
std::string myCurrentValue
std::map< std::string,
std::string > 
myHeaders
std::string myBody
bool myComplete
bool myHeadersComplete

Detailed Description

Parser for HTTP requests.

Request objects are typically used on the "server" side of the HTTP connection. After you've accepted a connection from a client, feed incoming data to an object of this class.

Note:
This class is a request parser. It cannot be used to format outgoing requests.

For high performance applications, you'll probably need to watch out for memory usage. Request objects may be used to parse additional HTTP requests by using the clear() method. This ensures that allocate buffers are re-used instead of dumped. Reusing request objects will likely leed to faster parsing and reduced memory fragmentation in long running processes.

  http::Request request;
  // parse request.
  while (!request.complete()) {
    const int transferred = ::recv(socket, data, size, 0);
    if (transferred == 0) {
      break;
    }
    request.feed(data, transferred);
  }
  if (!request.complete()) {
    // ...
  }
  // process request.
  const std::string& host = request.header("Host");
  if (host == "www.example.com") {
    // ...
  }
  // signal end of transfer.
  if (!(request.flags()&Flags::keepalive())) {
    ::shutdown(socket, SD_BOTH);
  }
  // prepare to process another request.
  request.clear();

Definition at line 61 of file Request.hpp.


Member Function Documentation

const std::string & http::Message::body ( ) const [inherited]

Obtain the entire request body.

Warning:
This value is undefined until complete() returns true.

Definition at line 155 of file Message.cpp.

std::size_t http::Message::feed ( const char *  data,
::size_t  size 
) [inherited]

Feed data to the parser.

Parameters:
dataAddress of first byte to read.
sizeNumber of bytes to read, starting at data.
Returns:
Number of bytes processed.
See also:
complete()

Definition at line 110 of file Message.cpp.

std::size_t http::Message::feed ( const void *  data,
::size_t  size 
) [inherited]

Feed data to the parser.

Parameters:
dataAddress of first byte to read.
sizeNumber of bytes to read, starting at data.
Returns:
Number of bytes processed.
See also:
complete()

Definition at line 105 of file Message.cpp.

const Flags http::Message::flags ( ) const [inherited]

Obtain parser flags.

Returns:
Flags documenting the parsed request/response object.

Definition at line 135 of file Message.cpp.

const std::string& http::Request::fragment ( ) const

Get the request fragment (subset of the URL).

Warning:
This value is unspecified until headerscomplete() returns true.
bool http::Message::hasheader ( const std::string &  field) const [inherited]

Check if the request included a header named field.

Parameters:
fieldName of header to check.
Warning:
This value is unspecified until headerscomplete() returns true.

Definition at line 140 of file Message.cpp.

std::string http::Message::header ( const std::string &  field) const [inherited]

Obtain the value of the header named field.

Parameters:
fieldName of header to check.
Returns:
An empty string if the header was not present, the header value otherwise.
Warning:
This value is unspecified until headerscomplete() returns true.

Definition at line 146 of file Message.cpp.

int http::Message::majorversion ( ) const [inherited]

Obtain the HTTP version used by the remote peer.

Returns:
A numeric error code (should be 1 for now).
const Method http::Request::method ( ) const

Obtain the HTTP method used by the client for the request.

Returns:
One of a few enumeration values.
Note:
There are a variety of methods other than GET and POST. Make sure to validate that this corresponds to one of the HTTP methods you plan to support.
int http::Message::minorversion ( ) const [inherited]

Obtain the HTTP revision used by the remote peer.

Returns:
A numeric error code (should be 0 or 1 for now).

Definition at line 130 of file Message.cpp.

const std::string& http::Request::path ( ) const

Get the request path (subset of the URL).

Warning:
This value is unspecified until headerscomplete() returns true.
bool http::Request::upgrade ( ) const

Check if the client requested a connection upgrade.

Warning:
This value is unspecified until headerscomplete() returns true.
const std::string& http::Request::url ( ) const

Get the request URL.

Warning:
This value is unspecified until headerscomplete() returns true.

The documentation for this class was generated from the following file:
 All Classes Functions