dodo  0.0.1
A C++ library to create containerized Linux services
dodo::network::protocol::http::HTTPRequest Class Reference

HTTPRequest class represents a HTTP request. More...

#include <httprequest.hpp>

Inheritance diagram for dodo::network::protocol::http::HTTPRequest:
Collaboration diagram for dodo::network::protocol::http::HTTPRequest:

Data Structures

class  HTTPRequestLine
 HTTPRequestLine class. More...
 

Public Types

enum  Method {
  meGET, meHEAD, mePOST, mePUT,
  mePATCH, meDELETE, meCONNECT, meOPTIONS,
  meTRACE, meINVALID
}
 The HTTP request method. More...
 
- Public Types inherited from dodo::network::protocol::http::HTTPFragment
enum  ParseError : unsigned int {
  peOk = 0, peIncomplete, peSystemError, peExpectCRLF,
  peUnFinishedToken, peExpectingHeaderColon, peInvalidHeaderFieldValue, peInvalidHeaderListEnd,
  peInvalidMethod, peInvalidHTTPVersion, peInvalidRequestLine, peInvalidContentLength,
  peUnexpectedBody, peInvalidTransferEncoding, peInvalidChunkHex, peInvalidLastChunk,
  peExpectingUnsignedInt
}
 Parse results returned by the parse functions. More...
 

Public Member Functions

virtual std::string asString () const
 Return the HTTPRequest as a string. More...
 
HTTPRequestLinegetRequestLine ()
 Get the HTTPRequestLine. More...
 
virtual ParseResult parse (VirtualReadBuffer &data)
 Read the HTTPRequest from the socket. More...
 
virtual ParseResult parseBody (VirtualReadBuffer &data)
 Parse a body and resturn it as a single string. More...
 
virtual common::SystemError send (BaseSocket *socket)
 Send this HTTPMessage to the socket. More...
 
void setRequestLine (const HTTPRequestLine &req)
 Set the HTTPRequestLine. More...
 
common::SystemError write (BaseSocket *socket) const
 Write the HTTPRequest to the socket. More...
 
- Public Member Functions inherited from dodo::network::protocol::http::HTTPMessage
 HTTPMessage ()
 Default constructor. More...
 
void addHeader (const std::string &key, const std::string &value)
 Add a header to the HTTPMessage. More...
 
void clearHeaders ()
 Clear all headers. More...
 
const common::BytesgetBody () const
 Return the HTTTMessage body. More...
 
const std::map< std::string, std::string > & getHeaders () const
 Return a reference to the headers map. More...
 
bool getHeaderValue (const std::string &key, std::string &value) const
 Get a header value as a string into value. More...
 
bool getHeaderValue (const std::string &key, unsigned long &value) const
 Get a header value as an unsigned long into value. More...
 
bool hasHeader (const std::string &header) const
 Return true when this header key exists in this HTTPMessage's headers. More...
 
void replaceHeader (const std::string &key, const std::string &value)
 Replace the value of a header. More...
 
void setBody (const std::string &body)
 Set the body. More...
 

Static Public Member Functions

static bool methodAllowsBody (Method method)
 Return true if the Method allows a body in the message. More...
 
static std::string methodAsString (Method method)
 Translate a Method enum to a human readable string. More...
 
