28 #ifndef HTTP_PARSER_IMPL_HPP
29 #define HTTP_PARSER_IMPL_HPP
41 inline void parser::set_version(std::string
const & version) {
45 inline std::string
const & parser::get_header(std::string
const & key)
const {
48 header_list::const_iterator h = m_headers.find(key);
50 if (h == m_headers.end()) {
57 inline bool parser::get_header_as_plist(std::string
const & key,
58 parameter_list & out)
const
60 header_list::const_iterator it = m_headers.find(key);
62 if (it == m_headers.end() || it->second.size() == 0) {
66 return this->parse_parameter_list(it->second,out);
69 inline void parser::append_header(std::string
const & key, std::string
const &
70 val, lib::error_code & ec)
72 if (std::find_if(key.begin(),key.end(),is_not_token_char) != key.end()) {
73 ec = error::make_error_code(error::invalid_header_name);
77 if (
this->get_header(key).empty()) {
80 m_headers[key] +=
", " + val;
82 ec = lib::error_code();
85 inline void parser::replace_header(std::string
const & key, std::string
const &
86 val, lib::error_code & ec)
88 if (std::find_if(key.begin(),key.end(),is_not_token_char) != key.end()) {
89 ec = error::make_error_code(error::invalid_header_name);
94 ec = lib::error_code();
97 inline void parser::remove_header(std::string
const & key,
100 if (std::find_if(key.begin(),key.end(),is_not_token_char) != key.end()) {
101 ec = error::make_error_code(error::invalid_header_name);
105 m_headers.erase(key);
106 ec = lib::error_code();
109 inline void parser::set_body(std::string
const & value, lib::error_code & ec) {
110 if (value.size() == 0) {
111 remove_header(
"Content-Length", ec);
115 ec = lib::error_code();
119 if (value.size() > m_body_bytes_max) {
120 ec = error::make_error_code(error::body_too_large);
124 std::stringstream len;
126 replace_header(
"Content-Length", len.str(), ec);
129 ec = lib::error_code();
132 inline bool parser::parse_parameter_list(std::string
const & in,
133 parameter_list & out)
const
135 if (in.size() == 0) {
139 std::string::const_iterator it;
140 it = extract_parameters(in.begin(),in.end(),out);
141 return (it == in.begin());
144 inline bool parser::prepare_body() {
145 if (!get_header(
"Content-Length").empty()) {
146 std::string
const & cl_header = get_header(
"Content-Length");
152 m_body_bytes_needed = std::strtoul(cl_header.c_str(),&end,10);
154 if (m_body_bytes_needed > m_body_bytes_max) {
155 ec = error::make_error_code(error::body_too_large);
159 m_body_encoding = body_encoding::plain;
161 }
else if (get_header(
"Transfer-Encoding") ==
"chunked") {
170 inline size_t parser::process_body(
char const * buf, size_t len) {
171 if (m_body_encoding == body_encoding::plain) {
172 size_t processed = (std::min)(m_body_bytes_needed,len);
173 m_body.append(buf,processed);
174 m_body_bytes_needed -= processed;
176 }
else if (m_body_encoding == body_encoding::chunked) {
178 throw exception(
"Unexpected body encoding",
179 status_code::internal_server_error);
181 throw exception(
"Unexpected body encoding",
182 status_code::internal_server_error);
186 inline void parser::process_header(std::string::iterator begin,
187 std::string::iterator end)
189 std::string::iterator cursor = std::search(
193 header_separator +
sizeof(header_separator) - 1
197 throw exception(
"Invalid header line",status_code::bad_request);
200 append_header(strip_lws(std::string(begin,cursor)),
201 strip_lws(std::string(cursor+
sizeof(header_separator)-1,end)));
204 inline header_list
const & parser::get_headers()
const {
208 inline std::string parser::raw_headers()
const {
209 std::stringstream raw;
211 header_list::const_iterator it;
212 for (it = m_headers.begin(); it != m_headers.end(); it++) {
213 raw << it->first <<
": " << it->second <<
"\r\n";