28 #ifndef HTTP_PARSER_REQUEST_IMPL_HPP
29 #define HTTP_PARSER_REQUEST_IMPL_HPP
35 #include <websocketpp/http/parser.hpp>
41 inline size_t request::consume(
char const * buf, size_t len) {
42 size_t bytes_processed;
44 if (m_ready) {
return 0;}
46 if (m_body_bytes_needed > 0) {
47 bytes_processed = process_body(buf,len);
51 return bytes_processed;
55 m_buf->append(buf,len);
58 std::string::iterator begin = m_buf->begin();
59 std::string::iterator end;
67 header_delimiter+
sizeof(header_delimiter)-1
70 m_header_bytes += (end-begin+
sizeof(header_delimiter));
72 if (m_header_bytes > max_header_size) {
74 throw exception(
"Maximum header size exceeded.",
75 status_code::request_header_fields_too_large);
78 if (end == m_buf->end()) {
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();
91 if (m_method.empty() || get_header(
"Host").empty()) {
92 throw exception(
"Incomplete Request",status_code::bad_request);
96 len -
static_cast<std::string::size_type>(m_buf->end()-end)
97 +
sizeof(header_delimiter) - 1
107 if (prepare_body()) {
108 bytes_processed += process_body(buf+bytes_processed,len-bytes_processed);
112 return bytes_processed;
117 return bytes_processed;
120 if (m_method.empty()) {
121 this->process(begin,end);
123 this->process_header(begin,end);
127 begin = end+(
sizeof(header_delimiter)-1);
131 inline std::string request::raw()
const {
133 std::stringstream ret;
135 ret << m_method <<
" " << m_uri <<
" " << get_version() <<
"\r\n";
136 ret << raw_headers() <<
"\r\n" << m_body;
141 inline std::string request::raw_head()
const {
143 std::stringstream ret;
145 ret << m_method <<
" " << m_uri <<
" " << get_version() <<
"\r\n";
146 ret << raw_headers() <<
"\r\n";
151 inline void request::set_method(std::string
const & method) {
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);
159 inline void request::set_uri(std::string
const & uri) {
164 inline void request::process(std::string::iterator begin, std::string::iterator
167 std::string::iterator cursor_start = begin;
168 std::string::iterator cursor_end = std::find(begin,end,
' ');
170 if (cursor_end == end) {
171 throw exception(
"Invalid request line1",status_code::bad_request);
174 set_method(std::string(cursor_start,cursor_end));
176 cursor_start = cursor_end+1;
177 cursor_end = std::find(cursor_start,end,
' ');
179 if (cursor_end == end) {
180 throw exception(
"Invalid request line2",status_code::bad_request);
183 set_uri(std::string(cursor_start,cursor_end));
184 set_version(std::string(cursor_end+1,end));