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 parent
87fd60c151
commit
a7ef4f53dc
16 files changed
+373
-337
No files matched your search
+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