Skip to content

Commit 61f39df

Browse files
committed
fix: fix bug with customheadersoption, added and integrated CustomHeaders class
1 parent 4d243d4 commit 61f39df

File tree

7 files changed

+54
-2
lines changed

7 files changed

+54
-2
lines changed

google/cloud/storage/internal/generic_request.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,12 @@ class GenericRequestBase<Derived, OverrideDefaultProject, Options...>
317317
*/
318318
template <typename Derived, typename... Options>
319319
class GenericRequest
320-
: public GenericRequestBase<Derived, CustomHeader, Fields, IfMatchEtag,
320+
: public GenericRequestBase<Derived, CustomHeader, CustomHeaders, Fields, IfMatchEtag,
321321
IfNoneMatchEtag, QuotaUser, UserIp,
322322
Options...> {
323323
public:
324324
using Super =
325-
GenericRequestBase<Derived, CustomHeader, Fields, IfMatchEtag,
325+
GenericRequestBase<Derived, CustomHeader, CustomHeaders, Fields, IfMatchEtag,
326326
IfNoneMatchEtag, QuotaUser, UserIp, Options...>;
327327

328328
template <typename H, typename... T>

google/cloud/storage/internal/rest/request_builder.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ RestRequestBuilder& RestRequestBuilder::AddOption(CustomHeader const& p) {
3232
return *this;
3333
}
3434

35+
RestRequestBuilder& RestRequestBuilder::AddOption(CustomHeaders const& p) {
36+
if (!p.headers().empty()) {
37+
for (const auto& header : p.headers()) {
38+
request_.AddHeader(header.first, header.second);
39+
}
40+
}
41+
return *this;
42+
}
43+
3544
RestRequestBuilder& RestRequestBuilder::AddOption(EncryptionKey const& p) {
3645
if (p.has_value()) {
3746
request_.AddHeader(std::string(EncryptionKey::prefix()) + "algorithm",

google/cloud/storage/internal/rest/request_builder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class RestRequestBuilder {
9595
/// Adds a custom header to the request.
9696
RestRequestBuilder& AddOption(CustomHeader const& p);
9797

98+
/// Adds custom headers to the request.
99+
RestRequestBuilder& AddOption(CustomHeaders const& p);
100+
98101
/// Adds one of the well-known encryption header groups to the request.
99102
RestRequestBuilder& AddOption(EncryptionKey const& p);
100103

google/cloud/storage/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
set(storage_client_integration_tests
1818
# cmake-format : sort
19+
elie_test.cc
1920
async_client_integration_test.cc
2021
auto_finalize_integration_test.cc
2122
bucket_integration_test.cc

google/cloud/storage/tests/storage_client_integration_tests.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ storage_client_integration_tests = [
2626
"curl_sign_blob_integration_test.cc",
2727
"decompressive_transcoding_integration_test.cc",
2828
"default_object_acl_integration_test.cc",
29+
"elie_test.cc",
2930
"error_injection_integration_test.cc",
3031
"error_parsing_integration_test.cc",
3132
"grpc_bucket_metadata_integration_test.cc",

google/cloud/storage/well_known_headers.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ std::ostream& operator<<(std::ostream& os, CustomHeader const& rhs) {
3131
return os << rhs.custom_header_name() << ": " << rhs.value();
3232
}
3333

34+
std::ostream& operator<<(std::ostream& os, const CustomHeaders& rhs) {
35+
if (rhs.headers().empty()) {
36+
return os;
37+
}
38+
for (const auto& header : rhs.headers()) {
39+
os << header.first << ": " << header.second << "\n";
40+
}
41+
return os;
42+
}
43+
3444
EncryptionKeyData EncryptionDataFromBinaryKey(std::string const& key) {
3545
return EncryptionKeyData{
3646
"AES256", internal::Base64Encode(key),

google/cloud/storage/well_known_headers.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ class CustomHeader
9898

9999
std::ostream& operator<<(std::ostream& os, CustomHeader const& rhs);
100100

101+
/**
102+
* An option to inject multiplecustom headers at once into the request.
103+
*
104+
* In some cases it is necessary to inject custom headers into the request. For
105+
* example, because the protocol has added new headers and the library has not
106+
* been updated to support them, or because
107+
*/
108+
class CustomHeaders
109+
: public internal::WellKnownHeader<CustomHeaders, std::string> {
110+
public:
111+
using Type = std::unordered_multimap<std::string, std::string>;
112+
113+
CustomHeaders() = default;
114+
CustomHeaders(std::initializer_list<Type::value_type> headers)
115+
: headers_(std::move(headers)) {}
116+
117+
void add(const std::string& name, const std::string& value) {
118+
headers_.insert({name, value});
119+
}
120+
121+
const Type& headers() const { return headers_; }
122+
123+
private:
124+
Type headers_;
125+
};
126+
127+
std::ostream& operator<<(std::ostream& os, const CustomHeaders& rhs);
128+
101129
/**
102130
* A pre-condition: apply this operation only if the HTTP Entity Tag matches.
103131
*

0 commit comments

Comments
 (0)