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

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

#include <response.hpp>

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

Public Types

typedef response type
 
typedef lib::shared_ptr< typeptr
 

Public Member Functions

 response ()
 
size_t consume (char const *buf, size_t len)
 Process bytes in the input buffer.
 
size_t consume (std::istream &s)
 Process bytes in the input buffer (istream version)
 
bool ready () const
 Returns true if the response is ready.
 
bool headers_ready () const
 Returns true if the response headers are fully parsed.
 
std::string raw () const
 Returns the full raw response.
 
void set_status (status_code::value code)
 Set response status code and message.
 
void set_status (status_code::value code, std::string const &msg)
 Set response status code and message.
 
status_code::value get_status_code () const
 Return the response status code.
 
const std::string & get_status_msg () const
 Return the response status message.
 
- 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::response provides the following functionality for working with HTTP responses.

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

http::response checks for header completeness separately from the full response. Once the header is complete, the Content-Length header is read to determine when to stop reading body bytes. If no Content-Length is present ready() will never return true. It is the responsibility of the caller to consume to determine when the response is complete (ie when the connection terminates, or some other metric).

Definition at line 57 of file response.hpp.

Member Typedef Documentation

◆ ptr

Definition at line 60 of file response.hpp.

◆ type

Constructor & Destructor Documentation

◆ response()

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

Definition at line 62 of file response.hpp.

63 : m_read(0)
64 , m_buf(lib::make_shared<std::string>())
65 , m_status_code(status_code::uninitialized)
66 , m_state(RESPONSE_LINE) {}

Member Function Documentation

◆ consume() [1/2]

size_t websocketpp::http::parser::response::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 response and the full headers need not be available before processing can begin. If the end of the response 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 responses, incomplete responses, and max header size being reached.

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

Definition at line 42 of file response.hpp.

