2017-07-20 08:01:46 -07:00
|
|
|
//
|
2019-02-21 07:00:31 -08:00
|
|
|
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-07-20 08:01:46 -07:00
|
|
|
//
|
|
|
|
|
// 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)
|
|
|
|
|
//
|
2017-07-20 13:40:34 -07:00
|
|
|
// Official repository: https://github.com/boostorg/beast
|
|
|
|
|
//
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
|
// Test that header file is self-contained.
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/http/read.hpp>
|
2016-05-04 17:27:50 -04:00
|
|
|
|
2016-11-20 07:32:41 -05:00
|
|
|
#include "test_parser.hpp"
|
2016-06-03 11:40:55 -04:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/ostream.hpp>
|
|
|
|
|
#include <boost/beast/core/flat_static_buffer.hpp>
|
|
|
|
|
#include <boost/beast/http/fields.hpp>
|
|
|
|
|
#include <boost/beast/http/dynamic_body.hpp>
|
|
|
|
|
#include <boost/beast/http/parser.hpp>
|
|
|
|
|
#include <boost/beast/http/string_body.hpp>
|
2018-11-11 14:07:55 -08:00
|
|
|
#include <boost/beast/_experimental/test/stream.hpp>
|
|
|
|
|
#include <boost/beast/_experimental/unit_test/suite.hpp>
|
2018-11-22 20:49:30 -08:00
|
|
|
#include <boost/beast/test/yield_to.hpp>
|
|
|
|
|
#include <boost/asio/io_context.hpp>
|
2018-12-05 14:18:57 -08:00
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
2020-05-08 13:54:40 +02:00
|
|
|
#include <boost/asio/strand.hpp>
|
|
|
|
|
#include <boost/asio/write.hpp>
|
2017-03-31 10:12:58 -04:00
|
|
|
#include <atomic>
|
2020-05-08 13:54:40 +02:00
|
|
|
|
2020-04-27 18:45:05 +02:00
|
|
|
#if BOOST_ASIO_HAS_CO_AWAIT
|
|
|
|
|
#include <boost/asio/use_awaitable.hpp>
|
|
|
|
|
#endif
|
2016-05-04 17:27:50 -04:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2016-05-04 17:27:50 -04:00
|
|
|
namespace beast {
|
|
|
|
|
namespace http {
|
|
|
|
|
|
|
|
|
|
class read_test
|
2016-05-06 19:14:17 -04:00
|
|
|
: public beast::unit_test::suite
|
2016-05-04 17:27:50 -04:00
|
|
|
, public test::enable_yield_to
|
|
|
|
|
{
|
|
|
|
|
public:
|
2016-04-30 10:29:39 -04:00
|
|
|
template<bool isRequest>
|
2016-11-20 07:32:41 -05:00
|
|
|
void
|
|
|
|
|
failMatrix(char const* s, yield_context do_yield)
|
2016-04-30 10:29:39 -04:00
|
|
|
{
|
|
|
|
|
static std::size_t constexpr limit = 100;
|
|
|
|
|
std::size_t n;
|
|
|
|
|
auto const len = strlen(s);
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
|
|
|
|
{
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2019-02-02 10:53:54 -08:00
|
|
|
b.commit(net::buffer_copy(
|
|
|
|
|
b.prepare(len), net::buffer(s, len)));
|
2018-05-01 14:19:35 -07:00
|
|
|
test::fail_count fc(n);
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc_, fc};
|
2016-11-20 07:32:41 -05:00
|
|
|
test_parser<isRequest> p(fc);
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-08-21 14:45:58 -07:00
|
|
|
ts.close_remote();
|
2017-08-15 22:20:13 -07:00
|
|
|
read(ts, b, p, ec);
|
2016-04-30 10:29:39 -04:00
|
|
|
if(! ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-04-30 10:29:39 -04:00
|
|
|
for(n = 0; n < limit; ++n)
|
|
|
|
|
{
|
|
|
|
|
static std::size_t constexpr pre = 10;
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2019-02-02 10:53:54 -08:00
|
|
|
b.commit(net::buffer_copy(
|
|
|
|
|
b.prepare(pre), net::buffer(s, pre)));
|
2018-05-01 14:19:35 -07:00
|
|
|
test::fail_count fc(n);
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc_, fc,
|
2017-08-15 22:20:13 -07:00
|
|
|
std::string(s + pre, len - pre)};
|
2016-11-20 07:32:41 -05:00
|
|
|
test_parser<isRequest> p(fc);
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-08-21 14:45:58 -07:00
|
|
|
ts.close_remote();
|
2017-08-15 22:20:13 -07:00
|
|
|
read(ts, b, p, ec);
|
2016-04-30 10:29:39 -04:00
|
|
|
if(! ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-04-30 10:29:39 -04:00
|
|
|
for(n = 0; n < limit; ++n)
|
|
|
|
|
{
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2019-02-02 10:53:54 -08:00
|
|
|
b.commit(net::buffer_copy(
|
|
|
|
|
b.prepare(len), net::buffer(s, len)));
|
2018-05-01 14:19:35 -07:00
|
|
|
test::fail_count fc(n);
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc_, fc};
|
2016-11-20 07:32:41 -05:00
|
|
|
test_parser<isRequest> p(fc);
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-08-21 14:45:58 -07:00
|
|
|
ts.close_remote();
|
2017-08-15 22:20:13 -07:00
|
|
|
async_read(ts, b, p, do_yield[ec]);
|
2016-04-30 10:29:39 -04:00
|
|
|
if(! ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-04-30 10:29:39 -04:00
|
|
|
for(n = 0; n < limit; ++n)
|
2017-09-09 06:39:49 -07:00
|
|
|
{
|
|
|
|
|
multi_buffer b;
|
2019-02-02 10:53:54 -08:00
|
|
|
b.commit(net::buffer_copy(
|
|
|
|
|
b.prepare(len), net::buffer(s, len)));
|
2018-05-01 14:19:35 -07:00
|
|
|
test::fail_count fc(n);
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc_, fc};
|
2017-09-09 06:39:49 -07:00
|
|
|
test_parser<isRequest> p(fc);
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-09-09 06:39:49 -07:00
|
|
|
ts.close_remote();
|
|
|
|
|
async_read_header(ts, b, p, do_yield[ec]);
|
|
|
|
|
if(! ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
BEAST_EXPECT(n < limit);
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
2016-04-30 10:29:39 -04:00
|
|
|
{
|
|
|
|
|
static std::size_t constexpr pre = 10;
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2019-02-02 10:53:54 -08:00
|
|
|
b.commit(net::buffer_copy(
|
|
|
|
|
b.prepare(pre), net::buffer(s, pre)));
|
2018-05-01 14:19:35 -07:00
|
|
|
test::fail_count fc(n);
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts(ioc_, fc,
|
2017-08-15 22:20:13 -07:00
|
|
|
std::string{s + pre, len - pre});
|
2016-11-20 07:32:41 -05:00
|
|
|
test_parser<isRequest> p(fc);
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-08-21 14:45:58 -07:00
|
|
|
ts.close_remote();
|
2017-08-15 22:20:13 -07:00
|
|
|
async_read(ts, b, p, do_yield[ec]);
|
2016-10-15 09:29:14 -04:00
|
|
|
if(! ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-04-30 10:29:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testThrow()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream c{ioc_, "GET / X"};
|
2017-08-21 14:45:58 -07:00
|
|
|
c.close_remote();
|
2017-05-31 08:01:55 -07:00
|
|
|
request_parser<dynamic_body> p;
|
2017-07-29 17:47:04 -07:00
|
|
|
read(c, b, p);
|
2016-04-30 10:29:39 -04:00
|
|
|
fail();
|
|
|
|
|
}
|
|
|
|
|
catch(std::exception const&)
|
|
|
|
|
{
|
|
|
|
|
pass();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-31 08:01:55 -07:00
|
|
|
void
|
|
|
|
|
testBufferOverflow()
|
|
|
|
|
{
|
|
|
|
|
{
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream c{ioc_};
|
2017-07-29 17:47:04 -07:00
|
|
|
ostream(c.buffer()) <<
|
2017-05-31 08:01:55 -07:00
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"10\r\n"
|
|
|
|
|
"****************\r\n"
|
|
|
|
|
"0\r\n\r\n";
|
2017-07-14 20:25:39 -07:00
|
|
|
flat_static_buffer<1024> b;
|
2017-05-31 08:01:55 -07:00
|
|
|
request<string_body> req;
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-29 17:47:04 -07:00
|
|
|
read(c, b, req);
|
2017-05-31 08:01:55 -07:00
|
|
|
pass();
|
|
|
|
|
}
|
|
|
|
|
catch(std::exception const& e)
|
|
|
|
|
{
|
|
|
|
|
fail(e.what(), __FILE__, __LINE__);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
{
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream c{ioc_};
|
2017-07-29 17:47:04 -07:00
|
|
|
ostream(c.buffer()) <<
|
2017-05-31 08:01:55 -07:00
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"10\r\n"
|
|
|
|
|
"****************\r\n"
|
|
|
|
|
"0\r\n\r\n";
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-07-14 20:25:39 -07:00
|
|
|
flat_static_buffer<10> b;
|
2017-05-31 08:01:55 -07:00
|
|
|
request<string_body> req;
|
2017-07-29 17:47:04 -07:00
|
|
|
read(c, b, req, ec);
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECTS(ec == error::buffer_overflow,
|
2017-05-31 08:01:55 -07:00
|
|
|
ec.message());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-30 10:29:39 -04:00
|
|
|
void testFailures(yield_context do_yield)
|
|
|
|
|
{
|
|
|
|
|
char const* req[] = {
|
|
|
|
|
"GET / HTTP/1.0\r\n"
|
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
|
"Empty:\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
,
|
|
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
|
"Content-Length: 2\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"**"
|
|
|
|
|
,
|
|
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"10\r\n"
|
|
|
|
|
"****************\r\n"
|
|
|
|
|
"0\r\n\r\n"
|
|
|
|
|
,
|
|
|
|
|
nullptr
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
char const* res[] = {
|
|
|
|
|
"HTTP/1.0 200 OK\r\n"
|
|
|
|
|
"Server: test\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
,
|
|
|
|
|
"HTTP/1.0 200 OK\r\n"
|
|
|
|
|
"Server: test\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"***"
|
|
|
|
|
,
|
|
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
|
"Server: test\r\n"
|
|
|
|
|
"Content-Length: 3\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"***"
|
|
|
|
|
,
|
|
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
|
"Server: test\r\n"
|
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"10\r\n"
|
|
|
|
|
"****************\r\n"
|
|
|
|
|
"0\r\n\r\n"
|
|
|
|
|
,
|
|
|
|
|
nullptr
|
|
|
|
|
};
|
|
|
|
|
for(std::size_t i = 0; req[i]; ++i)
|
|
|
|
|
failMatrix<true>(req[i], do_yield);
|
|
|
|
|
for(std::size_t i = 0; res[i]; ++i)
|
|
|
|
|
failMatrix<false>(res[i], do_yield);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-04 17:27:50 -04:00
|
|
|
void testRead(yield_context do_yield)
|
|
|
|
|
{
|
|
|
|
|
static std::size_t constexpr limit = 100;
|
|
|
|
|
std::size_t n;
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
2016-05-04 17:27:50 -04:00
|
|
|
{
|
2018-05-01 14:19:35 -07:00
|
|
|
test::fail_count fc{n};
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream c{ioc_, fc,
|
2016-05-04 17:27:50 -04:00
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
|
"Content-Length: 0\r\n"
|
|
|
|
|
"\r\n"
|
2017-07-29 17:47:04 -07:00
|
|
|
};
|
2017-04-27 18:26:21 -07:00
|
|
|
request<dynamic_body> m;
|
2016-05-04 17:27:50 -04:00
|
|
|
try
|
|
|
|
|
{
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2017-07-29 17:47:04 -07:00
|
|
|
read(c, b, m);
|
2016-05-04 17:27:50 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch(std::exception const&)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
2016-05-04 17:27:50 -04:00
|
|
|
{
|
2018-05-01 14:19:35 -07:00
|
|
|
test::fail_count fc{n};
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc_, fc,
|
2016-05-04 17:27:50 -04:00
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
|
"Content-Length: 0\r\n"
|
|
|
|
|
"\r\n"
|
2017-08-15 22:20:13 -07:00
|
|
|
};
|
2017-04-27 18:26:21 -07:00
|
|
|
request<dynamic_body> m;
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2017-08-15 22:20:13 -07:00
|
|
|
read(ts, b, m, ec);
|
2016-05-04 17:27:50 -04:00
|
|
|
if(! ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
2016-05-04 17:27:50 -04:00
|
|
|
{
|
2018-05-01 14:19:35 -07:00
|
|
|
test::fail_count fc{n};
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream c{ioc_, fc,
|
2016-05-04 17:27:50 -04:00
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
|
"Content-Length: 0\r\n"
|
|
|
|
|
"\r\n"
|
2017-07-29 17:47:04 -07:00
|
|
|
};
|
2017-04-27 18:26:21 -07:00
|
|
|
request<dynamic_body> m;
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2017-07-29 17:47:04 -07:00
|
|
|
async_read(c, b, m, do_yield[ec]);
|
2016-05-04 17:27:50 -04:00
|
|
|
if(! ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(n < limit);
|
2017-11-08 21:01:34 -05:00
|
|
|
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
|
|
|
|
{
|
2018-05-01 14:19:35 -07:00
|
|
|
test::fail_count fc{n};
|
2017-11-08 21:01:34 -05:00
|
|
|
test::stream c{ioc_, fc,
|
|
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
|
"Content-Length: 0\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
};
|
|
|
|
|
request_parser<dynamic_body> m;
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-11-08 21:01:34 -05:00
|
|
|
multi_buffer b;
|
|
|
|
|
async_read_some(c, b, m, do_yield[ec]);
|
|
|
|
|
if(! ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
BEAST_EXPECT(n < limit);
|
2016-05-04 17:27:50 -04:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:32:41 -05:00
|
|
|
void
|
|
|
|
|
testEof(yield_context do_yield)
|
2016-04-30 10:29:39 -04:00
|
|
|
{
|
|
|
|
|
{
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc_};
|
2017-06-03 08:56:21 -07:00
|
|
|
request_parser<dynamic_body> p;
|
2016-04-30 10:29:39 -04:00
|
|
|
error_code ec;
|
2017-08-21 14:45:58 -07:00
|
|
|
ts.close_remote();
|
2017-08-15 22:20:13 -07:00
|
|
|
read(ts, b, p, ec);
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(ec == http::error::end_of_stream);
|
2016-04-30 10:29:39 -04:00
|
|
|
}
|
|
|
|
|
{
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc_};
|
2017-06-03 08:56:21 -07:00
|
|
|
request_parser<dynamic_body> p;
|
2016-04-30 10:29:39 -04:00
|
|
|
error_code ec;
|
2017-08-21 14:45:58 -07:00
|
|
|
ts.close_remote();
|
2017-08-15 22:20:13 -07:00
|
|
|
async_read(ts, b, p, do_yield[ec]);
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(ec == http::error::end_of_stream);
|
2016-04-30 10:29:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-31 10:12:58 -04:00
|
|
|
// Ensure completion handlers are not leaked
|
|
|
|
|
struct handler
|
|
|
|
|
{
|
|
|
|
|
static std::atomic<std::size_t>&
|
|
|
|
|
count() { static std::atomic<std::size_t> n; return n; }
|
|
|
|
|
handler() { ++count(); }
|
|
|
|
|
~handler() { --count(); }
|
|
|
|
|
handler(handler const&) { ++count(); }
|
2017-09-03 18:06:09 -07:00
|
|
|
void operator()(error_code const&, std::size_t) const {}
|
2017-03-31 10:12:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
testIoService()
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
// Make sure handlers are not destroyed
|
2017-09-07 07:39:52 -07:00
|
|
|
// after calling io_context::stop
|
2018-11-30 14:58:38 -08:00
|
|
|
net::io_context ioc;
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc,
|
2017-03-31 10:12:58 -04:00
|
|
|
"GET / HTTP/1.1\r\n\r\n"};
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(handler::count() == 0);
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2017-06-07 16:30:49 -07:00
|
|
|
request<dynamic_body> m;
|
2017-08-15 22:20:13 -07:00
|
|
|
async_read(ts, b, m, handler{});
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(handler::count() > 0);
|
2017-09-07 07:39:52 -07:00
|
|
|
ioc.stop();
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(handler::count() > 0);
|
2017-09-07 07:39:52 -07:00
|
|
|
ioc.restart();
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(handler::count() > 0);
|
2017-09-07 07:39:52 -07:00
|
|
|
ioc.run_one();
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(handler::count() == 0);
|
2017-03-31 10:12:58 -04:00
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
// Make sure uninvoked handlers are
|
2017-09-07 07:39:52 -07:00
|
|
|
// destroyed when calling ~io_context
|
2017-03-31 10:12:58 -04:00
|
|
|
{
|
2018-11-30 14:58:38 -08:00
|
|
|
net::io_context ioc;
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc,
|
2017-03-31 10:12:58 -04:00
|
|
|
"GET / HTTP/1.1\r\n\r\n"};
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(handler::count() == 0);
|
2017-05-04 15:40:07 -07:00
|
|
|
multi_buffer b;
|
2017-06-07 16:30:49 -07:00
|
|
|
request<dynamic_body> m;
|
2017-08-15 22:20:13 -07:00
|
|
|
async_read(ts, b, m, handler{});
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(handler::count() > 0);
|
2017-03-31 10:12:58 -04:00
|
|
|
}
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(handler::count() == 0);
|
2017-03-31 10:12:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-29 05:03:05 -07:00
|
|
|
// https://github.com/boostorg/beast/issues/430
|
2017-06-07 10:48:35 -07:00
|
|
|
void
|
|
|
|
|
testRegression430()
|
|
|
|
|
{
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc_};
|
2017-08-15 22:20:13 -07:00
|
|
|
ts.read_size(1);
|
|
|
|
|
ostream(ts.buffer()) <<
|
2017-06-07 10:48:35 -07:00
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
|
"Content-Type: application/octet-stream\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"4\r\nabcd\r\n"
|
|
|
|
|
"0\r\n\r\n";
|
|
|
|
|
error_code ec;
|
|
|
|
|
flat_buffer fb;
|
2017-08-15 06:58:33 -07:00
|
|
|
response_parser<dynamic_body> p;
|
2017-08-15 22:20:13 -07:00
|
|
|
read(ts, fb, p, ec);
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECTS(! ec, ec.message());
|
2017-06-07 10:48:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template<class Parser, class Pred>
|
|
|
|
|
void
|
|
|
|
|
readgrind(string_view s, Pred&& pred)
|
|
|
|
|
{
|
|
|
|
|
for(std::size_t n = 1; n < s.size() - 1; ++n)
|
|
|
|
|
{
|
|
|
|
|
Parser p;
|
2018-05-01 12:39:47 -07:00
|
|
|
error_code ec = test::error::test_failure;
|
2017-06-07 10:48:35 -07:00
|
|
|
flat_buffer b;
|
2017-09-07 07:39:52 -07:00
|
|
|
test::stream ts{ioc_};
|
2017-08-15 22:20:13 -07:00
|
|
|
ostream(ts.buffer()) << s;
|
|
|
|
|
ts.read_size(n);
|
|
|
|
|
read(ts, b, p, ec);
|
2017-07-25 12:35:54 -07:00
|
|
|
if(! BEAST_EXPECTS(! ec, ec.message()))
|
2017-06-07 10:48:35 -07:00
|
|
|
continue;
|
|
|
|
|
pred(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
testReadGrind()
|
|
|
|
|
{
|
|
|
|
|
readgrind<test_parser<false>>(
|
|
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
|
"Content-Type: application/octet-stream\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"4\r\nabcd\r\n"
|
|
|
|
|
"0\r\n\r\n"
|
|
|
|
|
,[&](test_parser<false> const& p)
|
|
|
|
|
{
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(p.body == "abcd");
|
2017-06-07 10:48:35 -07:00
|
|
|
});
|
|
|
|
|
readgrind<test_parser<false>>(
|
|
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
|
"Server: test\r\n"
|
|
|
|
|
"Expect: Expires, MD5-Fingerprint\r\n"
|
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"5\r\n"
|
|
|
|
|
"*****\r\n"
|
|
|
|
|
"2;a;b=1;c=\"2\"\r\n"
|
|
|
|
|
"--\r\n"
|
|
|
|
|
"0;d;e=3;f=\"4\"\r\n"
|
|
|
|
|
"Expires: never\r\n"
|
|
|
|
|
"MD5-Fingerprint: -\r\n"
|
|
|
|
|
"\r\n"
|
|
|
|
|
,[&](test_parser<false> const& p)
|
|
|
|
|
{
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(p.body == "*****--");
|
2017-06-07 10:48:35 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-01 06:36:18 -08:00
|
|
|
struct copyable_handler
|
|
|
|
|
{
|
|
|
|
|
template<class... Args>
|
|
|
|
|
void
|
|
|
|
|
operator()(Args&&...) const
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
testAsioHandlerInvoke()
|
|
|
|
|
{
|
2018-11-30 14:58:38 -08:00
|
|
|
using strand = net::strand<
|
|
|
|
|
net::io_context::executor_type>;
|
2018-11-22 20:49:30 -08:00
|
|
|
|
2018-03-01 06:36:18 -08:00
|
|
|
// make sure things compile, also can set a
|
|
|
|
|
// breakpoint in asio_handler_invoke to make sure
|
|
|
|
|
// it is instantiated.
|
|
|
|
|
{
|
2018-11-30 14:58:38 -08:00
|
|
|
net::io_context ioc;
|
2018-11-22 20:49:30 -08:00
|
|
|
strand s{ioc.get_executor()};
|
2018-03-01 06:36:18 -08:00
|
|
|
test::stream ts{ioc};
|
|
|
|
|
flat_buffer b;
|
|
|
|
|
request_parser<dynamic_body> p;
|
2018-11-22 20:49:30 -08:00
|
|
|
async_read_some(ts, b, p,
|
2018-11-30 14:58:38 -08:00
|
|
|
net::bind_executor(
|
2018-11-22 20:49:30 -08:00
|
|
|
s, copyable_handler{}));
|
2018-03-01 06:36:18 -08:00
|
|
|
}
|
|
|
|
|
{
|
2018-11-30 14:58:38 -08:00
|
|
|
net::io_context ioc;
|
2018-11-22 20:49:30 -08:00
|
|
|
strand s{ioc.get_executor()};
|
2018-03-01 06:36:18 -08:00
|
|
|
test::stream ts{ioc};
|
|
|
|
|
flat_buffer b;
|
|
|
|
|
request_parser<dynamic_body> p;
|
2018-11-22 20:49:30 -08:00
|
|
|
async_read(ts, b, p,
|
2018-11-30 14:58:38 -08:00
|
|
|
net::bind_executor(
|
2018-11-22 20:49:30 -08:00
|
|
|
s, copyable_handler{}));
|
2018-03-01 06:36:18 -08:00
|
|
|
}
|
|
|
|
|
{
|
2018-11-30 14:58:38 -08:00
|
|
|
net::io_context ioc;
|
2018-11-22 20:49:30 -08:00
|
|
|
strand s{ioc.get_executor()};
|
2018-03-01 06:36:18 -08:00
|
|
|
test::stream ts{ioc};
|
|
|
|
|
flat_buffer b;
|
|
|
|
|
request<dynamic_body> m;
|
2018-11-22 20:49:30 -08:00
|
|
|
async_read(ts, b, m,
|
2018-11-30 14:58:38 -08:00
|
|
|
net::bind_executor(
|
2018-11-22 20:49:30 -08:00
|
|
|
s, copyable_handler{}));
|
2018-03-01 06:36:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 18:45:05 +02:00
|
|
|
#if BOOST_ASIO_HAS_CO_AWAIT
|
|
|
|
|
void testAwaitableCompiles(
|
|
|
|
|
test::stream& stream,
|
|
|
|
|
flat_buffer& dynbuf,
|
|
|
|
|
parser<true, string_body>& request_parser,
|
|
|
|
|
request<http::string_body>& request,
|
|
|
|
|
parser<false, string_body>& response_parser,
|
|
|
|
|
response<http::string_body>& response)
|
|
|
|
|
{
|
|
|
|
|
static_assert(std::is_same_v<
|
|
|
|
|
net::awaitable<std::size_t>, decltype(
|
|
|
|
|
http::async_read(stream, dynbuf, request, net::use_awaitable))>);
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same_v<
|
|
|
|
|
net::awaitable<std::size_t>, decltype(
|
|
|
|
|
http::async_read(stream, dynbuf, request_parser, net::use_awaitable))>);
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same_v<
|
|
|
|
|
net::awaitable<std::size_t>, decltype(
|
|
|
|
|
http::async_read(stream, dynbuf, response, net::use_awaitable))>);
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same_v<
|
|
|
|
|
net::awaitable<std::size_t>, decltype(
|
|
|
|
|
http::async_read(stream, dynbuf, response_parser, net::use_awaitable))>);
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same_v<
|
|
|
|
|
net::awaitable<std::size_t>, decltype(
|
|
|
|
|
http::async_read_some(stream, dynbuf, request_parser, net::use_awaitable))>);
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same_v<
|
|
|
|
|
net::awaitable<std::size_t>, decltype(
|
|
|
|
|
http::async_read_some(stream, dynbuf, response_parser, net::use_awaitable))>);
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same_v<
|
|
|
|
|
net::awaitable<std::size_t>, decltype(
|
|
|
|
|
http::async_read_header(stream, dynbuf, request_parser, net::use_awaitable))>);
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same_v<
|
|
|
|
|
net::awaitable<std::size_t>, decltype(
|
|
|
|
|
http::async_read_header(stream, dynbuf, response_parser, net::use_awaitable))>);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-05-08 13:54:40 +02:00
|
|
|
void testReadSomeHeader(net::yield_context yield)
|
|
|
|
|
{
|
|
|
|
|
std::string hdr =
|
|
|
|
|
"GET /foo HTTP/1.1" "\r\n"
|
|
|
|
|
"Connection: Keep-Alive" "\r\n"
|
|
|
|
|
"Content-Length: 6"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"\r\n";
|
|
|
|
|
std::string body =
|
|
|
|
|
"Hello!";
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// bytes_transferred returns length of header
|
|
|
|
|
request_parser<string_body> p;
|
|
|
|
|
test::stream s(ioc_);
|
|
|
|
|
|
|
|
|
|
s.append(string_view(hdr));
|
|
|
|
|
s.append(string_view(body));
|
|
|
|
|
flat_buffer fb;
|
|
|
|
|
error_code ec;
|
|
|
|
|
auto bt = async_read_header(s, fb, p, yield[ec]);
|
|
|
|
|
BEAST_EXPECTS(!ec, ec.message());
|
|
|
|
|
BEAST_EXPECT(bt == hdr.size());
|
|
|
|
|
|
|
|
|
|
// next read should be zero-size, success
|
|
|
|
|
bt = async_read_header(s, fb, p, yield[ec]);
|
|
|
|
|
BEAST_EXPECTS(!ec, ec.message());
|
|
|
|
|
BEAST_EXPECTS(bt == 0, std::to_string(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// incomplete header consumes all parsable header bytes
|
|
|
|
|
request_parser<string_body> p;
|
|
|
|
|
test::stream s(ioc_);
|
|
|
|
|
|
|
|
|
|
s.append(hdr.substr(0, hdr.size() - 1));
|
|
|
|
|
s.close();
|
|
|
|
|
flat_buffer fb;
|
|
|
|
|
error_code ec;
|
|
|
|
|
auto bt = async_read_header(s, fb, p, yield[ec]);
|
|
|
|
|
BEAST_EXPECTS(ec == error::partial_message, ec.message());
|
|
|
|
|
BEAST_EXPECTS(bt + fb.size() == hdr.size() - 1,
|
|
|
|
|
std::to_string(bt + fb.size()) +
|
|
|
|
|
" expected " +
|
|
|
|
|
std::to_string(hdr.size() - 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// read consumes and reports correct number of bytes
|
|
|
|
|
request_parser<string_body> p;
|
|
|
|
|
test::stream s(ioc_);
|
|
|
|
|
|
|
|
|
|
s.append(hdr);
|
|
|
|
|
s.append(body);
|
|
|
|
|
s.append(hdr);
|
|
|
|
|
s.append(body);
|
|
|
|
|
s.append(hdr);
|
|
|
|
|
s.append(body);
|
|
|
|
|
|
|
|
|
|
flat_buffer fb;
|
|
|
|
|
error_code ec;
|
|
|
|
|
auto bt = async_read_header(s, fb, p, yield[ec]);
|
|
|
|
|
BEAST_EXPECTS("ec", ec.message());
|
|
|
|
|
BEAST_EXPECT(bt == hdr.size());
|
|
|
|
|
auto bt2 = async_read_some(s, fb, p, yield[ec]);
|
|
|
|
|
BEAST_EXPECTS(!ec, ec.message());
|
|
|
|
|
BEAST_EXPECT(bt2 == body.size());
|
|
|
|
|
BEAST_EXPECTS(fb.size() / 2 == hdr.size() + body.size(),
|
|
|
|
|
std::to_string(fb.size() / 2) + " != " + std::to_string(hdr.size() + body.size()));
|
|
|
|
|
|
|
|
|
|
request_parser<string_body> p2;
|
|
|
|
|
bt = async_read(s, fb, p2, yield[ec]);
|
|
|
|
|
BEAST_EXPECTS(!ec, ec.message());
|
|
|
|
|
BEAST_EXPECTS(bt == hdr.size() + body.size(),
|
|
|
|
|
std::to_string(bt) +
|
|
|
|
|
" expected " +
|
|
|
|
|
std::to_string(hdr.size() + body.size()));
|
|
|
|
|
BEAST_EXPECTS(fb.size() == hdr.size() + body.size(),
|
|
|
|
|
std::to_string(fb.size()) + " != " + std::to_string(hdr.size() + body.size()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void testReadSomeHeader()
|
|
|
|
|
{
|
|
|
|
|
net::io_context ioc;
|
|
|
|
|
|
|
|
|
|
std::string hdr =
|
|
|
|
|
"GET /foo HTTP/1.1" "\r\n"
|
|
|
|
|
"Connection: Keep-Alive" "\r\n"
|
|
|
|
|
"Content-Length: 6"
|
|
|
|
|
"\r\n"
|
|
|
|
|
"\r\n";
|
|
|
|
|
std::string body =
|
|
|
|
|
"Hello!";
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// bytes_transferred returns length of header
|
|
|
|
|
request_parser<string_body> p;
|
|
|
|
|
test::stream s(ioc);
|
|
|
|
|
s.append(string_view(hdr));
|
|
|
|
|
s.append(string_view(body));
|
|
|
|
|
flat_buffer fb;
|
|
|
|
|
error_code ec;
|
|
|
|
|
auto bt = read_header(s, fb, p, ec);
|
|
|
|
|
BEAST_EXPECTS(!ec, ec.message());
|
|
|
|
|
BEAST_EXPECT(bt == hdr.size());
|
|
|
|
|
|
|
|
|
|
// next read should be zero-size, success
|
|
|
|
|
bt = read_header(s, fb, p, ec);
|
|
|
|
|
BEAST_EXPECTS(!ec, ec.message());
|
|
|
|
|
BEAST_EXPECTS(bt == 0, std::to_string(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// incomplete header consumes all parsable header bytes
|
|
|
|
|
request_parser<string_body> p;
|
|
|
|
|
test::stream s(ioc);
|
|
|
|
|
|
|
|
|
|
s.append(hdr.substr(0, hdr.size() - 1));
|
|
|
|
|
s.close();
|
|
|
|
|
flat_buffer fb;
|
|
|
|
|
error_code ec;
|
|
|
|
|
auto bt = read_header(s, fb, p, ec);
|
|
|
|
|
BEAST_EXPECTS(ec == error::partial_message, ec.message());
|
|
|
|
|
BEAST_EXPECTS(bt + fb.size() == hdr.size() - 1,
|
|
|
|
|
std::to_string(bt + fb.size()) +
|
|
|
|
|
" expected " +
|
|
|
|
|
std::to_string(hdr.size() - 1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-11-20 07:32:41 -05:00
|
|
|
void
|
|
|
|
|
run() override
|
2016-05-04 17:27:50 -04:00
|
|
|
{
|
2016-04-30 10:29:39 -04:00
|
|
|
testThrow();
|
2017-05-31 08:01:55 -07:00
|
|
|
testBufferOverflow();
|
2016-04-30 10:29:39 -04:00
|
|
|
|
2017-08-15 22:20:13 -07:00
|
|
|
yield_to([&](yield_context yield)
|
|
|
|
|
{
|
|
|
|
|
testFailures(yield);
|
|
|
|
|
});
|
|
|
|
|
yield_to([&](yield_context yield)
|
|
|
|
|
{
|
|
|
|
|
testRead(yield);
|
|
|
|
|
});
|
|
|
|
|
yield_to([&](yield_context yield)
|
|
|
|
|
{
|
|
|
|
|
testEof(yield);
|
|
|
|
|
});
|
2017-03-31 10:12:58 -04:00
|
|
|
|
|
|
|
|
testIoService();
|
2017-06-07 10:48:35 -07:00
|
|
|
testRegression430();
|
|
|
|
|
testReadGrind();
|
2018-03-01 06:36:18 -08:00
|
|
|
testAsioHandlerInvoke();
|
2020-04-27 18:45:05 +02:00
|
|
|
#if BOOST_ASIO_HAS_CO_AWAIT
|
|
|
|
|
boost::ignore_unused(&read_test::testAwaitableCompiles);
|
|
|
|
|
#endif
|
2020-05-08 13:54:40 +02:00
|
|
|
yield_to([&](yield_context yield)
|
|
|
|
|
{
|
|
|
|
|
testReadSomeHeader(yield);
|
|
|
|
|
});
|
|
|
|
|
testReadSomeHeader();
|
2016-05-04 17:27:50 -04:00
|
|
|
}
|
2020-05-08 13:54:40 +02:00
|
|
|
|
|
|
|
|
|
2016-05-04 17:27:50 -04:00
|
|
|
};
|
|
|
|
|
|
2017-08-01 17:01:57 -07:00
|
|
|
BEAST_DEFINE_TESTSUITE(beast,http,read);
|
2016-05-04 17:27:50 -04:00
|
|
|
|
|
|
|
|
} // http
|
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|