Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
websocketpp::http::parser::parser Class Reference

Base HTTP parser. More...

#include <parser.hpp>

Inheritance diagram for websocketpp::http::parser::parser:
Collaboration diagram for websocketpp::http::parser::parser:

Public Member Functions

 parser ()
 
std::string const & get_version () const
 Get the HTTP version string.
 
void set_version (std::string const &version)
 Set HTTP parser Version.
 
std::string const & get_header (std::string const &key) const
 Get the value of an HTTP header.
 
bool get_header_as_plist (std::string const &key, parameter_list &out) const
 Extract an HTTP parameter list from a parser header.
 
void append_header (std::string const &key, std::string const &val)
 Append a value to an existing HTTP header.
 
void replace_header (std::string const &key, std::string const &val)
 Set a value for an HTTP header, replacing an existing value.
 
void remove_header (std::string const &key)
 Remove a header from the parser.
 
std::string const & get_body () const
 Get HTTP body.
 
void set_body (std::string const &value)
 Set body content.
 
size_t get_max_body_size () const
 Get body size limit.
 
void set_max_body_size (size_t value)
 Set body size limit.
 
bool parse_parameter_list (std::string const &in, parameter_list &out) const
 Extract an HTTP parameter list from a string.
 

Protected Member Functions

void process_header (std::string::iterator begin, std::string::iterator end)
 Process a header line.
 
bool prepare_body ()
 Prepare the parser to begin parsing body data.
 
size_t process_body (char const *buf, size_t len)
 Process body data.
 
bool body_ready () const
 Check if the parser is done parsing the body.
 
std::string raw_headers () const
 Generate and return the HTTP headers as a string.
 

Protected Attributes

std::string m_version
 
header_list m_headers
 
size_t m_header_bytes
 
std::string m_body
 
size_t m_body_bytes_needed
 
size_t m_body_bytes_max
 
body_encoding::value m_body_encoding
 

Detailed Description

Includes methods and data elements common to all types of HTTP messages such as headers, versions, bodies, etc.

Definition at line 398 of file parser.hpp.

Constructor & Destructor Documentation

◆ parser()

websocketpp::http::parser::parser::parser ( )
inline

Definition at line 400 of file parser.hpp.

Member Function Documentation

◆ append_header()

void websocketpp::http::parser::parser::append_header ( std::string const & key,
std::string const & val )
inline

This method will set the value of the HTTP header key with the indicated value. If a header with the name key already exists, val will be appended to the existing value.

Todo

Make this method case insensitive.

Should there be any restrictions on which keys are allowed?

Exception free varient

See also
replace_header
Parameters
[in]keyThe name/key of the header to append to.
[in]valThe value to append.

Definition at line 67 of file parser.hpp.

69{
70 if (std::find_if(key.begin(),key.end(),is_not_token_char) != key.end()) {
71 throw exception("Invalid header name",status_code::bad_request);
72 }
73
74 if (this->get_header(key).empty()) {
75 m_headers[key] = val;
76 } else {
77 m_headers[key] += ", " + val;
78 }
79}
std::string const & get_header(std::string const &key) const
Get the value of an HTTP header.
Definition parser.hpp:45
bool is_not_token_char(unsigned char c)
Is the character a non-token.
uint8_t key[16]
Definition yubico_otp.c:41
Here is the call graph for this function:
Here is the caller graph for this function:

◆ body_ready()

bool websocketpp::http::parser::parser::body_ready ( ) const
inlineprotected

Behavior before a call to prepare_body is undefined.

Since
0.5.0
Returns
True if the message body has been completed loaded.

Definition at line 589 of file parser.hpp.

589 {
590 return (m_body_bytes_needed == 0);
591 }
Here is the caller graph for this function:

◆ get_body()

std::string const & websocketpp::http::parser::parser::get_body ( ) const
inline

Gets the body of the HTTP object

Returns
The body of the HTTP message.

Definition at line 495 of file parser.hpp.

495 {
496 return m_body;
497 }

◆ get_header()

std::string const & websocketpp::http::parser::parser::get_header ( std::string const & key) const
inline
Todo
Make this method case insensitive.
Parameters
[in]keyThe name/key of the header to get.
Returns
The value associated with the given HTTP header key.

Definition at line 45 of file parser.hpp.

45 {
46 header_list::const_iterator h = m_headers.find(key);
47
48 if (h == m_headers.end()) {
49 return empty_header;
50 } else {
51 return h->second;
52 }
53}
Here is the caller graph for this function:

◆ get_header_as_plist()

bool websocketpp::http::parser::parser::get_header_as_plist ( std::string const & key,
parameter_list & out ) const
inline

