0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-27 11:18:51 +02:00

ircd::resource: Add conditional method list generator.

This commit is contained in:
Jason Volk 2019-03-31 19:15:45 -07:00
parent c75d602d69
commit 7434a06ec6
2 changed files with 26 additions and 4 deletions

View file

@ -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;

View file

@ -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);
}