MistServer  2.5.3-Pro-19-gf5e75b1 ( Generic_64)
Public Member Functions | Static Public Member Functions | Data Fields | Private Member Functions | Static Private Member Functions | Private Attributes
HTTP::Parser Class Reference

Simple class for reading and writing HTTP 1.0 and 1.1. More...

#include <http_parser.h>

Public Member Functions

 Parser ()
 This constructor creates an empty HTTP::Parser, ready for use for either reading or writing. More...
 
 Parser ()
 
std::string & BuildRequest ()
 
std::string & BuildRequest ()
 Returns a string containing a valid HTTP 1.0 or 1.1 request, ready for sending. More...
 
std::string & BuildResponse ()
 
std::string & BuildResponse ()
 Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending. More...
 
std::string & BuildResponse (std::string code, std::string message)
 
std::string & BuildResponse (std::string code, std::string message)
 Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending. More...
 
void Chunkify (const std::string &bodypart, Socket::Connection &conn)
 Sends a string in chunked format if protocol is HTTP/1.1, sends as-is otherwise. More...
 
void Chunkify (const std::string &bodypart, Socket::Connection &conn)
 
void Chunkify (const char *data, unsigned int size, Socket::Connection &conn)
 Sends a string in chunked format if protocol is HTTP/1.1, sends as-is otherwise. More...
 
void Chunkify (const char *data, unsigned int size, Socket::Connection &conn)
 
void Clean ()
 Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing usage. More...
 
void Clean ()
 
void CleanPreserveHeaders ()
 Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing usage. More...
 
void CleanPreserveHeaders ()
 
std::string GetHeader (std::string i)
 Returns header i, if set. More...
 
std::string GetHeader (std::string i)
 
std::string getUrl ()
 Returns header i, if set. More...
 
std::string getUrl ()
 
std::string GetVar (std::string i)
 Returns POST variable i, if set. More...
 
std::string GetVar (std::string i)
 
void Proxy (Socket::Connection &from, Socket::Connection &to)
 After receiving a header with this object, and after a call with SendResponse/SendRequest with this object, this function call will: More...
 
void Proxy (Socket::Connection &from, Socket::Connection &to)
 
bool Read (Socket::Connection &conn)
 Attempt to read a whole HTTP request or response from a Socket::Connection. More...
 
bool Read (Socket::Connection &conn)
 
bool Read (std::string &strbuf)
 
bool Read (std::string &strbuf)
 Attempt to read a whole HTTP request or response from a std::string buffer. More...
 
void SendRequest (Socket::Connection &conn)
 Creates and sends a valid HTTP 1.0 or 1.1 request. More...
 
void SendRequest (Socket::Connection &conn)
 
void SendResponse (std::string code, std::string message, Socket::Connection &conn)
 Creates and sends a valid HTTP 1.0 or 1.1 response. More...
 
void SendResponse (std::string code, std::string message, Socket::Connection &conn)
 
void SetBody (std::string s)
 
void SetBody (std::string s)
 Function that sets the body of a response or request, along with the correct Content-Length header. More...
 
void SetBody (const char *buffer, int len)
 
void SetBody (const char *buffer, int len)
 Function that sets the body of a response or request, along with the correct Content-Length header. More...
 
void setCORSHeaders ()
 
void setCORSHeaders ()
 Sets the neccesary headers to allow Cross Origin Resource Sharing with all domains. More...
 
void SetHeader (std::string i, std::string v)
 
void SetHeader (std::string i, std::string v)
 Sets header i to string value v. More...
 
void SetHeader (std::string i, long long v)
 Sets header i to integer value v. More...
 
void SetHeader (std::string i, long long v)
 
void SetVar (std::string i, std::string v)
 
void SetVar (std::string i, std::string v)
 Sets POST variable i to string value v. More...
 
void StartResponse (std::string code, std::string message, Parser &request, Socket::Connection &conn, bool bufferAllChunks=false)
 Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request. More...
 
void StartResponse (std::string code, std::string message, Parser &request, Socket::Connection &conn, bool bufferAllChunks=false)
 
