Files
boost_beast/include/boost/beast/http/detail/basic_parser.hpp
T

272 lines
5.8 KiB
C++
Raw Normal View History

2016-11-20 07:32:41 -05:00
//
2019-02-21 07:00:31 -08:00
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-11-20 07:32:41 -05:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2016-11-20 07:32:41 -05:00
2017-07-20 13:40:34 -07:00
#ifndef BOOST_BEAST_HTTP_DETAIL_BASIC_PARSER_HPP
#define BOOST_BEAST_HTTP_DETAIL_BASIC_PARSER_HPP
2016-11-20 07:32:41 -05:00
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/static_string.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/core/detail/cpu_info.hpp>
#include <boost/beast/http/error.hpp>
#include <boost/beast/http/detail/rfc7230.hpp>
2017-06-30 08:46:22 -07:00
#include <boost/config.hpp>
2016-11-20 07:32:41 -05:00
#include <boost/version.hpp>
2017-06-26 15:32:35 -07:00
#include <algorithm>
2016-11-20 07:32:41 -05:00
#include <cstddef>
2018-07-30 01:28:48 +03:00
#include <limits>
2016-11-20 07:32:41 -05:00
#include <utility>
2017-07-20 13:40:34 -07:00
namespace boost {
2016-11-20 07:32:41 -05:00
namespace beast {
namespace http {
namespace detail {
2018-07-30 01:28:48 +03:00
struct basic_parser_base
2016-11-20 07:32:41 -05:00
{
2017-06-30 08:46:22 -07:00
// limit on the size of the obs-fold buffer
//
// https://stackoverflow.com/questions/686217/maximum-on-http-header-values
//
static std::size_t constexpr max_obs_fold = 4096;
2018-10-15 00:30:19 +03:00
template<class T>
struct is_unsigned_integer:
std::integral_constant<bool,
std::numeric_limits<T>::is_integer &&
! std::numeric_limits<T>::is_signed> {};
enum class state
{
nothing_yet = 0,
2017-06-30 08:46:22 -07:00
start_line,
fields,
body0,
body,
body_to_eof0,
body_to_eof,
chunk_header0,
chunk_header,
chunk_body,
complete
};
2016-11-20 07:32:41 -05:00
static
bool
is_digit(char c)
{
return static_cast<unsigned char>(c-'0') < 10;
}
static
bool
is_print(char c)
{
2017-06-30 08:46:22 -07:00
return static_cast<unsigned char>(c-32) < 95;
}
template<class FwdIt>
static
FwdIt
trim_front(FwdIt it, FwdIt const& end)
{
while(it != end)
{
if(*it != ' ' && *it != '\t')
break;
++it;
}
return it;
}
template<class RanIt>
static
RanIt
trim_back(
RanIt it, RanIt const& first)
{
while(it != first)
{
auto const c = it[-1];
if(c != ' ' && c != '\t')
break;
--it;
}
return it;
2016-11-20 07:32:41 -05:00
}
static
2017-05-05 14:45:15 -07:00
string_view
2016-11-20 07:32:41 -05:00
make_string(char const* first, char const* last)
{
return {first, static_cast<
std::size_t>(last - first)};
}
2017-06-30 08:46:22 -07:00
//--------------------------------------------------------------------------
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
static
bool
is_pathchar(char c);
BOOST_BEAST_DECL
static
bool
unhex(unsigned char& d, char c);
BOOST_BEAST_DECL
2017-07-12 18:06:36 -07:00
static
2017-06-30 08:46:22 -07:00
std::pair<char const*, bool>
find_fast(
char const* buf,
char const* buf_end,
char const* ranges,
2019-03-02 08:57:26 -08:00
size_t ranges_size);
2016-11-20 07:32:41 -05:00
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
2017-06-30 08:46:22 -07:00
static
char const*
find_eol(
char const* it, char const* last,
2019-03-02 08:57:26 -08:00
error_code& ec);
2017-06-30 08:46:22 -07:00
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
2017-06-30 08:46:22 -07:00
static
char const*
2019-03-02 08:57:26 -08:00
find_eom(char const* p, char const* last);
2017-06-30 08:46:22 -07:00
//--------------------------------------------------------------------------
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
2017-07-12 18:06:36 -07:00
static
2017-06-30 08:46:22 -07:00
char const*
parse_token_to_eol(
char const* p,
char const* last,
char const*& token_last,
2019-03-02 08:57:26 -08:00
error_code& ec);
2016-11-20 07:32:41 -05:00
2018-07-30 01:28:48 +03:00
template<class Iter, class T>
2016-11-20 07:32:41 -05:00
static
2018-10-15 00:30:19 +03:00
typename std::enable_if<is_unsigned_integer<T>::value, bool>::type
2018-07-30 01:28:48 +03:00
parse_dec(Iter it, Iter last, T& v)
2016-11-20 07:32:41 -05:00
{
2018-07-30 01:28:48 +03:00
if(it == last)
2016-11-20 07:32:41 -05:00
return false;
2018-07-30 01:28:48 +03:00
T tmp = 0;
do
2016-11-20 07:32:41 -05:00
{
2018-07-30 01:28:48 +03:00
if((! is_digit(*it)) ||
tmp > (std::numeric_limits<T>::max)() / 10)
2016-11-20 07:32:41 -05:00
return false;
2018-07-30 01:28:48 +03:00
tmp *= 10;
T const d = *it - '0';
if((std::numeric_limits<T>::max)() - tmp < d)
return false;
tmp += d;
2016-11-20 07:32:41 -05:00
}
2018-07-30 01:28:48 +03:00
while(++it != last);
v = tmp;
return true;
2016-11-20 07:32:41 -05:00
}
2018-10-15 00:30:19 +03:00
template<class Iter, class T>
2017-07-12 18:06:36 -07:00
static
2018-10-15 00:30:19 +03:00
typename std::enable_if<is_unsigned_integer<T>::value, bool>::type
parse_hex(Iter& it, T& v)
2016-11-20 07:32:41 -05:00
{
unsigned char d;
if(! unhex(d, *it))
return false;
2018-10-15 00:30:19 +03:00
T tmp = 0;
do
2016-11-20 07:32:41 -05:00
{
2018-10-15 00:30:19 +03:00
if(tmp > (std::numeric_limits<T>::max)() / 16)
2016-11-20 07:32:41 -05:00
return false;
2018-10-15 00:30:19 +03:00
tmp *= 16;
if((std::numeric_limits<T>::max)() - tmp < d)
return false;
tmp += d;
2016-11-20 07:32:41 -05:00
}
2018-10-15 00:30:19 +03:00
while(unhex(d, *++it));
v = tmp;
2016-11-20 07:32:41 -05:00
return true;
}
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
2016-11-20 07:32:41 -05:00
static
bool
2019-03-02 08:57:26 -08:00
parse_crlf(char const*& it);
2016-11-20 07:32:41 -05:00
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
2016-11-20 07:32:41 -05:00
static
2017-06-30 08:46:22 -07:00
void
parse_method(
char const*& it, char const* last,
2019-03-02 08:57:26 -08:00
string_view& result, error_code& ec);
2016-11-20 07:32:41 -05:00
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
2016-11-20 07:32:41 -05:00
static
2017-06-30 08:46:22 -07:00
void
parse_target(
char const*& it, char const* last,
2019-03-02 08:57:26 -08:00
string_view& result, error_code& ec);
2016-11-20 07:32:41 -05:00
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
2016-11-20 07:32:41 -05:00
static
2017-06-30 08:46:22 -07:00
void
parse_version(
char const*& it, char const* last,
2019-03-02 08:57:26 -08:00
int& result, error_code& ec);
2017-06-30 08:46:22 -07:00
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
2017-06-30 08:46:22 -07:00
static
void
parse_status(
char const*& it, char const* last,
2019-03-02 08:57:26 -08:00
unsigned short& result, error_code& ec);
2016-11-20 07:32:41 -05:00
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
static
2017-06-30 08:46:22 -07:00
void
parse_reason(
char const*& it, char const* last,
2019-03-02 08:57:26 -08:00
string_view& result, error_code& ec);
2016-11-20 07:32:41 -05:00
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
static
2017-06-30 08:46:22 -07:00
void
parse_field(
char const*& p,
char const* last,
string_view& name,
string_view& value,
2019-03-02 08:57:26 -08:00
static_string<max_obs_fold>& buf,
error_code& ec);
2017-07-12 18:06:36 -07:00
2019-03-02 08:57:26 -08:00
BOOST_BEAST_DECL
static
2017-07-12 18:06:36 -07:00
void
parse_chunk_extensions(
char const*& it,
char const* last,
2019-03-02 08:57:26 -08:00
error_code& ec);
2016-11-20 07:32:41 -05:00
};
} // detail
} // http
} // beast
2017-07-20 13:40:34 -07:00
} // boost
2016-11-20 07:32:41 -05:00
2019-03-02 08:57:26 -08:00
#ifdef BOOST_BEAST_HEADER_ONLY
#include <boost/beast/http/detail/basic_parser.ipp>
#endif
2016-11-20 07:32:41 -05:00
#endif