If the header requested doesn't exist or exists and is empty the parameter list is valid (but empty).

Parameters
[in]keyThe name/key of the HTTP header to use as input.
[out]outThe parameter list to store extracted parameters in.
Returns
Whether or not the input was a valid parameter list.

Definition at line 55 of file parser.hpp.

57{
58 header_list::const_iterator it = m_headers.find(key);
59
60 if (it == m_headers.end() || it->second.size() == 0) {
61 return false;
62 }
63
64 return this->parse_parameter_list(it->second,out);
65}
bool parse_parameter_list(std::string const &in, parameter_list &out) const
Extract an HTTP parameter list from a string.
Definition parser.hpp:107
Here is the call graph for this function:

◆ get_max_body_size()

size_t websocketpp::http::parser::parser::get_max_body_size ( ) const
inline

Retrieves the maximum number of bytes to parse & buffer before canceling a request.

Since
0.5.0
Returns
The maximum length of a message body.

Definition at line 519 of file parser.hpp.

519 {
520 return m_body_bytes_max;
521 }

◆ get_version()

std::string const & websocketpp::http::parser::parser::get_version ( ) const
inline
Returns
The version string for this parser

Definition at line 410 of file parser.hpp.

410 {
411 return m_version;
412 }
Here is the caller graph for this function:

◆ parse_parameter_list()

bool websocketpp::http::parser::parser::parse_parameter_list ( std::string const & in,
parameter_list & out ) const
inline
Parameters
[in]inThe input string.
[out]outThe parameter list to store extracted parameters in.
Returns
Whether or not the input was a valid parameter list.

Definition at line 107 of file parser.hpp.

109{
110 if (in.size() == 0) {
111 return false;
112 }
113
114 std::string::const_iterator it;
115 it = extract_parameters(in.begin(),in.end(),out);
116 return (it == in.begin());
117}
InputIterator extract_parameters(InputIterator begin, InputIterator end, parameter_list &parameters)
Extract HTTP parameters.
Definition parser.hpp:293
Here is the call graph for this function:
Here is the caller graph for this function:

◆ prepare_body()

bool websocketpp::http::parser::parser::prepare_body ( )
inlineprotected

Inspects headers to determine if the message has a body that needs to be read. If so, sets up the necessary state, otherwise returns false. If this method returns true and loading the message body is desired call process_body until it returns zero bytes or an error.

Must not be called until after all headers have been processed.

Since
0.5.0
Returns
True if more bytes are needed to load the body, false otherwise.

Definition at line 119 of file parser.hpp.

119 {
120 if (!get_header("Content-Length").empty()) {
121 std::string const & cl_header = get_header("Content-Length");
122 char * end;
123
124 // TODO: not 100% sure what the compatibility of this method is. Also,
125 // I believe this will only work up to 32bit sizes. Is there a need for
126 // > 4GiB HTTP payloads?
127 m_body_bytes_needed = std::strtoul(cl_header.c_str(),&end,10);
128
130 throw exception("HTTP message body too large",
132 }
133
135 return true;
136 } else if (get_header("Transfer-Encoding") == "chunked") {
137 // TODO
138 //m_body_encoding = body_encoding::chunked;
139 return false;
140 } else {
141 return false;
142 }
143}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_body()

size_t websocketpp::http::parser::parser::process_body ( char const * buf,
size_t len )
inlineprotected

Parses body data.

Since
0.5.0
Parameters
[in]beginAn iterator to the beginning of the sequence.
[in]endAn iterator to the end of the sequence.
Returns
The number of bytes processed

Definition at line 145 of file parser.hpp.

