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
99 changes: 31 additions & 68 deletions example/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,22 @@ void install_services(capy::application& app)
http::serializer::config());
}

/*

struct json_rpc;

// Handle POST by parsing JSON-RPC,
// storing `json_rpc` in `rp.request_data`
// This validates the JSON and can respond with an error
//
app.use("/rpc", json_rpc_post())

// Process the JSON-RPC command
app.post(
"/rpc",
json_post(),
do_json_rpc()
);

*/

class json_sink : public http::sink
{
public:
explicit
json_sink(json_sink&&) = default;

json_sink(
json::value& jv) : jv_(jv)
json::storage_ptr sp = {})
: pr_(new json::parser(std::move(sp)))
{
}

auto release() -> json::value
{
return pr_->release();
}

private:
results
on_write(
Expand All @@ -85,66 +74,52 @@ class json_sink : public http::sink
results rv;
if(more)
{
rv.bytes = pr_.write_some(
rv.bytes = pr_->write_some(
static_cast<char const*>(
b.data()), b.size(), rv.ec);
}
else
{
rv.bytes = pr_.write(
rv.bytes = pr_->write(
static_cast<char const*>(b.data()),
b.size(), rv.ec);
}
if(! rv.ec.failed())
{
jv_ = pr_.release();
return rv;
}
return rv;
}

json::value& jv_;
json::parser pr_;
std::unique_ptr<json::parser> pr_;
};

struct post_json_rpc
struct do_json_rpc
{
auto operator()(
http::route_params& rp) const ->
http::route_result
{
if(! rp.is_method(http::method::post))
return http::route::next;
BOOST_ASSERT(rp.parser.is_complete());
auto& jv = rp.route_data.emplace<json::value>();
rp.parser.set_body<json_sink>(jv);
system::error_code ec;
rp.parser.parse(ec);
if(ec.failed())
return ec;
return http::route::next;
return rp.read_body(
json_sink(),
[this, &rp](
json::value jv) ->
http::route_result
{
return dispatch(std::move(jv));
});
}
};

struct do_json_rpc
{
auto operator()(
http::route_params&) const ->
// process the JSON-RPC request
auto dispatch(
json::value jv) const ->
http::route_result
{
(void)jv;
return http::route::next;
}
};

#ifdef BOOST_CAPY_HAS_CORO
auto
do_request(
http::route_params& rp) ->
capy::task<http::route_result>
{
co_return http::route::next;
}
#endif
};

int server_main( int argc, char* argv[] )
{
Expand All @@ -170,23 +145,11 @@ int server_main( int argc, char* argv[] )
http::cors_options opts;
opts.allowedHeaders = "Content-Type";

#ifdef BOOST_CAPY_HAS_CORO
srv.wwwroot.use( http::co_route( do_request ) );
#endif

srv.wwwroot.use("/rpc",
srv.wwwroot.use(
"/rpc",
http::cors(opts),
post_json_rpc(),
[]( http::route_params& rp) ->
http::route_result
{
if(rp.parser.is_complete())
{
// auto s = rp.parser.body();
return http::route::next;
}
return http::route::next;
});
do_json_rpc()
);
srv.wwwroot.use("/", serve_static( argv[3] ));

app.start();
Expand Down
49 changes: 49 additions & 0 deletions include/boost/beast2/impl/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ is_complete_condition(http::parser& pr)

//------------------------------------------------

/** Asynchronously reads some data into the parser.

This function is used to asynchronously read data from a
stream into the parser's input sequence. This function will always
keep reading until a complete header is obtained.
The function call will invoke the completion token
with the following signature:
@code
void(system::error_code ec
std::size_t bytes_transferred);
@endcode
@note The parser's input sequence may contain additional data
beyond what was required to complete the header.
@param s The stream to read from.
@param pr The parser to read data into.
@param token The completion token.
*/
template<
class AsyncReadStream,
BOOST_ASIO_COMPLETION_TOKEN_FOR(
Expand All @@ -163,6 +180,22 @@ async_read_some(
s);
}

/** Asynchronously reads data into the parser until the header is complete.
This function is used to asynchronously read data from a
stream into the parser's input sequence until the parser's
header is complete.
The function call will invoke the completion token
with the following signature:
@code
void(system::error_code ec
std::size_t bytes_transferred);
@endcode
@note The parser's input sequence may contain additional data
beyond what was required to complete the header.
@param s The stream to read from.
@param pr The parser to read data into.
@param token The completion token.
*/
template<
class AsyncReadStream,
BOOST_ASIO_COMPLETION_TOKEN_FOR(
Expand All @@ -179,6 +212,22 @@ async_read_header(
return async_read_some(s, pr, std::move(token));
}

/** Asynchronously reads data into the parser until the message is complete.
This function is used to asynchronously read data from a
stream into the parser's input sequence until the parser's
message is complete.
The function call will invoke the completion token
with the following signature:
@code
void(system::error_code ec
std::size_t bytes_transferred);
@endcode
@note The parser's input sequence may contain additional data
beyond what was required to complete the message.
@param s The stream to read from.
@param pr The parser to read data into.
@param token The completion token.
*/
template<
class AsyncReadStream,
BOOST_ASIO_COMPLETION_TOKEN_FOR(
Expand Down
Loading
Loading