Google Cloud Storage C++ Client  1.24.0
A C++ Client Library for Google Cloud Storage
well_known_headers.h
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_WELL_KNOWN_HEADERS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_WELL_KNOWN_HEADERS_H
17 
19 #include "google/cloud/internal/random.h"
20 #include "google/cloud/optional.h"
21 #include "absl/types/optional.h"
22 #include <algorithm>
23 #include <cstdint>
24 #include <iostream>
25 #include <limits>
26 #include <random>
27 #include <string>
28 
29 namespace google {
30 namespace cloud {
31 namespace storage {
32 inline namespace STORAGE_CLIENT_NS {
33 namespace internal {
40 template <typename H, typename T>
41 class WellKnownHeader {
42  public:
43  WellKnownHeader() = default;
44  explicit WellKnownHeader(T value) : value_(std::move(value)) {}
45 
46  char const* header_name() const { return H::header_name(); }
47  bool has_value() const { return value_.has_value(); }
48  T const& value() const { return value_.value(); }
49  template <typename U>
50  T value_or(U&& default_val) {
51  return value_.value_or(std::forward<U>(default_val));
52  }
53 
54  private:
55  absl::optional<T> value_;
56 };
57 
58 template <typename H, typename T>
59 std::ostream& operator<<(std::ostream& os, WellKnownHeader<H, T> const& rhs) {
60  if (rhs.has_value()) {
61  return os << rhs.header_name() << ": " << rhs.value();
62  }
63  return os << rhs.header_name() << ": <not set>";
64 }
65 } // namespace internal
66 
74  : public internal::WellKnownHeader<ContentType, std::string> {
75  using WellKnownHeader<ContentType, std::string>::WellKnownHeader;
76  static char const* header_name() { return "content-type"; }
77 };
78 
87  : public internal::WellKnownHeader<CustomHeader, std::string> {
88  public:
89  CustomHeader() = default;
90  explicit CustomHeader(std::string name, std::string value)
91  : WellKnownHeader(std::move(value)), name_(std::move(name)) {}
92 
93  std::string const& custom_header_name() const { return name_; }
94 
95  private:
96  std::string name_;
97 };
98 
99 std::ostream& operator<<(std::ostream& os, CustomHeader const& rhs);
100 
110  : public internal::WellKnownHeader<IfMatchEtag, std::string> {
111  using WellKnownHeader<IfMatchEtag, std::string>::WellKnownHeader;
112  static char const* header_name() { return "If-Match"; }
113 };
114 
125  : public internal::WellKnownHeader<IfNoneMatchEtag, std::string> {
126  using WellKnownHeader<IfNoneMatchEtag, std::string>::WellKnownHeader;
127  static char const* header_name() { return "If-None-Match"; }
128 };
129 
139  std::string algorithm;
140  std::string key;
141  std::string sha256;
142 };
143 
150 EncryptionKeyData EncryptionDataFromBinaryKey(std::string const& key);
151 
158 EncryptionKeyData EncryptionDataFromBase64Key(std::string const& key);
159 
183  : public internal::WellKnownHeader<EncryptionKey, EncryptionKeyData> {
184  using WellKnownHeader<EncryptionKey, EncryptionKeyData>::WellKnownHeader;
185 
191  static EncryptionKey FromBinaryKey(std::string const& key);
192 
198  static EncryptionKey FromBase64Key(std::string const& key);
199 
200  static char const* prefix() { return "x-goog-encryption-"; }
201 };
202 
203 std::ostream& operator<<(std::ostream& os, EncryptionKey const& rhs);
204 
228  : public internal::WellKnownHeader<SourceEncryptionKey, EncryptionKeyData> {
229  using WellKnownHeader<SourceEncryptionKey,
230  EncryptionKeyData>::WellKnownHeader;
231 
237  static SourceEncryptionKey FromBinaryKey(std::string const& key);
238 
244  static SourceEncryptionKey FromBase64Key(std::string const& key);
245 
246  static char const* prefix() { return "x-goog-copy-source-encryption-"; }
247 };
248 
249 std::ostream& operator<<(std::ostream& os, SourceEncryptionKey const& rhs);
250 
268 template <typename Generator>
270  constexpr int kKeySize = 256 / std::numeric_limits<unsigned char>::digits;
271 
272  // NOLINTNEXTLINE(readability-identifier-naming)
273  constexpr auto minchar = (std::numeric_limits<char>::min)();
274  // NOLINTNEXTLINE(readability-identifier-naming)
275  constexpr auto maxchar = (std::numeric_limits<char>::max)();
276  std::uniform_int_distribution<int> uni(minchar, maxchar);
277  std::string key(static_cast<std::size_t>(kKeySize), ' ');
278  std::generate_n(key.begin(), key.size(),
279  [&uni, &gen] { return static_cast<char>(uni(gen)); });
280  return EncryptionDataFromBinaryKey(key);
281 }
282 
283 } // namespace STORAGE_CLIENT_NS
284 } // namespace storage
285 } // namespace cloud
286 } // namespace google
287 
288 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_WELL_KNOWN_HEADERS_H
google::cloud::storage::v1::operator<<
std::ostream & operator<<(std::ostream &os, BucketAccessControl const &rhs)
Definition: bucket_access_control.cc:27
optional.h
google::cloud::storage::v1::EncryptionKey
An optional parameter to set the Customer-Supplied Encryption key.
Definition: well_known_headers.h:183
google::cloud::storage::v1::IfMatchEtag::header_name
static char const * header_name()
Definition: well_known_headers.h:112
version.h
google::cloud::storage::v1::EncryptionKeyData
A simple wrapper for the encryption key attributes.
Definition: well_known_headers.h:138
google::cloud::storage::v1::EncryptionDataFromBinaryKey
EncryptionKeyData EncryptionDataFromBinaryKey(std::string const &key)
Formats a (potentially binary) encryption key in the format required by the Google Cloud Storage API.
Definition: well_known_headers.cc:33
google::cloud::storage::v1::EncryptionKey::prefix
static char const * prefix()
Definition: well_known_headers.h:200
google::cloud::storage::v1::EncryptionKeyData::algorithm
std::string algorithm
Definition: well_known_headers.h:139
google::cloud::storage::v1::IfNoneMatchEtag::header_name
static char const * header_name()
Definition: well_known_headers.h:127
google::cloud::storage::v1::SourceEncryptionKey
An optional parameter to set the Customer-Supplied Encryption key for rewrite source object.
Definition: well_known_headers.h:228
google::cloud::storage::v1::ContentType::header_name
static char const * header_name()
Definition: well_known_headers.h:76
google::cloud::storage::v1::IfNoneMatchEtag
A pre-condition: apply this operation only if the HTTP Entity Tag does not match.
Definition: well_known_headers.h:125
STORAGE_CLIENT_NS
#define STORAGE_CLIENT_NS
Definition: version.h:22
google::cloud::storage::v1::EncryptionKeyData::key
std::string key
Definition: well_known_headers.h:140
google::cloud::storage::v1::SourceEncryptionKey::prefix
static char const * prefix()
Definition: well_known_headers.h:246
google::cloud::storage::v1::CustomHeader::custom_header_name
std::string const & custom_header_name() const
Definition: well_known_headers.h:93
google::cloud::storage::v1::EncryptionKeyData::sha256
std::string sha256
Definition: well_known_headers.h:141
google::cloud::storage::v1::IfMatchEtag
A pre-condition: apply this operation only if the HTTP Entity Tag matches.
Definition: well_known_headers.h:110
google::cloud::storage::v1::EncryptionDataFromBase64Key
EncryptionKeyData EncryptionDataFromBase64Key(std::string const &key)
Formats an encryption key in base64 format to the data structure required by the Google Cloud Storage...
Definition: well_known_headers.cc:38
google::cloud::storage::v1::CustomHeader::CustomHeader
CustomHeader(std::string name, std::string value)
Definition: well_known_headers.h:90
google::cloud::storage::v1::ContentType
Set the MIME content type of an object.
Definition: well_known_headers.h:74
google::cloud::storage::v1::CreateKeyFromGenerator
EncryptionKeyData CreateKeyFromGenerator(Generator &gen)
Creates an encryption key parameter from a pseudo-random number generator.
Definition: well_known_headers.h:269
google::cloud::storage::v1::CustomHeader
An option to inject custom headers into the request.
Definition: well_known_headers.h:87
google::cloud::storage::v1::CustomHeader::CustomHeader
CustomHeader()=default
google