28 #ifndef HTTP_PARSER_RESPONSE_IMPL_HPP
29 #define HTTP_PARSER_RESPONSE_IMPL_HPP
36 #include <websocketpp/http/parser.hpp>
42 inline size_t response::consume(
char const * buf, size_t len) {
43 if (m_state == DONE) {
return 0;}
45 if (m_state == BODY) {
46 return this->process_body(buf,len);
50 m_buf->append(buf,len);
53 std::string::iterator begin = m_buf->begin();
54 std::string::iterator end = begin;
63 header_delimiter +
sizeof(header_delimiter) - 1
66 m_header_bytes += (end-begin+
sizeof(header_delimiter));
68 if (m_header_bytes > max_header_size) {
70 throw exception(
"Maximum header size exceeded.",
71 status_code::request_header_fields_too_large);
74 if (end == m_buf->end()) {
77 std::copy(begin,end,m_buf->begin());
78 m_buf->resize(
static_cast<std::string::size_type>(end-begin));
81 m_header_bytes -= m_buf->size();
90 if (m_state == RESPONSE_LINE) {
91 throw exception(
"Incomplete Request",status_code::bad_request);
95 std::string length = get_header(
"Content-Length");
101 std::istringstream ss(length);
103 if ((ss >> m_read).fail()) {
104 throw exception(
"Unable to parse Content-Length header",
105 status_code::bad_request);
113 len -
static_cast<std::string::size_type>(m_buf->end() - end)
114 +
sizeof(header_delimiter) - 1
119 read +=
this->process_body(buf+read,(len-read));
127 if (m_state == RESPONSE_LINE) {
128 this->process(begin,end);
131 this->process_header(begin,end);
135 begin = end+(
sizeof(header_delimiter) - 1);
139 inline size_t response::consume(std::istream & s) {
140 char buf[istream_buffer];
142 size_t bytes_processed;
146 s.getline(buf,istream_buffer);
147 bytes_read =
static_cast<size_t>(s.gcount());
149 if (s.fail() || s.eof()) {
150 bytes_processed =
this->consume(buf,bytes_read);
151 total += bytes_processed;
153 if (bytes_processed != bytes_read) {
157 }
else if (s.bad()) {
164 buf[bytes_read-1] =
'\n';
165 bytes_processed =
this->consume(buf,bytes_read);
166 total += bytes_processed;
168 if (bytes_processed != bytes_read) {
178 inline std::string response::raw()
const {
181 std::stringstream ret;
183 ret << get_version() <<
" " << m_status_code <<
" " << m_status_msg;
184 ret <<
"\r\n" << raw_headers() <<
"\r\n";
191 inline void response::set_status(status_code::value code) {
193 m_status_code = code;
194 m_status_msg = get_string(code);
197 inline void response::set_status(status_code::value code, std::string
const &
201 m_status_code = code;
205 inline void response::process(std::string::iterator begin,
206 std::string::iterator end)
208 std::string::iterator cursor_start = begin;
209 std::string::iterator cursor_end = std::find(begin,end,
' ');
211 if (cursor_end == end) {
212 throw exception(
"Invalid response line",status_code::bad_request);
215 set_version(std::string(cursor_start,cursor_end));
217 cursor_start = cursor_end+1;
218 cursor_end = std::find(cursor_start,end,
' ');
220 if (cursor_end == end) {
221 throw exception(
"Invalid request line",status_code::bad_request);
226 std::istringstream ss(std::string(cursor_start,cursor_end));
228 if ((ss >> code).fail()) {
229 throw exception(
"Unable to parse response code",status_code::bad_request);
232 set_status(status_code::value(code),std::string(cursor_end+1,end));
235 inline size_t response::process_body(
char const * buf, size_t len) {
257 m_body.append(buf,to_read);