2018-04-23 09:47:03 +02: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.
|
|
|
|
|
|
|
|
#include "media.h"
|
|
|
|
|
2019-08-22 08:57:07 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2018-04-23 09:47:03 +02:00
|
|
|
preview_url_resource
|
|
|
|
{
|
|
|
|
"/_matrix/media/r0/preview_url",
|
|
|
|
{
|
|
|
|
"(11.7.1.5) Get information about a URL for a client"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static unique_buffer<mutable_buffer>
|
|
|
|
request_url(const string_view &urle);
|
|
|
|
|
|
|
|
static json::strung
|
|
|
|
parse_og(const string_view &content);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2018-04-23 09:47:03 +02:00
|
|
|
get__preview_url(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2018-04-23 09:47:03 +02:00
|
|
|
{
|
|
|
|
const auto &url
|
|
|
|
{
|
|
|
|
request.query.at("url")
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &ts
|
|
|
|
{
|
|
|
|
request.query.get("ts", milliseconds(-1))
|
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> content_buffer
|
|
|
|
{
|
|
|
|
//request_url(url)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view content
|
|
|
|
{
|
|
|
|
content_buffer
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::strung ogs
|
|
|
|
{
|
|
|
|
parse_og(content)
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2018-04-23 09:47:03 +02:00
|
|
|
{
|
|
|
|
client, json::object{ogs}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::method
|
2018-04-23 09:47:03 +02:00
|
|
|
method_get
|
|
|
|
{
|
|
|
|
preview_url_resource, "GET", get__preview_url
|
|
|
|
};
|
|
|
|
|
|
|
|
json::strung
|
|
|
|
parse_og(const string_view &content)
|
|
|
|
{
|
|
|
|
std::vector<json::member> og;
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
og.data(), og.data() + og.size()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_buffer<mutable_buffer>
|
|
|
|
request_url(const string_view &urle)
|
|
|
|
{
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
24_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto url
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(buf, urle)
|
2018-04-23 09:47:03 +02:00
|
|
|
};
|
|
|
|
|
2020-03-24 19:59:32 +01:00
|
|
|
const rfc3986::uri uri
|
2018-04-23 09:47:03 +02:00
|
|
|
{
|
2020-03-24 19:59:32 +01:00
|
|
|
url
|
2018-04-23 09:47:03 +02:00
|
|
|
};
|
|
|
|
|
2020-03-24 19:59:32 +01:00
|
|
|
const net::hostport remote
|
2018-04-23 09:47:03 +02:00
|
|
|
{
|
2020-03-24 19:59:32 +01:00
|
|
|
uri
|
2018-04-23 09:47:03 +02:00
|
|
|
};
|
|
|
|
|
2020-03-24 19:59:32 +01:00
|
|
|
if(empty(host(remote)) || empty(uri.path))
|
2018-04-23 09:47:03 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::BAD_REQUEST, "M_BAD_URL",
|
2020-03-24 19:59:32 +01:00
|
|
|
"Required elements are missing from the supplied URL."
|
2018-04-23 09:47:03 +02:00
|
|
|
};
|
|
|
|
|
2022-05-21 02:39:22 +02:00
|
|
|
window_buffer window
|
2018-04-23 09:47:03 +02:00
|
|
|
{
|
|
|
|
buf + size(url)
|
|
|
|
};
|
|
|
|
|
|
|
|
http::request
|
|
|
|
{
|
2022-05-21 02:39:22 +02:00
|
|
|
window, host(remote), "GET", uri.path, 0, {},
|
2018-04-23 09:47:03 +02:00
|
|
|
{
|
|
|
|
{ "User-Agent", info::user_agent },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer out_head
|
|
|
|
{
|
2022-05-21 02:39:22 +02:00
|
|
|
window.completed()
|
2018-04-23 09:47:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer in_head
|
|
|
|
{
|
|
|
|
buf + size(url) + size(out_head)
|
|
|
|
};
|
|
|
|
|
|
|
|
server::request::opts sopts;
|
|
|
|
sopts.http_exceptions = false;
|
|
|
|
server::request request
|
|
|
|
{
|
|
|
|
remote, { out_head }, { in_head, mutable_buffer{} }, &sopts
|
|
|
|
};
|
|
|
|
|
|
|
|
request.wait(seconds(10)); //TODO: conf
|
|
|
|
|
|
|
|
const auto code
|
|
|
|
{
|
|
|
|
request.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(code != http::OK)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
assert(data(request.in.content) == data(request.in.dynamic));
|
2019-04-12 18:57:57 +02:00
|
|
|
return unique_buffer<mutable_buffer>
|
|
|
|
{
|
|
|
|
std::move(request.in.dynamic)
|
|
|
|
};
|
2018-04-23 09:47:03 +02:00
|
|
|
}
|