make http header parsing case insensitive, and remove some dead code (reset function)

This commit is contained in:
Dennis Luxen 2015-01-27 09:58:26 +01:00
parent 69e59b0ced
commit f0d3d23b5f
3 changed files with 33 additions and 28 deletions

View File

@ -67,7 +67,7 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
// no error detected, let's parse the request // no error detected, let's parse the request
CompressionType compression_type(noCompression); CompressionType compression_type(noCompression);
osrm::tribool result; osrm::tribool result;
std::tie(result, compression_type) = RequestParser().Parse( std::tie(result, compression_type) = RequestParser().parse(
request, incoming_data_buffer.data(), incoming_data_buffer.data() + bytes_transferred); request, incoming_data_buffer.data(), incoming_data_buffer.data() + bytes_transferred);
// the request has been parsed // the request has been parsed

View File

@ -29,6 +29,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Http/Request.h" #include "Http/Request.h"
#include <boost/algorithm/string/predicate.hpp>
namespace http namespace http
{ {
@ -37,15 +39,13 @@ RequestParser::RequestParser()
{ {
} }
void RequestParser::Reset() { state = internal_state::method_start; }
std::tuple<osrm::tribool, CompressionType> std::tuple<osrm::tribool, CompressionType>
RequestParser::Parse(Request &req, char *begin, char *end) RequestParser::parse(Request &request, char *begin, char *end)
{ {
while (begin != end) while (begin != end)
{ {
osrm::tribool result = consume(req, *begin++); osrm::tribool result = consume(request, *begin++);
if (result == osrm::tribool::yes || result == osrm::tribool::no) if (result != osrm::tribool::indeterminate)
{ {
return std::make_tuple(result, compression_type); return std::make_tuple(result, compression_type);
} }
@ -54,7 +54,7 @@ RequestParser::Parse(Request &req, char *begin, char *end)
return std::make_tuple(result, compression_type); return std::make_tuple(result, compression_type);
} }
osrm::tribool RequestParser::consume(Request &req, char input) osrm::tribool RequestParser::consume(Request &request, const char input)
{ {
switch (state) switch (state)
{ {
@ -82,7 +82,7 @@ osrm::tribool RequestParser::consume(Request &req, char input)
return osrm::tribool::no; return osrm::tribool::no;
} }
state = internal_state::uri; state = internal_state::uri;
req.uri.push_back(input); request.uri.push_back(input);
return osrm::tribool::indeterminate; return osrm::tribool::indeterminate;
case internal_state::uri: case internal_state::uri:
if (input == ' ') if (input == ' ')
@ -94,7 +94,7 @@ osrm::tribool RequestParser::consume(Request &req, char input)
{ {
return osrm::tribool::no; return osrm::tribool::no;
} }
req.uri.push_back(input); request.uri.push_back(input);
return osrm::tribool::indeterminate; return osrm::tribool::indeterminate;
case internal_state::http_version_h: case internal_state::http_version_h:
if (input == 'H') if (input == 'H')
@ -175,27 +175,27 @@ osrm::tribool RequestParser::consume(Request &req, char input)
} }
return osrm::tribool::no; return osrm::tribool::no;
case internal_state::header_line_start: case internal_state::header_line_start:
if (header.name == "Accept-Encoding") if (boost::iequals(header.name, "Accept-Encoding"))
{ {
/* giving gzip precedence over deflate */ /* giving gzip precedence over deflate */
if (header.value.find("deflate") != std::string::npos) if (boost::icontains(header.value, "deflate"))
{ {
compression_type = deflateRFC1951; compression_type = deflateRFC1951;
} }
if (header.value.find("gzip") != std::string::npos) if (boost::icontains(header.value, "gzip"))
{ {
compression_type = gzipRFC1952; compression_type = gzipRFC1952;
} }
} }
if ("Referer" == header.name) if (boost::iequals(header.name, "Referer"))
{ {
req.referrer = header.value; request.referrer = header.value;
} }
if ("User-Agent" == header.name) if (boost::iequals(header.name, "User-Agent"))
{ {
req.agent = header.value; request.agent = header.value;
} }
if (input == '\r') if (input == '\r')
@ -208,7 +208,7 @@ osrm::tribool RequestParser::consume(Request &req, char input)
return osrm::tribool::no; return osrm::tribool::no;
} }
state = internal_state::header_name; state = internal_state::header_name;
header.Clear(); header.clear();
header.name.push_back(input); header.name.push_back(input);
return osrm::tribool::indeterminate; return osrm::tribool::indeterminate;
case internal_state::header_lws: case internal_state::header_lws:
@ -270,14 +270,17 @@ osrm::tribool RequestParser::consume(Request &req, char input)
} }
} }
bool RequestParser::is_char(int character) const { return character >= 0 && character <= 127; } bool RequestParser::is_char(const int character) const
{
return character >= 0 && character <= 127;
}
bool RequestParser::is_CTL(int character) const bool RequestParser::is_CTL(const int character) const
{ {
return (character >= 0 && character <= 31) || (character == 127); return (character >= 0 && character <= 31) || (character == 127);
} }
bool RequestParser::is_special(int character) const bool RequestParser::is_special(const int character) const
{ {
switch (character) switch (character)
{ {
@ -306,5 +309,8 @@ bool RequestParser::is_special(int character) const
} }
} }
bool RequestParser::is_digit(int character) const { return character >= '0' && character <= '9'; } bool RequestParser::is_digit(const int character) const
{
return character >= '0' && character <= '9';
}
} }

View File

@ -43,20 +43,19 @@ class RequestParser
{ {
public: public:
RequestParser(); RequestParser();
void Reset();
std::tuple<osrm::tribool, CompressionType> Parse(Request &req, char *begin, char *end); std::tuple<osrm::tribool, CompressionType> parse(Request &request, char *begin, char *end);
private: private:
osrm::tribool consume(Request &req, char input); osrm::tribool consume(Request &req, const char input);
bool is_char(int c) const; bool is_char(const int character) const;
bool is_CTL(int c) const; bool is_CTL(const int character) const;
bool is_special(int c) const; bool is_special(const int character) const;
bool is_digit(int c) const; bool is_digit(const int character) const;
enum class internal_state : unsigned char enum class internal_state : unsigned char
{ {