28 #ifndef HTTP_PARSER_RESPONSE_IMPL_HPP
29 #define HTTP_PARSER_RESPONSE_IMPL_HPP
34 #include <websocketpp/http/parser.hpp>
41 if (m_state == DONE) {
return 0;}
43 if (m_state == BODY) {
44 return this->process_body(buf,len);
49 throw exception(
"Maximum header size exceeded.",
50 status_code::request_header_fields_too_large);
54 m_buf->append(buf,len);
57 std::string::iterator begin = m_buf->begin();
58 std::string::iterator end = begin;
70 if (end == m_buf->end()) {
73 std::copy(begin,end,m_buf->begin());
74 m_buf->resize(static_cast<std::string::size_type>(end-begin));
85 if (m_state == RESPONSE_LINE) {
86 throw exception(
"Incomplete Request",status_code::bad_request);
90 std::string length =
get_header(
"Content-Length");
96 std::istringstream ss(length);
98 if ((ss >> m_read).fail()) {
99 throw exception(
"Unable to parse Content-Length header",
100 status_code::bad_request);
108 len -
static_cast<std::string::size_type
>(m_buf->end() - end)
114 read += this->process_body(buf+read,(len-read));
122 if (m_state == RESPONSE_LINE) {
123 this->process(begin,end);
137 size_t bytes_processed;
142 bytes_read =
static_cast<size_t>(s.gcount());
144 if (s.fail() || s.eof()) {
145 bytes_processed = this->
consume(buf,bytes_read);
146 total += bytes_processed;
148 if (bytes_processed != bytes_read) {
152 }
else if (s.bad()) {
159 buf[bytes_read-1] =
'\n';
160 bytes_processed = this->
consume(buf,bytes_read);
161 total += bytes_processed;
163 if (bytes_processed != bytes_read) {
178 std::getline(s, line);
180 if (line[line.size()-1] ==
'\r') {
181 line.erase(line.end()-1);
183 std::stringstream ss(line);
192 ss.getline(char_val,256);
193 set_status(status_code::value(int_val),std::string(char_val));
204 std::stringstream ret;
206 ret <<
get_version() <<
" " << m_status_code <<
" " << m_status_msg;
216 m_status_code = code;
224 m_status_code = code;
228 inline void response::process(std::string::iterator begin,
229 std::string::iterator end)
231 std::string::iterator cursor_start = begin;
232 std::string::iterator cursor_end = std::find(begin,end,
' ');
234 if (cursor_end == end) {
235 throw exception(
"Invalid response line",status_code::bad_request);
240 cursor_start = cursor_end+1;
241 cursor_end = std::find(cursor_start,end,
' ');
243 if (cursor_end == end) {
244 throw exception(
"Invalid request line",status_code::bad_request);
249 std::istringstream ss(std::string(cursor_start,cursor_end));
251 if ((ss >> code).fail()) {
252 throw exception(
"Unable to parse response code",status_code::bad_request);
255 set_status(status_code::value(code),std::string(cursor_end+1,end));
258 inline size_t response::process_body(
const char *buf,
size_t len) {
280 m_body.append(buf,to_read);
289 #endif // HTTP_PARSER_RESPONSE_IMPL_HPP
bool parse_complete(std::istream &s)
DEPRECATED parse a complete response from a pre-delimited istream.
static char const header_delimiter[]
Literal value of the HTTP header delimiter.
void process_header(std::string::iterator begin, std::string::iterator end)
Process a header line.
bool parse_headers(std::istream &s)
Parse headers from an istream.
std::string const & get_header(std::string const &key) const
Get the value of an HTTP header.
std::string const & get_version() const
Get the HTTP version string.
size_t consume(const char *buf, size_t len)
Process bytes in the input buffer.
size_t const istream_buffer
Number of bytes to use for temporary istream read buffers.
Namespace for the WebSocket++ project.
size_t const max_header_size
Maximum size in bytes before rejecting an HTTP header as too big.
void set_status(status_code::value code)
Set response status code and message.
std::string raw_headers() const
Generate and return the HTTP headers as a string.
std::string get_string(value code)
Return a human readable interpretation of a WebSocket close code.
void set_version(std::string const &version)
Set HTTP parser Version.
std::string raw() const
Returns the full raw response.