forked from boostorg/beast
Refactor test build scripts
This commit is contained in:
1 parent
61b2b63b57
commit
809e822789
400 files changed
+647
-204
No files matched your search
@@ -0,0 +1,27 @@
|
||||
#
|
||||
# Copyright (c) 2016-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)
|
||||
#
|
||||
# Official repository: https://github.com/boostorg/beast
|
||||
#
|
||||
|
||||
GroupSources(example example)
|
||||
GroupSources(extras/boost/beast extras)
|
||||
GroupSources(include/boost/beast beast)
|
||||
GroupSources(test/doc "/")
|
||||
|
||||
add_executable (tests-doc
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
${EXAMPLE_INCLUDES}
|
||||
${EXTRAS_INCLUDES}
|
||||
${TEST_MAIN}
|
||||
Jamfile
|
||||
core_examples.cpp
|
||||
core_snippets.cpp
|
||||
http_examples.cpp
|
||||
http_snippets.cpp
|
||||
websocket_snippets.cpp
|
||||
exemplars.cpp
|
||||
)
|
||||
@@ -0,0 +1,24 @@
|
||||
#
|
||||
# Copyright (c) 2016-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)
|
||||
#
|
||||
# Official repository: https://github.com/boostorg/beast
|
||||
#
|
||||
|
||||
alias run-tests :
|
||||
[ compile core_snippets.cpp ]
|
||||
[ compile http_snippets.cpp ]
|
||||
[ compile websocket_snippets.cpp ]
|
||||
[ run core_examples.cpp $(TEST_MAIN) ]
|
||||
[ run http_examples.cpp $(TEST_MAIN) ]
|
||||
;
|
||||
|
||||
exe fat-tests :
|
||||
$(TEST_MAIN)
|
||||
core_examples.cpp
|
||||
http_examples.cpp
|
||||
;
|
||||
|
||||
explicit fat-tests ;
|
||||
@@ -0,0 +1,87 @@
|
||||
//
|
||||
// Copyright (c) 2016-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)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#include "example/common/detect_ssl.hpp"
|
||||
|
||||
#include <boost/beast/core/flat_buffer.hpp>
|
||||
#include <boost/beast/core/ostream.hpp>
|
||||
#include <boost/beast/test/pipe_stream.hpp>
|
||||
#include <boost/beast/test/yield_to.hpp>
|
||||
#include <boost/beast/unit_test/suite.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
class examples_test
|
||||
: public beast::unit_test::suite
|
||||
, public beast::test::enable_yield_to
|
||||
{
|
||||
public:
|
||||
void
|
||||
testDetect()
|
||||
{
|
||||
char buf[4];
|
||||
buf[0] = 0x16;
|
||||
buf[1] = 0;
|
||||
buf[2] = 0;
|
||||
buf[3] = 0;
|
||||
BEAST_EXPECT(boost::indeterminate(is_ssl_handshake(
|
||||
boost::asio::buffer(buf, 0))));
|
||||
BEAST_EXPECT(boost::indeterminate(is_ssl_handshake(
|
||||
boost::asio::buffer(buf, 1))));
|
||||
BEAST_EXPECT(boost::indeterminate(is_ssl_handshake(
|
||||
boost::asio::buffer(buf, 2))));
|
||||
BEAST_EXPECT(boost::indeterminate(is_ssl_handshake(
|
||||
boost::asio::buffer(buf, 3))));
|
||||
BEAST_EXPECT(is_ssl_handshake(
|
||||
boost::asio::buffer(buf, 4)));
|
||||
buf[0] = 0;
|
||||
BEAST_EXPECT(! is_ssl_handshake(
|
||||
boost::asio::buffer(buf, 1)));
|
||||
}
|
||||
|
||||
void
|
||||
testRead()
|
||||
{
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
ostream(p.server.buffer) <<
|
||||
"\x16***";
|
||||
error_code ec;
|
||||
flat_buffer b;
|
||||
auto const result = detect_ssl(p.server, b, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(result);
|
||||
}
|
||||
yield_to(
|
||||
[&](yield_context yield)
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
ostream(p.server.buffer) <<
|
||||
"\x16***";
|
||||
error_code ec;
|
||||
flat_buffer b;
|
||||
auto const result = async_detect_ssl(p.server, b, yield[ec]);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(result);
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
run()
|
||||
{
|
||||
testDetect();
|
||||
testRead();
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_BEAST_DEFINE_TESTSUITE(examples,core,beast);
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// Copyright (c) 2016-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)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
//[snippet_core_1a
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
//]
|
||||
|
||||
using namespace boost::beast;
|
||||
|
||||
namespace doc_core_snippets {
|
||||
|
||||
void fxx()
|
||||
{
|
||||
|
||||
//[snippet_core_1b
|
||||
//
|
||||
using namespace boost::beast;
|
||||
|
||||
boost::asio::io_service ios;
|
||||
boost::asio::io_service::work work{ios};
|
||||
std::thread t{[&](){ ios.run(); }};
|
||||
|
||||
error_code ec;
|
||||
boost::asio::ip::tcp::socket sock{ios};
|
||||
|
||||
//]
|
||||
|
||||
{
|
||||
//[snippet_core_2
|
||||
|
||||
char const* const host = "www.example.com";
|
||||
boost::asio::ip::tcp::resolver r{ios};
|
||||
boost::asio::ip::tcp::socket stream{ios};
|
||||
boost::asio::connect(stream, r.resolve({host, "http"}));
|
||||
|
||||
// At this point `stream` is a connected to a remote
|
||||
// host and may be used to perform stream operations.
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
} // fxx()
|
||||
|
||||
//[snippet_core_3
|
||||
|
||||
template<class SyncWriteStream>
|
||||
void write_string(SyncWriteStream& stream, string_view s)
|
||||
{
|
||||
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
||||
"SyncWriteStream requirements not met");
|
||||
boost::asio::write(stream, boost::asio::const_buffers_1(s.data(), s.size()));
|
||||
}
|
||||
|
||||
//]
|
||||
|
||||
} // doc_core_snippets
|
||||
@@ -0,0 +1,349 @@
|
||||
//
|
||||
// Copyright (c) 2016-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)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#include <boost/beast/core/error.hpp>
|
||||
#include <boost/beast/core/file_base.hpp>
|
||||
#include <boost/beast/http/message.hpp>
|
||||
#include <boost/beast/http/type_traits.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class BodyReader;
|
||||
class BodyWriter;
|
||||
|
||||
//[concept_Body
|
||||
|
||||
struct Body
|
||||
{
|
||||
// The type of message::body when used
|
||||
struct value_type;
|
||||
|
||||
/// The algorithm used for extracting buffers
|
||||
class reader;
|
||||
|
||||
/// The algorithm used for inserting buffers
|
||||
class writer;
|
||||
|
||||
/// Returns the body's payload size
|
||||
static
|
||||
std::uint64_t
|
||||
size(value_type const& body);
|
||||
};
|
||||
|
||||
static_assert(is_body<Body>::value, "");
|
||||
|
||||
//]
|
||||
|
||||
struct Body_BodyReader {
|
||||
struct value_type{};
|
||||
//[concept_BodyReader
|
||||
|
||||
struct BodyReader
|
||||
{
|
||||
public:
|
||||
/// The type of buffer returned by `get`.
|
||||
using const_buffers_type = boost::asio::const_buffers_1;
|
||||
|
||||
/** Construct the reader.
|
||||
|
||||
@param msg The message whose body is to be retrieved.
|
||||
|
||||
@param ec Set to the error, if any occurred.
|
||||
*/
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
explicit
|
||||
BodyReader(message<isRequest, Body, Fields> const& msg);
|
||||
|
||||
/** Initialize the reader
|
||||
|
||||
This is called after construction and before the first
|
||||
call to `get`. The message is valid and complete upon
|
||||
entry.
|
||||
|
||||
@param ec Set to the error, if any occurred.
|
||||
*/
|
||||
void
|
||||
init(error_code& ec)
|
||||
{
|
||||
// The specification requires this to indicate "no error"
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
/** Returns the next buffer in the body.
|
||||
|
||||
@li If the return value is `boost::none` (unseated optional) and
|
||||
`ec` does not contain an error, this indicates the end of the
|
||||
body, no more buffers are present.
|
||||
|
||||
@li If the optional contains a value, the first element of the
|
||||
pair represents a @b ConstBufferSequence containing one or
|
||||
more octets of the body data. The second element indicates
|
||||
whether or not there are additional octets of body data.
|
||||
A value of `true` means there is more data, and that the
|
||||
implementation will perform a subsequent call to `get`.
|
||||
A value of `false` means there is no more body data.
|
||||
|
||||
@li If `ec` contains an error code, the return value is ignored.
|
||||
|
||||
@param ec Set to the error, if any occurred.
|
||||
*/
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(error_code& ec)
|
||||
{
|
||||
// The specification requires this to indicate "no error"
|
||||
ec.assign(0, ec.category());
|
||||
|
||||
return boost::none; // for exposition only
|
||||
}
|
||||
};
|
||||
|
||||
//]
|
||||
using reader = BodyReader;
|
||||
};
|
||||
|
||||
static_assert(is_body_reader<Body_BodyReader>::value, "");
|
||||
|
||||
struct Body_BodyWriter {
|
||||
struct value_type{};
|
||||
//[concept_BodyWriter
|
||||
|
||||
struct BodyWriter
|
||||
{
|
||||
/** Construct the writer.
|
||||
|
||||
@param msg The message whose body is to be stored.
|
||||
*/
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
explicit
|
||||
BodyWriter(message<isRequest, Body, Fields>& msg);
|
||||
|
||||
/** Initialize the writer
|
||||
|
||||
This is called after construction and before the first
|
||||
call to `put`. The message is valid and complete upon
|
||||
entry.
|
||||
|
||||
@param ec Set to the error, if any occurred.
|
||||
*/
|
||||
void
|
||||
init(
|
||||
boost::optional<std::uint64_t> const& content_length,
|
||||
error_code& ec)
|
||||
{
|
||||
boost::ignore_unused(content_length);
|
||||
|
||||
// The specification requires this to indicate "no error"
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
/** Store buffers.
|
||||
|
||||
This is called zero or more times with parsed body octets.
|
||||
|
||||
@param buffers The constant buffer sequence to store.
|
||||
|
||||
@param ec Set to the error, if any occurred.
|
||||
|
||||
@return The number of bytes transferred from the input buffers.
|
||||
*/
|
||||
template<class ConstBufferSequence>
|
||||
std::size_t
|
||||
put(ConstBufferSequence const& buffers, error_code& ec)
|
||||
{
|
||||
// The specification requires this to indicate "no error"
|
||||
ec = {};
|
||||
|
||||
return boost::asio::buffer_size(buffers);
|
||||
}
|
||||
|
||||
/** Called when the body is complete.
|
||||
|
||||
@param ec Set to the error, if any occurred.
|
||||
*/
|
||||
void
|
||||
finish(error_code& ec)
|
||||
{
|
||||
// The specification requires this to indicate "no error"
|
||||
ec = {};
|
||||
}
|
||||
};
|
||||
|
||||
//]
|
||||
using writer = BodyWriter;
|
||||
};
|
||||
|
||||
static_assert(is_body_writer<Body_BodyWriter>::value, "");
|
||||
|
||||
//[concept_Fields
|
||||
|
||||
class Fields
|
||||
{
|
||||
public:
|
||||
struct reader;
|
||||
|
||||
protected:
|
||||
/** Returns the request-method string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
string_view
|
||||
get_method_impl() const;
|
||||
|
||||
/** Returns the request-target string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
string_view
|
||||
get_target_impl() const;
|
||||
|
||||
/** Returns the response reason-phrase string.
|
||||
|
||||
@note Only called for responses.
|
||||
*/
|
||||
string_view
|
||||
get_reason_impl() const;
|
||||
|
||||
/** Returns the chunked Transfer-Encoding setting
|
||||
*/
|
||||
bool
|
||||
get_chunked_impl() const;
|
||||
|
||||
/** Returns the keep-alive setting
|
||||
*/
|
||||
bool
|
||||
get_keep_alive_impl(unsigned version) const;
|
||||
|
||||
/** Set or clear the method string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
void
|
||||
set_method_impl(string_view s);
|
||||
|
||||
/** Set or clear the target string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
void
|
||||
set_target_impl(string_view s);
|
||||
|
||||
/** Set or clear the reason string.
|
||||
|
||||
@note Only called for responses.
|
||||
*/
|
||||
void
|
||||
set_reason_impl(string_view s);
|
||||
|
||||
/** Sets or clears the chunked Transfer-Encoding value
|
||||
*/
|
||||
void
|
||||
set_chunked_impl(bool value);
|
||||
|
||||
/** Sets or clears the Content-Length field
|
||||
*/
|
||||
void
|
||||
set_content_length_impl(boost::optional<std::uint64_t>);
|
||||
|
||||
/** Adjusts the Connection field
|
||||
*/
|
||||
void
|
||||
set_keep_alive_impl(unsigned version, bool keep_alive);
|
||||
};
|
||||
|
||||
static_assert(is_fields<Fields>::value,
|
||||
"Fields requirements not met");
|
||||
|
||||
//]
|
||||
|
||||
struct Fields_FieldsReader {
|
||||
using F = Fields_FieldsReader;
|
||||
//[concept_FieldsReader
|
||||
|
||||
struct FieldsReader
|
||||
{
|
||||
// The type of buffers returned by `get`
|
||||
struct const_buffers_type;
|
||||
|
||||
// Constructor for requests
|
||||
FieldsReader(F const& f, unsigned version, verb method);
|
||||
|
||||
// Constructor for responses
|
||||
FieldsReader(F const& f, unsigned version, unsigned status);
|
||||
|
||||
// Returns `true` if keep-alive is indicated
|
||||
bool
|
||||
keep_alive();
|
||||
|
||||
// Returns the serialized header buffers
|
||||
const_buffers_type
|
||||
get();
|
||||
};
|
||||
|
||||
//]
|
||||
};
|
||||
|
||||
//[concept_File
|
||||
|
||||
struct File
|
||||
{
|
||||
/** Default constructor
|
||||
|
||||
There is no open file initially.
|
||||
*/
|
||||
File();
|
||||
|
||||
/** Destructor
|
||||
|
||||
If the file is open it is first closed.
|
||||
*/
|
||||
~File();
|
||||
|
||||
/// Returns `true` if the file is open
|
||||
bool
|
||||
is_open() const;
|
||||
|
||||
/// Close the file if open
|
||||
void
|
||||
close(error_code& ec);
|
||||
|
||||
/// Open a file at the given path with the specified mode
|
||||
void
|
||||
open(char const* path, file_mode mode, error_code& ec);
|
||||
|
||||
/// Return the size of the open file
|
||||
std::uint64_t
|
||||
size(error_code& ec) const;
|
||||
|
||||
/// Return the current position in the open file
|
||||
std::uint64_t
|
||||
pos(error_code& ec) const;
|
||||
|
||||
/// Adjust the current position in the open file
|
||||
void
|
||||
seek(std::uint64_t offset, error_code& ec);
|
||||
|
||||
/// Read from the open file
|
||||
std::size_t
|
||||
read(void* buffer, std::size_t n, error_code& ec) const;
|
||||
|
||||
/// Write to the open file
|
||||
std::size_t
|
||||
write(void const* buffer, std::size_t n, error_code& ec);
|
||||
};
|
||||
|
||||
//]
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
} // boost
|
||||
@@ -0,0 +1,458 @@
|
||||
//
|
||||
// Copyright (c) 2016-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)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#include "example/doc/http_examples.hpp"
|
||||
|
||||
#include <boost/beast/core/flat_buffer.hpp>
|
||||
#include <boost/beast/core/read_size.hpp>
|
||||
#include <boost/beast/core/ostream.hpp>
|
||||
#include <boost/beast/core/detail/clamp.hpp>
|
||||
#include <boost/beast/http/chunk_encode.hpp>
|
||||
#include <boost/beast/http/parser.hpp>
|
||||
#include <boost/beast/http/read.hpp>
|
||||
#include <boost/beast/http/write.hpp>
|
||||
#include <boost/beast/test/pipe_stream.hpp>
|
||||
#include <boost/beast/test/string_istream.hpp>
|
||||
#include <boost/beast/test/string_ostream.hpp>
|
||||
#include <boost/beast/test/yield_to.hpp>
|
||||
#include <boost/beast/unit_test/suite.hpp>
|
||||
#include <sstream>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <list>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class examples_test
|
||||
: public beast::unit_test::suite
|
||||
, public beast::test::enable_yield_to
|
||||
{
|
||||
public:
|
||||
// two threads, for some examples using a pipe
|
||||
examples_test()
|
||||
: enable_yield_to(2)
|
||||
{
|
||||
}
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
static
|
||||
std::string
|
||||
to_string(message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << m;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
static
|
||||
std::string
|
||||
to_string(ConstBufferSequence const& bs)
|
||||
{
|
||||
using boost::asio::buffer_cast;
|
||||
using boost::asio::buffer_size;
|
||||
std::string s;
|
||||
s.reserve(buffer_size(bs));
|
||||
for(boost::asio::const_buffer b : bs)
|
||||
s.append(buffer_cast<char const*>(b),
|
||||
buffer_size(b));
|
||||
return s;
|
||||
}
|
||||
|
||||
template<bool isRequest>
|
||||
bool
|
||||
equal_body(string_view sv, string_view body)
|
||||
{
|
||||
test::string_istream si{
|
||||
get_io_service(), sv.to_string()};
|
||||
message<isRequest, string_body, fields> m;
|
||||
multi_buffer b;
|
||||
try
|
||||
{
|
||||
read(si, b, m);
|
||||
return m.body == body;
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
log << "equal_body: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
doExpect100Continue()
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
yield_to(
|
||||
[&](yield_context)
|
||||
{
|
||||
error_code ec;
|
||||
flat_buffer buffer;
|
||||
receive_expect_100_continue(
|
||||
p.server, buffer, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
},
|
||||
[&](yield_context)
|
||||
{
|
||||
flat_buffer buffer;
|
||||
request<string_body> req;
|
||||
req.version = 11;
|
||||
req.method_string("POST");
|
||||
req.target("/");
|
||||
req.insert(field::user_agent, "test");
|
||||
req.body = "Hello, world!";
|
||||
req.prepare_payload();
|
||||
|
||||
error_code ec;
|
||||
send_expect_100_continue(
|
||||
p.client, buffer, req, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
doCgiResponse()
|
||||
{
|
||||
std::string const s = "Hello, world!";
|
||||
test::pipe child{ios_};
|
||||
child.server.read_size(3);
|
||||
ostream(child.server.buffer) << s;
|
||||
child.client.close();
|
||||
test::pipe p{ios_};
|
||||
error_code ec;
|
||||
send_cgi_response(child.server, p.client, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(equal_body<false>(p.server.str(), s));
|
||||
}
|
||||
|
||||
void
|
||||
doRelay()
|
||||
{
|
||||
request<string_body> req;
|
||||
req.version = 11;
|
||||
req.method_string("POST");
|
||||
req.target("/");
|
||||
req.insert(field::user_agent, "test");
|
||||
req.body = "Hello, world!";
|
||||
req.prepare_payload();
|
||||
|
||||
test::pipe downstream{ios_};
|
||||
downstream.server.read_size(3);
|
||||
test::pipe upstream{ios_};
|
||||
upstream.client.write_size(3);
|
||||
|
||||
error_code ec;
|
||||
write(downstream.client, req);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
downstream.client.close();
|
||||
|
||||
flat_buffer buffer;
|
||||
relay<true>(upstream.client, downstream.server, buffer, ec,
|
||||
[&](header<true, fields>& h, error_code& ev)
|
||||
{
|
||||
ev = {};
|
||||
h.erase("Content-Length");
|
||||
h.set("Transfer-Encoding", "chunked");
|
||||
});
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(equal_body<true>(
|
||||
upstream.server.str(), req.body));
|
||||
}
|
||||
|
||||
void
|
||||
doReadStdStream()
|
||||
{
|
||||
std::string const s =
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"\r\n"
|
||||
"Hello, world!";
|
||||
std::istringstream is(s);
|
||||
error_code ec;
|
||||
flat_buffer buffer;
|
||||
response<string_body> res;
|
||||
read_istream(is, buffer, res, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(to_string(res) == s);
|
||||
}
|
||||
|
||||
void
|
||||
doWriteStdStream()
|
||||
{
|
||||
std::ostringstream os;
|
||||
request<string_body> req;
|
||||
req.version = 11;
|
||||
req.method(verb::get);
|
||||
req.target("/");
|
||||
req.insert(field::user_agent, "test");
|
||||
error_code ec;
|
||||
write_ostream(os, req, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(to_string(req) == os.str());
|
||||
}
|
||||
|
||||
void
|
||||
doCustomParser()
|
||||
{
|
||||
{
|
||||
string_view s{
|
||||
"POST / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 13\r\n"
|
||||
"\r\n"
|
||||
"Hello, world!"
|
||||
};
|
||||
error_code ec;
|
||||
custom_parser<true> p;
|
||||
p.put(boost::asio::buffer(
|
||||
s.data(), s.size()), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
{
|
||||
string_view s{
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"d\r\n"
|
||||
"Hello, world!"
|
||||
"\r\n"
|
||||
"0\r\n\r\n"
|
||||
};
|
||||
error_code ec;
|
||||
custom_parser<false> p;
|
||||
p.put(boost::asio::buffer(
|
||||
s.data(), s.size()), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
doHEAD()
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
yield_to(
|
||||
[&](yield_context)
|
||||
{
|
||||
error_code ec;
|
||||
flat_buffer buffer;
|
||||
do_server_head(p.server, buffer, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
},
|
||||
[&](yield_context)
|
||||
{
|
||||
error_code ec;
|
||||
flat_buffer buffer;
|
||||
auto res = do_head_request(p.client, buffer, "/", ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
});
|
||||
}
|
||||
|
||||
struct handler
|
||||
{
|
||||
std::string body;
|
||||
|
||||
template<class Body>
|
||||
void
|
||||
operator()(request<Body>&&)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
operator()(request<string_body>&& req)
|
||||
{
|
||||
body = req.body;
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
doDeferredBody()
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
ostream(p.server.buffer) <<
|
||||
"POST / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Type: multipart/form-data\r\n"
|
||||
"Content-Length: 13\r\n"
|
||||
"\r\n"
|
||||
"Hello, world!";
|
||||
|
||||
handler h;
|
||||
flat_buffer buffer;
|
||||
do_form_request(p.server, buffer, h);
|
||||
BEAST_EXPECT(h.body == "Hello, world!");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
doIncrementalRead()
|
||||
{
|
||||
test::pipe c{ios_};
|
||||
std::string s(2048, '*');
|
||||
ostream(c.server.buffer) <<
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Length: 2048\r\n"
|
||||
"Server: test\r\n"
|
||||
"\r\n" <<
|
||||
s;
|
||||
error_code ec;
|
||||
flat_buffer b;
|
||||
std::stringstream ss;
|
||||
read_and_print_body<false>(ss, c.server, b, ec);
|
||||
if(BEAST_EXPECTS(! ec, ec.message()))
|
||||
BEAST_EXPECT(ss.str() == s);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
doExplicitChunkSerialize()
|
||||
{
|
||||
auto const buf =
|
||||
[](string_view s)
|
||||
{
|
||||
return boost::asio::const_buffers_1{
|
||||
s.data(), s.size()};
|
||||
};
|
||||
test::pipe p{ios_};
|
||||
|
||||
response<empty_body> res{status::ok, 11};
|
||||
res.set(field::server, "test");
|
||||
res.set(field::accept, "Expires, Content-MD5");
|
||||
res.chunked(true);
|
||||
|
||||
error_code ec;
|
||||
response_serializer<empty_body> sr{res};
|
||||
write_header(p.client, sr, ec);
|
||||
|
||||
chunk_extensions exts;
|
||||
|
||||
boost::asio::write(p.client,
|
||||
make_chunk(buf("First")), ec);
|
||||
|
||||
exts.insert("quality", "1.0");
|
||||
boost::asio::write(p.client,
|
||||
make_chunk(buf("Hello, world!"), exts), ec);
|
||||
|
||||
exts.clear();
|
||||
exts.insert("file", "abc.txt");
|
||||
exts.insert("quality", "0.7");
|
||||
boost::asio::write(p.client,
|
||||
make_chunk(buf("The Next Chunk"), std::move(exts)), ec);
|
||||
|
||||
exts.clear();
|
||||
exts.insert("last");
|
||||
boost::asio::write(p.client,
|
||||
make_chunk(buf("Last one"), std::move(exts),
|
||||
std::allocator<double>{}), ec);
|
||||
|
||||
fields trailers;
|
||||
trailers.set(field::expires, "never");
|
||||
trailers.set(field::content_md5, "f4a5c16584f03d90");
|
||||
|
||||
boost::asio::write(p.client,
|
||||
make_chunk_last(
|
||||
trailers,
|
||||
std::allocator<double>{}
|
||||
), ec);
|
||||
BEAST_EXPECT(
|
||||
to_string(p.server.buffer.data()) ==
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Accept: Expires, Content-MD5\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"5\r\n"
|
||||
"First\r\n"
|
||||
"d;quality=1.0\r\n"
|
||||
"Hello, world!\r\n"
|
||||
"e;file=abc.txt;quality=0.7\r\n"
|
||||
"The Next Chunk\r\n"
|
||||
"8;last\r\n"
|
||||
"Last one\r\n"
|
||||
"0\r\n"
|
||||
"Expires: never\r\n"
|
||||
"Content-MD5: f4a5c16584f03d90\r\n"
|
||||
"\r\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
doExplicitChunkParse()
|
||||
{
|
||||
test::pipe c{ios_};
|
||||
ostream(c.client.buffer) <<
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Trailer: Expires, Content-MD5\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"5\r\n"
|
||||
"First\r\n"
|
||||
"d;quality=1.0\r\n"
|
||||
"Hello, world!\r\n"
|
||||
"e;file=abc.txt;quality=0.7\r\n"
|
||||
"The Next Chunk\r\n"
|
||||
"8;last\r\n"
|
||||
"Last one\r\n"
|
||||
"0\r\n"
|
||||
"Expires: never\r\n"
|
||||
"Content-MD5: f4a5c16584f03d90\r\n"
|
||||
"\r\n";
|
||||
|
||||
|
||||
error_code ec;
|
||||
flat_buffer b;
|
||||
std::stringstream ss;
|
||||
print_chunked_body<false>(ss, c.client, b, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(ss.str() ==
|
||||
"Chunk Body: First\n"
|
||||
"Extension: quality = 1.0\n"
|
||||
"Chunk Body: Hello, world!\n"
|
||||
"Extension: file = abc.txt\n"
|
||||
"Extension: quality = 0.7\n"
|
||||
"Chunk Body: The Next Chunk\n"
|
||||
"Extension: last\n"
|
||||
"Chunk Body: Last one\n"
|
||||
"Expires: never\n"
|
||||
"Content-MD5: f4a5c16584f03d90\n");
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
run()
|
||||
{
|
||||
doExpect100Continue();
|
||||
doCgiResponse();
|
||||
doRelay();
|
||||
doReadStdStream();
|
||||
doWriteStdStream();
|
||||
doCustomParser();
|
||||
doHEAD();
|
||||
doDeferredBody();
|
||||
doIncrementalRead();
|
||||
doExplicitChunkSerialize();
|
||||
doExplicitChunkParse();
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_BEAST_DEFINE_TESTSUITE(examples,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
} // boost
|
||||
@@ -0,0 +1,481 @@
|
||||
//
|
||||
// Copyright (c) 2016-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)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
using namespace boost::beast;
|
||||
|
||||
//[http_snippet_1
|
||||
|
||||
#include <boost/beast/http.hpp>
|
||||
using namespace boost::beast::http;
|
||||
|
||||
//]
|
||||
|
||||
namespace doc_http_snippets {
|
||||
|
||||
//[http_snippet_17
|
||||
// This function returns the buffer containing the next chunk body
|
||||
boost::asio::const_buffers_1 get_next_chunk_body();
|
||||
//]
|
||||
|
||||
boost::asio::const_buffers_1 get_next_chunk_body()
|
||||
{
|
||||
return {nullptr, 0};
|
||||
}
|
||||
|
||||
void fxx() {
|
||||
|
||||
boost::asio::io_service ios;
|
||||
boost::asio::io_service::work work{ios};
|
||||
std::thread t{[&](){ ios.run(); }};
|
||||
boost::asio::ip::tcp::socket sock{ios};
|
||||
|
||||
{
|
||||
//[http_snippet_2
|
||||
|
||||
request<empty_body> req;
|
||||
req.version = 11; // HTTP/1.1
|
||||
req.method(verb::get);
|
||||
req.target("/index.htm");
|
||||
req.set(field::accept, "text/html");
|
||||
req.set(field::user_agent, "Beast");
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_3
|
||||
|
||||
response<string_body> res;
|
||||
res.version = 11; // HTTP/1.1
|
||||
res.result(status::ok);
|
||||
res.set(field::server, "Beast");
|
||||
res.body = "Hello, world!";
|
||||
res.prepare_payload();
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_4
|
||||
|
||||
flat_buffer buffer; // (The parser is optimized for flat buffers)
|
||||
request<string_body> req;
|
||||
read(sock, buffer, req);
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_5
|
||||
|
||||
flat_buffer buffer;
|
||||
response<string_body> res;
|
||||
async_read(sock, buffer, res,
|
||||
[&](error_code ec)
|
||||
{
|
||||
std::cerr << ec.message() << std::endl;
|
||||
});
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_6
|
||||
|
||||
// This buffer's max size is too small for much of anything
|
||||
flat_buffer buffer{10};
|
||||
|
||||
// Try to read a request
|
||||
error_code ec;
|
||||
request<string_body> req;
|
||||
read(sock, buffer, req, ec);
|
||||
if(ec == error::buffer_overflow)
|
||||
std::cerr << "Buffer limit exceeded!" << std::endl;
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_7
|
||||
|
||||
response<string_body> res;
|
||||
res.version = 11;
|
||||
res.result(status::ok);
|
||||
res.set(field::server, "Beast");
|
||||
res.body = "Hello, world!";
|
||||
|
||||
error_code ec;
|
||||
write(sock, res, ec);
|
||||
if(ec == error::end_of_stream)
|
||||
sock.close();
|
||||
//]
|
||||
|
||||
//[http_snippet_8
|
||||
async_write(sock, res,
|
||||
[&](error_code)
|
||||
{
|
||||
if(ec)
|
||||
std::cerr << ec.message() << std::endl;
|
||||
});
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_10
|
||||
|
||||
response<string_body> res;
|
||||
|
||||
response_serializer<string_body> sr{res};
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_18
|
||||
// Prepare an HTTP/1.1 response with a chunked body
|
||||
response<empty_body> res{status::ok, 11};
|
||||
res.set(field::server, "Beast");
|
||||
|
||||
// Set Transfer-Encoding to "chunked".
|
||||
// If a Content-Length was present, it is removed.
|
||||
res.chunked(true);
|
||||
|
||||
// Set up the serializer
|
||||
response_serializer<empty_body> sr{res};
|
||||
|
||||
// Write the header first
|
||||
write_header(sock, sr);
|
||||
|
||||
// Now manually emit three chunks:
|
||||
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
|
||||
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
|
||||
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
|
||||
|
||||
// We are responsible for sending the last chunk:
|
||||
boost::asio::write(sock, make_chunk_last());
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_19
|
||||
// Prepare a set of chunk extension to emit with the body
|
||||
chunk_extensions ext;
|
||||
ext.insert("mp3");
|
||||
ext.insert("title", "Beale Street Blues");
|
||||
ext.insert("artist", "W.C. Handy");
|
||||
|
||||
// Write the next chunk with the chunk extensions
|
||||
// The implementation will make a copy of the extensions object,
|
||||
// so the caller does not need to manage lifetime issues.
|
||||
boost::asio::async_write(sock, make_chunk(get_next_chunk_body(), ext),
|
||||
[](error_code ec, std::size_t)
|
||||
{
|
||||
if(ec)
|
||||
std::cout << "Error: " << ec.message() << std::endl;
|
||||
});
|
||||
|
||||
// Write the next chunk with the chunk extensions
|
||||
// The implementation will make a copy of the extensions object, storing the copy
|
||||
// using the custom allocator, so the caller does not need to manage lifetime issues.
|
||||
boost::asio::async_write(sock,
|
||||
make_chunk(get_next_chunk_body(), ext, std::allocator<char>{}),
|
||||
[](error_code ec, std::size_t)
|
||||
{
|
||||
if(ec)
|
||||
std::cout << "Error: " << ec.message() << std::endl;
|
||||
});
|
||||
|
||||
// Write the next chunk with the chunk extensions
|
||||
// The implementation allocates memory using the default allocator and takes ownership
|
||||
// of the extensions object, so the caller does not need to manage lifetime issues.
|
||||
// Note: ext is moved
|
||||
boost::asio::async_write(sock, make_chunk(get_next_chunk_body(), std::move(ext)),
|
||||
[](error_code ec, std::size_t)
|
||||
{
|
||||
if(ec)
|
||||
std::cout << "Error: " << ec.message() << std::endl;
|
||||
});
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_20
|
||||
// Manually specify the chunk extensions.
|
||||
// Some of the strings contain spaces and a period and must be quoted
|
||||
boost::asio::write(sock, make_chunk(get_next_chunk_body(),
|
||||
";mp3"
|
||||
";title=\"Danny Boy\""
|
||||
";artist=\"Fred E. Weatherly\""
|
||||
));
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_21
|
||||
// Prepare a chunked HTTP/1.1 response with some trailer fields
|
||||
response<empty_body> res{status::ok, 11};
|
||||
res.set(field::server, "Beast");
|
||||
|
||||
// Inform the client of the trailer fields we will send
|
||||
res.set(field::trailer, "Content-MD5, Expires");
|
||||
|
||||
res.chunked(true);
|
||||
|
||||
// Serialize the header and two chunks
|
||||
response_serializer<empty_body> sr{res};
|
||||
write_header(sock, sr);
|
||||
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
|
||||
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
|
||||
|
||||
// Prepare the trailer
|
||||
fields trailer;
|
||||
trailer.set(field::content_md5, "f4a5c16584f03d90");
|
||||
trailer.set(field::expires, "never");
|
||||
|
||||
// Emit the trailer in the last chunk.
|
||||
// The implementation will use the default allocator to create the storage for holding
|
||||
// the serialized fields.
|
||||
boost::asio::write(sock, make_chunk_last(trailer));
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_22
|
||||
// Use a custom allocator for serializing the last chunk
|
||||
fields trailer;
|
||||
trailer.set(field::approved, "yes");
|
||||
boost::asio::write(sock, make_chunk_last(trailer, std::allocator<char>{}));
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_23
|
||||
// Manually emit a trailer.
|
||||
// We are responsible for ensuring that the trailer format adheres to the specification.
|
||||
string_view ext =
|
||||
"Content-MD5: f4a5c16584f03d90\r\n"
|
||||
"Expires: never\r\n"
|
||||
"\r\n";
|
||||
boost::asio::write(sock, make_chunk_last(boost::asio::const_buffers_1{ext.data(), ext.size()}));
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_24
|
||||
// Prepare a chunked HTTP/1.1 response and send the header
|
||||
response<empty_body> res{status::ok, 11};
|
||||
res.set(field::server, "Beast");
|
||||
res.chunked(true);
|
||||
response_serializer<empty_body> sr{res};
|
||||
write_header(sock, sr);
|
||||
|
||||
// Obtain three body buffers up front
|
||||
auto const cb1 = get_next_chunk_body();
|
||||
auto const cb2 = get_next_chunk_body();
|
||||
auto const cb3 = get_next_chunk_body();
|
||||
|
||||
// Manually emit a chunk by first writing the chunk-size header with the correct size
|
||||
boost::asio::write(sock, chunk_header{
|
||||
boost::asio::buffer_size(cb1) +
|
||||
boost::asio::buffer_size(cb2) +
|
||||
boost::asio::buffer_size(cb3)});
|
||||
|
||||
// And then output the chunk body in three pieces ("chunk the chunk")
|
||||
boost::asio::write(sock, cb1);
|
||||
boost::asio::write(sock, cb2);
|
||||
boost::asio::write(sock, cb3);
|
||||
|
||||
// When we go this deep, we are also responsible for the terminating CRLF
|
||||
boost::asio::write(sock, chunk_crlf{});
|
||||
//]
|
||||
}
|
||||
|
||||
} // fxx()
|
||||
|
||||
|
||||
|
||||
//[http_snippet_12
|
||||
|
||||
/** Send a message to a stream synchronously.
|
||||
|
||||
@param stream The stream to write to. This type must support
|
||||
the @b SyncWriteStream concept.
|
||||
|
||||
@param m The message to send. The Body type must support
|
||||
the @b BodyReader concept.
|
||||
*/
|
||||
template<
|
||||
class SyncWriteStream,
|
||||
bool isRequest, class Body, class Fields>
|
||||
void
|
||||
send(
|
||||
SyncWriteStream& stream,
|
||||
message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
// Check the template types
|
||||
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
||||
"SyncWriteStream requirements not met");
|
||||
static_assert(is_body_reader<Body>::value,
|
||||
"BodyReader requirements not met");
|
||||
|
||||
// Create the instance of serializer for the message
|
||||
serializer<isRequest, Body, Fields> sr{m};
|
||||
|
||||
// Loop until the serializer is finished
|
||||
do
|
||||
{
|
||||
// This call guarantees it will make some
|
||||
// forward progress, or otherwise return an error.
|
||||
write_some(stream, sr);
|
||||
}
|
||||
while(! sr.is_done());
|
||||
}
|
||||
|
||||
//]
|
||||
|
||||
//[http_snippet_13
|
||||
|
||||
template<class SyncReadStream>
|
||||
void
|
||||
print_response(SyncReadStream& stream)
|
||||
{
|
||||
static_assert(is_sync_read_stream<SyncReadStream>::value,
|
||||
"SyncReadStream requirements not met");
|
||||
|
||||
// Declare a parser for an HTTP response
|
||||
response_parser<string_body> parser;
|
||||
|
||||
// Read the entire message
|
||||
read(stream, parser);
|
||||
|
||||
// Now print the message
|
||||
std::cout << parser.get() << std::endl;
|
||||
}
|
||||
|
||||
//]
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
//[http_snippet_14
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
print_cxx14(message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
error_code ec;
|
||||
serializer<isRequest, Body, Fields> sr{m};
|
||||
do
|
||||
{
|
||||
sr.next(ec,
|
||||
[&sr](error_code& ec, auto const& buffer)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
std::cout << buffers(buffer);
|
||||
sr.consume(boost::asio::buffer_size(buffer));
|
||||
});
|
||||
}
|
||||
while(! ec && ! sr.is_done());
|
||||
if(! ec)
|
||||
std::cout << std::endl;
|
||||
else
|
||||
std::cerr << ec.message() << std::endl;
|
||||
}
|
||||
|
||||
//]
|
||||
#endif
|
||||
|
||||
//[http_snippet_15
|
||||
|
||||
template<class Serializer>
|
||||
struct lambda
|
||||
{
|
||||
Serializer& sr;
|
||||
|
||||
lambda(Serializer& sr_) : sr(sr_) {}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
void operator()(error_code& ec, ConstBufferSequence const& buffer) const
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
std::cout << buffers(buffer);
|
||||
sr.consume(boost::asio::buffer_size(buffer));
|
||||
}
|
||||
};
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
print(message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
error_code ec;
|
||||
serializer<isRequest, Body, Fields> sr{m};
|
||||
do
|
||||
{
|
||||
sr.next(ec, lambda<decltype(sr)>{sr});
|
||||
}
|
||||
while(! ec && ! sr.is_done());
|
||||
if(! ec)
|
||||
std::cout << std::endl;
|
||||
else
|
||||
std::cerr << ec.message() << std::endl;
|
||||
}
|
||||
|
||||
//]
|
||||
|
||||
#if BOOST_MSVC
|
||||
//[http_snippet_16
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
split_print_cxx14(message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
error_code ec;
|
||||
serializer<isRequest, Body, Fields> sr{m};
|
||||
sr.split(true);
|
||||
std::cout << "Header:" << std::endl;
|
||||
do
|
||||
{
|
||||
sr.next(ec,
|
||||
[&sr](error_code& ec, auto const& buffer)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
std::cout << buffers(buffer);
|
||||
sr.consume(boost::asio::buffer_size(buffer));
|
||||
});
|
||||
}
|
||||
while(! sr.is_header_done());
|
||||
if(! ec && ! sr.is_done())
|
||||
{
|
||||
std::cout << "Body:" << std::endl;
|
||||
do
|
||||
{
|
||||
sr.next(ec,
|
||||
[&sr](error_code& ec, auto const& buffer)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
std::cout << buffers(buffer);
|
||||
sr.consume(boost::asio::buffer_size(buffer));
|
||||
});
|
||||
}
|
||||
while(! ec && ! sr.is_done());
|
||||
}
|
||||
if(ec)
|
||||
std::cerr << ec.message() << std::endl;
|
||||
}
|
||||
|
||||
//]
|
||||
#endif
|
||||
|
||||
// Highest snippet:
|
||||
|
||||
} // doc_http_snippets
|
||||
@@ -0,0 +1,285 @@
|
||||
//
|
||||
// Copyright (c) 2016-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)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/spawn.hpp>
|
||||
#include <boost/asio/use_future.hpp>
|
||||
#include <algorithm>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
//[ws_snippet_1
|
||||
#include <boost/beast/websocket.hpp>
|
||||
using namespace boost::beast::websocket;
|
||||
//]
|
||||
|
||||
using namespace boost::beast;
|
||||
|
||||
namespace doc_ws_snippets {
|
||||
|
||||
void fxx() {
|
||||
|
||||
boost::asio::io_service ios;
|
||||
boost::asio::io_service::work work{ios};
|
||||
std::thread t{[&](){ ios.run(); }};
|
||||
error_code ec;
|
||||
boost::asio::ip::tcp::socket sock{ios};
|
||||
|
||||
{
|
||||
//[ws_snippet_2
|
||||
stream<boost::asio::ip::tcp::socket> ws{ios};
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[ws_snippet_3
|
||||
stream<boost::asio::ip::tcp::socket> ws{std::move(sock)};
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[ws_snippet_4
|
||||
stream<boost::asio::ip::tcp::socket&> ws{sock};
|
||||
//]
|
||||
|
||||
//[ws_snippet_5
|
||||
ws.next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_send);
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[ws_snippet_6
|
||||
std::string const host = "mywebapp.com";
|
||||
boost::asio::ip::tcp::resolver r{ios};
|
||||
stream<boost::asio::ip::tcp::socket> ws{ios};
|
||||
boost::asio::connect(ws.next_layer(), r.resolve({host, "ws"}));
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[ws_snippet_7
|
||||
boost::asio::ip::tcp::acceptor acceptor{ios};
|
||||
stream<boost::asio::ip::tcp::socket> ws{acceptor.get_io_service()};
|
||||
acceptor.accept(ws.next_layer());
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
stream<boost::asio::ip::tcp::socket> ws{ios};
|
||||
//[ws_snippet_8
|
||||
ws.handshake("localhost", "/");
|
||||
//]
|
||||
|
||||
//[ws_snippet_9
|
||||
ws.handshake_ex("localhost", "/",
|
||||
[](request_type& m)
|
||||
{
|
||||
m.insert(http::field::sec_websocket_protocol, "xmpp;ws-chat");
|
||||
});
|
||||
//]
|
||||
|
||||
//[ws_snippet_10
|
||||
response_type res;
|
||||
ws.handshake(res, "localhost", "/");
|
||||
if(! res.count(http::field::sec_websocket_protocol))
|
||||
throw std::invalid_argument("missing subprotocols");
|
||||
//]
|
||||
|
||||
//[ws_snippet_11
|
||||
ws.accept();
|
||||
//]
|
||||
|
||||
//[ws_snippet_12
|
||||
ws.accept_ex(
|
||||
[](response_type& m)
|
||||
{
|
||||
m.insert(http::field::server, "MyServer");
|
||||
});
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[ws_snippet_13]
|
||||
// Buffer required for reading HTTP messages
|
||||
flat_buffer buffer;
|
||||
|
||||
// Read the HTTP request ourselves
|
||||
http::request<http::string_body> req;
|
||||
http::read(sock, buffer, req);
|
||||
|
||||
// See if its a WebSocket upgrade request
|
||||
if(websocket::is_upgrade(req))
|
||||
{
|
||||
// Construct the stream, transferring ownership of the socket
|
||||
stream<boost::asio::ip::tcp::socket> ws{std::move(sock)};
|
||||
|
||||
// Accept the request from our message. Clients SHOULD NOT
|
||||
// begin sending WebSocket frames until the server has
|
||||
// provided a response, but just in case they did, we pass
|
||||
// any leftovers in the buffer to the accept function.
|
||||
//
|
||||
ws.accept(req, buffer.data());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Its not a WebSocket upgrade, so
|
||||
// handle it like a normal HTTP request.
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
stream<boost::asio::ip::tcp::socket> ws{ios};
|
||||
//[ws_snippet_14
|
||||
// Read into our buffer until we reach the end of the HTTP request.
|
||||
// No parsing takes place here, we are just accumulating data.
|
||||
boost::asio::streambuf buffer;
|
||||
boost::asio::read_until(sock, buffer, "\r\n\r\n");
|
||||
|
||||
// Now accept the connection, using the buffered data.
|
||||
ws.accept(buffer.data());
|
||||
//]
|
||||
}
|
||||
{
|
||||
stream<boost::asio::ip::tcp::socket> ws{ios};
|
||||
//[ws_snippet_15
|
||||
multi_buffer buffer;
|
||||
ws.read(buffer);
|
||||
|
||||
ws.text(ws.got_text());
|
||||
ws.write(buffer.data());
|
||||
buffer.consume(buffer.size());
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
stream<boost::asio::ip::tcp::socket> ws{ios};
|
||||
//[ws_snippet_16
|
||||
multi_buffer buffer;
|
||||
for(;;)
|
||||
if(ws.read_some(buffer, 0))
|
||||
break;
|
||||
ws.binary(ws.got_binary());
|
||||
consuming_buffers<multi_buffer::const_buffers_type> cb{buffer.data()};
|
||||
for(;;)
|
||||
{
|
||||
using boost::asio::buffer_size;
|
||||
if(buffer_size(cb) > 512)
|
||||
{
|
||||
ws.write_some(false, buffer_prefix(512, cb));
|
||||
cb.consume(512);
|
||||
}
|
||||
else
|
||||
{
|
||||
ws.write_some(true, cb);
|
||||
break;
|
||||
}
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
stream<boost::asio::ip::tcp::socket> ws{ios};
|
||||
//[ws_snippet_17
|
||||
auto cb =
|
||||
[](frame_type kind, string_view payload)
|
||||
{
|
||||
// Do something with the payload
|
||||
boost::ignore_unused(kind, payload);
|
||||
};
|
||||
ws.control_callback(cb);
|
||||
//]
|
||||
|
||||
//[ws_snippet_18
|
||||
ws.close(close_code::normal);
|
||||
//]
|
||||
|
||||
//[ws_snippet_19
|
||||
ws.auto_fragment(true);
|
||||
ws.write_buffer_size(16384);
|
||||
//]
|
||||
|
||||
//[ws_snippet_20
|
||||
multi_buffer buffer;
|
||||
ws.async_read(buffer,
|
||||
[](error_code, std::size_t)
|
||||
{
|
||||
// Do something with the buffer
|
||||
});
|
||||
//]
|
||||
}
|
||||
|
||||
} // fxx()
|
||||
|
||||
// workaround for https://github.com/chriskohlhoff/asio/issues/112
|
||||
#ifdef BOOST_MSVC
|
||||
//[ws_snippet_21
|
||||
void echo(stream<boost::asio::ip::tcp::socket>& ws,
|
||||
multi_buffer& buffer, boost::asio::yield_context yield)
|
||||
{
|
||||
ws.async_read(buffer, yield);
|
||||
std::future<void> fut =
|
||||
ws.async_write(buffer.data(), boost::asio::use_future);
|
||||
}
|
||||
//]
|
||||
#endif
|
||||
|
||||
} // doc_ws_snippets
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#if BOOST_BEAST_USE_OPENSSL
|
||||
|
||||
//[wss_snippet_1
|
||||
#include <boost/beast/websocket/ssl.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
//]
|
||||
|
||||
namespace doc_wss_snippets {
|
||||
|
||||
void fxx() {
|
||||
|
||||
boost::asio::io_service ios;
|
||||
boost::asio::io_service::work work{ios};
|
||||
std::thread t{[&](){ ios.run(); }};
|
||||
error_code ec;
|
||||
boost::asio::ip::tcp::socket sock{ios};
|
||||
|
||||
{
|
||||
//[wss_snippet_2
|
||||
boost::asio::ssl::context ctx{boost::asio::ssl::context::sslv23};
|
||||
stream<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> wss{ios, ctx};
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[wss_snippet_3
|
||||
boost::asio::ip::tcp::endpoint ep;
|
||||
boost::asio::ssl::context ctx{boost::asio::ssl::context::sslv23};
|
||||
stream<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> ws{ios, ctx};
|
||||
|
||||
// connect the underlying TCP/IP socket
|
||||
ws.next_layer().next_layer().connect(ep);
|
||||
|
||||
// perform SSL handshake
|
||||
ws.next_layer().handshake(boost::asio::ssl::stream_base::client);
|
||||
|
||||
// perform WebSocket handshake
|
||||
ws.handshake("localhost", "/");
|
||||
//]
|
||||
}
|
||||
|
||||
} // fxx()
|
||||
|
||||
} // doc_wss_snippets
|
||||
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user