145 {
147 size_t processed = (std::min)(m_body_bytes_needed,len);
148 m_body.append(buf,processed);
149 m_body_bytes_needed -= processed;
150 return processed;
152 // TODO:
153 throw exception("Unexpected body encoding",
155 } else {
156 throw exception("Unexpected body encoding",
158 }
159}
size_t len
uint8_t buf[2048]
Here is the caller graph for this function:

◆ process_header()

void websocketpp::http::parser::parser::process_header ( std::string::iterator begin,
std::string::iterator end )
inlineprotected
Todo
Update this method to be exception free.
Parameters
[in]beginAn iterator to the beginning of the sequence.
[in]endAn iterator to the end of the sequence.

Definition at line 161 of file parser.hpp.

163{
164 std::string::iterator cursor = std::search(
165 begin,
166 end,
167 header_separator,
168 header_separator + sizeof(header_separator) - 1
169 );
170
171 if (cursor == end) {
172 throw exception("Invalid header line",status_code::bad_request);
173 }
174
175 append_header(strip_lws(std::string(begin,cursor)),
176 strip_lws(std::string(cursor+sizeof(header_separator)-1,end)));
177}
void append_header(std::string const &key, std::string const &val)
Append a value to an existing HTTP header.
Definition parser.hpp:67
std::string strip_lws(std::string const &input)
Definition parser.hpp:379
Here is the call graph for this function:
Here is the caller graph for this function:

◆ raw_headers()

std::string websocketpp::http::parser::parser::raw_headers ( ) const
inlineprotected

Each headers will be followed by the \r
sequence including the last one. A second \r
sequence (blank header) is not appended by this method

Returns
The HTTP headers as a string.

Definition at line 179 of file parser.hpp.

179 {
180 std::stringstream raw;
181
182 header_list::const_iterator it;
183 for (it = m_headers.begin(); it != m_headers.end(); it++) {
184 raw << it->first << ": " << it->second << "\r\n";
185 }
186
187 return raw.str();
188}
Here is the caller graph for this function:

◆ remove_header()

void websocketpp::http::parser::parser::remove_header ( std::string const & key)
inline

Removes the header entirely from the parser. This is different than setting the value of the header to blank.

Todo
Make this method case insensitive.
Parameters
[in]keyThe name/key of the header to remove.

Definition at line 87 of file parser.hpp.

87 {
88 m_headers.erase(key);
89}
Here is the caller graph for this function:

◆ replace_header()

void websocketpp::http::parser::parser::replace_header ( std::string const & key,
std::string const & val )
inline

This method will set the value of the HTTP header key with the indicated value. If a header with the name key already exists, val will replace the existing value.

Todo

Make this method case insensitive.

Should there be any restrictions on which keys are allowed?

Exception free varient

See also
append_header
Parameters
[in]keyThe name/key of the header to append to.
[in]valThe value to append.

Definition at line 81 of file parser.hpp.

83{
84 m_headers[key] = val;
85}
Here is the caller graph for this function:

◆ set_body()

void websocketpp::http::parser::parser::set_body ( std::string const & value)
inline

Set the body content of the HTTP response to the parameter string. Note set_body will also set the Content-Length HTTP header to the appropriate value. If you want the Content-Length header to be something else, do so via replace_header("Content-Length") after calling set_body()

Parameters
valueString data to include as the body content.

Definition at line 91 of file parser.hpp.

91 {
92 if (value.size() == 0) {
93 remove_header("Content-Length");
94 m_body.clear();
95 return;
96 }
97
98 // TODO: should this method respect the max size? If so how should errors
99 // be indicated?
100
101 std::stringstream len;
102 len << value.size();
103 replace_header("Content-Length", len.str());
104 m_body = value;
105}
void remove_header(std::string const &key)
Remove a header from the parser.
Definition parser.hpp:87
void replace_header(std::string const &key, std::string const &val)
Set a value for an HTTP header, replacing an existing value.
Definition parser.hpp:81
#define value
Definition pkcs11.h:157
Here is the call graph for this function:

◆ set_max_body_size()

void websocketpp::http::parser::parser::set_max_body_size ( size_t value)
inline

Set the maximum number of bytes to parse and buffer before canceling a request.

Since
0.5.0
Parameters
valueThe size to set the max body length to.

Definition at line 532 of file parser.hpp.

532 {
534 }

◆ set_version()

void websocketpp::http::parser::parser::set_version ( std::string const & version)
inline

Input should be in format: HTTP/x.y where x and y are positive integers.

Todo
Does this method need any validation?
Parameters
[in]versionThe value to set the HTTP version to.

Definition at line 41 of file parser.hpp.

Member Data Documentation

◆ m_body

std::string websocketpp::http::parser::parser::m_body
protected

Definition at line 607 of file parser.hpp.

◆ m_body_bytes_max

size_t websocketpp::http::parser::parser::m_body_bytes_max
protected

Definition at line 609 of file parser.hpp.

◆ m_body_bytes_needed

size_t websocketpp::http::parser::parser::m_body_bytes_needed
protected

Definition at line 608 of file parser.hpp.

◆ m_body_encoding

body_encoding::value websocketpp::http::parser::parser::m_body_encoding
protected

Definition at line 610 of file parser.hpp.

◆ m_header_bytes

size_t websocketpp::http::parser::parser::m_header_bytes
protected

Definition at line 605 of file parser.hpp.

◆ m_headers

header_list websocketpp::http::parser::parser::m_headers
protected

Definition at line 603 of file parser.hpp.

◆ m_version

std::string websocketpp::http::parser::parser::m_version
protected

Definition at line 602 of file parser.hpp.


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