0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-28 11:48:54 +02:00

ircd::rfc3986: Add interface to extract host and port from remote via grammar.

This commit is contained in:
Jason Volk 2019-03-13 10:50:16 -07:00
parent f955f1d494
commit 50e7d81d36
2 changed files with 43 additions and 0 deletions

View file

@ -35,6 +35,9 @@ namespace ircd::rfc3986
void valid_remote(const string_view &); // host + optional :port
bool valid_remote(std::nothrow_t, const string_view &);
uint16_t port(const string_view &remote); // get portnum from valid remote
string_view host(const string_view &remote); // get host without portnum
string_view encode(const mutable_buffer &, const string_view &url);
string_view encode(const mutable_buffer &, const json::members &);
string_view decode(const mutable_buffer &, const string_view &url);

View file

@ -265,6 +265,46 @@ catch(const qi::expectation_failure<const char *> &e)
};
}
ircd::string_view
ircd::rfc3986::host(const string_view &str)
try
{
static const parser::rule<string_view> rule
{
raw[parser::host]
,"host"
};
string_view ret;
const char *start(str.data()), *const stop(start + str.size());
qi::parse(start, stop, eps > rule, ret);
return ret;
}
catch(const qi::expectation_failure<const char *> &e)
{
throw expectation_failure<error>{e};
}
uint16_t
ircd::rfc3986::port(const string_view &str)
try
{
static const parser::rule<uint16_t> rule
{
(eps > parser::host) >> -(lit(':') >> parser::port)
,"port"
};
uint16_t ret(0);
const char *start(str.data()), *const stop(start + str.size());
qi::parse(start, stop, rule, ret);
return ret;
}
catch(const qi::expectation_failure<const char *> &e)
{
throw expectation_failure<error>{e};
}
bool
ircd::rfc3986::valid_remote(std::nothrow_t,
const string_view &str)