void StartResponse (Parser &request, Socket::Connection &conn, bool bufferAllChunks=false)
 
void StartResponse (Parser &request, Socket::Connection &conn, bool bufferAllChunks=false)
 Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request. More...
 

Static Public Member Functions

static std::string urlencode (const std::string &in)
 URLencodes std::string data. More...
 
static std::string urlencode (const std::string &in)
 
static std::string urlunescape (const std::string &in)
 
static std::string urlunescape (const std::string &in)
 Unescapes URLencoded std::string data. More...
 

Data Fields

std::string body
 
bool bufferChunks
 
bool headerOnly
 If true, do not parse body if the length is a known size. More...
 
unsigned int length
 
std::string method
 
std::string protocol
 
bool sendingChunks
 
std::string url
 

Private Member Functions

bool parse (std::string &HTTPbuffer)
 Attempt to read a whole HTTP response or request from a data buffer. More...
 
bool parse (std::string &HTTPbuffer)
 
void parseVars (std::string data)
 
void parseVars (std::string data)
 Parses GET or POST-style variable data. More...
 
void Trim (std::string &s)
 Trims any whitespace at the front or back of the string. More...
 
void Trim (std::string &s)
 

Static Private Member Functions

static std::string hex (char dec)
 Helper function for urlescape. More...
 
static std::string hex (char dec)
 
static int unhex (char c)
 
static int unhex (char c)
 Helper function for urlunescape. More...
 

Private Attributes

std::string builder
 
unsigned int doingChunk
 
bool getChunks
 
std::map< std::string, std::string > headers
 
std::string read_buffer
 
bool seenHeaders
 
bool seenReq
 
std::map< std::string, std::string > vars
 

Detailed Description

Simple class for reading and writing HTTP 1.0 and 1.1.

Constructor & Destructor Documentation

HTTP::Parser::Parser ( )

This constructor creates an empty HTTP::Parser, ready for use for either reading or writing.

All this constructor does is call HTTP::Parser::Clean().

HTTP::Parser::Parser ( )

Member Function Documentation

std::string & HTTP::Parser::BuildRequest ( )

Returns a string containing a valid HTTP 1.0 or 1.1 request, ready for sending.

The request is build from internal variables set before this call is made. To be precise, method, url, protocol, headers and body are used.

Returns
A string containing a valid HTTP 1.0 or 1.1 request, ready for sending.
Todo:
Include GET/POST variable parsing?
std::string& HTTP::Parser::BuildRequest ( )
std::string& HTTP::Parser::BuildResponse ( )
std::string & HTTP::Parser::BuildResponse ( )

Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending.

The response is partly build from internal variables set before this call is made. To be precise, protocol, headers and body are used.

Returns
A string containing a valid HTTP 1.0 or 1.1 response, ready for sending. This function calls this->BuildResponse(this->method,this->url)
std::string& HTTP::Parser::BuildResponse ( std::string  code,
std::string  message 
)
std::string & HTTP::Parser::BuildResponse ( std::string  code,
std::string  message 
)

Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending.

The response is partly build from internal variables set before this call is made. To be precise, protocol, headers and body are used.

Parameters
codeThe HTTP response code. Usually you want 200.
messageThe HTTP response message. Usually you want "OK".
Returns
A string containing a valid HTTP 1.0 or 1.1 response, ready for sending.
Todo:
Include GET/POST variable parsing?
void HTTP::Parser::Chunkify ( const std::string &  bodypart,
Socket::Connection conn 
)

Sends a string in chunked format if protocol is HTTP/1.1, sends as-is otherwise.

Parameters
bodypartThe data to send.
connThe connection to use for sending.
void HTTP::Parser::Chunkify ( const std::string &  bodypart,
Socket::Connection conn 
)
void HTTP::Parser::Chunkify ( const char *  data,
unsigned int  size,
Socket::Connection conn 
)

Sends a string in chunked format if protocol is HTTP/1.1, sends as-is otherwise.

