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

Stores, parses, and manipulates HTTP requests. More...

#include <request.hpp>

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

Public Types

typedef request type
 
typedef lib::shared_ptr< typeptr
 

Public Member Functions

 request ()
 
size_t consume (char const *buf, size_t len)
 Process bytes in the input buffer.
 
bool ready () const
 Returns whether or not the request is ready for reading.
 
std::string raw () const
 Returns the full raw request (including the body)
 
std::string raw_head () const
 Returns the raw request headers only (similar to an HTTP HEAD request)
 
void set_method (std::string const &method)
 Set the HTTP method. Must be a valid HTTP token.
 
std::string const & get_method () const
 Return the request method.
 
void set_uri (std::string const &uri)
 Set the HTTP uri. Must be a valid HTTP uri.
 
std::string const & get_uri () const
 Return the requested URI.
 
- Public Member Functions inherited from websocketpp::http::parser::parser
 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.
 

Additional Inherited Members

- Protected Member Functions inherited from websocketpp::http::parser::parser
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 inherited from websocketpp::http::parser::parser
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

http::request provides the following functionality for working with HTTP requests.

  • Initialize request via manually setting each element
  • Initialize request via reading raw bytes and parsing
  • Once initialized, access individual parsed elements
  • Once initialized, read entire request as raw bytes

Definition at line 50 of file request.hpp.

Member Typedef Documentation

◆ ptr

Definition at line 53 of file request.hpp.

◆ type

Constructor & Destructor Documentation

◆ request()

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

Definition at line 55 of file request.hpp.

56 : m_buf(lib::make_shared<std::string>())
57 , m_ready(false) {}

Member Function Documentation

◆ consume()

size_t websocketpp::http::parser::request::consume ( char const * buf,
size_t len )
inline

Process up to len bytes from input buffer buf. Returns the number of bytes processed. Bytes left unprocessed means bytes left over after the final header delimiters.

Consume is a streaming processor. It may be called multiple times on one request and the full headers need not be available before processing can begin. If the end of the request was reached during this call to consume the ready flag will be set. Further calls to consume once ready will be ignored.

Consume will throw an http::exception in the case of an error. Typical error reasons include malformed requests, incomplete requests, and max header size being reached.

Parameters
bufPointer to byte buffer
lenSize of byte buffer
Returns
Number of bytes processed.

Definition at line 41 of file request.hpp.

