mirror of
https://github.com/matrix-construct/construct
synced 2024-12-25 15:04:10 +01:00
ircd::resource: Add conditional method list generator.
This commit is contained in:
parent
c75d602d69
commit
7434a06ec6
2 changed files with 26 additions and 4 deletions
|
@ -40,7 +40,10 @@ struct ircd::resource
|
|||
std::unique_ptr<method> default_method_head;
|
||||
std::unique_ptr<method> default_method_options;
|
||||
|
||||
string_view allow_methods_list(const mutable_buffer &buf) const;
|
||||
using method_closure = std::function<bool (const method &)>;
|
||||
string_view method_list(const mutable_buffer &buf, const method_closure &) const;
|
||||
string_view method_list(const mutable_buffer &buf) const;
|
||||
|
||||
response handle_options(client &, const request &) const;
|
||||
response handle_head(client &, const request &) const;
|
||||
|
||||
|
|
|
@ -198,7 +198,7 @@ catch(const std::out_of_range &e)
|
|||
thread_local char buf[512];
|
||||
const http::header headers[]
|
||||
{
|
||||
{ "Allow", allow_methods_list(buf) }
|
||||
{ "Allow", method_list(buf) }
|
||||
};
|
||||
|
||||
throw http::error
|
||||
|
@ -230,7 +230,19 @@ const
|
|||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::resource::allow_methods_list(const mutable_buffer &buf)
|
||||
ircd::resource::method_list(const mutable_buffer &buf)
|
||||
const
|
||||
{
|
||||
return method_list(buf, []
|
||||
(const method &)
|
||||
{
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::resource::method_list(const mutable_buffer &buf,
|
||||
const method_closure &closure)
|
||||
const
|
||||
{
|
||||
size_t len(0);
|
||||
|
@ -240,9 +252,16 @@ const
|
|||
auto it(begin(methods));
|
||||
if(it != end(methods))
|
||||
{
|
||||
len = strlcat(buf, it->first);
|
||||
assert(it->second);
|
||||
if(closure(*it->second))
|
||||
len = strlcat(buf, it->first);
|
||||
|
||||
for(++it; it != end(methods); ++it)
|
||||
{
|
||||
assert(it->second);
|
||||
if(!closure(*it->second))
|
||||
continue;
|
||||
|
||||
len = strlcat(buf, " ");
|
||||
len = strlcat(buf, it->first);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue