Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions example/client/burl/any_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#include <boost/asio/coroutine.hpp>
#include <boost/asio/immediate.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/buffers/prefix.hpp>
#include <boost/buffers/slice.hpp>
#include <boost/core/span.hpp>
#include <boost/http_proto/parser.hpp>
#include <boost/http_proto/serializer.hpp>

Expand Down Expand Up @@ -55,7 +56,7 @@ class any_stream

virtual void
async_write_some(
const buffers::const_buffer_subspan& buffers,
const buffers::slice_of<boost::span<buffers::const_buffer const>>& buffers,
asio::any_completion_handler<void(error_code, std::size_t)>
handler) override
{
Expand All @@ -64,7 +65,7 @@ class any_stream

virtual void
async_read_some(
const buffers::mutable_buffer_subspan& buffers,
const buffers::slice_of<boost::span<buffers::mutable_buffer const>>& buffers,
asio::any_completion_handler<void(error_code, std::size_t)>
handler) override
{
Expand Down Expand Up @@ -152,7 +153,7 @@ class any_stream
}
BOOST_ASIO_CORO_YIELD
stream_->async_write_some(
buffers::prefix(buffers, wr_remain_),
prefix(buffers, wr_remain_),
std::move(self));
wr_remain_ -= n;
self.complete(ec, n);
Expand Down Expand Up @@ -190,7 +191,7 @@ class any_stream
}
BOOST_ASIO_CORO_YIELD
stream_->async_read_some(
buffers::prefix(buffers, rd_remain_),
prefix(buffers, rd_remain_),
std::move(self));
rd_remain_ -= n;
self.complete(ec, n);
Expand Down Expand Up @@ -225,12 +226,12 @@ class any_stream

virtual void
async_write_some(
const buffers::const_buffer_subspan&,
const buffers::slice_of<boost::span<buffers::const_buffer const>>&,
asio::any_completion_handler<void(error_code, std::size_t)>) = 0;

virtual void
async_read_some(
const buffers::mutable_buffer_subspan&,
const buffers::slice_of<boost::span<buffers::mutable_buffer const>>&,
asio::any_completion_handler<void(error_code, std::size_t)>) = 0;

virtual void async_shutdown(
Expand All @@ -239,6 +240,30 @@ class any_stream
virtual ~base() = default;
};

static
buffers::slice_of<
boost::span<buffers::const_buffer const>>
prefix(
buffers::slice_of<
boost::span<buffers::const_buffer const>> bs,
std::size_t n)
{
buffers::keep_front(bs, n);
return bs;
}

static
buffers::slice_of<
boost::span<buffers::mutable_buffer const>>
prefix(
buffers::slice_of<boost::span<
buffers::mutable_buffer const>> bs,
std::size_t n)
{
buffers::keep_front(bs, n);
return bs;
}

std::unique_ptr<base> stream_;
asio::steady_timer rd_timer;
asio::steady_timer wr_timer;
Expand Down
15 changes: 7 additions & 8 deletions example/client/burl/multipart_form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "multipart_form.hpp"

#include <boost/buffers/copy.hpp>
#include <boost/buffers/prefix.hpp>
#include <boost/buffers/slice.hpp>
#include <boost/http_proto/file.hpp>
#include <boost/system/system_error.hpp>

Expand Down Expand Up @@ -153,13 +153,12 @@ multipart_form::source::on_read(buffers::mutable_buffer mb)

