MistServer
2.5.3-Pro-19-gf5e75b1 ( Generic_64)
|
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 |
Simple class for reading and writing HTTP 1.0 and 1.1.
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 | ( | ) |
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.
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.
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.
code | The HTTP response code. Usually you want 200. |
message | The HTTP response message. Usually you want "OK". |
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.
bodypart | The data to send. |
conn | The 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.
data | The data to send. |
size | The size of the data to send. |
conn | The 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.
|
staticprivate |
Helper function for urlescape.
Encodes a character as two hex digits.
|
staticprivate |
|
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.
HTTPbuffer | The data buffer to read from. |
|
private |
|
private |
Parses GET or POST-style variable data.
Saves to internal variable structure using HTTP::Parser::SetVar.
|
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:
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.
conn | The socket to read from. |
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.
strbuf | The buffer to read from. |
void HTTP::Parser::SendRequest | ( | Socket::Connection & | conn | ) |
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.
code | The HTTP response code. Usually you want 200. |
message | The HTTP response message. Usually you want "OK". |
conn | The Socket::Connection to send the response over. |
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.
s | The 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.
buffer | The buffer data to set the body to. |
len | Length 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.
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)
request | The HTTP request to respond to. |
conn | The connection to send over. |
|
private |
|
private |
Trims any whitespace at the front or back of the string.
Used when getting/setting headers.
s | The string to trim. The string itself will be changed, not returned. |
|
staticprivate |
|
staticprivate |
Helper function for urlunescape.
Takes a single char input and outputs its integer hex value.
|
static |
|
static |
URLencodes std::string data.
|
static |
Unescapes URLencoded std::string data.
|
static |
std::string HTTP::Parser::body |
bool HTTP::Parser::bufferChunks |
|
private |
|
private |
|
private |
bool HTTP::Parser::headerOnly |
If true, do not parse body if the length is a known size.
|
private |
unsigned int HTTP::Parser::length |
std::string HTTP::Parser::method |
std::string HTTP::Parser::protocol |
|
private |
|
private |
|
private |
bool HTTP::Parser::sendingChunks |
std::string HTTP::Parser::url |
|
private |