Add streambuf write():

This function performs serialization of a variable list of arguments
to a streambuf. It accepts a wide variety of argument types, using
boost::asio::buffer and boost::lexical_cast where possible.
This commit is contained in:
Vinnie Falco
2016-04-26 06:22:58 -04:00
parent 7cfdab936c
commit 4cfa1d5cd3
11 changed files with 271 additions and 98 deletions

View File

@@ -131,6 +131,7 @@
<member><link linkend="beast.ref.buffer_cat">buffer_cat</link></member>
<member><link linkend="beast.ref.prepare_buffer">prepare_buffer</link></member>
<member><link linkend="beast.ref.prepare_buffers">prepare_buffers</link></member>
<member><link linkend="beast.ref.write">write</link></member>
</simplelist>
</entry>
<entry valign="top">

View File

@@ -0,0 +1,146 @@
//
// Copyright (c) 2013-2016 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_DETAIL_WRITE_STREAMBUF_HPP
#define BEAST_DETAIL_WRITE_STREAMBUF_HPP
#include <boost/asio/buffer.hpp>
#include <boost/lexical_cast.hpp>
#include <utility>
namespace beast {
namespace detail {
// detects string literals.
template<class T>
struct is_string_literal : std::integral_constant<bool,
! std::is_same<T, typename std::remove_extent<T>::type>::value &&
std::is_same<char, typename std::remove_extent<T>::type>::value>
{
};
// `true` if a call to boost::asio::buffer(T const&) is possible
// note: we exclude string literals because boost::asio::buffer()
// will include the null terminator, which we don't want.
template<class T>
class is_BufferConvertible
{
template<class U, class R = decltype(
boost::asio::buffer(std::declval<U const&>()),
std::true_type{})>
static R check(int);
template<class>
static std::false_type check(...);
using type = decltype(check<T>(0));
public:
static bool const value = type::value &&
! is_string_literal<T>::value;
};
template<class Streambuf>
inline
void
write_streambuf(Streambuf&)
{
}
template<class Streambuf>
void
write_streambuf(Streambuf& streambuf,
boost::asio::const_buffer const& buffer)
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
streambuf.commit(buffer_copy(
streambuf.prepare(buffer_size(buffer)),
buffer));
}
template<class Streambuf>
void
write_streambuf(Streambuf& streambuf,
boost::asio::mutable_buffer const& buffer)
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
streambuf.commit(buffer_copy(
streambuf.prepare(buffer_size(buffer)),
buffer));
}
template<class Streambuf, class T>
typename std::enable_if<
is_BufferConvertible<T>::value &&
! std::is_convertible<T, boost::asio::const_buffer>::value &&
! std::is_convertible<T, boost::asio::mutable_buffer>::value
>::type
write_streambuf(Streambuf& streambuf, T const& t)
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
auto const buffers = boost::asio::buffer(t);
streambuf.commit(buffer_copy(
streambuf.prepare(buffer_size(buffers)),
buffers));
}
template<class Streambuf, class Buffers>
typename std::enable_if<
is_ConstBufferSequence<Buffers>::value &&
! is_BufferConvertible<Buffers>::value &&
! std::is_convertible<Buffers, boost::asio::const_buffer>::value &&
! std::is_convertible<Buffers, boost::asio::mutable_buffer>::value
>::type
write_streambuf(Streambuf& streambuf, Buffers const& buffers)
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
streambuf.commit(buffer_copy(
streambuf.prepare(buffer_size(buffers)),
buffers));
}
template<class Streambuf, std::size_t N>
void
write_streambuf(Streambuf& streambuf, const char (&s)[N])
{
using boost::asio::buffer_copy;
streambuf.commit(buffer_copy(
streambuf.prepare(N - 1),
boost::asio::buffer(s, N - 1)));
}
template<class Streambuf, class T>
typename std::enable_if<
! is_string_literal<T>::value &&
! is_ConstBufferSequence<T>::value &&
! is_BufferConvertible<T>::value &&
! std::is_convertible<T, boost::asio::const_buffer>::value &&
! std::is_convertible<T, boost::asio::mutable_buffer>::value
>::type
write_streambuf(Streambuf& streambuf, T const& t)
{
using boost::asio::buffer;
using boost::asio::buffer_copy;
auto const s = boost::lexical_cast<std::string>(t);
streambuf.commit(buffer_copy(
streambuf.prepare(s.size()), buffer(s)));
}
template<class Streambuf, class T0, class T1, class... TN>
void
write_streambuf(Streambuf& streambuf, T0&& t0, T1&& t1, TN... tn)
{
write_streambuf(streambuf, std::forward<T0>(t0));
write_streambuf(streambuf, std::forward<T1>(t1));
write_streambuf(streambuf, std::forward<TN>(tn)...);
}
} // detail
} // beast
#endif

View File

@@ -8,7 +8,6 @@
#ifndef BEAST_HTTP_BASIC_HEADERS_HPP
#define BEAST_HTTP_BASIC_HEADERS_HPP
#include <beast/http/detail/writes.hpp>
#include <beast/type_check.hpp>
#include <beast/detail/ci_char_traits.hpp>
#include <beast/detail/empty_base_optimization.hpp>

View File

@@ -10,6 +10,7 @@
#include <beast/http/error.hpp>
#include <beast/streambuf.hpp>
#include <beast/write_streambuf.hpp>
namespace beast {
namespace http {
@@ -84,7 +85,7 @@ struct write_preparation
msg.write_firstline(sb);
write_fields(sb, h);
detail::write(sb, "\r\n");
beast::write(sb, "\r\n");
}
private:

View File

@@ -1,66 +0,0 @@
//
// Copyright (c) 2013-2016 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_DETAIL_WRITES_HPP
#define BEAST_HTTP_DETAIL_WRITES_HPP
#include <beast/type_check.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/utility/string_ref.hpp>
#include <string>
#include <type_traits>
#include <utility>
namespace beast {
namespace http {
namespace detail {
template<class Streambuf, class T,
class = typename std::enable_if<
is_Streambuf<Streambuf>::value>::type>
void
write(Streambuf& streambuf, T&& t)
{
using boost::asio::buffer;
using boost::asio::buffer_copy;
auto const& s =
boost::lexical_cast<std::string>(
std::forward<T>(t));
streambuf.commit(buffer_copy(
streambuf.prepare(s.size()), buffer(s)));
}
template<class Streambuf, std::size_t N,
class = typename std::enable_if< (N>0) &&
is_Streambuf<Streambuf>::value>::type>
void
write(Streambuf& streambuf, char const(&s)[N])
{
using boost::asio::buffer;
using boost::asio::buffer_copy;
streambuf.commit(buffer_copy(
streambuf.prepare(N), buffer(s, N-1)));
}
template<class Streambuf,
class = typename std::enable_if<
is_Streambuf<Streambuf>::value>::type>
void
write(Streambuf& streambuf, boost::string_ref const& s)
{
using boost::asio::buffer;
using boost::asio::buffer_copy;
streambuf.commit(buffer_copy(
streambuf.prepare(s.size()),
buffer(s.data(), s.size())));
}
} // detail
} // http
} // beast
#endif

View File

@@ -8,7 +8,6 @@
#ifndef BEAST_HTTP_IMPL_BASIC_HEADERS_IPP
#define BEAST_HTTP_IMPL_BASIC_HEADERS_IPP
#include <beast/http/detail/writes.hpp>
#include <beast/type_check.hpp>
namespace beast {

View File

@@ -9,10 +9,11 @@
#define BEAST_HTTP_IMPL_MESSAGE_IPP
#include <beast/http/chunk_encode.hpp>
#include <beast/http/detail/writes.hpp>
#include <beast/http/detail/write_preparation.hpp>
#include <beast/http/resume_context.hpp>
#include <beast/http/rfc2616.hpp>
#include <beast/write_streambuf.hpp>
#include <beast/type_check.hpp>
#include <beast/http/detail/write_preparation.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/logic/tribool.hpp>
#include <condition_variable>
@@ -54,23 +55,23 @@ message<isRequest, Body, Headers>::
write_firstline(Streambuf& streambuf,
std::true_type) const
{
detail::write(streambuf, to_string(this->method));
detail::write(streambuf, " ");
detail::write(streambuf, this->url);
write(streambuf, to_string(this->method));
write(streambuf, " ");
write(streambuf, this->url);
switch(version)
{
case 10:
detail::write(streambuf, " HTTP/1.0\r\n");
write(streambuf, " HTTP/1.0\r\n");
break;
case 11:
detail::write(streambuf, " HTTP/1.1\r\n");
write(streambuf, " HTTP/1.1\r\n");
break;
default:
detail::write(streambuf, " HTTP/");
detail::write(streambuf, version / 10);
detail::write(streambuf, ".");
detail::write(streambuf, version % 10);
detail::write(streambuf, "\r\n");
write(streambuf, " HTTP/");
write(streambuf, version / 10);
write(streambuf, ".");
write(streambuf, version % 10);
write(streambuf, "\r\n");
break;
}
}
@@ -85,23 +86,23 @@ write_firstline(Streambuf& streambuf,
switch(version)
{
case 10:
detail::write(streambuf, "HTTP/1.0 ");
write(streambuf, "HTTP/1.0 ");
break;
case 11:
detail::write(streambuf, "HTTP/1.1 ");
write(streambuf, "HTTP/1.1 ");
break;
default:
detail::write(streambuf, " HTTP/");
detail::write(streambuf, version / 10);
detail::write(streambuf, ".");
detail::write(streambuf, version % 10);
detail::write(streambuf, " ");
write(streambuf, " HTTP/");
write(streambuf, version / 10);
write(streambuf, ".");
write(streambuf, version % 10);
write(streambuf, " ");
break;
}
detail::write(streambuf, this->status);
detail::write(streambuf, " ");
detail::write(streambuf, this->reason);
detail::write(streambuf, "\r\n");
write(streambuf, this->status);
write(streambuf, " ");
write(streambuf, this->reason);
write(streambuf, "\r\n");
}
namespace detail {
@@ -273,10 +274,10 @@ write_fields(Streambuf& streambuf, FieldSequence const& fields)
// "FieldSequence requirements not met");
for(auto const& field : fields)
{
detail::write(streambuf, field.name());
detail::write(streambuf, ": ");
detail::write(streambuf, field.value());
detail::write(streambuf, "\r\n");
write(streambuf, field.name());
write(streambuf, ": ");
write(streambuf, field.value());
write(streambuf, "\r\n");
}
}

View File

@@ -10,7 +10,6 @@
#include <beast/http/chunk_encode.hpp>
#include <beast/http/resume_context.hpp>
#include <beast/http/detail/writes.hpp>
#include <beast/http/detail/write_preparation.hpp>
#include <beast/buffer_cat.hpp>
#include <beast/bind_handler.hpp>

View File

@@ -0,0 +1,59 @@
//
// Copyright (c) 2013-2016 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_WRITE_STREAMBUF_HPP
#define BEAST_WRITE_STREAMBUF_HPP
#include <beast/type_check.hpp>
#include <beast/detail/write_streambuf.hpp>
#include <type_traits>
#include <utility>
namespace beast {
/** Write to a Streambuf.
This function appends the serialized representation of each provided
argument into the stream buffer. It is capable of converting the
following types of arguments:
* `boost::asio::const_buffer`
* `boost::asio::mutable_buffer`
* A type for which the call to `boost::asio::buffer()` is defined
* A type meeting the requirements of `ConstBufferSequence`
* A type meeting the requirements of `MutableBufferSequence`
For all types not listed above, the function will invoke
`boost::lexical_cast` on the argument in an attempt to convert to
a string, which is then appended to the stream buffer.
When this function serializes numbers, it converts them to
their text representation as if by a call to `std::to_string`.
@param streambuf The stream buffer to write to.
@param args A list of one or more arguments to write.
@throws Any exceptions thrown by `boost::lexical_cast`.
@note This function participates in overload resolution only if
the `streambuf` parameter meets the requirements of Streambuf.
*/
template<class Streambuf, class... Args>
#if GENERATING_DOCS
void
#else
typename std::enable_if<is_Streambuf<Streambuf>::value>::type
#endif
write(Streambuf& streambuf, Args&&... args)
{
detail::write_streambuf(streambuf, std::forward<Args>(args)...);
}
} // beast
#endif

View File

@@ -50,7 +50,6 @@ unit-test http_tests :
unit-test websocket_tests :
main.cpp
../src/beast_http_nodejs_parser.cpp
websocket/error.cpp
websocket/option.cpp
websocket/rfc6455.cpp

35
test/write_streambuf.cpp Normal file
View File

@@ -0,0 +1,35 @@
//
// Copyright (c) 2013-2016 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)
//
// Test that header file is self-contained.
#include <beast/write_streambuf.hpp>
#include <beast/streambuf.hpp>
#include <beast/detail/unit_test/suite.hpp>
namespace beast {
class write_streambuf_test : public beast::detail::unit_test::suite
{
public:
void run() override
{
streambuf sb;
std::string s;
write(sb, boost::asio::const_buffer{"", 0});
write(sb, boost::asio::mutable_buffer{nullptr, 0});
write(sb, boost::asio::null_buffers{});
write(sb, boost::asio::const_buffers_1{"", 0});
write(sb, boost::asio::mutable_buffers_1{nullptr, 0});
write(sb, s);
write(sb, 23);
pass();
}
};
BEAST_DEFINE_TESTSUITE(write_streambuf,core,beast);
} // beast