Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser work #48

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions include/boost/http_proto/detail/type_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// Copyright (c) 2023 Vinnie Falco ([email protected])
//
// 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)
//
// Official repository: https://github.com/cppalliance/http_proto
//

#ifndef BOOST_HTTP_PROTO_DETAIL_TYPE_TRAITS_HPP
#define BOOST_HTTP_PROTO_DETAIL_TYPE_TRAITS_HPP

#include <functional>
#include <type_traits>

namespace boost {
namespace http_proto {
namespace detail {

template<class T>
struct remove_cvref
{
using type =
typename std::remove_cv<
typename std::remove_reference<T>::type>::type;
};

template<class T>
using remove_cvref_t = typename remove_cvref<T>::type;

template<class T>
struct is_reference_wrapper_impl
: std::false_type
{
};

template<class T>
struct is_reference_wrapper_impl<
std::reference_wrapper<T>>
: std::true_type
{
};

#if !defined(BOOST_NO_CV_SPECIALIZATIONS)

template<class T>
struct is_reference_wrapper_impl<
std::reference_wrapper<T> const>
: std::true_type
{
};

template<class T>
struct is_reference_wrapper_impl<
std::reference_wrapper<T> volatile>
: std::true_type
{
};

template<class T>
struct is_reference_wrapper_impl<
std::reference_wrapper<T> const volatile>
: std::true_type
{
};

#endif

template<class T>
using is_reference_wrapper =
is_reference_wrapper_impl<remove_cvref_t<T>>;

} // detail
} // http_proto
} // boost

#endif
59 changes: 50 additions & 9 deletions include/boost/http_proto/impl/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@ namespace http_proto {

//------------------------------------------------

template<class DynamicBuffer, class>
typename std::decay<
DynamicBuffer>::type&
template<class ElasticBuffer>
typename std::enable_if<
! detail::is_reference_wrapper<
ElasticBuffer>::value &&
! is_sink<ElasticBuffer>::value>::type
parser::
set_body(
DynamicBuffer&& b)
ElasticBuffer&& eb)
{
// If this goes off it means you are trying
// to pass by lvalue reference. Use std::ref
// instead.
static_assert(
! std::is_reference<ElasticBuffer>::value,
"Use std::ref instead of pass-by-reference");

// Check ElasticBuffer type requirements
static_assert(
buffers::is_dynamic_buffer<ElasticBuffer>::value,
"Type requirements not met.");

// body must not be set already
if(how_ != how::in_place)
detail::throw_logic_error();
Expand All @@ -34,13 +48,40 @@ set_body(

auto& dyn = ws_.push(
buffers::any_dynamic_buffer_impl<typename
std::decay<DynamicBuffer>::type,
std::decay<ElasticBuffer>::type,
buffers_N>(std::forward<
DynamicBuffer>(b)));
dyn_ = &dyn;
how_ = how::dynamic;
ElasticBuffer>(eb)));
eb_ = &dyn;
how_ = how::elastic;
on_set_body();
}

template<class ElasticBuffer>
void
parser::
set_body(
std::reference_wrapper<ElasticBuffer> eb)
{
// Check ElasticBuffer type requirements
static_assert(
buffers::is_dynamic_buffer<ElasticBuffer>::value,
"Type requirements not met.");

// body must not be set already
if(how_ != how::in_place)
detail::throw_logic_error();

// headers must be complete
if(! got_header())
detail::throw_logic_error();

auto& dyn = ws_.push(
buffers::any_dynamic_buffer_impl<typename
std::decay<ElasticBuffer>::type&,
buffers_N>(eb));
eb_ = &dyn;
how_ = how::elastic;
on_set_body();
return dyn.buffer();
}

//------------------------------------------------
Expand Down
61 changes: 34 additions & 27 deletions include/boost/http_proto/impl/parser.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ parser(
parser_service>())
, h_(detail::empty{k})
, st_(state::reset)
, eb_(nullptr)
{
auto const n =
svc_.space_needed;
Expand All @@ -249,6 +250,12 @@ void
parser::
reset() noexcept
{
if(eb_)
{
eb_->~any_dynamic_buffer();
eb_ = nullptr;
}

ws_.clear();
st_ = state::start;
got_eof_ = false;
Expand Down Expand Up @@ -404,7 +411,7 @@ prepare() ->
return mutable_buffers_type(mbp_);
}

if(how_ == how::dynamic)
if(how_ == how::elastic)
{
// Overreads are not allowed, or
// else the caller will see extra
Expand All @@ -419,7 +426,7 @@ prepare() ->
if( n > svc_.cfg.max_prepare)
n = svc_.cfg.max_prepare;
nprepare_ = n;
return dyn_->prepare(n);
return eb_->prepare(n);
}

BOOST_ASSERT(
Expand All @@ -434,17 +441,17 @@ prepare() ->
{
// apply max_size()
auto avail =
dyn_->max_size() -
dyn_->size();
eb_->max_size() -
eb_->size();
if( n > avail)
n = avail;
}
// fill capacity() first,
// to avoid an allocation
{
auto avail =
dyn_->capacity() -
dyn_->size();
eb_->capacity() -
eb_->size();
if( n > avail &&
avail != 0)
n = avail;
Expand All @@ -466,7 +473,7 @@ prepare() ->
}
}
nprepare_ = n;
return dyn_->prepare(n);
return eb_->prepare(n);
}

