mirror of
https://github.com/boostorg/beast.git
synced 2025-08-04 07:14:32 +02:00
Rename to DynamicBuffer (fix #47)
This commit is contained in:
@@ -14,10 +14,10 @@
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** Adapts a @b `MutableBufferSequence` into a @b `Streambuf`.
|
||||
/** Adapts a @b `MutableBufferSequence` into a @b `DynamicBuffer`.
|
||||
|
||||
This class wraps a @b `MutableBufferSequence` to meet the requirements
|
||||
of @b `Streambuf`. Upon construction the input and output sequences are
|
||||
of @b `DynamicBuffer`. Upon construction the input and output sequences are
|
||||
empty. A copy of the mutable buffer sequence object is stored; however,
|
||||
ownership of the underlying memory is not transferred. The caller is
|
||||
responsible for making sure that referenced memory remains valid
|
||||
|
@@ -129,7 +129,7 @@ protected:
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** A `Streambuf` with a fixed size internal buffer.
|
||||
/** A `DynamicBuffer` with a fixed size internal buffer.
|
||||
|
||||
@tparam N The number of bytes in the internal buffer.
|
||||
|
||||
|
@@ -12,14 +12,14 @@
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** A @b `Streambuf` that uses multiple buffers internally.
|
||||
/** A @b `DynamicBuffer` that uses multiple buffers internally.
|
||||
|
||||
The implementation uses a sequence of one or more character arrays
|
||||
of varying sizes. Additional character array objects are appended to
|
||||
the sequence to accommodate changes in the size of the character
|
||||
sequence.
|
||||
|
||||
@note Meets the requirements of @b `Streambuf`.
|
||||
@note Meets the requirements of @b `DynamicBuffer`.
|
||||
*/
|
||||
using streambuf = basic_streambuf<std::allocator<char>>;
|
||||
|
||||
|
@@ -472,85 +472,85 @@ public:
|
||||
return s;
|
||||
}
|
||||
|
||||
template<class Streambuf>
|
||||
template<class DynamicBuffer>
|
||||
void
|
||||
headers(Streambuf& sb)
|
||||
headers(DynamicBuffer& db)
|
||||
{
|
||||
while(rand(6))
|
||||
{
|
||||
write(sb, field());
|
||||
write(sb, rand(4) ? ": " : ":");
|
||||
write(sb, value());
|
||||
write(sb, "\r\n");
|
||||
write(db, field());
|
||||
write(db, rand(4) ? ": " : ":");
|
||||
write(db, value());
|
||||
write(db, "\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
template<class Streambuf>
|
||||
template<class DynamicBuffer>
|
||||
void
|
||||
body(Streambuf& sb)
|
||||
body(DynamicBuffer& db)
|
||||
{
|
||||
if(! rand(4))
|
||||
{
|
||||
write(sb, "Content-Length: 0\r\n\r\n");
|
||||
write(db, "Content-Length: 0\r\n\r\n");
|
||||
return;
|
||||
}
|
||||
if(rand(2))
|
||||
{
|
||||
auto const len = rand(500);
|
||||
write(sb, "Content-Length: ", len, "\r\n\r\n");
|
||||
for(auto const& b : sb.prepare(len))
|
||||
write(db, "Content-Length: ", len, "\r\n\r\n");
|
||||
for(auto const& b : db.prepare(len))
|
||||
{
|
||||
auto p = boost::asio::buffer_cast<char*>(b);
|
||||
auto n = boost::asio::buffer_size(b);
|
||||
while(n--)
|
||||
*p++ = static_cast<char>(32 + rand(26+26+10+6));
|
||||
}
|
||||
sb.commit(len);
|
||||
db.commit(len);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto len = rand(500);
|
||||
write(sb, "Transfer-Encoding: chunked\r\n\r\n");
|
||||
write(db, "Transfer-Encoding: chunked\r\n\r\n");
|
||||
while(len > 0)
|
||||
{
|
||||
auto n = std::min(1 + rand(300), len);
|
||||
len -= n;
|
||||
write(sb, to_hex(n), "\r\n");
|
||||
for(auto const& b : sb.prepare(n))
|
||||
write(db, to_hex(n), "\r\n");
|
||||
for(auto const& b : db.prepare(n))
|
||||
{
|
||||
auto p = boost::asio::buffer_cast<char*>(b);
|
||||
auto m = boost::asio::buffer_size(b);
|
||||
while(m--)
|
||||
*p++ = static_cast<char>(32 + rand(26+26+10+6));
|
||||
}
|
||||
sb.commit(n);
|
||||
write(sb, "\r\n");
|
||||
db.commit(n);
|
||||
write(db, "\r\n");
|
||||
}
|
||||
write(sb, "0\r\n\r\n");
|
||||
write(db, "0\r\n\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
template<class Streambuf>
|
||||
template<class DynamicBuffer>
|
||||
void
|
||||
request(Streambuf& sb)
|
||||
request(DynamicBuffer& db)
|
||||
{
|
||||
write(sb, method(), " ", uri(), " HTTP/1.1\r\n");
|
||||
headers(sb);
|
||||
body(sb);
|
||||
write(db, method(), " ", uri(), " HTTP/1.1\r\n");
|
||||
headers(db);
|
||||
body(db);
|
||||
}
|
||||
|
||||
template<class Streambuf>
|
||||
template<class DynamicBuffer>
|
||||
void
|
||||
response(Streambuf& sb)
|
||||
response(DynamicBuffer& db)
|
||||
{
|
||||
write(sb, "HTTP/1.");
|
||||
write(sb, rand(2) ? "0" : "1");
|
||||
write(sb, " ", 100 + rand(401), " ");
|
||||
write(sb, token());
|
||||
write(sb, "\r\n");
|
||||
headers(sb);
|
||||
body(sb);
|
||||
write(sb, "\r\n");
|
||||
write(db, "HTTP/1.");
|
||||
write(db, rand(2) ? "0" : "1");
|
||||
write(db, " ", 100 + rand(401), " ");
|
||||
write(db, token());
|
||||
write(db, "\r\n");
|
||||
headers(db);
|
||||
body(db);
|
||||
write(db, "\r\n");
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -109,15 +109,15 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
template<class NextLayer, class Streambuf>
|
||||
template<class NextLayer, class DynamicBuffer>
|
||||
static
|
||||
void
|
||||
read(stream<NextLayer>& ws, opcode& op, Streambuf& sb)
|
||||
read(stream<NextLayer>& ws, opcode& op, DynamicBuffer& db)
|
||||
{
|
||||
frame_info fi;
|
||||
for(;;)
|
||||
{
|
||||
ws.read_frame(fi, sb);
|
||||
ws.read_frame(fi, db);
|
||||
op = fi.op;
|
||||
if(fi.fin)
|
||||
break;
|
||||
@@ -453,11 +453,11 @@ public:
|
||||
if(! expect(! ec, ec.message()))
|
||||
break;
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.read(op, sb, ec);
|
||||
streambuf db;
|
||||
ws.read(op, db, ec);
|
||||
if(! expect(! ec, ec.message()))
|
||||
break;
|
||||
expect(to_string(sb.data()) ==
|
||||
expect(to_string(db.data()) ==
|
||||
std::string{v.data(), v.size()});
|
||||
v.push_back(n+1);
|
||||
}
|
||||
@@ -479,11 +479,11 @@ public:
|
||||
if(! expect(! ec, ec.message()))
|
||||
break;
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.async_read(op, sb, do_yield[ec]);
|
||||
streambuf db;
|
||||
ws.async_read(op, db, do_yield[ec]);
|
||||
if(! expect(! ec, ec.message()))
|
||||
break;
|
||||
expect(to_string(sb.data()) ==
|
||||
expect(to_string(db.data()) ==
|
||||
std::string{v.data(), v.size()});
|
||||
v.push_back(n+1);
|
||||
}
|
||||
@@ -544,9 +544,9 @@ public:
|
||||
|
||||
// Read
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
streambuf db;
|
||||
++count;
|
||||
ws.async_read(op, sb,
|
||||
ws.async_read(op, db,
|
||||
[&](error_code ec)
|
||||
{
|
||||
--count;
|
||||
@@ -600,11 +600,11 @@ public:
|
||||
ws.write(buffer_cat(sbuf("TEXT"),
|
||||
cbuf(0x03, 0xea, 0xf0, 0x28, 0x8c, 0xbc)));
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
streambuf db;
|
||||
std::size_t count = 0;
|
||||
// Read text message with bad utf8.
|
||||
// Causes a close to be sent, blocking writes.
|
||||
ws.async_read(op, sb,
|
||||
ws.async_read(op, db,
|
||||
[&](error_code ec)
|
||||
{
|
||||
// Read should fail with protocol error
|
||||
@@ -612,7 +612,7 @@ public:
|
||||
expect(ec == error::failed,
|
||||
ec.message());
|
||||
// Reads after failure are aborted
|
||||
ws.async_read(op, sb,
|
||||
ws.async_read(op, db,
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
@@ -668,11 +668,11 @@ public:
|
||||
ws.set_option(message_type(opcode::binary));
|
||||
ws.write(sbuf("CLOSE"));
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
streambuf db;
|
||||
std::size_t count = 0;
|
||||
// Read a close frame.
|
||||
// Sends a close frame, blocking writes.
|
||||
ws.async_read(op, sb,
|
||||
ws.async_read(op, db,
|
||||
[&](error_code ec)
|
||||
{
|
||||
// Read should complete with error::closed
|
||||
@@ -734,9 +734,9 @@ public:
|
||||
ws.set_option(message_type(opcode::binary));
|
||||
ws.write(sbuf("CLOSE"));
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
streambuf db;
|
||||
std::size_t count = 0;
|
||||
ws.async_read(op, sb,
|
||||
ws.async_read(op, db,
|
||||
[&](error_code ec)
|
||||
{
|
||||
++count;
|
||||
@@ -785,8 +785,8 @@ public:
|
||||
});
|
||||
});
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.async_read(op, sb,
|
||||
streambuf db;
|
||||
ws.async_read(op, db,
|
||||
[&](error_code ec)
|
||||
{
|
||||
expect(ec == error::closed, ec.message());
|
||||
@@ -811,8 +811,8 @@ public:
|
||||
try
|
||||
{
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.read(op, sb);
|
||||
streambuf db;
|
||||
ws.read(op, db);
|
||||
fail();
|
||||
return false;
|
||||
}
|
||||
@@ -846,10 +846,10 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
read(ws, op, sb);
|
||||
streambuf db;
|
||||
read(ws, op, db);
|
||||
expect(op == opcode::text);
|
||||
expect(to_string(sb.data()) == "Hello");
|
||||
expect(to_string(db.data()) == "Hello");
|
||||
}
|
||||
|
||||
// close, no payload
|
||||
@@ -882,11 +882,11 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.read(op, sb);
|
||||
streambuf db;
|
||||
ws.read(op, db);
|
||||
expect(pong == 1);
|
||||
expect(op == opcode::binary);
|
||||
expect(to_string(sb.data()) == "Hello");
|
||||
expect(to_string(db.data()) == "Hello");
|
||||
}
|
||||
ws.set_option(pong_callback{});
|
||||
|
||||
@@ -903,10 +903,10 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.read(op, sb);
|
||||
streambuf db;
|
||||
ws.read(op, db);
|
||||
expect(pong == 1);
|
||||
expect(to_string(sb.data()) == "Hello, World!");
|
||||
expect(to_string(db.data()) == "Hello, World!");
|
||||
}
|
||||
ws.set_option(pong_callback{});
|
||||
|
||||
@@ -916,9 +916,9 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.read(op, sb);
|
||||
expect(to_string(sb.data()) == "Hello");
|
||||
streambuf db;
|
||||
ws.read(op, db);
|
||||
expect(to_string(db.data()) == "Hello");
|
||||
}
|
||||
ws.set_option(auto_fragment_size(0));
|
||||
|
||||
@@ -930,9 +930,9 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.read(op, sb);
|
||||
expect(to_string(sb.data()) == s);
|
||||
streambuf db;
|
||||
ws.read(op, db);
|
||||
expect(to_string(db.data()) == s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -944,10 +944,10 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.read(op, sb);
|
||||
streambuf db;
|
||||
ws.read(op, db);
|
||||
expect(op == opcode::text);
|
||||
expect(to_string(sb.data()) == "Hello");
|
||||
expect(to_string(db.data()) == "Hello");
|
||||
}
|
||||
|
||||
// cause close
|
||||
@@ -1040,9 +1040,9 @@ public:
|
||||
[&](error_code ev)
|
||||
{
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
streambuf db;
|
||||
error_code ec;
|
||||
ws.async_read(op, sb, do_yield[ec]);
|
||||
ws.async_read(op, db, do_yield[ec]);
|
||||
if(! ec)
|
||||
{
|
||||
fail();
|
||||
@@ -1085,12 +1085,12 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.async_read(op, sb, do_yield[ec]);
|
||||
streambuf db;
|
||||
ws.async_read(op, db, do_yield[ec]);
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
expect(op == opcode::text);
|
||||
expect(to_string(sb.data()) == "Hello");
|
||||
expect(to_string(db.data()) == "Hello");
|
||||
}
|
||||
|
||||
// close, no payload
|
||||
@@ -1133,12 +1133,12 @@ public:
|
||||
throw system_error{ec};
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.async_read(op, sb, do_yield[ec]);
|
||||
streambuf db;
|
||||
ws.async_read(op, db, do_yield[ec]);
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
expect(op == opcode::binary);
|
||||
expect(to_string(sb.data()) == "Hello");
|
||||
expect(to_string(db.data()) == "Hello");
|
||||
ws.set_option(pong_callback{});
|
||||
}
|
||||
|
||||
@@ -1161,11 +1161,11 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.async_read(op, sb, do_yield[ec]);
|
||||
streambuf db;
|
||||
ws.async_read(op, db, do_yield[ec]);
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
expect(to_string(sb.data()) == "Hello, World!");
|
||||
expect(to_string(db.data()) == "Hello, World!");
|
||||
}
|
||||
ws.set_option(pong_callback{});
|
||||
}
|
||||
@@ -1176,11 +1176,11 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.async_read(op, sb, do_yield[ec]);
|
||||
streambuf db;
|
||||
ws.async_read(op, db, do_yield[ec]);
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
expect(to_string(sb.data()) == "Hello");
|
||||
expect(to_string(db.data()) == "Hello");
|
||||
}
|
||||
ws.set_option(auto_fragment_size(0));
|
||||
|
||||
@@ -1194,11 +1194,11 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.async_read(op, sb, do_yield[ec]);
|
||||
streambuf db;
|
||||
ws.async_read(op, db, do_yield[ec]);
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
expect(to_string(sb.data()) == s);
|
||||
expect(to_string(db.data()) == s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1214,12 +1214,12 @@ public:
|
||||
{
|
||||
// receive echoed message
|
||||
opcode op;
|
||||
streambuf sb;
|
||||
ws.async_read(op, sb, do_yield[ec]);
|
||||
streambuf db;
|
||||
ws.async_read(op, db, do_yield[ec]);
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
expect(op == opcode::text);
|
||||
expect(to_string(sb.data()) == "Hello");
|
||||
expect(to_string(db.data()) == "Hello");
|
||||
}
|
||||
|
||||
// cause close
|
||||
|
@@ -94,7 +94,7 @@ private:
|
||||
stream<socket_type> ws;
|
||||
boost::asio::io_service::strand strand;
|
||||
opcode op;
|
||||
beast::streambuf sb;
|
||||
beast::streambuf db;
|
||||
int id;
|
||||
|
||||
data(bool log_, socket_type&& sock_)
|
||||
@@ -177,22 +177,22 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
template<class Streambuf, std::size_t N>
|
||||
template<class DynamicBuffer, std::size_t N>
|
||||
static
|
||||
bool
|
||||
match(Streambuf& sb, char const(&s)[N])
|
||||
match(DynamicBuffer& db, char const(&s)[N])
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
using boost::asio::buffer_copy;
|
||||
if(sb.size() < N-1)
|
||||
if(db.size() < N-1)
|
||||
return false;
|
||||
static_string<N-1> t;
|
||||
t.resize(N-1);
|
||||
buffer_copy(buffer(t.data(), t.size()),
|
||||
sb.data());
|
||||
db.data());
|
||||
if(t != s)
|
||||
return false;
|
||||
sb.consume(N-1);
|
||||
db.consume(N-1);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -217,10 +217,10 @@ private:
|
||||
case 1:
|
||||
if(ec)
|
||||
return fail(ec, "async_handshake");
|
||||
d.sb.consume(d.sb.size());
|
||||
d.db.consume(d.db.size());
|
||||
// read message
|
||||
d.state = 2;
|
||||
d.ws.async_read(d.op, d.sb,
|
||||
d.ws.async_read(d.op, d.db,
|
||||
d.strand.wrap(std::move(*this)));
|
||||
return;
|
||||
|
||||
@@ -230,33 +230,33 @@ private:
|
||||
return;
|
||||
if(ec)
|
||||
return fail(ec, "async_read");
|
||||
if(match(d.sb, "RAW"))
|
||||
if(match(d.db, "RAW"))
|
||||
{
|
||||
d.state = 1;
|
||||
boost::asio::async_write(d.ws.next_layer(),
|
||||
d.sb.data(), d.strand.wrap(std::move(*this)));
|
||||
d.db.data(), d.strand.wrap(std::move(*this)));
|
||||
return;
|
||||
}
|
||||
else if(match(d.sb, "TEXT"))
|
||||
else if(match(d.db, "TEXT"))
|
||||
{
|
||||
d.state = 1;
|
||||
d.ws.set_option(message_type{opcode::text});
|
||||
d.ws.async_write(
|
||||
d.sb.data(), d.strand.wrap(std::move(*this)));
|
||||
d.db.data(), d.strand.wrap(std::move(*this)));
|
||||
return;
|
||||
}
|
||||
else if(match(d.sb, "PING"))
|
||||
else if(match(d.db, "PING"))
|
||||
{
|
||||
ping_data payload;
|
||||
d.sb.consume(buffer_copy(
|
||||
d.db.consume(buffer_copy(
|
||||
buffer(payload.data(), payload.size()),
|
||||
d.sb.data()));
|
||||
d.db.data()));
|
||||
d.state = 1;
|
||||
d.ws.async_ping(payload,
|
||||
d.strand.wrap(std::move(*this)));
|
||||
return;
|
||||
}
|
||||
else if(match(d.sb, "CLOSE"))
|
||||
else if(match(d.db, "CLOSE"))
|
||||
{
|
||||
d.state = 1;
|
||||
d.ws.async_close({},
|
||||
@@ -266,7 +266,7 @@ private:
|
||||
// write message
|
||||
d.state = 1;
|
||||
d.ws.set_option(message_type(d.op));
|
||||
d.ws.async_write(d.sb.data(),
|
||||
d.ws.async_write(d.db.data(),
|
||||
d.strand.wrap(std::move(*this)));
|
||||
return;
|
||||
|
||||
|
@@ -151,22 +151,22 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
template<class Streambuf, std::size_t N>
|
||||
template<class DynamicBuffer, std::size_t N>
|
||||
static
|
||||
bool
|
||||
match(Streambuf& sb, char const(&s)[N])
|
||||
match(DynamicBuffer& db, char const(&s)[N])
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
using boost::asio::buffer_copy;
|
||||
if(sb.size() < N-1)
|
||||
if(db.size() < N-1)
|
||||
return false;
|
||||
static_string<N-1> t;
|
||||
t.resize(N-1);
|
||||
buffer_copy(buffer(t.data(), t.size()),
|
||||
sb.data());
|
||||
db.data());
|
||||
if(t != s)
|
||||
return false;
|
||||
sb.consume(N-1);
|
||||
db.consume(N-1);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user