From 9821d7f333ce02e71a5d2b30ce773c5d6f3f8815 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 7 May 2018 14:46:53 -0700 Subject: [PATCH] ircd::m::v1: Add key query request. --- include/ircd/m/v1/key.h | 45 +++++++++++++++++++ include/ircd/m/v1/v1.h | 1 + ircd/m/v1.cc | 95 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 include/ircd/m/v1/key.h diff --git a/include/ircd/m/v1/key.h b/include/ircd/m/v1/key.h new file mode 100644 index 000000000..017a5920a --- /dev/null +++ b/include/ircd/m/v1/key.h @@ -0,0 +1,45 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// 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. + +#pragma once +#define HAVE_IRCD_M_V1_KEY_H + +namespace ircd::m::v1 +{ + struct key; +}; + +struct ircd::m::v1::key +:server::request +{ + struct opts; + + explicit operator json::array() const + { + const json::object object{in.content}; + const json::array &server_keys{object.get("server_keys")}; + return server_keys; + } + + using server_key = std::pair; // server_name, key_id + + key(const vector_view &, const mutable_buffer &, opts); + key() = default; +}; + +struct ircd::m::v1::key::opts +{ + net::hostport remote; + m::request request; + server::out out; + server::in in; + const struct server::request::opts *sopts {nullptr}; + bool dynamic {false}; +}; diff --git a/include/ircd/m/v1/v1.h b/include/ircd/m/v1/v1.h index 419b4887d..e3ff7b4c5 100644 --- a/include/ircd/m/v1/v1.h +++ b/include/ircd/m/v1/v1.h @@ -18,6 +18,7 @@ namespace ircd::m::v1 } #include "version.h" +#include "key.h" #include "query.h" #include "user.h" #include "make_join.h" diff --git a/ircd/m/v1.cc b/ircd/m/v1.cc index 2765de04c..d9322c5d9 100644 --- a/ircd/m/v1.cc +++ b/ircd/m/v1.cc @@ -927,6 +927,101 @@ ircd::m::v1::query::query(const string_view &type, { } +/////////////////////////////////////////////////////////////////////////////// +// +// v1/key.h +// + +namespace ircd::m::v1 +{ + static const_buffer + _make_server_keys(const vector_view &, + const mutable_buffer &); +} + +ircd::m::v1::key::key(const vector_view &keys, + const mutable_buffer &buf_, + opts opts) +:server::request{[&] +{ + assert(!!opts.remote); + + if(!defined(json::get<"origin"_>(opts.request))) + json::get<"origin"_>(opts.request) = my_host(); + + if(!defined(json::get<"destination"_>(opts.request))) + json::get<"destination"_>(opts.request) = host(opts.remote); + + if(defined(json::get<"content"_>(opts.request))) + opts.out.content = json::get<"content"_>(opts.request); + + if(!defined(json::get<"content"_>(opts.request))) + json::get<"content"_>(opts.request) = json::object{opts.out.content}; + + if(!defined(json::get<"uri"_>(opts.request))) + json::get<"uri"_>(opts.request) = "/_matrix/key/v2/query"; + + json::get<"method"_>(opts.request) = "POST"; + + window_buffer buf{buf_}; + if(!defined(json::get<"content"_>(opts.request))) + { + buf([&keys](const mutable_buffer &buf) + { + return _make_server_keys(keys, buf); + }); + + json::get<"content"_>(opts.request) = json::object{buf.completed()}; + opts.out.content = json::get<"content"_>(opts.request); + } + + opts.out.head = opts.request(buf); + + if(!size(opts.in)) + { + opts.in.head = buf + size(opts.out.head); + opts.in.content = opts.dynamic? + mutable_buffer{}: // server::request will allocate new mem + opts.in.head; // server::request will auto partition + } + + return server::request + { + opts.remote, std::move(opts.out), std::move(opts.in), opts.sopts + }; +}()} +{ +} + +static ircd::const_buffer +ircd::m::v1::_make_server_keys(const vector_view &keys, + const mutable_buffer &buf) +{ + json::stack out{buf}; + { + json::stack::object top{out}; + json::stack::member server_keys{top, "server_keys"}; + for(const auto &sk : keys) + { + json::stack::object object{server_keys}; + json::stack::member server_name{object, sk.first}; + { + json::stack::object object{server_name}; + json::stack::member key_name{object, sk.second}; + { + json::stack::object object{key_name}; + json::stack::member mvut + { + object, "minimum_valid_until_ts", json::value{0L} + }; + } + } + } + } + + return out.completed(); +} + /////////////////////////////////////////////////////////////////////////////// // // v1/version.h