41 {
42 size_t bytes_processed;
43
44 if (m_ready) {return 0;}
45
46 if (m_body_bytes_needed > 0) {
47 bytes_processed = process_body(buf,len);
48 if (body_ready()) {
49 m_ready = true;
50 }
51 return bytes_processed;
52 }
53
54 // copy new header bytes into buffer
55 m_buf->append(buf,len);
56
57 // Search for delimiter in buf. If found read until then. If not read all
58 std::string::iterator begin = m_buf->begin();
59 std::string::iterator end;
60
61 for (;;) {
62 // search for line delimiter
63 end = std::search(
64 begin,
65 m_buf->end(),
66 header_delimiter,
67 header_delimiter+sizeof(header_delimiter)-1
68 );
69
70 m_header_bytes += (end-begin+sizeof(header_delimiter));
71
73 // exceeded max header size
74 throw exception("Maximum header size exceeded.",
76 }
77
78 if (end == m_buf->end()) {
79 // we are out of bytes. Discard the processed bytes and copy the
80 // remaining unprecessed bytes to the beginning of the buffer
81 std::copy(begin,end,m_buf->begin());
82 m_buf->resize(static_cast<std::string::size_type>(end-begin));
83 m_header_bytes -= m_buf->size();
84
85 return len;
86 }
87
88 //the range [begin,end) now represents a line to be processed.
89 if (end-begin == 0) {
90 // we got a blank line
91 if (m_method.empty() || get_header("Host").empty()) {
92 throw exception("Incomplete Request",status_code::bad_request);
93 }
94
95 bytes_processed = (
96 len - static_cast<std::string::size_type>(m_buf->end()-end)
97 + sizeof(header_delimiter) - 1
98 );
99
100 // frees memory used temporarily during request parsing
101 m_buf.reset();
102
103 // if this was not an upgrade request and has a content length
104 // continue capturing content-length bytes and expose them as a
105 // request body.
106
107 if (prepare_body()) {
108 bytes_processed += process_body(buf+bytes_processed,len-bytes_processed);
109 if (body_ready()) {
110 m_ready = true;
111 }
112 return bytes_processed;
113 } else {
114 m_ready = true;
115
116 // return number of bytes processed (starting bytes - bytes left)
117 return bytes_processed;
118 }
119 } else {
120 if (m_method.empty()) {
121 this->process(begin,end);
122 } else {
123 this->process_header(begin,end);
124 }
125 }
126
127 begin = end+(sizeof(header_delimiter)-1);
128 }
129}
size_t process_body(char const *buf, size_t len)
Process body data.
Definition parser.hpp:145
void process_header(std::string::iterator begin, std::string::iterator end)
Process a header line.
Definition parser.hpp:161
bool body_ready() const
Check if the parser is done parsing the body.
Definition parser.hpp:589
bool prepare_body()
Prepare the parser to begin parsing body data.
Definition parser.hpp:119
std::string const & get_header(std::string const &key) const
Get the value of an HTTP header.
Definition parser.hpp:45
size_t const max_header_size
Maximum size in bytes before rejecting an HTTP header as too big.
Definition constants.hpp:65
size_t len
uint8_t buf[2048]
Here is the call graph for this function:

◆ get_method()

std::string const & websocketpp::http::parser::request::get_method ( ) const
inline

Definition at line 96 of file request.hpp.

96 {
97 return m_method;
98 }

◆ get_uri()

std::string const & websocketpp::http::parser::request::get_uri ( ) const
inline

Definition at line 104 of file request.hpp.

104 {
105 return m_uri;
106 }

◆ raw()

std::string websocketpp::http::parser::request::raw ( ) const
inline

Definition at line 131 of file request.hpp.

131 {
132 // TODO: validation. Make sure all required fields have been set?
133 std::stringstream ret;
134
135 ret << m_method << " " << m_uri << " " << get_version() << "\r\n";
136 ret << raw_headers() << "\r\n" << m_body;
137
138 return ret.str();
139}
std::string raw_headers() const
Generate and return the HTTP headers as a string.
Definition parser.hpp:179
std::string const & get_version() const
Get the HTTP version string.
Definition parser.hpp:410
CK_RV ret
Here is the call graph for this function:

◆ raw_head()

std::string websocketpp::http::parser::request::raw_head ( ) const
inline

Definition at line 141 of file request.hpp.

141 {
142 // TODO: validation. Make sure all required fields have been set?
143 std::stringstream ret;
144
145 ret << m_method << " " << m_uri << " " << get_version() << "\r\n";
146 ret << raw_headers() << "\r\n";
147
148 return ret.str();
149}
Here is the call graph for this function:

◆ ready()

bool websocketpp::http::parser::request::ready ( ) const
inline

Definition at line 82 of file request.hpp.

82 {
83 return m_ready;
84 }

◆ set_method()

void websocketpp::http::parser::request::set_method ( std::string const & method)
inline

Definition at line 151 of file request.hpp.

151 {
152 if (std::find_if(method.begin(),method.end(),is_not_token_char) != method.end()) {
153 throw exception("Invalid method token.",status_code::bad_request);
154 }
155
156 m_method = method;
157}
bool is_not_token_char(unsigned char c)
Is the character a non-token.
Here is the call graph for this function:

◆ set_uri()

void websocketpp::http::parser::request::set_uri ( std::string const & uri)
inline

Definition at line 159 of file request.hpp.

159 {
160 // TODO: validation?
161 m_uri = uri;
162}

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