mirror of
https://github.com/matrix-construct/construct
synced 2024-12-27 07:54:05 +01:00
ircd::server: Add options to request; add http code exception option.
This commit is contained in:
parent
7f9f970b5b
commit
5a51638db8
2 changed files with 38 additions and 1 deletions
|
@ -63,12 +63,22 @@ struct ircd::server::in
|
|||
struct ircd::server::request
|
||||
:ctx::future<http::code>
|
||||
{
|
||||
struct opts;
|
||||
|
||||
static const opts opts_default;
|
||||
|
||||
server::tag *tag {nullptr};
|
||||
|
||||
public:
|
||||
/// Transmission data
|
||||
server::out out;
|
||||
|
||||
/// Reception data
|
||||
server::in in;
|
||||
|
||||
/// Options
|
||||
const struct opts *opts { &opts_default };
|
||||
|
||||
request(const net::hostport &, server::out out, server::in in);
|
||||
request() = default;
|
||||
request(request &&) noexcept;
|
||||
|
@ -78,6 +88,15 @@ struct ircd::server::request
|
|||
~request() noexcept;
|
||||
};
|
||||
|
||||
struct ircd::server::request::opts
|
||||
{
|
||||
/// When true, HTTP responses above the 200's are thrown as exceptions
|
||||
/// from the future::get() on this object. Otherwise, if false any code
|
||||
/// received is returned in the value and exceptions are thrown when no
|
||||
/// code can be returned.
|
||||
bool http_exceptions {true};
|
||||
};
|
||||
|
||||
inline
|
||||
ircd::server::request::request(const net::hostport &hostport,
|
||||
server::out out,
|
||||
|
@ -96,6 +115,7 @@ noexcept
|
|||
,tag{std::move(o.tag)}
|
||||
,out{std::move(o.out)}
|
||||
,in{std::move(o.in)}
|
||||
,opts{std::move(o.opts)}
|
||||
{
|
||||
if(tag)
|
||||
associate(*this, *tag, std::move(o));
|
||||
|
@ -111,6 +131,7 @@ noexcept
|
|||
out = std::move(o.out);
|
||||
in = std::move(o.in);
|
||||
tag = std::move(o.tag);
|
||||
opts = std::move(o.opts);
|
||||
|
||||
if(tag)
|
||||
associate(*this, *tag, std::move(o));
|
||||
|
|
|
@ -215,6 +215,10 @@ ircd::server::interrupt_all()
|
|||
// request
|
||||
//
|
||||
|
||||
decltype(ircd::server::request::opts_default)
|
||||
ircd::server::request::opts_default
|
||||
{};
|
||||
|
||||
/// Canceling a request is tricky. This allows a robust way to let the user's
|
||||
/// request go out of scope at virtually any time without disrupting the
|
||||
/// pipeline and other requests.
|
||||
|
@ -1980,8 +1984,20 @@ ircd::server::tag::set_value(args&&... a)
|
|||
if(abandoned())
|
||||
return;
|
||||
|
||||
const http::code &code
|
||||
{
|
||||
std::forward<args>(a)...
|
||||
};
|
||||
|
||||
assert(request->opts);
|
||||
if(request->opts->http_exceptions && code >= http::code(300))
|
||||
{
|
||||
set_exception(http::error{code});
|
||||
return;
|
||||
}
|
||||
|
||||
assert(p.valid());
|
||||
p.set_value(std::forward<args>(a)...);
|
||||
p.set_value(code);
|
||||
}
|
||||
|
||||
template<class... args>
|
||||
|
|
Loading…
Reference in a new issue