forked from boostorg/beast
New ostream() returns dynamic buffer output stream (API Change):
This eliminates beast::write output for dynamic buffers and replaces it with the function ostream() that wraps a caller provided dynamic buffer and returns the result as a std::ostream derived object. Callers may now produce formatted output into any object meeting the requirements of DynamicBuffer using operator<< and the standard stream modifiers such as std::endl. This new technique is more efficient, as implementations of operator<< can now write directly into the output using std::ostream::write and std::ostream::put. Example of use: beast::streambuf sb; beast::ostream(sb) << "Hello, world!" << std::endl;
This commit is contained in:
+1
-1
@@ -29,6 +29,7 @@ unit-test core-tests :
|
||||
core/handler_alloc.cpp
|
||||
core/handler_concepts.cpp
|
||||
core/handler_ptr.cpp
|
||||
core/ostream.cpp
|
||||
core/placeholders.cpp
|
||||
core/prepare_buffer.cpp
|
||||
core/prepare_buffers.cpp
|
||||
@@ -37,7 +38,6 @@ unit-test core-tests :
|
||||
core/stream_concepts.cpp
|
||||
core/streambuf.cpp
|
||||
core/to_string.cpp
|
||||
core/write_dynabuf.cpp
|
||||
core/base64.cpp
|
||||
core/empty_base_optimization.cpp
|
||||
core/get_lowest_layer.cpp
|
||||
|
||||
@@ -22,6 +22,7 @@ add_executable (core-tests
|
||||
handler_alloc.cpp
|
||||
handler_concepts.cpp
|
||||
handler_ptr.cpp
|
||||
ostream.cpp
|
||||
placeholders.cpp
|
||||
prepare_buffer.cpp
|
||||
prepare_buffers.cpp
|
||||
@@ -30,7 +31,6 @@ add_executable (core-tests
|
||||
stream_concepts.cpp
|
||||
streambuf.cpp
|
||||
to_string.cpp
|
||||
write_dynabuf.cpp
|
||||
base64.cpp
|
||||
empty_base_optimization.cpp
|
||||
get_lowest_layer.cpp
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/core/ostream.hpp>
|
||||
|
||||
#include <beast/core/streambuf.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <ostream>
|
||||
|
||||
#include <beast/core/to_string.hpp>
|
||||
|
||||
namespace beast {
|
||||
|
||||
class ostream_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void run() override
|
||||
{
|
||||
streambuf sb;
|
||||
ostream(sb) << "Hello, world!\n";
|
||||
BEAST_EXPECT(to_string(sb.data()) == "Hello, world!\n");
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(ostream,core,beast);
|
||||
|
||||
} // beast
|
||||
@@ -273,13 +273,6 @@ public:
|
||||
BEAST_EXPECT(test::buffer_count(sb.data()) == 4);
|
||||
}
|
||||
|
||||
void testOutputStream()
|
||||
{
|
||||
streambuf sb;
|
||||
sb << "x";
|
||||
BEAST_EXPECT(to_string(sb.data()) == "x");
|
||||
}
|
||||
|
||||
void testCapacity()
|
||||
{
|
||||
using boost::asio::buffer_size;
|
||||
@@ -353,7 +346,6 @@ public:
|
||||
testConsume();
|
||||
testMatrix();
|
||||
testIterators();
|
||||
testOutputStream();
|
||||
testCapacity();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/core/write_dynabuf.hpp>
|
||||
|
||||
#include <beast/core/streambuf.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
|
||||
namespace beast {
|
||||
|
||||
class write_dynabuf_test : public beast::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_dynabuf,core,beast);
|
||||
|
||||
} // beast
|
||||
+28
-22
@@ -8,7 +8,7 @@
|
||||
#ifndef BEAST_HTTP_TEST_MESSAGE_FUZZ_HPP
|
||||
#define BEAST_HTTP_TEST_MESSAGE_FUZZ_HPP
|
||||
|
||||
#include <beast/core/write_dynabuf.hpp>
|
||||
#include <beast/core/ostream.hpp>
|
||||
#include <beast/http/detail/rfc7230.hpp>
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
@@ -319,7 +319,7 @@ public:
|
||||
}
|
||||
|
||||
std::string
|
||||
uri()
|
||||
target()
|
||||
{
|
||||
//switch(rand(4))
|
||||
switch(1)
|
||||
@@ -348,7 +348,7 @@ public:
|
||||
|
||||
#if 0
|
||||
std::string
|
||||
uri()
|
||||
target()
|
||||
{
|
||||
static char constexpr alpha[63] =
|
||||
"0123456789" "ABCDEFGHIJ" "KLMNOPQRST"
|
||||
@@ -476,13 +476,13 @@ public:
|
||||
void
|
||||
fields(DynamicBuffer& db)
|
||||
{
|
||||
auto os = ostream(db);
|
||||
while(rand(6))
|
||||
{
|
||||
write(db, field());
|
||||
write(db, rand(4) ? ": " : ":");
|
||||
write(db, value());
|
||||
write(db, "\r\n");
|
||||
}
|
||||
os <<
|
||||
field() <<
|
||||
(rand(4) ? ": " : ":") <<
|
||||
value() <<
|
||||
"\r\n";
|
||||
}
|
||||
|
||||
template<class DynamicBuffer>
|
||||
@@ -491,13 +491,15 @@ public:
|
||||
{
|
||||
if(! rand(4))
|
||||
{
|
||||
write(db, "Content-Length: 0\r\n\r\n");
|
||||
ostream(db) <<
|
||||
"Content-Length: 0\r\n\r\n";
|
||||
return;
|
||||
}
|
||||
if(rand(2))
|
||||
{
|
||||
auto const len = rand(500);
|
||||
write(db, "Content-Length: ", len, "\r\n\r\n");
|
||||
ostream(db) <<
|
||||
"Content-Length: " << len << "\r\n\r\n";
|
||||
for(auto const& b : db.prepare(len))
|
||||
{
|
||||
auto p = boost::asio::buffer_cast<char*>(b);
|
||||
@@ -510,12 +512,14 @@ public:
|
||||
else
|
||||
{
|
||||
auto len = rand(500);
|
||||
write(db, "Transfer-Encoding: chunked\r\n\r\n");
|
||||
ostream(db) <<
|
||||
"Transfer-Encoding: chunked\r\n\r\n";
|
||||
while(len > 0)
|
||||
{
|
||||
auto n = (std::min)(1 + rand(300), len);
|
||||
len -= n;
|
||||
write(db, to_hex(n), "\r\n");
|
||||
ostream(db) <<
|
||||
to_hex(n) << "\r\n";
|
||||
for(auto const& b : db.prepare(n))
|
||||
{
|
||||
auto p = boost::asio::buffer_cast<char*>(b);
|
||||
@@ -524,9 +528,9 @@ public:
|
||||
*p++ = static_cast<char>(32 + rand(26+26+10+6));
|
||||
}
|
||||
db.commit(n);
|
||||
write(db, "\r\n");
|
||||
ostream(db) << "\r\n";
|
||||
}
|
||||
write(db, "0\r\n\r\n");
|
||||
ostream(db) << "0\r\n\r\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -534,7 +538,8 @@ public:
|
||||
void
|
||||
request(DynamicBuffer& db)
|
||||
{
|
||||
write(db, method(), " ", uri(), " HTTP/1.1\r\n");
|
||||
ostream(db) <<
|
||||
method() << " " << target() << " HTTP/1.1\r\n";
|
||||
fields(db);
|
||||
body(db);
|
||||
}
|
||||
@@ -543,14 +548,15 @@ public:
|
||||
void
|
||||
response(DynamicBuffer& db)
|
||||
{
|
||||
write(db, "HTTP/1.");
|
||||
write(db, rand(2) ? "0" : "1");
|
||||
write(db, " ", 100 + rand(401), " ");
|
||||
write(db, token());
|
||||
write(db, "\r\n");
|
||||
ostream(db) <<
|
||||
"HTTP/1." <<
|
||||
(rand(2) ? "0" : "1") << " " <<
|
||||
(100 + rand(401)) << " " <<
|
||||
token() <<
|
||||
"\r\n";
|
||||
fields(db);
|
||||
body(db);
|
||||
write(db, "\r\n");
|
||||
ostream(db) << "\r\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user