osrm-backend/src/server/request_parser.cpp

367 lines
11 KiB
C++
Raw Normal View History

2016-01-02 11:13:44 -05:00
#include "server/request_parser.hpp"
2013-11-14 12:44:36 -05:00
2016-01-02 11:13:44 -05:00
#include "server/http/compression_type.hpp"
#include "server/http/header.hpp"
#include "server/http/request.hpp"
2014-10-03 04:23:21 -04:00
#include <boost/algorithm/string/predicate.hpp>
2015-02-09 11:38:40 -05:00
#include <string>
2016-01-05 10:51:13 -05:00
namespace osrm
{
namespace server
{
2013-11-14 12:44:36 -05:00
RequestParser::RequestParser()
: state(internal_state::method_start), current_header({"", ""}),
2016-01-05 10:51:13 -05:00
selected_compression(http::no_compression), is_post_header(false), content_length(0)
{
}
2013-11-14 12:44:36 -05:00
2016-01-29 17:02:18 -05:00
std::tuple<RequestParser::RequestStatus, http::compression_type>
2016-01-05 10:51:13 -05:00
RequestParser::parse(http::request &current_request, char *begin, char *end)
2014-04-28 09:08:54 -04:00
{
while (begin != end)
{
2016-01-28 18:43:19 -05:00
RequestStatus result = consume(current_request, *begin++);
if (result != RequestStatus::indeterminate)
2014-04-28 09:08:54 -04:00
{
return std::make_tuple(result, selected_compression);
2013-11-14 12:44:36 -05:00
}
}
2016-01-28 18:43:19 -05:00
RequestStatus result = RequestStatus::indeterminate;
2016-01-05 06:04:04 -05:00
if (state == internal_state::post_request && content_length <= 0)
2015-05-27 09:40:10 -04:00
{
2016-01-28 18:43:19 -05:00
result = RequestStatus::valid;
2015-05-27 09:40:10 -04:00
}
return std::make_tuple(result, selected_compression);
2013-11-14 12:44:36 -05:00
}
2016-01-29 17:02:18 -05:00
RequestParser::RequestStatus RequestParser::consume(http::request &current_request, const char input)
2014-04-28 09:08:54 -04:00
{
switch (state)
2014-04-28 09:08:54 -04:00
{
case internal_state::method_start:
if (!is_char(input) || is_CTL(input) || is_special(input))
2014-04-28 09:08:54 -04:00
{
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2013-11-14 12:44:36 -05:00
}
2016-01-05 06:04:04 -05:00
if (input == 'P')
2015-05-27 09:40:10 -04:00
{
state = internal_state::post_O;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2015-05-27 09:40:10 -04:00
}
state = internal_state::method;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2015-05-27 09:40:10 -04:00
case internal_state::post_O:
2016-01-05 06:04:04 -05:00
if (input == 'O')
2015-05-27 09:40:10 -04:00
{
2016-01-05 06:04:04 -05:00
state = internal_state::post_S;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2015-05-27 09:40:10 -04:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2015-05-27 09:40:10 -04:00
case internal_state::post_S:
2016-01-05 06:04:04 -05:00
if (input == 'S')
2015-05-27 09:40:10 -04:00
{
2016-01-05 06:04:04 -05:00
state = internal_state::post_T;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2015-05-27 09:40:10 -04:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2015-05-27 09:40:10 -04:00
case internal_state::post_T:
2016-01-05 06:04:04 -05:00
if (input == 'T')
2015-05-27 09:40:10 -04:00
{
2016-01-05 06:04:04 -05:00
is_post_header = true;
state = internal_state::method;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2015-05-27 09:40:10 -04:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2015-05-27 09:40:10 -04:00
case internal_state::post_request:
current_request.uri.push_back(input);
--content_length;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
case internal_state::method:
2014-04-28 09:08:54 -04:00
if (input == ' ')
{
state = internal_state::uri;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2014-04-28 09:08:54 -04:00
}
if (!is_char(input) || is_CTL(input) || is_special(input))
2014-04-28 09:08:54 -04:00
{
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
case internal_state::uri_start:
if (is_CTL(input))
2014-04-28 09:08:54 -04:00
{
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2013-11-14 12:44:36 -05:00
}
state = internal_state::uri;
current_request.uri.push_back(input);
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
case internal_state::uri:
2014-04-28 09:08:54 -04:00
if (input == ' ')
{
state = internal_state::http_version_h;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2014-04-28 09:08:54 -04:00
}
if (is_CTL(input))
2014-04-28 09:08:54 -04:00
{
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2013-11-14 12:44:36 -05:00
}
current_request.uri.push_back(input);
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
case internal_state::http_version_h:
2014-04-28 09:08:54 -04:00
if (input == 'H')
{
state = internal_state::http_version_t_1;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::http_version_t_1:
2014-04-28 09:08:54 -04:00
if (input == 'T')
{
state = internal_state::http_version_t_2;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::http_version_t_2:
2014-04-28 09:08:54 -04:00
if (input == 'T')
{
state = internal_state::http_version_p;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::http_version_p:
2014-04-28 09:08:54 -04:00
if (input == 'P')
{
state = internal_state::http_version_slash;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::http_version_slash:
2014-04-28 09:08:54 -04:00
if (input == '/')
{
state = internal_state::http_version_major_start;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::http_version_major_start:
if (is_digit(input))
2014-04-28 09:08:54 -04:00
{
state = internal_state::http_version_major;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::http_version_major:
2014-04-28 09:08:54 -04:00
if (input == '.')
{
state = internal_state::http_version_minor_start;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2014-04-28 09:08:54 -04:00
}
if (is_digit(input))
2014-04-28 09:08:54 -04:00
{
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::http_version_minor_start:
if (is_digit(input))
2014-04-28 09:08:54 -04:00
{
state = internal_state::http_version_minor;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::http_version_minor:
2014-04-28 09:08:54 -04:00
if (input == '\r')
{
state = internal_state::expecting_newline_1;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
if (is_digit(input))
2014-04-28 09:08:54 -04:00
{
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::expecting_newline_1:
2014-04-28 09:08:54 -04:00
if (input == '\n')
{
state = internal_state::header_line_start;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::header_line_start:
if (boost::iequals(current_header.name, "Accept-Encoding"))
2014-04-28 09:08:54 -04:00
{
2013-11-14 12:44:36 -05:00
/* giving gzip precedence over deflate */
if (boost::icontains(current_header.value, "deflate"))
2014-04-28 09:08:54 -04:00
{
2016-01-05 10:51:13 -05:00
selected_compression = http::deflate_rfc1951;
2014-04-28 09:08:54 -04:00
}
if (boost::icontains(current_header.value, "gzip"))
2014-04-28 09:08:54 -04:00
{
2016-01-05 10:51:13 -05:00
selected_compression = http::gzip_rfc1952;
2014-04-28 09:08:54 -04:00
}
2013-11-14 12:44:36 -05:00
}
if (boost::iequals(current_header.name, "Referer"))
2014-04-28 09:08:54 -04:00
{
current_request.referrer = current_header.value;
2014-04-28 09:08:54 -04:00
}
2013-11-14 12:44:36 -05:00
if (boost::iequals(current_header.name, "User-Agent"))
2014-04-28 09:08:54 -04:00
{
current_request.agent = current_header.value;
2014-04-28 09:08:54 -04:00
}
if (boost::iequals(current_header.name, "Content-Length"))
{
2016-01-05 06:04:04 -05:00
try
{
content_length = std::stoi(current_header.value);
}
catch (const std::exception &e)
{
// Ignore the header if the parameter isn't an int
}
}
2015-06-01 03:42:22 -04:00
if (boost::iequals(current_header.name, "Content-Type"))
{
if (!boost::icontains(current_header.value, "application/x-www-form-urlencoded"))
{
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2015-06-01 03:42:22 -04:00
}
}
2013-11-14 12:44:36 -05:00
2014-04-28 09:08:54 -04:00
if (input == '\r')
{
state = internal_state::expecting_newline_3;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2014-04-28 09:08:54 -04:00
}
if (!is_char(input) || is_CTL(input) || is_special(input))
2014-04-28 09:08:54 -04:00
{
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2013-11-14 12:44:36 -05:00
}
state = internal_state::header_name;
current_header.clear();
current_header.name.push_back(input);
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
case internal_state::header_lws:
2014-04-28 09:08:54 -04:00
if (input == '\r')
{
state = internal_state::expecting_newline_2;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2014-04-28 09:08:54 -04:00
}
if (input == ' ' || input == '\t')
{
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
if (is_CTL(input))
{
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2013-11-14 12:44:36 -05:00
}
state = internal_state::header_value;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
case internal_state::header_name:
2014-04-28 09:08:54 -04:00
if (input == ':')
{
state = internal_state::space_before_header_value;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2014-04-28 09:08:54 -04:00
}
if (!is_char(input) || is_CTL(input) || is_special(input))
2014-04-28 09:08:54 -04:00
{
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2013-11-14 12:44:36 -05:00
}
current_header.name.push_back(input);
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
case internal_state::space_before_header_value:
2014-04-28 09:08:54 -04:00
if (input == ' ')
{
state = internal_state::header_value;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
case internal_state::header_value:
2014-04-28 09:08:54 -04:00
if (input == '\r')
{
state = internal_state::expecting_newline_2;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2014-04-28 09:08:54 -04:00
}
if (is_CTL(input))
2014-04-28 09:08:54 -04:00
{
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2013-11-14 12:44:36 -05:00
}
current_header.value.push_back(input);
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
case internal_state::expecting_newline_2:
2014-04-28 09:08:54 -04:00
if (input == '\n')
{
state = internal_state::header_line_start;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2013-11-14 12:44:36 -05:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2015-05-27 09:40:10 -04:00
case internal_state::expecting_newline_3:
if (input == '\n')
2015-05-27 09:40:10 -04:00
{
if (is_post_header)
2015-05-27 09:40:10 -04:00
{
2016-01-05 06:04:04 -05:00
if (content_length > 0)
{
current_request.uri.push_back('?');
}
2015-05-27 09:40:10 -04:00
state = internal_state::post_request;
2016-01-28 18:43:19 -05:00
return RequestStatus::indeterminate;
2015-05-27 09:40:10 -04:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::valid;
2015-05-27 09:40:10 -04:00
}
2016-01-28 18:43:19 -05:00
return RequestStatus::invalid;
2015-05-27 09:40:10 -04:00
default: // should never be reached
2016-01-28 18:43:19 -05:00
return input == '\n' ? RequestStatus::valid : RequestStatus::invalid;
2013-11-14 12:44:36 -05:00
}
}
bool RequestParser::is_char(const int character) const
{
return character >= 0 && character <= 127;
}
2013-11-14 12:44:36 -05:00
bool RequestParser::is_CTL(const int character) const
{
return (character >= 0 && character <= 31) || (character == 127);
}
2013-11-14 12:44:36 -05:00
bool RequestParser::is_special(const int character) const
2014-04-28 09:08:54 -04:00
{
switch (character)
2014-04-28 09:08:54 -04:00
{
case '(':
case ')':
case '<':
case '>':
case '@':
case ',':
case ';':
case ':':
case '\\':
case '"':
case '/':
case '[':
case ']':
case '?':
case '=':
case '{':
case '}':
case ' ':
case '\t':
return true;
default:
return false;
2013-11-14 12:44:36 -05:00
}
}
bool RequestParser::is_digit(const int character) const
{
return character >= '0' && character <= '9';
}
2013-11-14 12:44:36 -05:00
}
2016-01-05 10:51:13 -05:00
}