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&
14GetMethod(clients::
http::Request& req, std::string_view url, std::string_view body, clients::
http::
HttpMethod method) {
18 case clients::http::HttpMethod::kGet:
19 return req.get(std::string{url});
20 case clients::http::HttpMethod::kHead:
21 return req.head(std::string{url});
22 case clients::http::HttpMethod::kPost:
23 return req.post(std::string{url}, std::string{body});
24 case clients::http::HttpMethod::kPut:
25 return req.put(std::string{url}, std::string{body});
26 case clients::http::HttpMethod::kDelete:
27 return req.delete_method(std::string{url});
29 throw std::runtime_error(
"Unknown http method");
35 if (!r.bucket.empty()) {
36 r.headers[USERVER_NAMESPACE::http::
headers::kHost] = r.bucket +
"." + api_url_;
38 r.headers[USERVER_NAMESPACE::http::
headers::kHost] = api_url_;
40 LOG_DEBUG() <<
"S3 Host: " << r.headers[USERVER_NAMESPACE::http::
headers::kHost];
42 const std::string full_url = GetUrl(r, connection_type_);
43 LOG_DEBUG() <<
"S3 file full_url: " << full_url;
46 http_client_.CreateNotSignedRequest().timeout(config_.timeout).retry(config_.retries).headers(r.headers);
48 if (config_.proxy.has_value()) {
49 http_req.proxy(config_.proxy.value());
51 http_req.SetDestinationMetricName(
52 fmt::format(
"{}/{}", r.headers[USERVER_NAMESPACE::http::headers::kHost], method_name)
54 std::shared_ptr<clients::
http::Response> response;
56 response = GetMethod(http_req, full_url, r.body, r.method).perform();
57 response->raise_for_status();
59 LOG_WARNING() <<
"S3Api : Http Request Timeout: " << full_url;
62 LOG_INFO() <<
"S3Api : Http Request to mds failed " << response->body() <<
" : " << full_url;
69 auto headers = r.headers;
70 if (!r.bucket.empty())
71 headers[USERVER_NAMESPACE::http::headers::kHost] = r.bucket +
"." + api_url_;
73 headers[USERVER_NAMESPACE::http::headers::kHost] = api_url_;
75 const std::string full_url = GetUrl(r, connection_type_);
78 http_client_.CreateNotSignedRequest().headers(headers).retry(config_.retries).timeout(config_.timeout);
79 return GetMethod(http_req, full_url, r.body, r.method).perform();
83 return DoStartApiRequest(request);
87 std::string full_url = api_url_;
88 bool is_localhost = api_url_.find(
"localhost:") != std::string::npos;
89 const auto schema_pos = full_url.find(
"://");
90 if (schema_pos == std::string::npos) {
91 if (!is_localhost && !r.bucket.empty()) {
92 full_url = fmt::format(
"{}.{}", r.bucket, api_url_);
94 if (connection_type == S3ConnectionType::kHttps) {
95 full_url =
"https://" + full_url;
97 full_url =
"http://" + full_url;
100 if (!is_localhost && !r.bucket.empty()) {
101 const auto schema = full_url.substr(0, schema_pos);
102 const auto schemaless_url = full_url.substr(schema_pos + 3);
103 full_url = fmt::format(
"{}://{}.{}", schema, r.bucket, schemaless_url);
106 if (!r.req.empty()) {
114 clients::
http::Client& http_client,
115 S3ConnectionType connection_type,
116 std::string server_url,
119 return std::make_shared<
S3Connection>(http_client, connection_type, std::move(server_url), params);