mirror of
https://github.com/matrix-construct/construct
synced 2024-12-27 07:54:05 +01:00
ircd::http: Grammar exception translator can be made aware of request vs. response.
This commit is contained in:
parent
daae800cc1
commit
bf91eb6883
2 changed files with 37 additions and 5 deletions
|
@ -262,6 +262,7 @@ struct ircd::http::request::head
|
|||
struct ircd::http::response
|
||||
{
|
||||
struct head;
|
||||
struct chunk;
|
||||
|
||||
// compose a response into buffer
|
||||
response(window_buffer &,
|
||||
|
@ -291,6 +292,15 @@ struct ircd::http::response::head
|
|||
head() = default;
|
||||
};
|
||||
|
||||
struct ircd::http::response::chunk
|
||||
:line
|
||||
{
|
||||
size_t size {0};
|
||||
|
||||
chunk(parse::capstan &pc);
|
||||
chunk() = default;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
T
|
||||
ircd::http::query::string::get(const string_view &key,
|
||||
|
|
32
ircd/http.cc
32
ircd/http.cc
|
@ -19,7 +19,7 @@ namespace ircd::http
|
|||
|
||||
extern const std::unordered_map<ircd::http::code, ircd::string_view> reason;
|
||||
|
||||
[[noreturn]] void throw_error(const qi::expectation_failure<const char *> &);
|
||||
[[noreturn]] void throw_error(const qi::expectation_failure<const char *> &, const bool &internal = false);
|
||||
}
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT
|
||||
|
@ -675,19 +675,41 @@ ircd::http::writeline(window_buffer &write)
|
|||
});
|
||||
}
|
||||
|
||||
/// Called to translate a grammar exception into an http::error within our
|
||||
/// system. This will then usually propagate back to our client.
|
||||
///
|
||||
/// If we are a client to another server, set internal=true. Even though this
|
||||
/// still generates an HTTP error, the code is 500 so if it propagates back to
|
||||
/// a client it does not indicate to *that* client that *they* made a bad
|
||||
/// request from a 400 back to them.
|
||||
void
|
||||
ircd::http::throw_error(const qi::expectation_failure<const char *> &e)
|
||||
ircd::http::throw_error(const qi::expectation_failure<const char *> &e,
|
||||
const bool &internal)
|
||||
{
|
||||
const auto rule
|
||||
const auto &code_
|
||||
{
|
||||
internal?
|
||||
code::INTERNAL_SERVER_ERROR:
|
||||
code::BAD_REQUEST
|
||||
};
|
||||
|
||||
const char *const &fmtstr
|
||||
{
|
||||
internal?
|
||||
"I expected a valid HTTP %s. Server sent %zu invalid characters starting with `%s'.":
|
||||
"I require a valid HTTP %s. You sent %zu invalid characters starting with `%s'."
|
||||
};
|
||||
|
||||
const auto &rule
|
||||
{
|
||||
ircd::string(e.what_)
|
||||
};
|
||||
|
||||
throw error
|
||||
{
|
||||
code::BAD_REQUEST, fmt::snstringf
|
||||
code_, fmt::snstringf
|
||||
{
|
||||
512, "I require a valid HTTP %s. You sent %zu invalid characters starting with `%s'.",
|
||||
512, fmtstr,
|
||||
between(rule, "<", ">"),
|
||||
size_t(e.last - e.first),
|
||||
string_view{e.first, e.last}
|
||||
|
|
Loading…
Reference in a new issue