Refactor chunked-encoding serialization:

New buffer sequence classes are provided to allow full
control over the serialization of chunk-encoded message
payloads:

* chunk_header

    A ConstBufferSequence representing the chunk header.
    It includes a hexadecimal-encoded size, an optional
    set of chunk extensions, and the trailing CRLF
    required to denote the end of the chunk header.

    This allows the caller to manually serialize the chunk
    body in one or more calls to a stream output function.
    The caller must also output an object of type `chunk_crlf`
    to the stream to indicate the end of the chunk body.

* chunk_crlf

    A small ConstBufferSequence that simply represents
    the two character sequence "\r\n" (CRLF). This is needed
    for the case where the caller wants to output a chunk
    body as a series of buffers (i.e. "chunking a chunk").

* chunk_body

    A ConstBufferSequence representing a complete chunk.
    This includes the size, an optional set of chunk extensions,
    a caller provided buffer containing the body, and the
    required CRLF that follows.

* chunk_final

    A ConstBufferSequence representing a final chunk. It
    includes an optional set of caller-provided field trailers

* chunk_extensions

    A container for building a set of chunk extensions to use
    during serialization. The use of the container is optional,
    callers may provide their own buffer containing a correctly
    formatted set of chunk extensions, or they may use their
    own convenience container which meets the requirements.

The basic_fields container is modified to allow construction
outside the context of a message. The container can be used
to provide trailers to `chunk_final`.

Actions Required:

* Remove references to ChunkDecorators. Use the new chunk-encoding
  buffer sequences to manually produce a chunked payload body in
  the case where control over the chunk-extensions and/or trailers
  is required.
This commit is contained in:
Vinnie Falco committed 2017-07-20 08:15:31 -07:00
1 parent 02de2e5cbb
commit ff15cf8688
69 files changed
+2312 -686

No files matched your search

+1
View File
@@ -13,6 +13,7 @@
#include <beast/http/basic_dynamic_body.hpp>
#include <beast/http/basic_parser.hpp>
#include <beast/http/buffer_body.hpp>
#include <beast/http/chunk_encode.hpp>
#include <beast/http/dynamic_body.hpp>
#include <beast/http/empty_body.hpp>
#include <beast/http/error.hpp>
+698
View File
@@ -0,0 +1,698 @@
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
#ifndef BEAST_HTTP_CHUNK_ENCODE_HPP
#define BEAST_HTTP_CHUNK_ENCODE_HPP
#include <beast/config.hpp>
#include <beast/core/buffer_cat.hpp>
#include <beast/core/string.hpp>
#include <beast/http/type_traits.hpp>
#include <beast/http/detail/chunk_encode.hpp>
#include <boost/asio/buffer.hpp>
#include <memory>
#include <type_traits>
namespace beast {
namespace http {
/** A chunked encoding crlf
This implements a @b ConstBufferSequence holding the CRLF
(`"\r\n"`) used as a delimiter in a @em chunk.
To use this class, pass an instance of it to a
stream algorithm as the buffer sequence:
@code
// writes "\r\n"
boost::asio::write(stream, chunk_crlf{});
@endcode
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
struct chunk_crlf
{
/// Constructor
chunk_crlf() = default;
//-----
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using value_type = implementation_defined;
#else
using value_type = detail::chunk_crlf_iter::value_type;
#endif
/// Required for @b ConstBufferSequence
using const_iterator = value_type const*;
/// Required for @b ConstBufferSequence
chunk_crlf(chunk_crlf const&) = default;
/// Required for @b ConstBufferSequence
const_iterator
begin() const
{
return &detail::chunk_crlf_iter::value;
}
/// Required for @b ConstBufferSequence
const_iterator
end() const
{
return begin() + 1;
}
};
//------------------------------------------------------------------------------
/** A @em chunk header
This implements a @b ConstBufferSequence representing the
header of a @em chunk. The serialized format is as follows:
@code
chunk-header = 1*HEXDIG chunk-ext CRLF
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
@endcode
The chunk extension is optional. After the header and
chunk body have been serialized, it is the callers
responsibility to also serialize the final CRLF (`"\r\n"`).
This class allows the caller to emit piecewise chunk bodies,
by first serializing the chunk header using this class and then
serializing the chunk body in a series of one or more calls to
a stream write operation.
To use this class, pass an instance of it to a
stream algorithm as the buffer sequence:
@code
// writes "400;x\r\n"
boost::asio::write(stream, chunk_header{1024, "x"});
@endcode
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
class chunk_header
{
using view_type = buffer_cat_view<
detail::chunk_size, // chunk-size
boost::asio::const_buffers_1, // chunk-extensions
chunk_crlf>; // CRLF
std::shared_ptr<
detail::chunk_extensions> exts_;
view_type view_;
public:
/** Constructor
This constructs a buffer sequence representing a
@em chunked-body size and terminating CRLF (`"\r\n"`)
with no chunk extensions.
@param size The size of the chunk body that follows.
The value must be greater than zero.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
explicit
chunk_header(std::size_t size);
/** Constructor
This constructs a buffer sequence representing a
@em chunked-body size and terminating CRLF (`"\r\n"`)
with provided chunk extensions.
@param size The size of the chunk body that follows.
The value must be greater than zero.
@param extensions The chunk extensions string. This
string must be formatted correctly as per rfc7230,
using this BNF syntax:
@code
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
@endcode
The data pointed to by this string view must remain
valid for the lifetime of any operations performed on
the object.
@see https://tools.ietf.org/html/rfc7230#section-4.1.1
*/
chunk_header(
std::size_t size,
string_view extensions);
/** Constructor
This constructs a buffer sequence representing a
@em chunked-body size and terminating CRLF (`"\r\n"`)
with provided chunk extensions.
The default allocator is used to provide storage for the
extensions object.
@param size The size of the chunk body that follows.
The value must be greater than zero.
@param extensions The chunk extensions object. The expression
`extensions.str()` must be valid, and the return type must
be convertible to @ref string_view. This object will be copied
or moved as needed to ensure that the chunk header object retains
ownership of the buffers provided by the chunk extensions object.
@note This function participates in overload resolution only
if @b ChunkExtensions meets the requirements stated above.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ChunkExtensions
#if ! BEAST_DOXYGEN
, class = typename std::enable_if<
detail::is_chunk_extensions<
ChunkExtensions>::value>::type
#endif
>
chunk_header(
std::size_t size,
ChunkExtensions&& extensions);
/** Constructor
This constructs a buffer sequence representing a
@em chunked-body size and terminating CRLF (`"\r\n"`)
with provided chunk extensions.
The specified allocator is used to provide storage for the
extensions object.
@param size The size of the chunk body that follows.
The value be greater than zero.
@param extensions The chunk extensions object. The expression
`extensions.str()` must be valid, and the return type must
be convertible to @ref string_view. This object will be copied
or moved as needed to ensure that the chunk header object retains
ownership of the buffers provided by the chunk extensions object.
@param allocator The allocator to provide storage for the moved
or copied extensions object.
@note This function participates in overload resolution only
if @b ChunkExtensions meets the requirements stated above.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ChunkExtensions, class Allocator
#if ! BEAST_DOXYGEN
, class = typename std::enable_if<
detail::is_chunk_extensions<
ChunkExtensions>::value>::type
#endif
>
chunk_header(
std::size_t size,
ChunkExtensions&& extensions,
Allocator const& allocator);
//-----
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using value_type = implementation_defined;
#else
using value_type = typename view_type::value_type;
#endif
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using const_iterator = implementation_defined;
#else
using const_iterator = typename view_type::const_iterator;
#endif
/// Required for @b ConstBufferSequence
chunk_header(chunk_header const&) = default;
/// Required for @b ConstBufferSequence
const_iterator
begin() const
{
return view_.begin();
}
/// Required for @b ConstBufferSequence
const_iterator
end() const
{
return view_.end();
}
};
//------------------------------------------------------------------------------
/** A @em chunk
This implements a @b ConstBufferSequence representing
a @em chunk. The serialized format is as follows:
@code
chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
chunk-size = 1*HEXDIG
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
chunk-data = 1*OCTET ; a sequence of chunk-size octets
@endcode
The chunk extension is optional.
To use this class, pass an instance of it to a
stream algorithm as the buffer sequence.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ConstBufferSequence>
class chunk_body
{
using view_type = buffer_cat_view<
detail::chunk_size, // chunk-size
boost::asio::const_buffers_1, // chunk-extensions
chunk_crlf, // CRLF
ConstBufferSequence, // chunk-body
chunk_crlf>; // CRLF
std::shared_ptr<
detail::chunk_extensions> exts_;
view_type view_;
public:
/** Constructor
This constructs buffers representing a complete @em chunk
with no chunk extensions and having the size and contents
of the specified buffer sequence.
@param buffers A buffer sequence representing the chunk
body. Although the buffers object may be copied as necessary,
ownership of the underlying memory blocks is retained by the
caller, which must guarantee that they remain valid while this
object is in use.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
explicit
chunk_body(
ConstBufferSequence const& buffers);
/** Constructor
This constructs buffers representing a complete @em chunk
with the passed chunk extensions and having the size and
contents of the specified buffer sequence.
@param buffers A buffer sequence representing the chunk
body. Although the buffers object may be copied as necessary,
ownership of the underlying memory blocks is retained by the
caller, which must guarantee that they remain valid while this
object is in use.
@param extensions The chunk extensions string. This
string must be formatted correctly as per rfc7230,
using this BNF syntax:
@code
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
@endcode
The data pointed to by this string view must remain
valid for the lifetime of any operations performed on
the object.
@see https://tools.ietf.org/html/rfc7230#section-4.1.1
*/
chunk_body(
ConstBufferSequence const& buffers,
string_view extensions);
/** Constructor
This constructs buffers representing a complete @em chunk
with the passed chunk extensions and having the size and
contents of the specified buffer sequence.
The default allocator is used to provide storage for the
extensions object.
@param buffers A buffer sequence representing the chunk
body. Although the buffers object may be copied as necessary,
ownership of the underlying memory blocks is retained by the
caller, which must guarantee that they remain valid while this
object is in use.
@param extensions The chunk extensions object. The expression
`extensions.str()` must be valid, and the return type must
be convertible to @ref string_view. This object will be copied
or moved as needed to ensure that the chunk header object retains
ownership of the buffers provided by the chunk extensions object.
@note This function participates in overload resolution only
if @b ChunkExtensions meets the requirements stated above.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ChunkExtensions
#if ! BEAST_DOXYGEN
, class = typename std::enable_if<
! std::is_convertible<typename std::decay<
ChunkExtensions>::type, string_view>::value>::type
#endif
>
chunk_body(
ConstBufferSequence const& buffers,
ChunkExtensions&& extensions);
/** Constructor
This constructs buffers representing a complete @em chunk
with the passed chunk extensions and having the size and
contents of the specified buffer sequence.
The specified allocator is used to provide storage for the
extensions object.
@param buffers A buffer sequence representing the chunk
body. Although the buffers object may be copied as necessary,
ownership of the underlying memory blocks is retained by the
caller, which must guarantee that they remain valid while this
object is in use.
@param extensions The chunk extensions object. The expression
`extensions.str()` must be valid, and the return type must
be convertible to @ref string_view. This object will be copied
or moved as needed to ensure that the chunk header object retains
ownership of the buffers provided by the chunk extensions object.
@param allocator The allocator to provide storage for the moved
or copied extensions object.
@note This function participates in overload resolution only
if @b ChunkExtensions meets the requirements stated above.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ChunkExtensions, class Allocator
#if ! BEAST_DOXYGEN
, class = typename std::enable_if<
! std::is_convertible<typename std::decay<
ChunkExtensions>::type, string_view>::value>::type
#endif
>
chunk_body(
ConstBufferSequence const& buffers,
ChunkExtensions&& extensions,
Allocator const& allocator);
//-----
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using value_type = implementation_defined;
#else
using value_type = typename view_type::value_type;
#endif
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using const_iterator = implementation_defined;
#else
using const_iterator = typename view_type::const_iterator;
#endif
/// Required for @b ConstBufferSequence
const_iterator
begin() const
{
return view_.begin();
}
/// Required for @b ConstBufferSequence
const_iterator
end() const
{
return view_.end();
}
};
//------------------------------------------------------------------------------
/** A chunked-encoding last chunk
*/
template<class Trailer = chunk_crlf>
class chunk_last
{
static_assert(
is_fields<Trailer>::value ||
is_const_buffer_sequence<Trailer>::value,
"Trailer requirements not met");
using buffers_type = typename
detail::buffers_or_fields<Trailer>::type;
using view_type =
buffer_cat_view<
detail::chunk_size0, // "0\r\n"
buffers_type>; // Trailer (includes CRLF)
template<class Allocator>
buffers_type
prepare(Trailer const& trailer, Allocator const& alloc);
buffers_type
prepare(Trailer const& trailer, std::true_type);
buffers_type
prepare(Trailer const& trailer, std::false_type);
std::shared_ptr<void> sp_;
view_type view_;
public:
/** Constructor
The last chunk will have an empty trailer
*/
chunk_last();
/** Constructor
@param trailer The trailer to use. This may be
a type meeting the requirements of either Fields
or ConstBufferSequence. If it is a ConstBufferSequence,
the trailer must be formatted correctly as per rfc7230
including a CRLF on its own line to denote the end
of the trailer.
*/
explicit
chunk_last(Trailer const& trailer);
/** Constructor
@param trailer The trailer to use. This type must
meet the requirements of Fields.
@param allocator The allocator to use for storing temporary
data associated with the serialized trailer buffers.
*/
#if BEAST_DOXYGEN
template<class Allocator>
chunk_last(Trailer const& trailer, Allocator const& allocator);
#else
template<class DeducedTrailer, class Allocator,
class = typename std::enable_if<
is_fields<DeducedTrailer>::value>::type>
chunk_last(
DeducedTrailer const& trailer, Allocator const& allocator);
#endif
//-----
/// Required for @b ConstBufferSequence
chunk_last(chunk_last const&) = default;
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using value_type = implementation_defined;
#else
using value_type =
typename view_type::value_type;
#endif
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using const_iterator = implementation_defined;
#else
using const_iterator =
typename view_type::const_iterator;
#endif
/// Required for @b ConstBufferSequence
const_iterator
begin() const
{
return view_.begin();
}
/// Required for @b ConstBufferSequence
const_iterator
end() const
{
return view_.end();
}
};
//------------------------------------------------------------------------------
/** A set of chunk extensions
This container stores a set of chunk extensions suited
for use with @ref chunk_header and @ref chunk_body.
Meets the requirements of ChunkExtensions
*/
template<class Allocator>
class basic_chunk_extensions
{
std::basic_string<char,
std::char_traits<char>, Allocator> s_;
public:
/// Constructor
basic_chunk_extensions() = default;
/// Constructor
basic_chunk_extensions(basic_chunk_extensions&&) = default;
/// Constructor
basic_chunk_extensions(basic_chunk_extensions const&) = default;
/** Constructor
@param allocator The allocator to use for storing the serialized extension
*/
explicit
basic_chunk_extensions(Allocator const& allocator)
: s_(allocator)
{
}
/** Clear the chunk extensions
This preserves the capacity of the internal string
used to hold the serialized representation.
*/
void
clear()
{
s_.clear();
}
/** Insert an extension name with an empty value
@param name The name of the extension
*/
void
insert(string_view name);
/** Insert an extension value
@param name The name of the extension
@param value The value to insert. Depending on the
contents, the serialized extension may use a quoted string.
*/
void
insert(string_view name, string_view value);
/// Return the serialized representation of the chunk extension
string_view
str() const
{
return s_;
}
};
//------------------------------------------------------------------------------
/// A set of chunk extensions
using chunk_extensions =
basic_chunk_extensions<std::allocator<char>>;
/** Returns a @ref chunk_body
This functions constructs and returns a complete
@ref chunk_body for a chunk body represented by the
specified buffer sequence.
@param buffers The buffers representing the chunk body.
@param args Optional arguments passed to the @ref chunk_body constructor.
@note This function is provided as a notational convenience
to omit specification of the class template arguments.
*/
template<class ConstBufferSequence, class... Args>
auto
make_chunk(
ConstBufferSequence const& buffers,
Args&&... args) ->
chunk_body<ConstBufferSequence>
{
return chunk_body<ConstBufferSequence>(
buffers, std::forward<Args>(args)...);
}
/** Returns a @ref chunk_last
@note This function is provided as a notational convenience
to omit specification of the class template arguments.
*/
inline
chunk_last<chunk_crlf>
make_chunk_last()
{
return chunk_last<chunk_crlf>{};
}
/** Returns a @ref chunk_last
This function construct and returns a complete
@ref chunk_last for a last chunk containing the
specified trailers.
@param trailer A ConstBufferSequence or
@note This function is provided as a notational convenience
to omit specification of the class template arguments.
@param args Optional arguments passed to the @ref chunk_last
constructor.
*/
template<class Trailer, class... Args>
chunk_last<Trailer>
make_chunk_last(
Trailer const& trailer,
Args&&... args)
{
return chunk_last<Trailer>{
trailer, std::forward<Args>(args)...};
}
} // http
} // beast
#include <beast/http/impl/chunk_encode.ipp>
#endif
+140 -7
View File
@@ -8,6 +8,8 @@
#ifndef BEAST_HTTP_DETAIL_CHUNK_ENCODE_HPP
#define BEAST_HTTP_DETAIL_CHUNK_ENCODE_HPP
#include <beast/core/type_traits.hpp>
#include <beast/http/type_traits.hpp>
#include <boost/asio/buffer.hpp>
#include <algorithm>
#include <array>
@@ -17,15 +19,56 @@ namespace beast {
namespace http {
namespace detail {
struct chunk_extensions
{
virtual ~chunk_extensions() = default;
virtual boost::asio::const_buffers_1 str() = 0;
};
template<class ChunkExtensions>
struct chunk_extensions_impl : chunk_extensions
{
ChunkExtensions ext_;
chunk_extensions_impl(ChunkExtensions&& ext)
: ext_(std::move(ext))
{
}
chunk_extensions_impl(ChunkExtensions const& ext)
: ext_(ext)
{
}
boost::asio::const_buffers_1
str() override
{
auto const s = ext_.str();
return {s.data(), s.size()};
}
};
template<class T, class = void>
struct is_chunk_extensions : std::false_type {};
template<class T>
struct is_chunk_extensions<T, beast::detail::void_t<decltype(
std::declval<string_view&>() = std::declval<T&>().str(),
(void)0)>> : std::true_type
{
};
//------------------------------------------------------------------------------
/** A buffer sequence containing a chunk-encoding header
*/
class chunk_header
class chunk_size
{
public:
// Storage for the longest hex string we might need
class value_type
{
friend class chunk_header;
friend class chunk_size;
// First byte holds the length
char buf_[1 + 2 * sizeof(std::size_t)];
@@ -63,14 +106,13 @@ public:
using const_iterator = value_type const*;
chunk_header(chunk_header const& other) = default;
chunk_size(chunk_size const& other) = default;
/** Construct a chunk header
@param n The number of octets in this chunk.
*/
explicit
chunk_header(std::size_t n)
chunk_size(std::size_t n)
{
value_.prepare(n);
}
@@ -93,7 +135,7 @@ private:
template<class>
void
chunk_header::
chunk_size::
value_type::
prepare(std::size_t n)
{
@@ -102,6 +144,8 @@ prepare(std::size_t n)
buf_[0] = static_cast<char>(last - it);
}
//------------------------------------------------------------------------------
/// Returns a buffer sequence holding a CRLF for chunk encoding
inline
boost::asio::const_buffers_1
@@ -113,11 +157,100 @@ chunk_crlf()
/// Returns a buffer sequence holding a final chunk header
inline
boost::asio::const_buffers_1
chunk_final()
chunk_last()
{
return {"0\r\n", 3};
}
//------------------------------------------------------------------------------
template<class = void>
struct chunk_crlf_iter_type
{
class value_type
{
char const s[2] = {'\r', '\n'};
public:
value_type() = default;
operator
boost::asio::const_buffer() const
{
return {s, sizeof(s)};
}
};
static value_type value;
};
template<class T>
typename chunk_crlf_iter_type<T>::value_type
chunk_crlf_iter_type<T>::value;
using chunk_crlf_iter = chunk_crlf_iter_type<void>;
//------------------------------------------------------------------------------
template<class = void>
struct chunk_size0_iter_type
{
class value_type
{
char const s[3] = {'0', '\r', '\n'};
public:
value_type() = default;
operator
boost::asio::const_buffer() const
{
return {s, sizeof(s)};
}
};
static value_type value;
};
template<class T>
typename chunk_size0_iter_type<T>::value_type
chunk_size0_iter_type<T>::value;
using chunk_size0_iter = chunk_size0_iter_type<void>;
struct chunk_size0
{
using value_type = chunk_size0_iter::value_type;
using const_iterator = value_type const*;
const_iterator
begin() const
{
return &chunk_size0_iter::value;
}
const_iterator
end() const
{
return begin() + 1;
}
};
//------------------------------------------------------------------------------
template<class T,
bool = is_fields<T>::value>
struct buffers_or_fields
{
using type = typename
T::reader::const_buffers_type;
};
template<class T>
struct buffers_or_fields<T, false>
{
using type = T;
};
} // detail
} // http
} // beast
+13 -3
View File
@@ -48,6 +48,8 @@ namespace http {
template<class Allocator>
class basic_fields
{
friend class fields_test; // for `header`
static std::size_t constexpr max_static_buffer = 4096;
using off_t = std::uint16_t;
@@ -162,9 +164,7 @@ private:
boost::intrusive::compare<key_compare>>::type;
protected:
friend class fields_test; // for `header`
public:
/// Destructor
~basic_fields();
@@ -481,6 +481,16 @@ public:
std::size_t
erase(string_view name);
/** Return a buffer sequence representing the trailers.
This function returns a buffer sequence holding the
serialized representation of the trailer fields promised
in the Accept field. Before calling this function the
Accept field must contain the exact trailer fields
desired. Each field must also exist.
*/
/// Swap this container with another
void
swap(basic_fields& other);
+283
View File
@@ -0,0 +1,283 @@
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
#ifndef BEAST_HTTP_IMPL_CHUNK_ENCODE_IPP
#define BEAST_HTTP_IMPL_CHUNK_ENCODE_IPP
#include <beast/http/detail/rfc7230.hpp>
namespace beast {
namespace http {
inline
chunk_header::
chunk_header(std::size_t size)
: view_(
size,
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{})
{
BOOST_ASSERT(size > 0);
}
inline
chunk_header::
chunk_header(
std::size_t size,
string_view extensions)
: view_(
size,
boost::asio::const_buffers_1{
extensions.data(), extensions.size()},
chunk_crlf{})
{
BOOST_ASSERT(size > 0);
}
template<class ChunkExtensions, class>
chunk_header::
chunk_header(
std::size_t size,
ChunkExtensions&& extensions)
: exts_(std::make_shared<detail::chunk_extensions_impl<
typename std::decay<ChunkExtensions>::type>>(
std::forward<ChunkExtensions>(extensions)))
, view_(
size,
exts_->str(),
chunk_crlf{})
{
static_assert(
detail::is_chunk_extensions<ChunkExtensions>::value,
"ChunkExtensions requirements not met");
BOOST_ASSERT(size > 0);
}
template<class ChunkExtensions, class Allocator, class>
chunk_header::
chunk_header(
std::size_t size,
ChunkExtensions&& extensions,
Allocator const& allocator)
: exts_(std::allocate_shared<detail::chunk_extensions_impl<
typename std::decay<ChunkExtensions>::type>>(allocator,
std::forward<ChunkExtensions>(extensions)))
, view_(
size,
exts_->str(),
chunk_crlf{})
{
static_assert(
detail::is_chunk_extensions<ChunkExtensions>::value,
"ChunkExtensions requirements not met");
BOOST_ASSERT(size > 0);
}
//------------------------------------------------------------------------------
template<class ConstBufferSequence>
chunk_body<ConstBufferSequence>::
chunk_body(ConstBufferSequence const& buffers)
: view_(
boost::asio::buffer_size(buffers),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
buffers,
chunk_crlf{})
{
}
template<class ConstBufferSequence>
chunk_body<ConstBufferSequence>::
chunk_body(
ConstBufferSequence const& buffers,
string_view extensions)
: view_(
boost::asio::buffer_size(buffers),
boost::asio::const_buffers_1{
extensions.data(), extensions.size()},
chunk_crlf{},
buffers,
chunk_crlf{})
{
}
template<class ConstBufferSequence>
template<class ChunkExtensions, class>
chunk_body<ConstBufferSequence>::
chunk_body(
ConstBufferSequence const& buffers,
ChunkExtensions&& extensions)
: exts_(std::make_shared<detail::chunk_extensions_impl<
typename std::decay<ChunkExtensions>::type>>(
std::forward<ChunkExtensions>(extensions)))
, view_(
boost::asio::buffer_size(buffers),
exts_->str(),
chunk_crlf{},
buffers,
chunk_crlf{})
{
}
template<class ConstBufferSequence>
template<class ChunkExtensions, class Allocator, class>
chunk_body<ConstBufferSequence>::
chunk_body(
ConstBufferSequence const& buffers,
ChunkExtensions&& extensions,
Allocator const& allocator)
: exts_(std::allocate_shared<detail::chunk_extensions_impl<
typename std::decay<ChunkExtensions>::type>>(allocator,
std::forward<ChunkExtensions>(extensions)))
, view_(
boost::asio::buffer_size(buffers),
exts_->str(),
chunk_crlf{},
buffers,
chunk_crlf{})
{
}
//------------------------------------------------------------------------------
template<class Trailer>
template<class Allocator>
auto
chunk_last<Trailer>::
prepare(Trailer const& trailer, Allocator const& allocator) ->
buffers_type
{
auto sp = std::allocate_shared<typename
Trailer::reader>(allocator, trailer);
sp_ = sp;
return sp->get();
}
template<class Trailer>
auto
chunk_last<Trailer>::
prepare(Trailer const& trailer, std::true_type) ->
buffers_type
{
auto sp = std::make_shared<
typename Trailer::reader>(trailer);
sp_ = sp;
return sp->get();
}
template<class Trailer>
auto
chunk_last<Trailer>::
prepare(Trailer const& trailer, std::false_type) ->
buffers_type
{
return trailer;
}
template<class Trailer>
chunk_last<Trailer>::
chunk_last()
: view_(
detail::chunk_size0{},
Trailer{})
{
}
template<class Trailer>
chunk_last<Trailer>::
chunk_last(Trailer const& trailer)
: view_(
detail::chunk_size0{},
prepare(trailer, is_fields<Trailer>{}))
{
}
template<class Trailer>
template<class DeducedTrailer, class Allocator, class>
chunk_last<Trailer>::
chunk_last(
DeducedTrailer const& trailer, Allocator const& allocator)
: view_(
detail::chunk_size0{},
prepare(trailer, allocator))
{
}
//------------------------------------------------------------------------------
template<class Allocator>
void
basic_chunk_extensions<Allocator>::
insert(string_view name)
{
/*
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
*/
s_.reserve(1 + name.size());
s_.push_back(';');
s_.append(name.data(), name.size());
}
template<class Allocator>
void
basic_chunk_extensions<Allocator>::
insert(string_view name, string_view value)
{
/*
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
token = 1*tchar
quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
qdtext = HTAB / SP / "!" / %x23-5B ; '#'-'[' / %x5D-7E ; ']'-'~' / obs-text
quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
obs-text = %x80-FF
*/
bool is_token = true;
for(auto const c : value)
{
if(! detail::is_tchar(c))
{
is_token = false;
break;
}
}
if(is_token)
{
s_.reserve(1 + name.size() + 1 + value.size());
s_.push_back(';');
s_.append(name.data(), name.size());
s_.push_back('=');
s_.append(value.data(), value.size());
return;
}
// quoted-string
s_.reserve(
1 + name.size() + 1 +
1 + value.size() + 20 + 1);
s_.push_back(';');
s_.append(name.data(), name.size());
s_.append("=\"", 2);
for(auto const c : value)
{
if(c == '\\')
s_.append(R"(\\)", 2);
else if(c == '\"')
s_.append(R"(\")", 2);
else
s_.push_back(c);
}
s_.push_back('"');
}
} // http
} // beast
#endif
+42 -22
View File
@@ -11,10 +11,11 @@
#include <beast/core/buffer_cat.hpp>
#include <beast/core/string.hpp>
#include <beast/core/static_string.hpp>
#include <beast/core/detail/buffers_ref.hpp>
#include <beast/http/verb.hpp>
#include <beast/http/rfc7230.hpp>
#include <beast/http/status.hpp>
#include <beast/http/detail/chunk_encode.hpp>
#include <beast/http/chunk_encode.hpp>
#include <boost/throw_exception.hpp>
#include <stdexcept>
#include <string>
@@ -140,18 +141,20 @@ public:
}
};
using view_type = buffer_cat_view<
boost::asio::const_buffers_1,
boost::asio::const_buffers_1,
boost::asio::const_buffers_1,
field_range,
chunk_crlf>;
basic_fields const& f_;
boost::asio::const_buffer cb_[3];
boost::optional<view_type> view_;
char buf_[13];
public:
using const_buffers_type =
buffer_cat_view<
boost::asio::const_buffers_1,
boost::asio::const_buffers_1,
boost::asio::const_buffers_1,
field_range,
boost::asio::const_buffers_1>;
beast::detail::buffers_ref<view_type>;
reader(basic_fields const& f,
unsigned version, verb v);
@@ -159,18 +162,28 @@ public:
reader(basic_fields const& f,
unsigned version, unsigned code);
reader(basic_fields const& f);
const_buffers_type
get() const
{
return buffer_cat(
boost::asio::const_buffers_1{cb_[0]},
boost::asio::const_buffers_1{cb_[1]},
boost::asio::const_buffers_1{cb_[2]},
field_range(f_.list_.begin(), f_.list_.end()),
detail::chunk_crlf());
return const_buffers_type(*view_);
}
};
template<class Allocator>
basic_fields<Allocator>::reader::
reader(basic_fields const& f)
: f_(f)
{
view_.emplace(
boost::asio::const_buffers_1{nullptr, 0},
boost::asio::const_buffers_1{nullptr, 0},
boost::asio::const_buffers_1{nullptr, 0},
field_range(f_.list_.begin(), f_.list_.end()),
chunk_crlf());
}
template<class Allocator>
basic_fields<Allocator>::reader::
reader(basic_fields const& f,
@@ -188,12 +201,8 @@ reader(basic_fields const& f,
sv = f_.get_method_impl();
else
sv = to_string(v);
cb_[0] = {sv.data(), sv.size()};
// target_or_reason_ has a leading SP
cb_[1] = {
f_.target_or_reason_.data(),
f_.target_or_reason_.size()};
buf_[0] = ' ';
buf_[1] = 'H';
@@ -206,7 +215,15 @@ reader(basic_fields const& f,
buf_[8] = '0' + static_cast<char>(version % 10);
buf_[9] = '\r';
buf_[10]= '\n';
cb_[2] = {buf_, 11};
view_.emplace(
boost::asio::const_buffers_1{sv.data(), sv.size()},
boost::asio::const_buffers_1{
f_.target_or_reason_.data(),
f_.target_or_reason_.size()},
boost::asio::const_buffers_1{buf_, 11},
field_range(f_.list_.begin(), f_.list_.end()),
chunk_crlf());
}
template<class Allocator>
@@ -234,16 +251,19 @@ reader(basic_fields const& f,
buf_[10]= '0' + static_cast<char>((code / 10) % 10);
buf_[11]= '0' + static_cast<char>(code % 10);
buf_[12]= ' ';
cb_[0] = {buf_, 13};
string_view sv;
if(! f_.target_or_reason_.empty())
sv = f_.target_or_reason_;
else
sv = obsolete_reason(static_cast<status>(code));
cb_[1] = {sv.data(), sv.size()};
cb_[2] = detail::chunk_crlf();
view_.emplace(
boost::asio::const_buffers_1{buf_, 13},
boost::asio::const_buffers_1{sv.data(), sv.size()},
boost::asio::const_buffers_1{"\r\n", 2},
field_range(f_.list_.begin(), f_.list_.end()),
chunk_crlf{});
}
//------------------------------------------------------------------------------
+34 -32
View File
@@ -30,7 +30,7 @@ namespace beast {
namespace http {
namespace detail {
template<class, class, bool, class, class>
template<class, class, bool, class>
class write_some_win32_op;
} // detail
@@ -50,16 +50,16 @@ struct basic_file_body<file_win32>
friend class writer;
friend struct basic_file_body<file_win32>;
template<class, class, bool, class, class>
template<class, class, bool, class>
friend class detail::write_some_win32_op;
template<class Protocol, bool isRequest,
class Fields, class Decorator>
template<
class Protocol, bool isRequest, class Fields>
friend
void
write_some(
boost::asio::basic_stream_socket<Protocol>& sock,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr,
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr,
error_code& ec);
file_win32 file_;
@@ -99,16 +99,16 @@ struct basic_file_body<file_win32>
class reader
{
template<class, class, bool, class, class>
template<class, class, bool, class>
friend class detail::write_some_win32_op;
template<class Protocol, bool isRequest,
class Fields, class Decorator>
template<
class Protocol, bool isRequest, class Fields>
friend
void
write_some(
boost::asio::basic_stream_socket<Protocol>& sock,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr,
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr,
error_code& ec);
value_type& body_; // The body we are reading from
@@ -329,13 +329,14 @@ public:
#if BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
template<class Protocol, class Handler,
bool isRequest, class Fields, class Decorator>
template<
class Protocol, class Handler,
bool isRequest, class Fields>
class write_some_win32_op
{
boost::asio::basic_stream_socket<Protocol>& sock_;
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr_;
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr_;
bool header_ = false;
Handler h_;
@@ -347,8 +348,8 @@ public:
write_some_win32_op(
DeducedHandler&& h,
boost::asio::basic_stream_socket<Protocol>& s,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr)
serializer<isRequest,
basic_file_body<file_win32>,Fields>& sr)
: sock_(s)
, sr_(sr)
, h_(std::forward<DeducedHandler>(h))
@@ -398,11 +399,12 @@ public:
}
};
template<class Protocol, class Handler,
bool isRequest, class Fields, class Decorator>
template<
class Protocol, class Handler,
bool isRequest, class Fields>
void
write_some_win32_op<
Protocol, Handler, isRequest, Fields, Decorator>::
Protocol, Handler, isRequest, Fields>::
operator()()
{
if(! sr_.is_header_done())
@@ -449,11 +451,12 @@ operator()()
overlapped.release();
}
template<class Protocol, class Handler,
bool isRequest, class Fields, class Decorator>
template<
class Protocol, class Handler,
bool isRequest, class Fields>
void
write_some_win32_op<
Protocol, Handler,isRequest, Fields, Decorator>::
Protocol, Handler, isRequest, Fields>::
operator()(error_code ec, std::size_t bytes_transferred)
{
if(! ec)
@@ -484,13 +487,12 @@ operator()(error_code ec, std::size_t bytes_transferred)
//------------------------------------------------------------------------------
template<class Protocol,
bool isRequest, class Fields, class Decorator>
template<class Protocol, bool isRequest, class Fields>
void
write_some(
boost::asio::basic_stream_socket<Protocol>& sock,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr,
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr,
error_code& ec)
{
if(! sr.is_header_done())
@@ -552,20 +554,20 @@ write_some(
template<
class Protocol,
bool isRequest, class Fields, class Decorator,
bool isRequest, class Fields,
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write_some(
boost::asio::basic_stream_socket<Protocol>& sock,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr,
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr,
WriteHandler&& handler)
{
async_completion<WriteHandler,
void(error_code)> init{handler};
detail::write_some_win32_op<Protocol, handler_type<
WriteHandler, void(error_code)>, isRequest, Fields,
Decorator>{init.completion_handler, sock, sr}();
WriteHandler, void(error_code)>, isRequest, Fields>{
init.completion_handler, sock, sr}();
return init.result.get();
}
+43 -102
View File
@@ -18,30 +18,30 @@
namespace beast {
namespace http {
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
frdinit(std::true_type)
{
frd_.emplace(m_, m_.version, m_.method());
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
frdinit(std::false_type)
{
frd_.emplace(m_, m_.version, m_.result_int());
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
template<class T1, class T2, class Visit>
inline
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
do_visit(error_code& ec, Visit& visit)
{
// VFALCO work-around for missing variant::emplace
@@ -54,30 +54,20 @@ do_visit(error_code& ec, Visit& visit)
//------------------------------------------------------------------------------
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
serializer<isRequest, Body, Fields, ChunkDecorator>::
template<
bool isRequest, class Body, class Fields>
serializer<isRequest, Body, Fields>::
serializer(value_type& m)
: m_(m)
, rd_(m_)
{
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer(value_type& m, ChunkDecorator const& d)
: m_(m)
, rd_(m_)
, d_(d)
{
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
template<class Visit>
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
next(error_code& ec, Visit&& visit)
{
using boost::asio::buffer_size;
@@ -177,46 +167,25 @@ next(error_code& ec, Visit&& visit)
v_ = cb7_t{
boost::in_place_init,
frd_->get(),
detail::chunk_header{
buffer_size(result->first)},
[&]()
{
auto sv = d_(result->first);
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf(),
buffer_size(result->first),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
result->first,
detail::chunk_crlf(),
detail::chunk_final(),
[&]()
{
auto sv = d_(
boost::asio::null_buffers{});
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf()};
chunk_crlf{},
detail::chunk_last(),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{}};
goto go_all_c;
}
#endif
v_ = cb4_t{
boost::in_place_init,
frd_->get(),
detail::chunk_header{
buffer_size(result->first)},
[&]()
{
auto sv = d_(result->first);
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf(),
buffer_size(result->first),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
result->first,
detail::chunk_crlf()};
chunk_crlf{}};
s_ = do_header_c;
BEAST_FALLTHROUGH;
}
@@ -250,45 +219,24 @@ next(error_code& ec, Visit&& visit)
// do it all in one buffer
v_ = cb6_t{
boost::in_place_init,
detail::chunk_header{
buffer_size(result->first)},
[&]()
{
auto sv = d_(result->first);
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf(),
buffer_size(result->first),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
result->first,
detail::chunk_crlf(),
detail::chunk_final(),
[&]()
{
auto sv = d_(
boost::asio::null_buffers{});
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf()};
chunk_crlf{},
detail::chunk_last(),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{}};
goto go_body_final_c;
}
#endif
v_ = cb5_t{
boost::in_place_init,
detail::chunk_header{
buffer_size(result->first)},
[&]()
{
auto sv = d_(result->first);
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf(),
buffer_size(result->first),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
result->first,
detail::chunk_crlf()};
chunk_crlf{}};
s_ = do_body_c + 2;
BEAST_FALLTHROUGH;
}
@@ -315,16 +263,9 @@ next(error_code& ec, Visit&& visit)
case do_final_c:
v_ = cb8_t{
boost::in_place_init,
detail::chunk_final(),
[&]()
{
auto sv = d_(
boost::asio::null_buffers{});
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf()};
detail::chunk_last(),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{}};
s_ = do_final_c + 1;
BEAST_FALLTHROUGH;
@@ -345,10 +286,10 @@ next(error_code& ec, Visit&& visit)
}
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
consume(std::size_t n)
{
using boost::asio::buffer_size;
+110 -102
View File
@@ -28,13 +28,13 @@ namespace beast {
namespace http {
namespace detail {
template<class Stream, class Handler,
bool isRequest, class Body,
class Fields, class Decorator>
template<
class Stream, class Handler,
bool isRequest, class Body, class Fields>
class write_some_op
{
Stream& s_;
serializer<isRequest,Body, Fields, Decorator>& sr_;
serializer<isRequest,Body, Fields>& sr_;
Handler h_;
class lambda
@@ -67,9 +67,8 @@ public:
write_some_op(write_some_op const&) = default;
template<class DeducedHandler>
write_some_op(DeducedHandler&& h,
Stream& s, serializer<isRequest, Body,
Fields, Decorator>& sr)
write_some_op(DeducedHandler&& h, Stream& s,
serializer<isRequest, Body, Fields>& sr)
: s_(s)
, sr_(sr)
, h_(std::forward<DeducedHandler>(h))
@@ -118,12 +117,12 @@ public:
}
};
template<class Stream, class Handler,
bool isRequest, class Body,
class Fields, class Decorator>
template<
class Stream, class Handler,
bool isRequest, class Body, class Fields>
void
write_some_op<Stream, Handler,
isRequest, Body, Fields, Decorator>::
write_some_op<
Stream, Handler, isRequest, Body, Fields>::
operator()()
{
error_code ec;
@@ -150,12 +149,12 @@ operator()()
bind_handler(std::move(*this), ec, 0));
}
template<class Stream, class Handler,
bool isRequest, class Body,
class Fields, class Decorator>
template<
class Stream, class Handler,
bool isRequest, class Body, class Fields>
void
write_some_op<Stream, Handler,
isRequest, Body, Fields, Decorator>::
write_some_op<
Stream, Handler, isRequest, Body, Fields>::
operator()(
error_code ec, std::size_t bytes_transferred)
{
@@ -173,11 +172,11 @@ operator()(
struct serializer_is_header_done
{
template<bool isRequest, class Body,
class Fields, class Decorator>
template<
bool isRequest, class Body, class Fields>
bool
operator()(serializer<isRequest, Body,
Fields, Decorator>& sr) const
operator()(
serializer<isRequest, Body, Fields>& sr) const
{
return sr.is_header_done();
}
@@ -185,11 +184,11 @@ struct serializer_is_header_done
struct serializer_is_done
{
template<bool isRequest, class Body,
class Fields, class Decorator>
template<
bool isRequest, class Body, class Fields>
bool
operator()(serializer<isRequest, Body,
Fields, Decorator>& sr) const
operator()(
serializer<isRequest, Body, Fields>& sr) const
{
return sr.is_done();
}
@@ -199,14 +198,12 @@ struct serializer_is_done
template<
class Stream, class Handler, class Predicate,
bool isRequest, class Body,
class Fields, class Decorator>
bool isRequest, class Body, class Fields>
class write_op
{
int state_ = 0;
Stream& s_;
serializer<isRequest,
Body, Fields, Decorator>& sr_;
serializer<isRequest, Body, Fields>& sr_;
Handler h_;
public:
@@ -215,8 +212,7 @@ public:
template<class DeducedHandler>
write_op(DeducedHandler&& h, Stream& s,
serializer<isRequest, Body, Fields,
Decorator>& sr)
serializer<isRequest, Body, Fields>& sr)
: s_(s)
, sr_(sr)
, h_(std::forward<DeducedHandler>(h))
@@ -265,11 +261,10 @@ public:
template<
class Stream, class Handler, class Predicate,
bool isRequest, class Body,
class Fields, class Decorator>
bool isRequest, class Body, class Fields>
void
write_op<Stream, Handler, Predicate,
isRequest, Body, Fields, Decorator>::
isRequest, Body, Fields>::
operator()(error_code ec)
{
if(ec)
@@ -317,13 +312,12 @@ class write_msg_op
struct data
{
Stream& s;
serializer<isRequest,
Body, Fields, no_chunk_decorator> sr;
serializer<isRequest, Body, Fields> sr;
data(Handler&, Stream& s_, message<
isRequest, Body, Fields>& m_)
: s(s_)
, sr(m_, no_chunk_decorator{})
, sr(m_)
{
}
};
@@ -466,11 +460,11 @@ namespace detail {
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields, class Decorator>
bool isRequest, class Body, class Fields>
void
write_some(
SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec)
{
if(! sr.is_done())
@@ -492,30 +486,34 @@ write_some(
ec.assign(0, ec.category());
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write_some(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
WriteHandler&& handler)
async_write_some(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler)
{
async_completion<WriteHandler,
void(error_code)> init{handler};
detail::write_some_op<AsyncWriteStream,
handler_type<WriteHandler, void(error_code)>,
isRequest, Body, Fields, Decorator>{
isRequest, Body, Fields>{
init.completion_handler, stream, sr}();
return init.result.get();
}
} // detail
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr)
write_some(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@@ -529,13 +527,14 @@ write_some(SyncWriteStream& stream, serializer<
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class SyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
error_code& ec)
write_some(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@@ -546,13 +545,15 @@ write_some(SyncWriteStream& stream, serializer<
detail::write_some(stream, sr, ec);
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write_some(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
WriteHandler&& handler)
async_write_some(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler)
{
static_assert(is_async_write_stream<
AsyncWriteStream>::value,
@@ -567,12 +568,12 @@ async_write_some(AsyncWriteStream& stream, serializer<
//------------------------------------------------------------------------------
template<class SyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_header(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr)
write_header(SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@@ -586,13 +587,14 @@ write_header(SyncWriteStream& stream, serializer<
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class SyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_header(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
error_code& ec)
write_header(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@@ -620,13 +622,15 @@ write_header(SyncWriteStream& stream, serializer<
}
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write_header(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
WriteHandler&& handler)
async_write_header(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler)
{
static_assert(is_async_write_stream<
AsyncWriteStream>::value,
@@ -641,7 +645,7 @@ async_write_header(AsyncWriteStream& stream, serializer<
detail::write_op<AsyncWriteStream, handler_type<
WriteHandler, void(error_code)>,
detail::serializer_is_header_done,
isRequest, Body, Fields, Decorator>{
isRequest, Body, Fields>{
init.completion_handler, stream, sr}(
error_code{}, 0);
return init.result.get();
@@ -651,12 +655,11 @@ async_write_header(AsyncWriteStream& stream, serializer<
template<
class SyncWriteStream,
bool isRequest, class Body,
class Fields, class Decorator>
bool isRequest, class Body, class Fields>
void
write(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields, Decorator>& sr)
serializer<isRequest, Body, Fields>& sr)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@@ -668,12 +671,11 @@ write(
template<
class SyncWriteStream,
bool isRequest, class Body,
class Fields, class Decorator>
bool isRequest, class Body, class Fields>
void
write(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields, Decorator>& sr,
serializer<isRequest, Body, Fields>& sr,
error_code& ec)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
@@ -689,13 +691,15 @@ write(
}
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
WriteHandler&& handler)
async_write(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler)
{
static_assert(is_async_write_stream<
AsyncWriteStream>::value,
@@ -709,19 +713,19 @@ async_write(AsyncWriteStream& stream, serializer<
void(error_code)> init{handler};
detail::write_op<AsyncWriteStream, handler_type<
WriteHandler, void(error_code)>,
detail::serializer_is_done,
isRequest, Body, Fields, Decorator>{
init.completion_handler, stream, sr}(
error_code{});
detail::serializer_is_done, isRequest, Body, Fields>{
init.completion_handler, stream, sr}(error_code{});
return init.result.get();
}
//------------------------------------------------------------------------------
template<class SyncWriteStream,
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream,
write(
SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
@@ -736,12 +740,14 @@ write(SyncWriteStream& stream,
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class SyncWriteStream,
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream,
write(
SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg,
error_code& ec)
error_code& ec)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@@ -753,14 +759,16 @@ write(SyncWriteStream& stream,
write(stream, sr, ec);
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class WriteHandler>
class WriteHandler>
async_return_type<
WriteHandler, void(error_code)>
async_write(AsyncWriteStream& stream,
async_write(
AsyncWriteStream& stream,
message<isRequest, Body, Fields>& msg,
WriteHandler&& handler)
WriteHandler&& handler)
{
static_assert(
is_async_write_stream<AsyncWriteStream>::value,
+47 -138
View File
@@ -15,7 +15,7 @@
#include <beast/core/string.hpp>
#include <beast/core/type_traits.hpp>
#include <beast/http/message.hpp>
#include <beast/http/detail/chunk_encode.hpp>
#include <beast/http/chunk_encode.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/optional.hpp>
#include <boost/variant.hpp>
@@ -29,34 +29,6 @@
namespace beast {
namespace http {
/** A chunk decorator which does nothing.
When selected as a chunk decorator, objects of this type
affect the output of messages specifying chunked
transfer encodings as follows:
@li chunk headers will have empty chunk extensions, and
@li final chunks will have an empty set of trailers.
@see @ref serializer
*/
struct no_chunk_decorator
{
template<class ConstBufferSequence>
string_view
operator()(ConstBufferSequence const&) const
{
return {};
}
string_view
operator()(boost::asio::null_buffers) const
{
return {};
}
};
/** Provides buffer oriented HTTP message serialization functionality.
An object of this type is used to serialize a complete
@@ -67,64 +39,27 @@ struct no_chunk_decorator
if the contents of the message indicate that chunk encoding
is required. If the semantics of the message indicate that
the connection should be closed after the message is sent, the
function @ref keep_alive will return `true`.
function @ref keep_alive will return `false`.
Upon construction, an optional chunk decorator may be
specified. This decorator is a function object called with
each buffer sequence of the body when the chunked transfer
encoding is indicate in the message header. The decorator
will be called with an empty buffer sequence (actually
the type `boost::asio::null_buffers`) to indicate the
final chunk. The decorator may return a string which forms
the chunk extension for chunks, and the field trailers
for the final chunk.
In C++11 the decorator must be declared as a class or
struct with a templated operator() thusly:
@code
// The implementation guarantees that operator()
// will be called only after the view returned by
// any previous calls to operator() are no longer
// needed. The decorator instance is intended to
// manage the lifetime of the storage for all returned
// views.
//
struct decorator
{
// Returns the chunk-extension for each chunk,
// or an empty string for no chunk extension. The
// buffer must include the leading semicolon (";")
// and follow the format for chunk extensions defined
// in rfc7230.
//
template<class ConstBufferSequence>
string_view
operator()(ConstBufferSequence const&) const;
// Returns a set of field trailers for the final chunk.
// Each field should be formatted according to rfc7230
// including the trailing "\r\n" for each field. If
// no trailers are indicated, an empty string is returned.
//
string_view
operator()(boost::asio::null_buffers) const;
};
@endcode
Chunked output produced by the serializer never contains chunk
extensions or trailers, and the location of chunk boundaries
is not specified. If callers require chunk extensions, trailers,
or control over the exact contents of each chunk they should
use the serializer to write just the message header, and then
assume control over serializing the chunked payload by using
the chunk buffer sequence types @ref chunk_body, @ref chunk_crlf,
@ref chunk_header, and @ref chunk_last.
@tparam isRequest `true` if the message is a request.
@tparam Body The body type of the message.
@tparam Fields The type of fields in the message.
@tparam ChunkDecorator The type of chunk decorator to use.
*/
template<
bool isRequest,
class Body,
class Fields = fields,
class ChunkDecorator = no_chunk_decorator>
class Fields = fields>
class serializer
{
public:
@@ -199,50 +134,50 @@ private:
using cb4_t = consuming_buffers<buffer_cat_view<
typename Fields::reader::const_buffers_type,// header
detail::chunk_header, // chunk-header
detail::chunk_size, // chunk-size
boost::asio::const_buffers_1, // chunk-ext
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
typename reader::const_buffers_type, // body
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb4_t = buffer_prefix_view<cb4_t const&>;
using cb5_t = consuming_buffers<buffer_cat_view<
detail::chunk_header, // chunk-header
detail::chunk_size, // chunk-header
boost::asio::const_buffers_1, // chunk-ext
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
typename reader::const_buffers_type, // body
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb5_t = buffer_prefix_view<cb5_t const&>;
#ifndef BEAST_NO_BIG_VARIANTS
using cb6_t = consuming_buffers<buffer_cat_view<
detail::chunk_header, // chunk-header
boost::asio::const_buffers_1, // chunk-ext
boost::asio::const_buffers_1, // crlf
detail::chunk_size, // chunk-header
boost::asio::const_buffers_1, // chunk-size
chunk_crlf, // crlf
typename reader::const_buffers_type, // body
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
boost::asio::const_buffers_1, // chunk-final
boost::asio::const_buffers_1, // trailers
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb6_t = buffer_prefix_view<cb6_t const&>;
using cb7_t = consuming_buffers<buffer_cat_view<
typename Fields::reader::const_buffers_type,// header
detail::chunk_header, // chunk-header
detail::chunk_size, // chunk-size
boost::asio::const_buffers_1, // chunk-ext
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
typename reader::const_buffers_type, // body
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
boost::asio::const_buffers_1, // chunk-final
boost::asio::const_buffers_1, // trailers
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb7_t = buffer_prefix_view<cb7_t const&>;
#endif
using cb8_t = consuming_buffers<buffer_cat_view<
boost::asio::const_buffers_1, // chunk-final
boost::asio::const_buffers_1, // trailers
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb8_t = buffer_prefix_view<cb8_t const&>;
value_type& m_;
@@ -268,7 +203,6 @@ private:
bool chunked_;
bool keep_alive_;
bool more_;
ChunkDecorator d_;
public:
/** Constructor
@@ -288,25 +222,6 @@ public:
explicit
serializer(value_type& msg);
/** Constructor
The implementation guarantees that the message passed on
construction will not be accessed until the first call to
@ref next. This allows the message to be lazily created.
For example, if the header is filled in before serialization.
@param msg A reference to the message to serialize, which must
remain valid for the lifetime of the serializer. Depending on
the type of Body used, this may or may not be a `const` reference.
@param decorator The decorator to use.
@note This function participates in overload resolution only if
Body::reader is constructible from a `const` message reference.
*/
explicit
serializer(value_type& msg, ChunkDecorator const& decorator);
/// Returns the message being serialized
value_type&
get()
@@ -314,22 +229,6 @@ public:
return m_;
}
/** Provides access to the associated @b BodyReader
This function provides access to the instance of the reader
associated with the body and created by the serializer
upon construction. The behavior of accessing this object
is defined by the specification of the particular reader
and its associated body.
@return A reference to the reader.
*/
reader&
reader_impl()
{
return rd_;
}
/// Returns the serialized buffer size limit
std::size_t
limit()
@@ -478,21 +377,31 @@ public:
*/
void
consume(std::size_t n);
/** Provides low-level access to the associated @b BodyReader
This function provides access to the instance of the reader
associated with the body and created by the serializer
upon construction. The behavior of accessing this object
is defined by the specification of the particular reader
and its associated body.
@return A reference to the reader.
*/
reader&
reader_impl()
{
return rd_;
}
};
/// A serializer for HTTP/1 requests
template<
class Body,
class Fields = fields,
class ChunkDecorator = no_chunk_decorator>
using request_serializer = serializer<true, Body, Fields, ChunkDecorator>;
template<class Body, class Fields = fields>
using request_serializer = serializer<true, Body, Fields>;
/// A serializer for HTTP/1 responses
template<
class Body,
class Fields = fields,
class ChunkDecorator = no_chunk_decorator>
using response_serializer = serializer<false, Body, Fields, ChunkDecorator>;
template<class Body, class Fields = fields>
using response_serializer = serializer<false, Body, Fields>;
} // http
} // beast
+76 -53
View File
@@ -60,11 +60,13 @@ namespace http {
@see serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr);
write_some(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr);
/** Write part of a message to a stream using a serializer.
@@ -98,12 +100,14 @@ write_some(SyncWriteStream& stream, serializer<
@see @ref async_write_some, @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
error_code& ec);
write_some(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec);
/** Write part of a message to a stream asynchronously using a serializer.
@@ -151,17 +155,19 @@ write_some(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<WriteHandler, void(error_code)>
#endif
async_write_some(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
WriteHandler&& handler);
async_write_some(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler);
//------------------------------------------------------------------------------
@@ -190,11 +196,13 @@ async_write_some(AsyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_header(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr);
write_header(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr);
/** Write a header to a stream using a serializer.
@@ -221,12 +229,14 @@ write_header(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_header(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
error_code& ec);
write_header(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec);
/** Write a header to a stream asynchronously using a serializer.
@@ -267,17 +277,19 @@ write_header(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<WriteHandler, void(error_code)>
#endif
async_write_header(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
WriteHandler&& handler);
async_write_header(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler);
//------------------------------------------------------------------------------
@@ -303,11 +315,13 @@ async_write_header(AsyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr);
write(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr);
/** Write a complete message to a stream using a serializer.
@@ -331,12 +345,14 @@ write(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
error_code& ec);
write(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec);
/** Write a complete message to a stream asynchronously using a serializer.
@@ -374,17 +390,19 @@ write(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<WriteHandler, void(error_code)>
#endif
async_write(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
WriteHandler&& handler);
async_write(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler);
//------------------------------------------------------------------------------
@@ -412,10 +430,12 @@ async_write(AsyncWriteStream& stream, serializer<
@see @ref message
*/
template<class SyncWriteStream,
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream,
write(
SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg);
/** Write a complete message to a stream.
@@ -442,12 +462,14 @@ write(SyncWriteStream& stream,
@see @ref message
*/
template<class SyncWriteStream,
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream,
write(
SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg,
error_code& ec);
error_code& ec);
/** Write a complete message to a stream asynchronously.
@@ -488,14 +510,15 @@ write(SyncWriteStream& stream,
@see @ref message
*/
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class WriteHandler>
async_return_type<
WriteHandler, void(error_code)>
async_write(AsyncWriteStream& stream,
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write(
AsyncWriteStream& stream,
message<isRequest, Body, Fields>& msg,
WriteHandler&& handler);
WriteHandler&& handler);
//------------------------------------------------------------------------------