auto copy = [&](core::string_view sv)
{
auto copied = buffers::copy(
mb,
buffers::sans_prefix(
buffers::const_buffer{ sv.data(), sv.size() },
static_cast<std::size_t>(skip_)));
buffers::const_buffer source(sv.data(), sv.size());
buffers::trim_front(mb, static_cast<std::size_t>(skip_));

mb = buffers::sans_prefix(mb, copied);
auto copied = buffers::copy(mb, source);

buffers::trim_front(mb, copied);
rs.bytes += copied;
skip_ += copied;

Expand Down Expand Up @@ -189,7 +188,7 @@ multipart_form::source::on_read(buffers::mutable_buffer mb)
if(rs.ec)
return false;

mb = buffers::sans_prefix(mb, read);
buffers::trim_front(mb, read);
rs.bytes += read;
skip_ += read;

Expand Down
2 changes: 1 addition & 1 deletion example/client/burl/multipart_form.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef BURL_MULTIPART_FORM_HPP
#define BURL_MULTIPART_FORM_HPP

#include <boost/buffers/mutable_buffer.hpp>
#include <boost/buffers/buffer.hpp>
#include <boost/core/detail/string_view.hpp>
#include <boost/http_proto/method.hpp>
#include <boost/http_proto/source.hpp>
Expand Down
8 changes: 4 additions & 4 deletions example/client/jsonrpc/jsonrpc/any_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#define JSONRPC_ANY_STREAM_HPP

#include <boost/asio/any_completion_handler.hpp>
#include <boost/buffers/const_buffer_span.hpp>
#include <boost/buffers/mutable_buffer_span.hpp>
#include <boost/buffers/buffer.hpp>
#include <boost/core/span.hpp>
#include <boost/url/url_view.hpp>

namespace jsonrpc {
Expand All @@ -33,12 +33,12 @@ class any_stream

virtual void
async_write_some(
const boost::buffers::const_buffer_subspan&,
const boost::span<boost::buffers::const_buffer const>&,
boost::asio::any_completion_handler<void(boost::system::error_code, std::size_t)>) = 0;

virtual void
async_read_some(
const boost::buffers::mutable_buffer_subspan&,
const boost::span<boost::buffers::mutable_buffer const>&,
boost::asio::any_completion_handler<void(boost::system::error_code, std::size_t)>) = 0;

virtual void
Expand Down
60 changes: 42 additions & 18 deletions example/client/jsonrpc/jsonrpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class stream_impl : public any_stream

void
async_write_some(
const buffers::const_buffer_subspan& buffers,
const boost::span<boost::buffers::const_buffer const>& buffers,
asio::any_completion_handler<void(system::error_code, std::size_t)>
handler) override
{
Expand All @@ -96,7 +96,7 @@ class stream_impl : public any_stream

void
async_read_some(
const buffers::mutable_buffer_subspan& buffers,
const boost::span<boost::buffers::mutable_buffer const>& buffers,
asio::any_completion_handler<void(system::error_code, std::size_t)>
handler) override
{
Expand Down Expand Up @@ -250,6 +250,33 @@ class json_sink : public http_proto::sink
}
};

class json_source : public http_proto::source
{
json::serializer& jsr_;
json::value v_;

public:
json_source(json::serializer& jsr, json::value v)
: jsr_(jsr)
, v_(std::move(v))
{
jsr_.reset(&v_);
}

private:
results
on_read(
buffers::mutable_buffer b) override
{
results ret;
ret.bytes = jsr_.read(
static_cast<char*>(b.data()),
b.size()).size();
ret.finished = jsr_.done();
return ret;
}
};

} //namespace

// ----------------------------------------------
Expand Down Expand Up @@ -288,21 +315,17 @@ class client::request_op
{
{
auto sp = params_.storage();
auto body = json::serialize(
json::object
json::object value(
{
{
{ "jsonrpc", "2.0" },
{ "method", method_ },
{ "params", std::move(params_)},
{ "id", id_ }
},
sp
});
c_.req_.set_payload_size(body.size());
c_.sr_.template start<
http_proto::string_body>(
c_.req_, std::move(body));
{ "jsonrpc", "2.0" },
{ "method", method_ },
{ "params", std::move(params_)},
{ "id", id_ }
},
sp);
c_.req_.set_chunked(true);
c_.sr_.template start<json_source>(
c_.req_, c_.jsr_, std::move(value));
}

BOOST_ASIO_CORO_YIELD
Expand Down Expand Up @@ -330,8 +353,6 @@ class client::request_op
auto* id = obj.if_contains("id");
if(!ver || *ver != "2.0")
return self.complete({ errc::version_error }, {});
if(!id || *id != id_)
return self.complete({ errc::id_mismatch }, {});
if(err && err->is_object())
{
auto* code = err->get_object().if_contains("code");
Expand All @@ -350,6 +371,9 @@ class client::request_op
}
else if(res)
{
if(!id || *id != id_)
return self.complete({ errc::id_mismatch }, {});

return self.complete({}, std::move(*res));
}
self.complete({ errc::invalid_response }, {});
Expand Down
2 changes: 2 additions & 0 deletions example/client/jsonrpc/jsonrpc/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <boost/http_proto/request.hpp>
#include <boost/http_proto/response_parser.hpp>
#include <boost/http_proto/serializer.hpp>
#include <boost/json/serializer.hpp>
#include <boost/json/stream_parser.hpp>
#include <boost/json/value_to.hpp>
#include <boost/rts/context_fwd.hpp>
Expand All @@ -38,6 +39,7 @@ class client
boost::http_proto::response_parser pr_;
boost::http_proto::request req_;
boost::json::stream_parser jpr_;
boost::json::serializer jsr_;
std::uint64_t id_ = 0;

public:
Expand Down
3 changes: 2 additions & 1 deletion example/client/visit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX "" FILES ${PFILES})
add_executable(http_io_example_client_visit ${PFILES})

target_compile_definitions(http_io_example_client_visit
PRIVATE BOOST_ASIO_NO_DEPRECATED)
PRIVATE
BOOST_ASIO_NO_DEPRECATED)

set_property(TARGET http_io_example_client_visit
PROPERTY FOLDER "examples")
Expand Down
1 change: 1 addition & 0 deletions example/client/visit/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ project
<library>/boost/http_io//boost_http_io
<library>/boost/url//boost_url
<include>.
<define>BOOST_ASIO_NO_DEPRECATED
;

exe visit :
Expand Down
7 changes: 6 additions & 1 deletion example/client/visit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ struct worker
do_resolve()
{
resolver.async_resolve(
boost::core::string_view(url.encoded_host()),
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
url.encoded_host(),
url.scheme(),
#else
static_cast<std::string>(url.encoded_host()),
static_cast<std::string>(url.scheme()),
#endif
[&](
boost::system::error_code ec,
resolver_type::results_type results)
Expand Down
3 changes: 1 addition & 2 deletions test/unit/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
// Official repository: https://github.com/vinniefalco/http_io
//

#include <boost/buffers/const_buffer.hpp>
#include <boost/buffers/mutable_buffer.hpp>
#include <boost/buffers/buffer.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/static_assert.hpp>

Expand Down
Loading