Skip to content
Open
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
5 changes: 2 additions & 3 deletions example/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ int server_main( int argc, char* argv[] )
//srv.wwwroot.use(post_work());
srv.wwwroot.use(
http_proto::cors(),
[]( http_proto::Request& req,
http_proto::Response& res) ->
http_proto::route_result
[]( http_proto::route_params&) ->
http_proto::route_result
{
return http_proto::route::next;
});
Expand Down
5 changes: 2 additions & 3 deletions example/server/post_work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ struct task
http_proto::route_result
post_work::
operator()(
http_proto::Request&,
http_proto::Response& res) const
http_proto::route_params& p) const
{
return res.post(task());
return p.post(task());
}

} // beast2
Expand Down
3 changes: 1 addition & 2 deletions example/server/post_work.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ struct post_work
{
system::error_code
operator()(
http_proto::Request&,
http_proto::Response& res) const;
http_proto::route_params&) const;
};

} // beast2
Expand Down
9 changes: 4 additions & 5 deletions example/server/serve_detached.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,18 @@ class serve_detached

system::error_code
operator()(
http_proto::Request&,
http_proto::Response& res) const
http_proto::route_params& p) const
{
return res.detach(
return p.detach(
[&](http_proto::resumer resume)
{
asio::post(*tp_,
[&, resume]()
{
// Simulate some asynchronous work
std::this_thread::sleep_for(std::chrono::seconds(1));
res.status(http_proto::status::ok);
res.set_body("Hello from serve_detached!\n");
p.status(http_proto::status::ok);
p.set_body("Hello from serve_detached!\n");
resume(http_proto::route::send);
// resume( res.send("Hello from serve_detached!\n") );
});
Expand Down
18 changes: 8 additions & 10 deletions example/server/serve_log_admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class serve_log_page

system::error_code
operator()(
http_proto::Request&,
http_proto::Response& res) const
http_proto::route_params& p) const
{
auto const v = ls_.get_sections();
std::string s;
Expand Down Expand Up @@ -72,9 +71,9 @@ class serve_log_page
format_to(s, "</body>\n");
format_to(s, "</html>\n");

res.status(http_proto::status::ok);
res.message.set(http_proto::field::content_type, "text/html; charset=UTF-8");
res.set_body(std::move(s));
p.status(http_proto::status::ok);
p.res.set(http_proto::field::content_type, "text/html; charset=UTF-8");
p.set_body(std::move(s));
return http_proto::route::send;
}

Expand All @@ -97,12 +96,11 @@ class handle_submit

system::error_code
operator()(
http_proto::Request&,
http_proto::Response& res) const
http_proto::route_params& p) const
{
res.status(http_proto::status::ok);
res.message.set(http_proto::field::content_type, "plain/text; charset=UTF-8");
res.set_body("submit");
p.status(http_proto::status::ok);
p.res.set(http_proto::field::content_type, "plain/text; charset=UTF-8");
p.set_body("submit");
return http_proto::route::send;
}

Expand Down
79 changes: 38 additions & 41 deletions include/boost/beast2/server/http_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ class http_stream
using work_guard = asio::executor_work_guard<decltype(
std::declval<AsyncStream&>().get_executor())>;
std::unique_ptr<work_guard> pwg_;
http_proto::Request req_;
ResponseAsio<AsyncStream&> res_;
asio_route_params<AsyncStream&> p_;
};

//------------------------------------------------
Expand Down Expand Up @@ -173,12 +172,12 @@ http_stream(
, stream_(stream)
, routes_(std::move(routes))
, close_(close)
, res_(stream_)
, p_(stream_)
{
req_.parser = http_proto::request_parser(app);
p_.parser = http_proto::request_parser(app);

res_.serializer = http_proto::serializer(app);
res_.detach = http_proto::detacher(*this);
p_.serializer = http_proto::serializer(app);
p_.detach = http_proto::detacher(*this);
}

// called to start a new HTTP session.
Expand All @@ -191,8 +190,8 @@ on_stream_begin(
{
pconfig_ = &config;

req_.parser.reset();
res_.data.clear();
p_.parser.reset();
p_.session_data.clear();
do_read();
}

Expand All @@ -202,9 +201,9 @@ void
http_stream<AsyncStream>::
do_read()
{
req_.parser.start();
p_.parser.start();

beast2::async_read(stream_, req_.parser,
beast2::async_read(stream_, p_.parser,
call_mf(&http_stream::on_read, this));
}

Expand All @@ -225,7 +224,7 @@ on_read(
"{} http_stream::on_read bytes={}",
this->id(), bytes_transferred);

BOOST_ASSERT(req_.parser.is_complete());
BOOST_ASSERT(p_.parser.is_complete());

on_headers();
}
Expand All @@ -237,27 +236,26 @@ http_stream<AsyncStream>::
on_headers()
{
// set up Request and Response objects
res_.serializer.reset();
// VFALCO HACK for now we make a copy of the message
req_.message = req_.parser.get();
//res_.message.set_version(req_.message.version());
res_.message.set_start_line( // VFALCO WTF
http_proto::status::ok, req_.message.version());
res_.message.set_keep_alive(req_.message.keep_alive());
res_.data.clear();
p_.req = p_.parser.get();
p_.route_data.clear();
p_.res.set_start_line( // VFALCO WTF
http_proto::status::ok, p_.req.version());
p_.res.set_keep_alive(p_.req.keep_alive());
p_.serializer.reset();

// parse the URL
{
auto rv = urls::parse_uri_reference(req_.message.target());
auto rv = urls::parse_uri_reference(p_.req.target());
if(rv.has_error())
{
// error parsing URL
res_.status(http_proto::status::bad_request);
res_.set_body("Bad Request: " + rv.error().message());
p_.status(http_proto::status::bad_request);
p_.set_body("Bad Request: " + rv.error().message());
return do_respond(rv.error());
}

req_.url = rv.value();
p_.url = rv.value();
}

// invoke handlers for the route
Expand All @@ -275,11 +273,11 @@ do_dispatch(
{
BOOST_ASSERT(! pwg_); // can't be detached
rv = routes_.dispatch(
req_.message.method(), req_.url, req_, res_);
p_.req.method(), p_.url, p_);
}
else
{
rv = routes_.resume(req_, res_, rv);
rv = routes_.resume(p_, rv);
}

do_respond(rv);
Expand All @@ -303,13 +301,13 @@ do_respond(
{
// VFALCO what if the connection was closed or keep-alive=false?
// handler sendt the response?
BOOST_ASSERT(res_.serializer.is_done());
BOOST_ASSERT(p_.serializer.is_done());
return on_write(system::error_code(), 0);
}

if(rv == http_proto::route::detach)
{
// didn't call res.detach()?
// didn't call detach()?
if(! pwg_)
detail::throw_logic_error();
return;
Expand All @@ -319,20 +317,19 @@ do_respond(
{
// unhandled request
auto const status = http_proto::status::not_found;
res_.status(status);
//res_.message.set_keep_alive(false); // VFALCO?
res_.set_body(http_proto::to_string(status));
p_.status(status);
p_.set_body(http_proto::to_string(status));
}
else if(rv != http_proto::route::send)
{
// error message of last resort
BOOST_ASSERT(rv.failed());
BOOST_ASSERT(! http_proto::is_route_result(rv));
res_.status(http_proto::status::internal_server_error);
p_.status(http_proto::status::internal_server_error);
std::string s;
format_to(s, "An internal server error occurred: {}", rv.message());
res_.message.set_keep_alive(false); // VFALCO?
res_.set_body(s);
p_.res.set_keep_alive(false); // VFALCO?
p_.set_body(s);
}

do_write();
Expand All @@ -344,8 +341,8 @@ void
http_stream<AsyncStream>::
do_write()
{
BOOST_ASSERT(! res_.serializer.is_done());
beast2::async_write(stream_, res_.serializer,
BOOST_ASSERT(! p_.serializer.is_done());
beast2::async_write(stream_, p_.serializer,
call_mf(&http_stream::on_write, this));
}

Expand All @@ -362,13 +359,13 @@ on_write(
if(ec.failed())
return do_fail("http_stream::on_write", ec);

BOOST_ASSERT(res_.serializer.is_done());
BOOST_ASSERT(p_.serializer.is_done());

LOG_TRC(this->sect_)(
"{} http_stream::on_write bytes={}",
this->id(), bytes_transferred);

if(res_.message.keep_alive())
if(p_.res.keep_alive())
return do_read();

do_close();
Expand Down Expand Up @@ -419,8 +416,8 @@ do_fail(
LOG_TRC(this->sect_)("{}: {}", s, ec.message());

// tidy up lingering objects
req_.parser.reset();
res_.serializer.reset();
p_.parser.reset();
p_.serializer.reset();

close_(ec);
}
Expand All @@ -441,9 +438,9 @@ void
http_stream<AsyncStream>::
clear() noexcept
{
req_.parser.reset();
res_.serializer.reset();
res_.message.clear();
p_.parser.reset();
p_.serializer.reset();
p_.res.clear();
}

} // beast2
Expand Down
9 changes: 5 additions & 4 deletions include/boost/beast2/server/route_handler_asio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
namespace boost {
namespace beast2 {

/** Response object for Asio HTTP route handlers
/** Route parameters object for Asio HTTP route handlers
*/
template<class AsyncStream>
class ResponseAsio : public http_proto::Response
class asio_route_params
: public http_proto::route_params
{
public:
using stream_type = typename std::decay<AsyncStream>::type;
Expand All @@ -30,7 +31,7 @@ class ResponseAsio : public http_proto::Response

template<class... Args>
explicit
ResponseAsio(
asio_route_params(
Args&&... args)
: stream(std::forward<Args>(args)...)
{
Expand All @@ -44,7 +45,7 @@ class ResponseAsio : public http_proto::Response

template<class AsyncStream>
void
ResponseAsio<AsyncStream>::
asio_route_params<AsyncStream>::
do_post()
{
asio::post(
Expand Down
2 changes: 1 addition & 1 deletion include/boost/beast2/server/router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace beast2 {

/** The sans-IO router type
*/
using router = http_proto::basic_router<http_proto::Request, http_proto::Response>;
using router = http_proto::basic_router<http_proto::route_params>;

} // beast2
} // boost
Expand Down
2 changes: 1 addition & 1 deletion include/boost/beast2/server/router_asio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace beast2 {
*/
template<class Stream>
using router_asio = http_proto::basic_router<
http_proto::Request, ResponseAsio<Stream>>;
asio_route_params<Stream>>;

} // beast2
} // boost
Expand Down
3 changes: 1 addition & 2 deletions include/boost/beast2/server/serve_redirect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ struct serve_redirect
BOOST_BEAST2_DECL
http_proto::route_result
operator()(
http_proto::Request&,
http_proto::Response&) const;
http_proto::route_params&) const;
};

} // beast2
Expand Down
3 changes: 1 addition & 2 deletions include/boost/beast2/server/serve_static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ struct serve_static
*/
BOOST_BEAST2_DECL
system::error_code operator()(
http_proto::Request&,
http_proto::Response&) const;
http_proto::route_params&) const;

private:
struct impl;
Expand Down
10 changes: 10 additions & 0 deletions src/server/route_rule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ struct route_seg
core::string_view constraint;
char ptype = 0; // ':' | '?' | NULL
char modifier = 0;
char term; // param terminator or NULL
};

struct param_segment_rule_t
Expand Down Expand Up @@ -343,6 +344,15 @@ struct path_rule_t
return rv1.error();
route_seg rs = rv1.value();
rs.prefix = { it2, it1 };
if(it != end)
{
if( *it == ':' ||
*it == '*')
{
// can't have ":id:id"
return grammar::error::syntax;
}
}
rv.segs.push_back(rs);
it1 = it;
continue;
Expand Down
Loading
Loading