0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-10-31 19:08:59 +01:00

ircd::http: Simplify interface; DRY.

This commit is contained in:
Jason Volk 2018-08-30 18:58:59 -07:00
parent 83edde639f
commit e5f94ed704
2 changed files with 17 additions and 35 deletions

View file

@ -134,9 +134,9 @@ struct ircd::http::query
struct ircd::http::query::string
:string_view
{
void for_each(const std::function<void (const query &)> &) const;
bool until(const std::function<bool (const query &)> &) const;
using closure = std::function<bool (const query &)>;
bool for_each(const closure &) const;
string_view at(const string_view &key) const;
string_view operator[](const string_view &key) const;
template<class T> T at(const string_view &key) const;

View file

@ -544,28 +544,29 @@ ircd::http::query::string::operator[](const string_view &key)
const
{
string_view ret;
const auto match([&key, &ret](const query &query) -> bool
const auto match{[&key, &ret]
(const query &query) -> bool
{
if(query.first == key)
{
ret = query.second;
return false; // false to break out of until()
}
else return true;
});
if(query.first != key)
return true;
until(match);
ret = query.second;
return false; // false to break out of for_each()
}};
for_each(match);
return ret;
}
bool
ircd::http::query::string::until(const std::function<bool (const query &)> &closure)
ircd::http::query::string::for_each(const closure &view)
const
{
const auto action([&closure](const auto &attribute, const auto &context, auto &halt)
const auto action{[&view]
(const auto &attribute, const auto &context, auto &continue_)
{
halt = closure(attribute);
});
continue_ = view(attribute);
}};
const parser::rule<unused_type> grammar
{
@ -573,29 +574,10 @@ const
};
const string_view &s(*this);
const char *start(s.data()), *const stop(s.data() + s.size());
const char *start(s.begin()), *const stop(s.end());
return qi::parse(start, stop, grammar);
}
void
ircd::http::query::string::for_each(const std::function<void (const query &)> &closure)
const
{
const auto action([&closure](const auto &attribute, const auto &context, auto &halt)
{
closure(attribute);
});
const parser::rule<unused_type> grammar
{
-parser.question >> (parser.query[action] % parser.ampersand)
};
const string_view &s(*this);
const char *start(s.data()), *const stop(s.data() + s.size());
qi::parse(start, stop, grammar);
}
size_t
ircd::http::parser::content_length(const string_view &str)
{