From a068981452d13e2b79fc3812186847ddecb96164 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 7 Mar 2020 12:53:33 -0800 Subject: [PATCH] ircd::m::fed: Simplify and rename well known fetch function. --- include/ircd/m/fed/fed.h | 2 +- matrix/fed.cc | 35 ++++++++++++++++++++++------------- modules/console.cc | 4 ++-- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/include/ircd/m/fed/fed.h b/include/ircd/m/fed/fed.h index b2593c44f..0f282bf6e 100644 --- a/include/ircd/m/fed/fed.h +++ b/include/ircd/m/fed/fed.h @@ -14,7 +14,7 @@ namespace ircd::m::fed { net::hostport matrix_service(net::hostport); - net::hostport well_known(const mutable_buffer &out, const net::hostport &remote); + string_view fetch_well_known(const mutable_buffer &out, const string_view &origin); id::event::buf fetch_head(const id::room &room_id, const net::hostport &remote, const id::user &); id::event::buf fetch_head(const id::room &room_id, const net::hostport &remote); } diff --git a/matrix/fed.cc b/matrix/fed.cc index 0c86de46f..4d71030e4 100644 --- a/matrix/fed.cc +++ b/matrix/fed.cc @@ -1737,11 +1737,22 @@ ircd::m::fed::fetch_head(const id::room &room_id, return prev_event_id; } -ircd::net::hostport -ircd::m::fed::well_known(const mutable_buffer &buf, - const net::hostport &remote) +ircd::string_view +ircd::m::fed::fetch_well_known(const mutable_buffer &buf, + const string_view &origin) try { + const net::hostport remote + { + origin + }; + + // Hard target https service; do not inherit matrix service from remote. + const net::hostport target + { + host(remote), "https", port(remote) + }; + const unique_buffer head_buf { 16_KiB @@ -1753,12 +1764,6 @@ try wb, host(remote), "GET", "/.well-known/matrix/server", }; - // Hard target https service; do not inherit matrix service from remote. - const net::hostport target - { - host(remote), "https", port(remote) - }; - const const_buffer out_head { wb.completed() @@ -1809,7 +1814,12 @@ try string_view{response}, }; - return matrix_service(ret); + // Move the returned string to the front of the buffer; this overwrites + // any other incoming content to focus on just the unquoted string. + return string_view + { + data(buf), move(buf, m_server) + }; } catch(const ctx::interrupted &) { @@ -1817,13 +1827,12 @@ catch(const ctx::interrupted &) } catch(const std::exception &e) { - thread_local char rembuf[rfc3986::DOMAIN_BUFSIZE * 2]; log::derror { log, "Matrix server well-known query for %s :%s", - string(rembuf, remote), + origin, e.what(), }; - return remote; + return origin; } diff --git a/modules/console.cc b/modules/console.cc index 1d0621df6..ee0ee4955 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -14693,7 +14693,7 @@ console_cmd__well_known__matrix__server(opt &out, const string_view &line) "remote" }}; - const net::hostport &remote + const string_view &remote { param.at("remote") }; @@ -14705,7 +14705,7 @@ console_cmd__well_known__matrix__server(opt &out, const string_view &line) const net::hostport result { - m::fed::well_known(buf, remote) + m::fed::fetch_well_known(buf, remote) }; out << result << std::endl;