userver: /data/code/userver/libraries/s3api/src/s3api/s3_connection.cpp Source File
Loading...
Searching...
No Matches
s3_connection.cpp
1#include <s3api/s3_connection.hpp>
2
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>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace s3api {
11
12namespace {
13clients::http::Request& GetMethod(
14 clients::http::Request& req,
15 std::string_view url,
16 std::string_view body,
17 clients::http::HttpMethod method
18) {
19 // TODO: Get rid of extra string_view->string conversion once
20 // http::Request can work with string_view directly
21 switch (method) {
22 case clients::http::HttpMethod::kGet:
23 return req.get(std::string{url});
24 case clients::http::HttpMethod::kHead:
25 return req.head(std::string{url});
26 case clients::http::HttpMethod::kPost:
27 return req.post(std::string{url}, std::string{body});
28 case clients::http::HttpMethod::kPut:
29 return req.put(std::string{url}, std::string{body});
30 case clients::http::HttpMethod::kDelete:
31 return req.delete_method(std::string{url});
32 default:
33 throw std::runtime_error("Unknown http method");
34 }
35}
36} // namespace
37
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_;
41 } else {
42 r.headers[USERVER_NAMESPACE::http::headers::kHost] = api_url_;
43 }
44 LOG_DEBUG() << "S3 Host: " << r.headers[USERVER_NAMESPACE::http::headers::kHost];
45
46 const std::string full_url = GetUrl(r, connection_type_);
47 LOG_DEBUG() << "S3 file full_url: " << full_url;
48
49 auto http_req =
50 http_client_.CreateNotSignedRequest().timeout(config_.timeout).retry(config_.retries).headers(r.headers);
51
52 if (config_.proxy.has_value()) {
53 http_req.proxy(config_.proxy.value());
54 }
55 http_req
56 .SetDestinationMetricName(fmt::format("{}/{}", r.headers[USERVER_NAMESPACE::http::headers::kHost], method_name)
57 );
58 std::shared_ptr<clients::http::Response> response;
59 try {
60 response = GetMethod(http_req, full_url, r.body, r.method).perform();
61 response->raise_for_status();
62 } catch (const clients::http::TimeoutException& e) {
63 LOG_WARNING() << "S3Api : Http Request Timeout: " << full_url;
64 throw;
65 } catch (const clients::http::HttpException& exc) {
66 LOG_INFO() << "S3Api : Http Request to mds failed " << response->body() << " : " << full_url;
67 throw;
68 }
69 return response;
70}
71
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_;
76 } else {
77 headers[USERVER_NAMESPACE::http::headers::kHost] = api_url_;
78 }
79
80 const std::string full_url = GetUrl(r, connection_type_);
81
82 auto http_req =
83 http_client_.CreateNotSignedRequest().headers(headers).retry(config_.retries).timeout(config_.timeout);
84 return GetMethod(http_req, full_url, r.body, r.method).perform();
85}
86
87std::shared_ptr<clients::http::Response> S3Connection::StartApiRequest(const Request& request) const {
88 return DoStartApiRequest(request);
89}
90
91std::string S3Connection::GetUrl(const Request& r, S3ConnectionType connection_type) const {
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_);
98 }
99 if (connection_type == S3ConnectionType::kHttps) {
100 full_url = "https://" + full_url;
101 } else {
102 full_url = "http://" + full_url;
103 }
104 } else {
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);
109 }
110 }
111 if (!r.req.empty()) {
112 full_url += '/';
113 full_url += r.req;
114 }
115 return full_url;
116}
117
119 clients::http::Client& http_client,
120 S3ConnectionType connection_type,
121 std::string server_url,
122 const ConnectionCfg& params
123) {
124 return std::make_shared<S3Connection>(http_client, connection_type, std::move(server_url), params);
125}
126
127} // namespace s3api
128
129USERVER_NAMESPACE_END