1#include <s3api/s3_connection.hpp>
3#include <userver/clients/http/client.hpp>
4#include <userver/http/common_headers.hpp>
5#include <userver/logging/log.hpp>
6#include <userver/s3api/models/request.hpp>
13clients::http::Request& GetMethod(
14 clients::http::Request& req,
16 std::string_view body,
23 return req
.get(std::string{url}
);
25 return req
.head(std::string{url}
);
27 return req
.post(std::string{url}
, std::string{body}
);
29 return req
.put(std::string{url}
, std::string{body}
);
33 throw std::runtime_error(
"Unknown http method");
38std::shared_ptr<clients::http::Response>
S3Connection::RequestApi(
Request& r, std::string_view method_name) {
39 if (!r.bucket.empty()) {
40 r.headers
[USERVER_NAMESPACE::
http::
headers::kHost
] = r.bucket +
"." + api_url_;
42 r.headers
[USERVER_NAMESPACE::
http::
headers::kHost
] = api_url_;
46 const std::string full_url = GetUrl(r, connection_type_);
47 LOG_DEBUG() <<
"S3 file full_url: " << full_url;
52 if (config_.proxy.has_value()) {
53 http_req
.proxy(config_.proxy.value()
);
58 std::shared_ptr<clients::http::Response> response;
60 response = GetMethod(http_req, full_url, r.body, r.method)
.perform();
61 response->raise_for_status();
63 LOG_WARNING() <<
"S3Api : Http Request Timeout: " << full_url;
66 LOG_INFO() <<
"S3Api : Http Request to mds failed " << response->
body() <<
" : " << full_url;
72std::shared_ptr<clients::http::Response>
S3Connection::DoStartApiRequest(
const Request& r)
const {
73 auto headers = r.headers;
74 if (!r.bucket.empty()) {
75 headers
[USERVER_NAMESPACE::
http::
headers::kHost
] = r.bucket +
"." + api_url_;
80 const std::string full_url = GetUrl(r, connection_type_);
84 return GetMethod(http_req, full_url, r.body, r.method)
.perform();
87std::shared_ptr<clients::http::Response>
S3Connection::StartApiRequest(
const Request& request)
const {
88 return DoStartApiRequest(request);
92 std::string full_url = api_url_;
93 const bool is_localhost = api_url_.find(
"localhost:") != std::string::npos;
94 const auto schema_pos = full_url.find(
"://");
95 if (schema_pos == std::string::npos) {
96 if (!is_localhost && !r.bucket.empty()) {
97 full_url = fmt::format(
"{}.{}", r.bucket, api_url_);
99 if (connection_type == S3ConnectionType::kHttps) {
100 full_url =
"https://" + full_url;
102 full_url =
"http://" + full_url;
105 if (!is_localhost && !r.bucket.empty()) {
106 const auto schema = full_url.substr(0, schema_pos);
107 const auto schemaless_url = full_url.substr(schema_pos + 3);
108 full_url = fmt::format(
"{}://{}.{}", schema, r.bucket, schemaless_url);
111 if (!r.req.empty()) {
119 clients::http::
Client& http_client,
120 S3ConnectionType connection_type,
121 std::string server_url,
124 return std::make_shared<
S3Connection>(http_client, connection_type, std::move(server_url), params);