Parameters
dataThe data to send.
sizeThe size of the data to send.
connThe connection to use for sending.
void HTTP::Parser::Chunkify ( const char *  data,
unsigned int  size,
Socket::Connection conn 
)
void HTTP::Parser::Clean ( )

Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing usage.

void HTTP::Parser::Clean ( )
void HTTP::Parser::CleanPreserveHeaders ( )

Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing usage.

void HTTP::Parser::CleanPreserveHeaders ( )
std::string HTTP::Parser::GetHeader ( std::string  i)
std::string HTTP::Parser::GetHeader ( std::string  i)

Returns header i, if set.

std::string HTTP::Parser::getUrl ( )

Returns header i, if set.

std::string HTTP::Parser::getUrl ( )
std::string HTTP::Parser::GetVar ( std::string  i)
std::string HTTP::Parser::GetVar ( std::string  i)

Returns POST variable i, if set.

std::string HTTP::Parser::hex ( char  dec)
staticprivate

Helper function for urlescape.

Encodes a character as two hex digits.

static std::string HTTP::Parser::hex ( char  dec)
staticprivate
bool HTTP::Parser::parse ( std::string &  HTTPbuffer)
private

Attempt to read a whole HTTP response or request from a data buffer.

If succesful, fills its own fields with the proper data and removes the response/request from the data buffer.

Parameters
HTTPbufferThe data buffer to read from.
Returns
True on success, false otherwise.
Todo:
Make this not resize HTTPbuffer in parts, but read all at once and then remove the entire request, like doxygen claims it does?
bool HTTP::Parser::parse ( std::string &  HTTPbuffer)
private
void HTTP::Parser::parseVars ( std::string  data)
private

Parses GET or POST-style variable data.

Saves to internal variable structure using HTTP::Parser::SetVar.

void HTTP::Parser::parseVars ( std::string  data)
private
void HTTP::Parser::Proxy ( Socket::Connection from,
Socket::Connection to 
)

After receiving a header with this object, and after a call with SendResponse/SendRequest with this object, this function call will:

  • Retrieve all the body from the 'from' Socket::Connection.
  • Forward those contents as-is to the 'to' Socket::Connection. It blocks until completed or either of the connections reaches an error state.
void HTTP::Parser::Proxy ( Socket::Connection from,
Socket::Connection to 
)
bool HTTP::Parser::Read ( Socket::Connection conn)

Attempt to read a whole HTTP request or response from a Socket::Connection.

If a whole request could be read, it is removed from the front of the socket buffer and true returned. If not, as much as can be interpreted is removed and false returned.

Parameters
connThe socket to read from.
Returns
True if a whole request or response was read, false otherwise.
bool HTTP::Parser::Read ( Socket::Connection conn)
bool HTTP::Parser::Read ( std::string &  strbuf)
bool HTTP::Parser::Read ( std::string &  strbuf)

Attempt to read a whole HTTP request or response from a std::string buffer.

If a whole request could be read, it is removed from the front of the given buffer and true returned. If not, as much as can be interpreted is removed and false returned.

Parameters
strbufThe buffer to read from.
Returns
True if a whole request or response was read, false otherwise.
void HTTP::Parser::SendRequest ( Socket::Connection conn)

Creates and sends a valid HTTP 1.0 or 1.1 request.

The request is build from internal variables set before this call is made. To be precise, method, url, protocol, headers and body are used.

Todo:
Include GET/POST variable parsing?
void HTTP::Parser::SendRequest ( Socket::Connection conn)
void HTTP::Parser::SendResponse ( std::string  code,
std::string  message,
Socket::Connection conn 
)

Creates and sends a valid HTTP 1.0 or 1.1 response.

The response is partly build from internal variables set before this call is made. To be precise, protocol, headers and body are used. This call will attempt to buffer as little as possible and block until the whole request is sent.

Parameters
codeThe HTTP response code. Usually you want 200.
messageThe HTTP response message. Usually you want "OK".
connThe Socket::Connection to send the response over.
Todo:
Include GET/POST variable parsing?
void HTTP::Parser::SendResponse ( std::string  code,
std::string  message,
Socket::Connection conn 
)
void HTTP::Parser::SetBody ( std::string  s)

