replace boost::tribool with enum based implementation

This commit is contained in:
Dennis Luxen 2015-01-23 17:46:40 +01:00
parent b89304a24b
commit fb3bc22c64
3 changed files with 66 additions and 64 deletions

View File

@ -66,13 +66,13 @@ 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);
boost::tribool result; osrm::tribool result;
std::tie(result, std::ignore) = std::tie(result, std::ignore) =
RequestParser().Parse(request, incoming_data_buffer.data(), RequestParser().Parse(request, incoming_data_buffer.data(),
incoming_data_buffer.data() + bytes_transferred, compression_type); incoming_data_buffer.data() + bytes_transferred, compression_type);
// the request has been parsed // the request has been parsed
if (result) if (result == osrm::tribool::yes)
{ {
request.endpoint = TCP_socket.remote_endpoint().address(); request.endpoint = TCP_socket.remote_endpoint().address();
request_handler.handle_request(request, reply); request_handler.handle_request(request, reply);
@ -112,7 +112,7 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
strand.wrap(boost::bind(&Connection::handle_write, this->shared_from_this(), strand.wrap(boost::bind(&Connection::handle_write, this->shared_from_this(),
boost::asio::placeholders::error))); boost::asio::placeholders::error)));
} }
else if (!result) else if (result == osrm::tribool::no)
{ // request is not parseable { // request is not parseable
reply = Reply::StockReply(Reply::badRequest); reply = Reply::StockReply(Reply::badRequest);

View File

@ -1,6 +1,6 @@
/* /*
Copyright (c) 2013, Project OSRM, Dennis Luxen, others Copyright (c) 2015, Project OSRM, Dennis Luxen, others
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, Redistribution and use in source and binary forms, with or without modification,
@ -36,22 +36,22 @@ RequestParser::RequestParser() : state_(method_start), header({"", ""}) {}
void RequestParser::Reset() { state_ = method_start; } void RequestParser::Reset() { state_ = method_start; }
std::tuple<boost::tribool, char *> std::tuple<osrm::tribool, char *>
RequestParser::Parse(Request &req, char *begin, char *end, http::CompressionType &compression_type) RequestParser::Parse(Request &req, char *begin, char *end, http::CompressionType &compression_type)
{ {
while (begin != end) while (begin != end)
{ {
boost::tribool result = consume(req, *begin++, compression_type); osrm::tribool result = consume(req, *begin++, compression_type);
if (result || !result) if (result == osrm::tribool::yes || result == osrm::tribool::no)
{ {
return std::make_tuple(result, begin); return std::make_tuple(result, begin);
} }
} }
boost::tribool result = boost::indeterminate; osrm::tribool result = osrm::tribool::indeterminate;
return std::make_tuple(result, begin); return std::make_tuple(result, begin);
} }
boost::tribool osrm::tribool
RequestParser::consume(Request &req, char input, http::CompressionType &compression_type) RequestParser::consume(Request &req, char input, http::CompressionType &compression_type)
{ {
switch (state_) switch (state_)
@ -59,119 +59,119 @@ RequestParser::consume(Request &req, char input, http::CompressionType &compress
case method_start: case method_start:
if (!isChar(input) || isCTL(input) || isTSpecial(input)) if (!isChar(input) || isCTL(input) || isTSpecial(input))
{ {
return false; return osrm::tribool::no;
} }
state_ = method; state_ = method;
return boost::indeterminate; return osrm::tribool::indeterminate;
case method: case method:
if (input == ' ') if (input == ' ')
{ {
state_ = uri; state_ = uri;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
if (!isChar(input) || isCTL(input) || isTSpecial(input)) if (!isChar(input) || isCTL(input) || isTSpecial(input))
{ {
return false; return osrm::tribool::no;
} }
return boost::indeterminate; return osrm::tribool::indeterminate;
case uri_start: case uri_start:
if (isCTL(input)) if (isCTL(input))
{ {
return false; return osrm::tribool::no;
} }
state_ = uri; state_ = uri;
req.uri.push_back(input); req.uri.push_back(input);
return boost::indeterminate; return osrm::tribool::indeterminate;
case uri: case uri:
if (input == ' ') if (input == ' ')
{ {
state_ = http_version_h; state_ = http_version_h;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
if (isCTL(input)) if (isCTL(input))
{ {
return false; return osrm::tribool::no;
} }
req.uri.push_back(input); req.uri.push_back(input);
return boost::indeterminate; return osrm::tribool::indeterminate;
case http_version_h: case http_version_h:
if (input == 'H') if (input == 'H')
{ {
state_ = http_version_t_1; state_ = http_version_t_1;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case http_version_t_1: case http_version_t_1:
if (input == 'T') if (input == 'T')
{ {
state_ = http_version_t_2; state_ = http_version_t_2;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case http_version_t_2: case http_version_t_2:
if (input == 'T') if (input == 'T')
{ {
state_ = http_version_p; state_ = http_version_p;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case http_version_p: case http_version_p:
if (input == 'P') if (input == 'P')
{ {
state_ = http_version_slash; state_ = http_version_slash;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case http_version_slash: case http_version_slash:
if (input == '/') if (input == '/')
{ {
state_ = http_version_major_start; state_ = http_version_major_start;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case http_version_major_start: case http_version_major_start:
if (isDigit(input)) if (isDigit(input))
{ {
state_ = http_version_major; state_ = http_version_major;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case http_version_major: case http_version_major:
if (input == '.') if (input == '.')
{ {
state_ = http_version_minor_start; state_ = http_version_minor_start;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
if (isDigit(input)) if (isDigit(input))
{ {
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case http_version_minor_start: case http_version_minor_start:
if (isDigit(input)) if (isDigit(input))
{ {
state_ = http_version_minor; state_ = http_version_minor;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case http_version_minor: case http_version_minor:
if (input == '\r') if (input == '\r')
{ {
state_ = expecting_newline_1; state_ = expecting_newline_1;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
if (isDigit(input)) if (isDigit(input))
{ {
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case expecting_newline_1: case expecting_newline_1:
if (input == '\n') if (input == '\n')
{ {
state_ = header_line_start; state_ = header_line_start;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case header_line_start: case header_line_start:
if (header.name == "Accept-Encoding") if (header.name == "Accept-Encoding")
{ {
@ -199,74 +199,74 @@ RequestParser::consume(Request &req, char input, http::CompressionType &compress
if (input == '\r') if (input == '\r')
{ {
state_ = expecting_newline_3; state_ = expecting_newline_3;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
if (!isChar(input) || isCTL(input) || isTSpecial(input)) if (!isChar(input) || isCTL(input) || isTSpecial(input))
{ {
return false; return osrm::tribool::no;
} }
state_ = header_name; state_ = header_name;
header.Clear(); header.Clear();
header.name.push_back(input); header.name.push_back(input);
return boost::indeterminate; return osrm::tribool::indeterminate;
case header_lws: case header_lws:
if (input == '\r') if (input == '\r')
{ {
state_ = expecting_newline_2; state_ = expecting_newline_2;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
if (input == ' ' || input == '\t') if (input == ' ' || input == '\t')
{ {
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
if (isCTL(input)) if (isCTL(input))
{ {
return false; return osrm::tribool::no;
} }
state_ = header_value; state_ = header_value;
return boost::indeterminate; return osrm::tribool::indeterminate;
case header_name: case header_name:
if (input == ':') if (input == ':')
{ {
state_ = space_before_header_value; state_ = space_before_header_value;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
if (!isChar(input) || isCTL(input) || isTSpecial(input)) if (!isChar(input) || isCTL(input) || isTSpecial(input))
{ {
return false; return osrm::tribool::no;
} }
header.name.push_back(input); header.name.push_back(input);
return boost::indeterminate; return osrm::tribool::indeterminate;
case space_before_header_value: case space_before_header_value:
if (input == ' ') if (input == ' ')
{ {
state_ = header_value; state_ = header_value;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
case header_value: case header_value:
if (input == '\r') if (input == '\r')
{ {
state_ = expecting_newline_2; state_ = expecting_newline_2;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
if (isCTL(input)) if (isCTL(input))
{ {
return false; return osrm::tribool::no;
} }
header.value.push_back(input); header.value.push_back(input);
return boost::indeterminate; return osrm::tribool::indeterminate;
case expecting_newline_2: case expecting_newline_2:
if (input == '\n') if (input == '\n')
{ {
state_ = header_line_start; state_ = header_line_start;
return boost::indeterminate; return osrm::tribool::indeterminate;
} }
return false; return osrm::tribool::no;
default: // expecting_newline_3: default: // expecting_newline_3
return (input == '\n'); return (input == '\n' ? osrm::tribool::yes : osrm::tribool::no);
// default: // default:
// return false; // return osrm::tribool::no;
} }
} }

View File

@ -31,7 +31,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Http/CompressionType.h" #include "Http/CompressionType.h"
#include "Http/Header.h" #include "Http/Header.h"
#include <boost/logic/tribool.hpp> #include "../data_structures/tribool.hpp"
// #include <boost/logic/tribool.hpp>
#include <tuple> #include <tuple>
@ -46,11 +48,11 @@ class RequestParser
RequestParser(); RequestParser();
void Reset(); void Reset();
std::tuple<boost::tribool, char *> std::tuple<osrm::tribool, char *>
Parse(Request &req, char *begin, char *end, CompressionType &compression_type); Parse(Request &req, char *begin, char *end, CompressionType &compression_type);
private: private:
boost::tribool consume(Request &req, char input, CompressionType &compression_type); osrm::tribool consume(Request &req, char input, CompressionType &compression_type);
inline bool isChar(int c); inline bool isChar(int c);