httpxx 0.1
Streaming HTTP Parser for C++
|
Parser for HTTP responses. More...
#include <Response.hpp>
Public Member Functions | |
Response () | |
Build a fresh, independant HTTP response parser. | |
int | status () const |
Obtain the HTTP status code returned by the server. | |
void | clear () |
Empty all request content, but keep allocated buffers. | |
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 |
Parser for HTTP responses.
Response
objects are typically used on the "client" side of the HTTP connection. After you've sent an HTTP request to the server, feed incoming data to an object of this class.
For high performance applications, you'll probably need to watch out for memory usage. Response objects may be used to parse additional HTTP responses by using the clear()
method. This ensures that allocate buffers are re-used instead of dumped. Reusing response objects will likely leed to faster parsing and reduced memory fragmentation in long running processes.
http::Response response; // parse response. while (!response.complete()) { const int transferred = ::recv(socket, data, size, 0); if (transferred == 0) { break; } response.feed(data, transferred); } if (!response.complete()) { // ... } // process response. const std::string& contenttype = request.header("Content-Type"); if (contenttype == "text/html") { // ... } // signal end of transfer. if (!(response.flags()&Flags::keepalive())) { ::shutdown(socket, SD_BOTH); } // prepare to process another response. response.clear();
Definition at line 60 of file Response.hpp.
const std::string & http::Message::body | ( | ) | const [inherited] |
Obtain the entire request body.
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.
data | Address of first byte to read. |
size | Number of bytes to read, starting at data. |
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.
data | Address of first byte to read. |
size | Number of bytes to read, starting at data. |
Definition at line 105 of file Message.cpp.
const Flags http::Message::flags | ( | ) | const [inherited] |
Obtain parser flags.
Definition at line 135 of file Message.cpp.
bool http::Message::hasheader | ( | const std::string & | field | ) | const [inherited] |
Check if the request included a header named field.
field | Name of header to check. |
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.
field | Name of header to check. |
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.
int http::Message::minorversion | ( | ) | const [inherited] |
Obtain the HTTP revision used by the remote peer.
Definition at line 130 of file Message.cpp.
int http::Response::status | ( | ) | const |
Obtain the HTTP status code returned by the server.
Definition at line 20 of file Response.cpp.