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&
14GetMethod(clients::http::Request& req, std::string_view url, std::string_view body, clients::http::HttpMethod method) {
15 // TODO: Get rid of extra string_view->string conversion once
16 // http::Request can work with string_view directly
17 switch (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});
28 default:
29 throw std::runtime_error("Unknown http method");
30 }
31}
32} // namespace
33
34std::shared_ptr<clients::http::Response> S3Connection::RequestApi(Request& r, std::string_view method_name) {
35 if (!r.bucket.empty()) {
36 r.headers[USERVER_NAMESPACE::http::headers::kHost] = r.bucket + "." + api_url_;
37 } else {
38 r.headers[USERVER_NAMESPACE::http::headers::kHost] = api_url_;
39 }
40 LOG_DEBUG() << "S3 Host: " << r.headers[USERVER_NAMESPACE::http::headers::kHost];
41
42 const std::string full_url = GetUrl(r, connection_type_);
43 LOG_DEBUG() << "S3 file full_url: " << full_url;
44
45 auto http_req =
46 http_client_.CreateNotSignedRequest().timeout(config_.timeout).retry(config_.retries).headers(r.headers);
47
48 if (config_.proxy.has_value()) {
49 http_req.proxy(config_.proxy.value());
50 }
51 http_req.SetDestinationMetricName(
52 fmt::format("{}/{}", r.headers[USERVER_NAMESPACE::http::headers::kHost], method_name)
53 );
54 std::shared_ptr<clients::http::Response> response;
55 try {
56 response = GetMethod(http_req, full_url, r.body, r.method).perform();
57 response->raise_for_status();
58 } catch (const clients::http::TimeoutException& e) {
59 LOG_WARNING() << "S3Api : Http Request Timeout: " << full_url;
60 throw;
61 } catch (const clients::http::HttpException& exc) {
62 LOG_INFO() << "S3Api : Http Request to mds failed " << response->body() << " : " << full_url;
63 throw;
64 }
65 return response;
66}
67
68std::shared_ptr<clients::http::Response> S3Connection::DoStartApiRequest(const Request& r) const {
69 auto headers = r.headers;
70 if (!r.bucket.empty())
71 headers[USERVER_NAMESPACE::http::headers::kHost] = r.bucket + "." + api_url_;
72 else
73 headers[USERVER_NAMESPACE::http::headers::kHost] = api_url_;
74
75 const std::string full_url = GetUrl(r, connection_type_);
76
77 auto http_req =
78 http_client_.CreateNotSignedRequest().headers(headers).retry(config_.retries).timeout(config_.timeout);
79 return GetMethod(http_req, full_url, r.body, r.method).perform();
80}
81
82std::shared_ptr<clients::http::Response> S3Connection::StartApiRequest(const Request& request) const {
83 return DoStartApiRequest(request);
84}
85
86std::string S3Connection::GetUrl(const Request& r, S3ConnectionType connection_type) const {
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_);
93 }
94 if (connection_type == S3ConnectionType::kHttps) {
95 full_url = "https://" + full_url;
96 } else {
97 full_url = "http://" + full_url;
98 }
99 } else {
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);
104 }
105 }
106 if (!r.req.empty()) {
107 full_url += '/';
108 full_url += r.req;
109 }
110 return full_url;
111}
112
113std::shared_ptr<S3Connection> MakeS3Connection(
114 clients::http::Client& http_client,
115 S3ConnectionType connection_type,
116 std::string server_url,
117 const ConnectionCfg& params
118) {
119 return std::make_shared<S3Connection>(http_client, connection_type, std::move(server_url), params);
120}
121
122} // namespace s3api
123
124USERVER_NAMESPACE_END