2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-12-12 21:14:47 +01:00
|
|
|
|
|
|
|
#include <ircd/spirit.h>
|
|
|
|
|
|
|
|
namespace ircd::rfc3986
|
|
|
|
{
|
2018-03-17 18:44:40 +01:00
|
|
|
using namespace ircd::spirit;
|
2017-12-12 21:14:47 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 01:41:47 +01:00
|
|
|
struct ircd::rfc3986::encoder
|
|
|
|
:karma::grammar<char *, const string_view &>
|
|
|
|
{
|
|
|
|
void throw_illegal()
|
|
|
|
{
|
|
|
|
throw encoding_error("Generator Protection: urlencode");
|
|
|
|
}
|
|
|
|
|
|
|
|
karma::rule<char *, const string_view &> url_encoding
|
|
|
|
{
|
|
|
|
*(karma::char_("A-Za-z0-9") | (karma::lit('%') << karma::hex))
|
|
|
|
,"url encoding"
|
|
|
|
};
|
|
|
|
|
|
|
|
encoder(): encoder::base_type{url_encoding} {}
|
|
|
|
}
|
|
|
|
const ircd::rfc3986::encoder;
|
|
|
|
|
|
|
|
struct ircd::rfc3986::decoder
|
|
|
|
:qi::grammar<const char *, mutable_buffer>
|
|
|
|
{
|
|
|
|
template<class R = unused_type, class... S> using rule = qi::rule<const char *, R, S...>;
|
|
|
|
|
|
|
|
rule<> url_illegal
|
|
|
|
{
|
|
|
|
char_(0x00, 0x1f)
|
|
|
|
,"url illegal"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<char()> url_encodable
|
|
|
|
{
|
|
|
|
char_("A-Za-z0-9")
|
|
|
|
,"url encodable character"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<char()> urlencoded_character
|
|
|
|
{
|
|
|
|
'%' > qi::uint_parser<char, 16, 2, 2>{}
|
|
|
|
,"urlencoded character"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<mutable_buffer> url_decode
|
|
|
|
{
|
|
|
|
*((char_ - '%') | urlencoded_character)
|
|
|
|
,"urldecode"
|
|
|
|
};
|
|
|
|
|
|
|
|
decoder(): decoder::base_type { url_decode } {}
|
|
|
|
}
|
|
|
|
const ircd::rfc3986::decoder;
|
|
|
|
|
2019-02-26 21:58:51 +01:00
|
|
|
decltype(ircd::rfc3986::parser::port)
|
|
|
|
ircd::rfc3986::parser::port
|
2017-12-12 21:14:47 +01:00
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
ushort_
|
|
|
|
,"port number"
|
|
|
|
};
|
2017-12-12 21:14:47 +01:00
|
|
|
|
2019-02-26 21:58:51 +01:00
|
|
|
decltype(ircd::rfc3986::parser::ip4_octet)
|
|
|
|
ircd::rfc3986::parser::ip4_octet
|
2017-12-12 21:14:47 +01:00
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
repeat(1,3)[char_("0-9")]
|
|
|
|
,"IPv4 octet"
|
|
|
|
};
|
2017-12-12 21:14:47 +01:00
|
|
|
|
2019-02-26 21:58:51 +01:00
|
|
|
decltype(ircd::rfc3986::parser::ip4_literal)
|
|
|
|
ircd::rfc3986::parser::ip4_literal
|
|
|
|
{
|
|
|
|
repeat(3)[ip4_octet >> '.'] >> ip4_octet
|
|
|
|
,"IPv4 literal"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::ip6_char)
|
|
|
|
ircd::rfc3986::parser::ip6_char
|
|
|
|
{
|
|
|
|
char_("0-9a-fA-F")
|
|
|
|
,"IPv6 character"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::ip6_h16)
|
|
|
|
ircd::rfc3986::parser::ip6_h16
|
|
|
|
{
|
|
|
|
repeat(1,4)[ip6_char]
|
|
|
|
,"IPv6 hexdigit"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::ip6_piece)
|
|
|
|
ircd::rfc3986::parser::ip6_piece
|
|
|
|
{
|
|
|
|
ip6_h16 >> ':'
|
|
|
|
,"IPv6 address piece"
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is reversed from the BNF in the RFC otherwise it requires
|
|
|
|
// backtracking during the repeat[]; parsers are adjusted accordingly.
|
|
|
|
decltype(ircd::rfc3986::parser::ip6_ipiece)
|
|
|
|
ircd::rfc3986::parser::ip6_ipiece
|
|
|
|
{
|
|
|
|
':' >> ip6_h16
|
|
|
|
,"IPv6 address piece"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::ip6_ls32)
|
|
|
|
ircd::rfc3986::parser::ip6_ls32
|
|
|
|
{
|
|
|
|
(ip6_h16 >> ':' >> ip6_h16) | ip4_literal
|
|
|
|
};
|
|
|
|
|
|
|
|
/// https://tools.ietf.org/html/rfc3986 Appendix A
|
|
|
|
decltype(ircd::rfc3986::parser::ip6_addr)
|
|
|
|
ircd::rfc3986::parser::ip6_addr
|
|
|
|
{
|
|
|
|
{ repeat(6)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ lit("::") >> repeat(5)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> lit("::") >> repeat(4)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,1)[ip6_ipiece] >> lit("::") >> repeat(3)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,2)[ip6_ipiece] >> lit("::") >> repeat(2)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,3)[ip6_ipiece] >> lit("::") >> ip6_piece >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,4)[ip6_ipiece] >> lit("::") >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,5)[ip6_ipiece] >> lit("::") >> -ip6_h16 },
|
|
|
|
{ lit("::") >> -ip6_h16 },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::ip6_address)
|
|
|
|
ircd::rfc3986::parser::ip6_address
|
|
|
|
{
|
|
|
|
ip6_addr[0] | ip6_addr[1] | ip6_addr[2] |
|
|
|
|
ip6_addr[3] | ip6_addr[4] | ip6_addr[5] |
|
|
|
|
ip6_addr[6] | ip6_addr[7] | ip6_addr[8]
|
|
|
|
,"IPv6 address"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::ip6_literal)
|
|
|
|
ircd::rfc3986::parser::ip6_literal
|
|
|
|
{
|
|
|
|
'[' >> ip6_address >> ']'
|
|
|
|
,"ip6 literal"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::hostname)
|
|
|
|
ircd::rfc3986::parser::hostname
|
|
|
|
{
|
|
|
|
char_("A-Za-z0-9") >> *(char_("A-Za-z0-9\x2D")) // x2D is '-'
|
|
|
|
,"hostname"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::domain)
|
|
|
|
ircd::rfc3986::parser::domain
|
|
|
|
{
|
|
|
|
hostname % '.'
|
|
|
|
,"domain"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::host)
|
|
|
|
ircd::rfc3986::parser::host
|
|
|
|
{
|
|
|
|
ip6_literal | ip4_literal | domain
|
|
|
|
,"host"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::rfc3986::parser::remote)
|
|
|
|
ircd::rfc3986::parser::remote
|
|
|
|
{
|
|
|
|
host >> -(':' > port)
|
|
|
|
,"remote"
|
|
|
|
};
|
2017-12-12 21:14:47 +01:00
|
|
|
|
2018-12-07 01:27:05 +01:00
|
|
|
ircd::string_view
|
2018-12-07 01:41:47 +01:00
|
|
|
ircd::rfc3986::encode(const mutable_buffer &out,
|
|
|
|
const json::members &members)
|
2018-12-07 01:27:05 +01:00
|
|
|
{
|
|
|
|
window_buffer buf{out};
|
|
|
|
const auto append{[&buf](const json::member &member)
|
|
|
|
{
|
2018-12-07 02:31:22 +01:00
|
|
|
assert(type(member.first) == json::STRING);
|
|
|
|
if(unlikely(!member.second.serial && type(member.second) != json::STRING))
|
2019-01-14 00:50:04 +01:00
|
|
|
throw panic
|
2018-12-07 02:31:22 +01:00
|
|
|
{
|
|
|
|
"Cannot encode non-serial json::member type '%s'",
|
|
|
|
reflect(type(member.second))
|
|
|
|
};
|
|
|
|
|
2018-12-07 01:41:47 +01:00
|
|
|
consume(buf, size(encode(buf, member.first)));
|
2018-12-07 01:27:05 +01:00
|
|
|
consume(buf, copy(buf, "="_sv));
|
2018-12-07 01:41:47 +01:00
|
|
|
consume(buf, size(encode(buf, member.second)));
|
2018-12-07 01:27:05 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
auto it(begin(members));
|
|
|
|
if(it != end(members))
|
|
|
|
{
|
|
|
|
append(*it);
|
|
|
|
for(++it; it != end(members); ++it)
|
|
|
|
{
|
|
|
|
consume(buf, copy(buf, "&"_sv));
|
|
|
|
append(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.completed();
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:14:47 +01:00
|
|
|
ircd::string_view
|
2018-12-07 01:41:47 +01:00
|
|
|
ircd::rfc3986::encode(const mutable_buffer &buf,
|
|
|
|
const string_view &url)
|
2017-12-12 21:14:47 +01:00
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
char *out(data(buf));
|
2017-12-12 21:14:47 +01:00
|
|
|
karma::generate(out, maxwidth(size(buf))[encoder], url);
|
2018-12-07 01:41:47 +01:00
|
|
|
return string_view
|
2017-12-12 21:14:47 +01:00
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
data(buf), size_t(std::distance(data(buf), out))
|
2017-12-12 21:14:47 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-12-07 01:41:47 +01:00
|
|
|
ircd::rfc3986::decode(const mutable_buffer &buf,
|
|
|
|
const string_view &url)
|
2017-12-12 21:14:47 +01:00
|
|
|
try
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
const char *start(url.data()), *const stop
|
2017-12-12 21:14:47 +01:00
|
|
|
{
|
|
|
|
start + std::min(size(url), size(buf))
|
|
|
|
};
|
|
|
|
|
2018-12-07 01:41:47 +01:00
|
|
|
mutable_buffer mb
|
|
|
|
{
|
|
|
|
data(buf), size_t(0)
|
|
|
|
};
|
|
|
|
|
2017-12-12 21:14:47 +01:00
|
|
|
qi::parse(start, stop, eps > decoder, mb);
|
2018-12-07 01:41:47 +01:00
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
data(mb), size(mb)
|
|
|
|
};
|
2017-12-12 21:14:47 +01:00
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
const auto rule
|
|
|
|
{
|
|
|
|
ircd::string(e.what_)
|
|
|
|
};
|
|
|
|
|
|
|
|
throw decoding_error
|
|
|
|
{
|
|
|
|
"I require a valid urlencoded %s. You sent %zu invalid chars starting with `%s'.",
|
|
|
|
between(rule, "<", ">"),
|
|
|
|
size_t(e.last - e.first),
|
|
|
|
string_view{e.first, e.last}
|
|
|
|
};
|
|
|
|
}
|
2018-10-03 02:19:50 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::rfc3986::valid_remote(std::nothrow_t,
|
|
|
|
const string_view &str)
|
|
|
|
{
|
|
|
|
static const auto &rule
|
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
parser::remote >> eoi
|
2018-10-03 02:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(str.data()), *const stop(start + str.size());
|
|
|
|
return qi::parse(start, stop, rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::rfc3986::valid_remote(const string_view &str)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const auto &rule
|
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
parser::remote >> eoi
|
2018-10-03 02:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(str.data()), *const stop(start + str.size());
|
|
|
|
qi::parse(start, stop, eps > rule);
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
throw expectation_failure<error>{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::rfc3986::valid_host(std::nothrow_t,
|
|
|
|
const string_view &str)
|
|
|
|
{
|
|
|
|
static const auto &rule
|
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
parser::host >> eoi
|
2018-10-03 02:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(str.data()), *const stop(start + str.size());
|
|
|
|
return qi::parse(start, stop, rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::rfc3986::valid_host(const string_view &str)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const auto &rule
|
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
parser::host >> eoi
|
2018-10-03 02:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(str.data()), *const stop(start + str.size());
|
|
|
|
qi::parse(start, stop, eps > rule);
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
throw expectation_failure<error>{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::rfc3986::valid_domain(std::nothrow_t,
|
|
|
|
const string_view &str)
|
|
|
|
{
|
|
|
|
static const auto &rule
|
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
parser::domain >> eoi
|
2018-10-03 02:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(str.data()), *const stop(start + str.size());
|
|
|
|
return qi::parse(start, stop, rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::rfc3986::valid_domain(const string_view &str)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const auto &rule
|
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
parser::host >> eoi
|
2018-10-03 02:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(str.data()), *const stop(start + str.size());
|
|
|
|
qi::parse(start, stop, eps > rule);
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
throw expectation_failure<error>{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::rfc3986::valid_hostname(std::nothrow_t,
|
|
|
|
const string_view &str)
|
|
|
|
{
|
|
|
|
static const auto &rule
|
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
parser::hostname >> eoi
|
2018-10-03 02:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(str.data()), *const stop(start + str.size());
|
|
|
|
return qi::parse(start, stop, rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::rfc3986::valid_hostname(const string_view &str)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const auto &rule
|
|
|
|
{
|
2019-02-26 21:58:51 +01:00
|
|
|
parser::hostname >> eoi
|
2018-10-03 02:19:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(str.data()), *const stop(start + str.size());
|
|
|
|
qi::parse(start, stop, eps > rule);
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
throw expectation_failure<error>{e};
|
|
|
|
}
|