// VFALCO TODO
Expand All @@ -481,7 +488,7 @@ prepare() ->
{
BOOST_ASSERT(is_plain());

if(how_ == how::dynamic)
if(how_ == how::elastic)
{
// attempt to transfer in-place
// body into the dynamic buffer.
Expand Down Expand Up @@ -604,14 +611,14 @@ commit(
break;
}

if(how_ == how::dynamic)
if(how_ == how::elastic)
{
if(dyn_->size() < dyn_->max_size())
if(eb_->size() < eb_->max_size())
{
BOOST_ASSERT(body_avail_ == 0);
BOOST_ASSERT(
body_buf_->size() == 0);
dyn_->commit(n);
eb_->commit(n);
}
else
{
Expand Down Expand Up @@ -659,7 +666,7 @@ commit(

BOOST_ASSERT(is_plain());
BOOST_ASSERT(n == 0);
if( how_ == how::dynamic ||
if( how_ == how::elastic ||
how_ == how::sink)
{
// intended no-op
Expand Down Expand Up @@ -865,7 +872,7 @@ parse(
break;
}

if(how_ == how::dynamic)
if(how_ == how::elastic)
{
// state already updated in commit
if(h_.md.payload == payload::size)
Expand All @@ -876,8 +883,8 @@ parse(
if(body_avail_ != 0)
{
BOOST_ASSERT(
dyn_->max_size() -
dyn_->size() <
eb_->max_size() -
eb_->size() <
payload_remain_);
ec = BOOST_HTTP_PROTO_ERR(
error::buffer_overflow);
Expand All @@ -895,7 +902,7 @@ parse(
}
BOOST_ASSERT(
h_.md.payload == payload::to_eof);
if( dyn_->size() == dyn_->max_size() &&
if( eb_->size() == eb_->max_size() &&
body_avail_ > 0)
{
// got here from the 1-byte read
Expand Down Expand Up @@ -924,7 +931,7 @@ parse(

// transfer in_place data into set body

if(how_ == how::dynamic)
if(how_ == how::elastic)
{
init_dynamic(ec);
if(! ec.failed())
Expand Down Expand Up @@ -1003,13 +1010,13 @@ parse(
case how::in_place:
break;

case how::dynamic:
case how::elastic:
{
if(body_buf_->size() == 0)
break;
BOOST_ASSERT(dyn_->size() == 0);
BOOST_ASSERT(eb_->size() == 0);
auto n = buffers::buffer_copy(
dyn_->prepare(
eb_->prepare(
body_buf_->size()),
body_buf_->data());
body_buf_->consume(n);
Expand Down Expand Up @@ -1245,7 +1252,7 @@ on_set_body()

nprepare_ = 0; // invalidate

if(how_ == how::dynamic)
if(how_ == how::elastic)
{
if(h_.md.payload == payload::none)
{
Expand Down Expand Up @@ -1288,7 +1295,7 @@ init_dynamic(
BOOST_ASSERT(
body_total_ == body_avail_);
auto const space_left =
dyn_->max_size() - dyn_->size();
eb_->max_size() - eb_->size();

if(h_.md.payload == payload::size)
{
Expand All @@ -1299,14 +1306,14 @@ init_dynamic(
return;
}
// reserve the full size
dyn_->prepare(h_.md.payload_size);
eb_->prepare(h_.md.payload_size);
// transfer in-place body
auto n = body_avail_;
if( n > h_.md.payload_size)
n = h_.md.payload_size;
dyn_->commit(
eb_->commit(
buffers::buffer_copy(
dyn_->prepare(n),
eb_->prepare(n),
body_buf_->data()));
BOOST_ASSERT(body_avail_ == n);
BOOST_ASSERT(body_total_ == n);
Expand Down Expand Up @@ -1334,9 +1341,9 @@ init_dynamic(
error::buffer_overflow);
return;
}
dyn_->commit(
eb_->commit(
buffers::buffer_copy(
dyn_->prepare(body_avail_),
eb_->prepare(body_avail_),
body_buf_->data()));
body_buf_->consume(body_avail_);
body_avail_ = 0;
Expand Down
Loading
Loading