static Method methodFromString (const std::string &s)
 Translate an uppercase string ('GET', "POST', ..) into a Method enum. More...
 
- Static Public Member Functions inherited from dodo::network::protocol::http::HTTPFragment
static std::string getParseResultAsString (ParseError error)
 Return the string description of a ParseError. More...
 

Protected Attributes

HTTPRequestLine request_line_
 The HTTPRequestLine of the HTTPRequest. More...
 
- Protected Attributes inherited from dodo::network::protocol::http::HTTPMessage
common::Bytes body_
 The message body (if any). More...
 
std::map< std::string, std::string > headers_
 The message headers. More...
 

Additional Inherited Members

- Static Public Attributes inherited from dodo::network::protocol::http::HTTPMessage
static const char charCR = char(13)
 CR character. More...
 
static const char charHT = char(9)
 Horizontal tab character. More...
 
static const char charLF = char(10)
 LF character. More...
 
static const char charSP = char(32)
 Space character. More...
 
- Protected Member Functions inherited from dodo::network::protocol::http::HTTPMessage
ParseResult eatCRLF (VirtualReadBuffer &data)
 Consume a CR LF sequence only when it is there. More...
 
ParseResult parseChunkedBody (VirtualReadBuffer &data)
 Parse a chunked body (transfer-encoding: chunked) and resturn it as a single string. More...
 
ParseResult parseChunkHex (VirtualReadBuffer &data, unsigned long &value)
 Parse a hexadecimal chunk size (a hex value followed by CR LF). More...
 
ParseResult parseCRLF (VirtualReadBuffer &data)
 Consume a CR LF sequence. More...
 
ParseResult parseFieldValue (VirtualReadBuffer &data, std::string &value)
 Parse a header field value. More...
 
ParseResult parseHeaders (VirtualReadBuffer &data)
 Parse a header section and update headers_. More...
 
ParseResult parseToken (VirtualReadBuffer &buffer, std::string &token)
 Parse a token (such as a header field name). More...
 
- Static Protected Member Functions inherited from dodo::network::protocol::http::HTTPMessage
static common::SystemError eatSpace (VirtualReadBuffer &data)
 Call buffer.next() as long as buffer.get() is whitespace ( charSP or charHT). More...
 
static bool isCTL (char c)
 Check is the char is a CTL character. More...
 
static bool isSeparator (char c)
 Return true if the char is a separator. More...
 
static bool isSP (char c)
 Return true if the char is whitespace character (charSP or charHT) More...
 
static ParseResult parseUInt (VirtualReadBuffer &data, unsigned int &value)
 Parse an unsigned integer. More...
 

Detailed Description

HTTPRequest class represents a HTTP request.

A HTTPRequest contains

#include <iostream>
#include <dodo.hpp>
using namespace dodo::network;
using namespace dodo::common;
const std::string host = "httpbin.org";
struct RTStats {
double dns_time = 0.0;
double handshake_time = 0.0;
double send_time = 0.0;
double read_time = 0.0;
size_t body_bytes = 0;
size_t underflows = 0;
};
// argv[1] = host
// argv[2] = method
// argv[3] = uri
int main( int argc, char* argv[] ) {
if ( argc < 4 ) return 1;
int error = 0;
RTStats rtstats;
std::string host = argv[1];
std::string method = argv[2];
std::string uri = argv[3];
try {
SocketParams sockparams( SocketParams::AddressFamily::afUNSPEC, SocketParams::stSTREAM, SocketParams::pnTCP );
Address address;
std::string canonicalname = "";
sw.start();
SystemError syserr = Address::getHostAddrInfo( host, sockparams, address, canonicalname );
rtstats.dns_time = sw.getElapsedSeconds();
if ( syserr.ok() ) {
std::cout << host << " = " << address.asString() << std::endl;
tlscontext.setOptions( SSL_OP_ALL );
TLSSocket sock( true, sockparams, tlscontext, san );
sock.setReceiveBufSize( 8192*100 );
address.setPort( 443 );
syserr = sock.connect( address );
rtstats.handshake_time = sw.getElapsedSeconds();
if( syserr.ok() ) {
req.addHeader( "host", host );
std::cout << "----------------------------------- BEGIN REQ -----------------------------------" << std::endl;
std::cout << req.asString() << std::endl;
std::cout << "----------------------------------- END REQ -----------------------------------" << std::endl;
//syserr = sock.send( req.asString().c_str(), req.asString().length() );
syserr = req.send( &sock );
rtstats.send_time = sw.getElapsedSeconds();
if ( syserr.ok() ) {
SocketReadBuffer rbuf( &sock );
syserr = rbuf.underflow();
if ( syserr.ok() ) {
HTTPFragment::ParseResult parse_result = resp.parse( rbuf );
rtstats.read_time = sw.getElapsedSeconds();
if ( parse_result.ok() ) {
rtstats.underflows = rbuf.getUnderflowCount();
std::cout << "----------------------------------- BEGIN RSP -----------------------------------" << std::endl;
std::cout << resp.asString() << std::endl;
rtstats.body_bytes = resp.asString().length();
std::cout << "----------------------------------- END RSP -----------------------------------" << std::endl;
} else {
std::cerr << "invalid HTTP response: " << parse_result.asString() << std::endl;
error = 1;
}
} else {
std::cerr << "socket read error: " << syserr.asString() << std::endl;
error = 1;
}
rtstats.read_time = sw.getElapsedSeconds();
} else {
std::cerr << "send failed: " << syserr.asString() << std::endl;
error = 1;
}
} else {
std::cerr << "connect failed: " << syserr.asString() << std::endl;
error = 1;
}
} else {
std::cerr << "resolving of " << host << " failed : " << syserr.asString() << std::endl;
}
}
catch ( const std::exception &e ) {
std::cerr << e.what() << std::endl;
error = 1;
}
std::cout << "DNS " << std::fixed << std::setprecision(3) << rtstats.dns_time*1000 << "ms" << std::endl;
std::cout << "HANDSHAKE " << std::fixed << std::setprecision(3) << rtstats.handshake_time*1000 << "ms" << std::endl;
std::cout << "SEND " << std::fixed << std::setprecision(3) << rtstats.send_time*1000 << "ms" << std::endl;
std::cout << "RECEIVE " << std::fixed << std::setprecision(3) << rtstats.read_time*1000 << "ms" << std::endl;
std::cout << "RSP body size " << std::fixed << std::setprecision(0) << rtstats.body_bytes << "B" << std::endl;
std::cout << "receive underflows " << std::fixed << std::setprecision(0) << rtstats.underflows << std::endl;
if ( rtstats.underflows ) std::cout << "receive bytes / roundtrip " << std::fixed << std::setprecision(0) << rtstats.body_bytes/rtstats.underflows << std::endl;
return error;
}

Definition at line 42 of file httprequest.hpp.

Member Enumeration Documentation

◆ Method

The HTTP request method.

Enumerator
meINVALID 

The parser was presented an invalid method.

Definition at line 48 of file httprequest.hpp.

Member Function Documentation

◆ asString()

◆ getRequestLine()

HTTPRequestLine& dodo::network::protocol::http::HTTPRequest::getRequestLine ( )
inline

Get the HTTPRequestLine.

Returns
The HTTPRequestLine.

Definition at line 170 of file httprequest.hpp.

References request_line_.

◆ methodAllowsBody()

bool dodo::network::protocol::http::HTTPRequest::methodAllowsBody ( Method  method)
static

Return true if the Method allows a body in the message.

Parameters
methodthe Method to check.
Returns
if the Method allows a body.

Definition at line 60 of file httprequest.cpp.

Referenced by asString(), and send().

Here is the caller graph for this function:

◆ methodAsString()

std::string dodo::network::protocol::http::HTTPRequest::methodAsString ( Method  method)
static

Translate a Method enum to a human readable string.

Parameters
methodThe method to translate.
Returns
the string.

Definition at line 32 of file httprequest.cpp.

Referenced by dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::asString().

Here is the caller graph for this function:

◆ methodFromString()

HTTPRequest::Method dodo::network::protocol::http::HTTPRequest::methodFromString ( const std::string &  s)
static

Translate an uppercase string ('GET', "POST', ..) into a Method enum.

Parameters
sThe uppercase string to translate.
Returns
the Method (and meINVALID if the string is not a Method).

Definition at line 47 of file httprequest.cpp.

References meINVALID.

Referenced by dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::parse().

Here is the caller graph for this function:

◆ parse()

◆ parseBody()

◆ send()

◆ setRequestLine()

void dodo::network::protocol::http::HTTPRequest::setRequestLine ( const HTTPRequestLine req)

Set the HTTPRequestLine.

Parameters
reqThe HTTPRequestLine to set.

◆ write()

common::SystemError dodo::network::protocol::http::HTTPRequest::write ( BaseSocket socket) const

Write the HTTPRequest to the socket.

Parameters
socketThe BaseSocket to write to.
Returns
The SystemError.

Definition at line 249 of file httprequest.cpp.

References asString(), and dodo::network::BaseSocket::send().

Here is the call graph for this function:

Field Documentation

◆ request_line_

HTTPRequestLine dodo::network::protocol::http::HTTPRequest::request_line_
protected

The HTTPRequestLine of the HTTPRequest.

Definition at line 208 of file httprequest.hpp.

Referenced by asString(), getRequestLine(), parse(), and send().


The documentation for this class was generated from the following files:
dodo::network::protocol::http::HTTPMessage::addHeader
void addHeader(const std::string &key, const std::string &value)
Add a header to the HTTPMessage.
Definition: httpmessage.cpp:37
dodo::network::X509Common::SAN
Subject AltName record.
Definition: x509cert.hpp:70
dodo::network::TLSContext
TLS security context.
Definition: tlscontext.hpp:50
dodo::network::SocketReadBuffer
SocketReadBuffer is a VirtualReadBuffer that reads from the BaseSocket in chunks internally.
Definition: socketreadbuffer.hpp:99
dodo::network::protocol::http::HTTPRequest
HTTPRequest class represents a HTTP request.
Definition: httprequest.hpp:42
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::setMethod
void setMethod(HTTPRequest::Method method)
Set the HTTPRequest::Method.
Definition: httprequest.hpp:101
dodo::network::Address::asString
std::string asString(bool withport=false) const
Return a string representation of this Address.
Definition: address.cpp:107
dodo::network::protocol::http::HTTPFragment::ParseResult::ok
bool ok() const
Test if parseError == peOk && systemError == common::SystemError::ecOK.
Definition: httpfragment.hpp:93
dodo::network::protocol::http::HTTPRequest::methodFromString
static Method methodFromString(const std::string &s)
Translate an uppercase string ('GET', "POST', ..) into a Method enum.
Definition: httprequest.cpp:47
dodo::network::protocol::http::HTTPVersion
HHTP version comprising a major and minor number, convertable from and to string as in HTTP requests.
Definition: httpversion.hpp:38
dodo::network::Address
Generic network Address, supporting ipv4 and ipv6 transparently.
Definition: address.hpp:90
dodo::network::X509Common::SANType::stDNS
@ stDNS
A DNS name such as myhost.mydomain.org.
dodo::common::SystemError::ok
bool ok() const
Returns true when this->errorcode_ == ecOK.
Definition: systemerror.hpp:254
dodo::network::Address::getHostAddrInfo
static common::SystemError getHostAddrInfo(const std::string &hostname, AddrInfo &info)
Get Address information on a host in a AddrInfo struct.
Definition: address.cpp:144
dodo::common::SystemError::asString
std::string asString() const
Get the system error string.
Definition: systemerror.hpp:322
dodo::network::TLSContext::TLSVersion::tls1_1
@ tls1_1
TLS 1.1 disables SSLv2, SSLv3 and TLS 1.0.
dodo::network::protocol::http::HTTPRequest::send
virtual common::SystemError send(BaseSocket *socket)
Send this HTTPMessage to the socket.
Definition: httprequest.cpp:99
dodo::network::protocol::http::HTTPResponse::parse
virtual ParseResult parse(VirtualReadBuffer &buffer)
Read a complete HTTPFragment from a VirtualReadBuffer.
Definition: httpresponse.cpp:32
dodo::common::StopWatch::start
void start()
Start the stopwatch.
Definition: util.hpp:69
dodo::network::protocol::http
The HTTP 1.1 protocol.
Definition: network.hpp:76
dodo::network::Address::setPort
void setPort(uint16_t port)
Set the port number.
Definition: address.cpp:136
dodo::network::TLSSocket
Socket for TLS encrypted traffic between trusted endpoints.
Definition: tlssocket.hpp:40
dodo::network::protocol::http::HTTPResponse
HTTPResponse class.
Definition: httpresponse.hpp:40
dodo.hpp
Includes all dodo headers.
dodo::network::SocketParams
Socket parameters - the family (domain), socket type and protocol triplet.
Definition: socketparams.hpp:35
dodo::common::StopWatch
StopWatch timing class.
Definition: util.hpp:54
dodo::network::protocol::http::HTTPRequest::getRequestLine
HTTPRequestLine & getRequestLine()
Get the HTTPRequestLine.
Definition: httprequest.hpp:170
dodo::network::protocol::http::HTTPFragment::ParseResult
Used to convey parsing succces.
Definition: httpfragment.hpp:75
dodo::network::TLSContext::PeerVerification::pvVerifyFQDN
@ pvVerifyFQDN
As pvVerifyPeer, but the remote DNS name must match either the peer cert commonname or match one of t...
dodo::network::protocol::http::HTTPResponse::asString
virtual std::string asString() const
Return the HTTPResponse as a string.
Definition: httpresponse.cpp:71
dodo::network::protocol::http::HTTPFragment::ParseResult::asString
std::string asString() const
Return the ParseResult as a human readable string.
Definition: httpfragment.hpp:122
dodo::network
Interface for network communication.
Definition: address.hpp:37
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::setHTTPVersion
void setHTTPVersion(const HTTPVersion &version)
Set the HTTP version.
Definition: httprequest.hpp:125
dodo::common::StopWatch::getElapsedSeconds
double getElapsedSeconds() const
Return the number of seconds between.
Definition: util.hpp:93
dodo::initLibrary
void initLibrary()
Initialize the dodo library.
Definition: dodo.hpp:41
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
dodo::closeLibrary
void closeLibrary()
Close the dodo library.
Definition: dodo.hpp:51
dodo::network::protocol::http::HTTPRequest::HTTPRequestLine::setRequestURI
void setRequestURI(const std::string &uri)
Set the request uri.
Definition: httprequest.hpp:113
dodo::network::protocol::http::HTTPRequest::asString
virtual std::string asString() const
Return the HTTPRequest as a string.
Definition: httprequest.cpp:86