42 {
43 if (m_state == DONE) {return 0;}
44
45 if (m_state == BODY) {
46 return this->process_body(buf,len);
47 }
48
49 // copy new header bytes into buffer
50 m_buf->append(buf,len);
51
52 // Search for delimiter in buf. If found read until then. If not read all
53 std::string::iterator begin = m_buf->begin();
54 std::string::iterator end = begin;
55
56
57 for (;;) {
58 // search for delimiter
59 end = std::search(
60 begin,
61 m_buf->end(),
62 header_delimiter,
63 header_delimiter + sizeof(header_delimiter) - 1
64 );
65
66 m_header_bytes += (end-begin+sizeof(header_delimiter));
67
69 // exceeded max header size
70 throw exception("Maximum header size exceeded.",
72 }
73
74 if (end == m_buf->end()) {
75 // we are out of bytes. Discard the processed bytes and copy the
76 // remaining unprecessed bytes to the beginning of the buffer
77 std::copy(begin,end,m_buf->begin());
78 m_buf->resize(static_cast<std::string::size_type>(end-begin));
79
80 m_read += len;
81 m_header_bytes -= m_buf->size();
82
83 return len;
84 }
85
86 //the range [begin,end) now represents a line to be processed.
87
88 if (end-begin == 0) {
89 // we got a blank line
90 if (m_state == RESPONSE_LINE) {
91 throw exception("Incomplete Request",status_code::bad_request);
92 }
93
94 // TODO: grab content-length
95 std::string length = get_header("Content-Length");
96
97 if (length.empty()) {
98 // no content length found, read indefinitely
99 m_read = 0;
100 } else {
101 std::istringstream ss(length);
102
103 if ((ss >> m_read).fail()) {
104 throw exception("Unable to parse Content-Length header",
106 }
107 }
108
109 m_state = BODY;
110
111 // calc header bytes processed (starting bytes - bytes left)
112 size_t read = (
113 len - static_cast<std::string::size_type>(m_buf->end() - end)
114 + sizeof(header_delimiter) - 1
115 );
116
117 // if there were bytes left process them as body bytes
118 if (read < len) {
119 read += this->process_body(buf+read,(len-read));
120 }
121
122 // frees memory used temporarily during header parsing
123 m_buf.reset();
124
125 return read;
126 } else {
127 if (m_state == RESPONSE_LINE) {
128 this->process(begin,end);
129 m_state = HEADERS;
130 } else {
131 this->process_header(begin,end);
132 }
133 }
134
135 begin = end+(sizeof(header_delimiter) - 1);
136 }
137}
void process_header(std::string::iterator begin, std::string::iterator end)
Process a header line.
Definition parser.hpp:161
std::string const & get_header(std::string const &key) const
Get the value of an HTTP header.
Definition parser.hpp:45
static const Segment ss(Segment::ss)
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:
Here is the caller graph for this function:

◆ consume() [2/2]

size_t websocketpp::http::parser::response::consume ( std::istream & s)
inline

Process bytes from istream s. 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 response and the full headers need not be available before processing can begin. If the end of the response 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 responses, incomplete responses, and max header size being reached.

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

Definition at line 139 of file response.hpp.

139 {
140 char buf[istream_buffer];
141 size_t bytes_read;
142 size_t bytes_processed;
143 size_t total = 0;
144
145 while (s.good()) {
146 s.getline(buf,istream_buffer);
147 bytes_read = static_cast<size_t>(s.gcount());
148
149 if (s.fail() || s.eof()) {
150 bytes_processed = this->consume(buf,bytes_read);
151 total += bytes_processed;
152
153 if (bytes_processed != bytes_read) {
154 // problem
155 break;
156 }
157 } else if (s.bad()) {
158 // problem
159 break;
160 } else {
161 // the delimiting newline was found. Replace the trailing null with
162 // the newline that was discarded, since our raw consume function
163 // expects the newline to be be there.
164 buf[bytes_read-1] = '\n';
165 bytes_processed = this->consume(buf,bytes_read);
166 total += bytes_processed;
167
168 if (bytes_processed != bytes_read) {
169 // problem
170 break;
171 }
172 }
173 }
174
175 return total;
176}
size_t consume(char const *buf, size_t len)
Process bytes in the input buffer.
Definition response.hpp:42
size_t const istream_buffer
Number of bytes to use for temporary istream read buffers.
Definition constants.hpp:71
char * s
Here is the call graph for this function:

◆ get_status_code()

status_code::value websocketpp::http::parser::response::get_status_code ( ) const
inline

Definition at line 152 of file response.hpp.

152 {
153 return m_status_code;
154 }

◆ get_status_msg()

const std::string & websocketpp::http::parser::response::get_status_msg ( ) const
inline

Definition at line 157 of file response.hpp.

157 {
158 return m_status_msg;
159 }

◆ headers_ready()

bool websocketpp::http::parser::response::headers_ready ( ) const
inline

Definition at line 121 of file response.hpp.

121 {
122 return (m_state == BODY || m_state == DONE);
123 }

◆ raw()

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

Definition at line 178 of file response.hpp.

178 {
179 // TODO: validation. Make sure all required fields have been set?
180
181 std::stringstream ret;
182
183 ret << get_version() << " " << m_status_code << " " << m_status_msg;
184 ret << "\r\n" << raw_headers() << "\r\n";
185
186 ret << m_body;
187
188 return ret.str();
189}
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:

◆ ready()

bool websocketpp::http::parser::response::ready ( ) const
inline
Note
will never return true if the content length header is not present

Definition at line 116 of file response.hpp.

116 {
117 return m_state == DONE;
118 }

◆ set_status() [1/2]

void websocketpp::http::parser::response::set_status ( status_code::value code)
inline

Sets the response status code to code and looks up the corresponding message for standard codes. Non-standard codes will be entered as Unknown use set_status(status_code::value,std::string) overload to set both values explicitly.

Parameters
codeCode to set
msgMessage to set

Definition at line 191 of file response.hpp.

191 {
192 // TODO: validation?
193 m_status_code = code;
194 m_status_msg = get_string(code);
195}
std::string get_string(value code)
Return a human readable interpretation of a WebSocket close code.
Definition close.hpp:227

◆ set_status() [2/2]

void websocketpp::http::parser::response::set_status ( status_code::value code,
std::string const & msg )
inline

Sets the response status code and message to independent custom values. use set_status(status_code::value) to set the code and have the standard message be automatically set.

Parameters
codeCode to set
msgMessage to set

Definition at line 197 of file response.hpp.

199{
200 // TODO: validation?
201 m_status_code = code;
202 m_status_msg = msg;
203}

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