AWS SDK for C++

AWS SDK for C++ Version 1.11.925

Loading...
Searching...
No Matches
AwsSmithyClientUtils.h
1
6#pragma once
7
8#include <iomanip>
9#include <aws/core/http/HttpRequest.h>
10
11#include <cassert>
12
13#include <aws/core/http/HttpClient.h>
14#include <aws/core/utils/HashingUtils.h>
15#include <aws/core/utils/crypto/MD5.h>
16#include <aws/core/utils/logging/LogMacros.h>
17
18
19namespace smithy
20{
21 namespace client
22 {
23 static const char AWS_SMITHY_CLIENT_UTILS_TAG[] = "AwsSmithyClientUtils";
24
25 class Utils
26 {
27 public:
33
34 static void AppendHeaderValueToRequest(const std::shared_ptr<Aws::Http::HttpRequest>& httpRequest,
35 const Aws::String& header,
36 const Aws::String& value)
37 {
38 assert(httpRequest);
39 assert(!header.empty());
40
41 if (!httpRequest->HasHeader(header.c_str()))
42 {
43 httpRequest->SetHeaderValue(header, value);
44 }
45 else
46 {
47 Aws::String contentEncoding = httpRequest->GetHeaderValue(header.c_str());
48 contentEncoding.append(",").append(value);
49 httpRequest->SetHeaderValue(header, contentEncoding);
50 }
51 }
52
53 static void AddContentBodyToRequest(const std::shared_ptr<Aws::Http::HttpRequest>& httpRequest,
54 const std::shared_ptr<Aws::IOStream>& body,
55 const std::shared_ptr<Aws::Http::HttpClient>& httpClient,
56 bool needsContentMd5,
57 bool isChunked)
58 {
59 assert(httpRequest);
60
61 httpRequest->AddContentBody(body);
62
63 //If there is no body, we have a content length of 0
64 //note: we also used to remove content-type, but S3 actually needs content-type on InitiateMultipartUpload and it isn't
65 //forbidden by the spec. If we start getting weird errors related to this, make sure it isn't caused by this removal.
66 if (!body)
67 {
68 AWS_LOGSTREAM_TRACE(AWS_SMITHY_CLIENT_UTILS_TAG, "No content body, content-length headers");
69
70 if (httpRequest->GetMethod() == HttpMethod::HTTP_POST || httpRequest->GetMethod() ==
71 HttpMethod::HTTP_PUT)
72 {
73 httpRequest->SetHeaderValue(Aws::Http::CONTENT_LENGTH_HEADER, "0");
74 }
75 else
76 {
77 httpRequest->DeleteHeader(Aws::Http::CONTENT_LENGTH_HEADER);
78 }
79 }
80
81 //Add transfer-encoding:chunked to header
82 if (body && isChunked && !httpRequest->HasHeader(Aws::Http::CONTENT_LENGTH_HEADER))
83 {
84 httpRequest->SetTransferEncoding(Aws::Http::CHUNKED_VALUE);
85 }
86 //in the scenario where we are adding a content body as a stream, the request object likely already
87 //has a content-length header set and we don't want to seek the stream just to find this information.
88 else if (body && !httpRequest->HasHeader(Aws::Http::CONTENT_LENGTH_HEADER))
89 {
90 assert(httpClient);
91 if (!httpClient->SupportsChunkedTransferEncoding())
92 {
93 AWS_LOGSTREAM_WARN(AWS_SMITHY_CLIENT_UTILS_TAG,
94 "This http client doesn't support transfer-encoding:chunked. " <<
95 "The request may fail if it's not a seekable stream.");
96 }
97 AWS_LOGSTREAM_TRACE(AWS_SMITHY_CLIENT_UTILS_TAG,
98 "Found body, but content-length has not been set, attempting to compute content-length");
99 body->seekg(0, body->end);
100 auto streamSize = body->tellg();
101 body->seekg(0, body->beg);
103 ss << streamSize;
104 httpRequest->SetContentLength(ss.str());
105 }
106
107 if (needsContentMd5 && body && !httpRequest->HasHeader(Aws::Http::CONTENT_MD5_HEADER))
108 {
109 AWS_LOGSTREAM_TRACE(AWS_SMITHY_CLIENT_UTILS_TAG, "Found body, and content-md5 needs to be set" <<
110 ", attempting to compute content-md5");
111
112 //changing the internal state of the hash computation is not a logical state
113 //change as far as constness goes for this class. Due to the platform specificness
114 //of hash computations, we can't control the fact that computing a hash mutates
115 //state on some platforms such as windows (but that isn't a concern of this class.
116
117 // TODO: check refactoring from:
118 // auto md5HashResult = const_cast<AWSClient*>(this)->m_hash->Calculate(*body);
120 auto md5HashResult = hash.Calculate(*body);
121 body->clear();
122 if (md5HashResult.IsSuccess())
123 {
124 httpRequest->SetHeaderValue(Aws::Http::CONTENT_MD5_HEADER,
125 Aws::Utils::HashingUtils::Base64Encode(md5HashResult.GetResult()));
126 }
127 }
128 }
129
130 static bool DoesResponseGenerateError(const std::shared_ptr<Aws::Http::HttpResponse>& response)
131 {
132 assert(response);
133 if (response->HasClientError())
134 return true;
135
136 static const int SUCCESS_RESPONSE_MIN = 200;
137 static const int SUCCESS_RESPONSE_MAX = 299;
138
139 const int responseCode = static_cast<int>(response->GetResponseCode());
140 return responseCode < SUCCESS_RESPONSE_MIN || responseCode > SUCCESS_RESPONSE_MAX;
141 }
142
144 {
145 const HeaderValueCollection& headers = error.GetResponseHeaders();
146 auto awsDateHeaderIter = headers.find(Aws::Utils::StringUtils::ToLower(Aws::Http::AWS_DATE_HEADER));
147 auto dateHeaderIter = headers.find(Aws::Utils::StringUtils::ToLower(Aws::Http::DATE_HEADER));
148 if (awsDateHeaderIter != headers.end())
149 {
150 return DateTime(awsDateHeaderIter->second.c_str(), DateFormat::AutoDetect);
151 }
152 else if (dateHeaderIter != headers.end())
153 {
154 return DateTime(dateHeaderIter->second.c_str(), DateFormat::AutoDetect);
155 }
156 return DateTime();
157 }
158 };
159 }
160}
const Aws::Http::HeaderValueCollection & GetResponseHeaders() const
Definition AWSError.h:191
virtual HashResult Calculate(const Aws::String &str) override
static Aws::String Base64Encode(const ByteBuffer &byteBuffer)
static Aws::String ToLower(const char *source)
static Aws::Utils::DateTime GetServerTimeFromError(const ClientError &error)
static void AppendHeaderValueToRequest(const std::shared_ptr< Aws::Http::HttpRequest > &httpRequest, const Aws::String &header, const Aws::String &value)
static bool DoesResponseGenerateError(const std::shared_ptr< Aws::Http::HttpResponse > &response)
Aws::Utils::DateTime DateTime
static void AddContentBodyToRequest(const std::shared_ptr< Aws::Http::HttpRequest > &httpRequest, const std::shared_ptr< Aws::IOStream > &body, const std::shared_ptr< Aws::Http::HttpClient > &httpClient, bool needsContentMd5, bool isChunked)
Aws::Http::HeaderValueCollection HeaderValueCollection
AWS_CORE_API const char CHUNKED_VALUE[]
AWS_CORE_API const char CONTENT_LENGTH_HEADER[]
AWS_CORE_API const char DATE_HEADER[]
AWS_CORE_API const char CONTENT_MD5_HEADER[]
AWS_CORE_API const char AWS_DATE_HEADER[]
Aws::Map< Aws::String, Aws::String > HeaderValueCollection
Definition HttpTypes.h:56
std::basic_stringstream< char, std::char_traits< char >, Aws::Allocator< char > > StringStream
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition AWSString.h:97
static const char AWS_SMITHY_CLIENT_UTILS_TAG[]