Function that sets the body of a response or request, along with the correct Content-Length header.

Parameters
sThe string to set the body to.
void HTTP::Parser::SetBody ( std::string  s)
void HTTP::Parser::SetBody ( const char *  buffer,
int  len 
)

Function that sets the body of a response or request, along with the correct Content-Length header.

Parameters
bufferThe buffer data to set the body to.
lenLength of the buffer data.
void HTTP::Parser::SetBody ( const char *  buffer,
int  len 
)
void HTTP::Parser::setCORSHeaders ( )
void HTTP::Parser::setCORSHeaders ( )

Sets the neccesary headers to allow Cross Origin Resource Sharing with all domains.

void HTTP::Parser::SetHeader ( std::string  i,
std::string  v 
)
void HTTP::Parser::SetHeader ( std::string  i,
std::string  v 
)

Sets header i to string value v.

void HTTP::Parser::SetHeader ( std::string  i,
long long  v 
)

Sets header i to integer value v.

void HTTP::Parser::SetHeader ( std::string  i,
long long  v 
)
void HTTP::Parser::SetVar ( std::string  i,
std::string  v 
)

Sets POST variable i to string value v.

void HTTP::Parser::SetVar ( std::string  i,
std::string  v 
)
void HTTP::Parser::StartResponse ( std::string  code,
std::string  message,
Parser request,
Socket::Connection conn,
bool  bufferAllChunks = false 
)
void HTTP::Parser::StartResponse ( std::string  code,
std::string  message,
HTTP::Parser request,
Socket::Connection conn,
bool  bufferAllChunks = false 
)

Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request.

The headers must be set before this call is made. This call sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses a zero-content-length HTTP/1.0 response.

Parameters
codeThe HTTP response code. Usually you want 200.
messageThe HTTP response message. Usually you want "OK".
requestThe HTTP request to respond to.
connThe connection to send over.
void HTTP::Parser::StartResponse ( Parser request,
Socket::Connection conn,
bool  bufferAllChunks = false 
)
void HTTP::Parser::StartResponse ( HTTP::Parser request,
Socket::Connection conn,
bool  bufferAllChunks = false 
)

Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request.

The headers must be set before this call is made. This call sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses a zero-content-length HTTP/1.0 response. This call simply calls StartResponse("200", "OK", request, conn)

Parameters
requestThe HTTP request to respond to.
connThe connection to send over.
void HTTP::Parser::Trim ( std::string &  s)
private
void HTTP::Parser::Trim ( std::string &  s)
private

Trims any whitespace at the front or back of the string.

Used when getting/setting headers.

Parameters
sThe string to trim. The string itself will be changed, not returned.
static int HTTP::Parser::unhex ( char  c)
staticprivate
int HTTP::Parser::unhex ( char  c)
staticprivate

Helper function for urlunescape.

Takes a single char input and outputs its integer hex value.

static std::string HTTP::Parser::urlencode ( const std::string &  in)
static
std::string HTTP::Parser::urlencode ( const std::string &  in)
static

URLencodes std::string data.

std::string HTTP::Parser::urlunescape ( const std::string &  in)
static

Unescapes URLencoded std::string data.

static std::string HTTP::Parser::urlunescape ( const std::string &  in)
static

Field Documentation

std::string HTTP::Parser::body
bool HTTP::Parser::bufferChunks
std::string HTTP::Parser::builder
private
unsigned int HTTP::Parser::doingChunk
private
bool HTTP::Parser::getChunks
private
bool HTTP::Parser::headerOnly

If true, do not parse body if the length is a known size.

std::map< std::string, std::string > HTTP::Parser::headers
private
unsigned int HTTP::Parser::length
std::string HTTP::Parser::method
std::string HTTP::Parser::protocol
std::string HTTP::Parser::read_buffer
private
bool HTTP::Parser::seenHeaders
private
bool HTTP::Parser::seenReq
private
bool HTTP::Parser::sendingChunks
std::string HTTP::Parser::url
std::map< std::string, std::string > HTTP::Parser::vars
private

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