MinIO C++ SDK
utils.h
1 // MinIO C++ Library for Amazon S3 Compatible Cloud Storage
2 // Copyright 2022 MinIO, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef _MINIO_UTILS_H
17 #define _MINIO_UTILS_H
18 
19 #include <openssl/buffer.h>
20 #include <openssl/evp.h>
21 
22 #include <cmath>
23 #include <cstring>
24 #include <curlpp/cURLpp.hpp>
25 #include <list>
26 #include <regex>
27 #include <set>
28 
29 #include "error.h"
30 
31 namespace minio {
32 namespace utils {
33 inline constexpr unsigned int kMaxMultipartCount = 10000; // 10000 parts
34 inline constexpr unsigned long kMaxObjectSize =
35  5L * 1024 * 1024 * 1024 * 1024; // 5TiB
36 inline constexpr unsigned long kMaxPartSize = 5L * 1024 * 1024 * 1024; // 5GiB
37 inline constexpr unsigned int kMinPartSize = 5 * 1024 * 1024; // 5MiB
38 
39 // FormatTime formats time as per format.
40 std::string FormatTime(const std::tm* time, const char* format);
41 
42 // StringToBool converts string to bool.
43 bool StringToBool(std::string_view str);
44 
45 // BoolToString converts bool to string.
46 inline const char* const BoolToString(bool b) { return b ? "true" : "false"; }
47 
48 // Trim trims leading and trailing character of a string.
49 std::string Trim(std::string_view str, char ch = ' ');
50 
51 // CheckNonemptystring checks whether string is not empty after trimming
52 // whitespaces.
53 bool CheckNonEmptyString(std::string_view str);
54 
55 // ToLower converts string to lower case.
56 std::string ToLower(std::string str);
57 
58 // StartsWith returns whether str starts with prefix or not.
59 bool StartsWith(std::string_view str, std::string_view prefix);
60 
61 // EndsWith returns whether str ends with suffix or not.
62 bool EndsWith(std::string_view str, std::string_view suffix);
63 
64 // Contains returns whether str has ch.
65 bool Contains(std::string_view str, char ch);
66 
67 // Contains returns whether str has substr.
68 bool Contains(std::string_view str, std::string_view substr);
69 
70 // Join returns a string of joined values by delimiter.
71 std::string Join(std::list<std::string> values, std::string delimiter);
72 
73 // Join returns a string of joined values by delimiter.
74 std::string Join(std::vector<std::string> values, std::string delimiter);
75 
76 // EncodePath does URL encoding of path. It also normalizes multiple slashes.
77 std::string EncodePath(std::string_view path);
78 
79 // Sha256hash computes SHA-256 of data and return hash as hex encoded value.
80 std::string Sha256Hash(std::string_view str);
81 
82 // Base64Encode encodes string to base64.
83 std::string Base64Encode(std::string_view str);
84 
85 // Md5sumHash computes MD5 of data and return hash as Base64 encoded value.
86 std::string Md5sumHash(std::string_view str);
87 
88 error::Error CheckBucketName(std::string_view bucket_name, bool strict = false);
89 error::Error ReadPart(std::istream& stream, char* buf, size_t size,
90  size_t& bytes_read);
91 error::Error CalcPartInfo(long object_size, size_t& part_size,
92  long& part_count);
93 
97 class Time {
98  private:
99  struct timeval tv_;
100  bool utc_;
101 
102  public:
103  Time();
104  Time(std::time_t tv_sec, suseconds_t tv_usec, bool utc);
105  std::tm* ToUTC();
106  std::string ToSignerDate();
107  std::string ToAmzDate();
108  std::string ToHttpHeaderValue();
109  static Time FromHttpHeaderValue(const char* value);
110  std::string ToISO8601UTC();
111  static Time FromISO8601UTC(const char* value);
112  static Time Now();
113  operator bool() const { return tv_.tv_sec != 0 && tv_.tv_usec != 0; }
114 }; // class Time
115 
119 class Multimap {
120  private:
121  std::map<std::string, std::set<std::string>> map_;
122  std::map<std::string, std::set<std::string>> keys_;
123 
124  public:
125  Multimap();
126  Multimap(const Multimap& headers);
127  void Add(std::string key, std::string value);
128  void AddAll(const Multimap& headers);
129  std::list<std::string> ToHttpHeaders();
130  std::string ToQueryString();
131  operator bool() const { return !map_.empty(); }
132  bool Contains(std::string_view key);
133  std::list<std::string> Get(std::string_view key);
134  std::string GetFront(std::string_view key);
135  std::list<std::string> Keys();
136  void GetCanonicalHeaders(std::string& signed_headers,
137  std::string& canonical_headers);
138  std::string GetCanonicalQueryString();
139 }; // class Multimap
140 
144 struct Url {
145  bool is_https;
146  std::string host;
147  std::string path;
148  std::string query_string;
149 
150  operator bool() const { return !host.empty(); }
151  std::string String();
152 }; // struct Url
153 
157 struct CharBuffer : std::streambuf {
158  CharBuffer(char* buf, size_t size) { this->setg(buf, buf, buf + size); }
159 
160  pos_type seekoff(off_type off, std::ios_base::seekdir dir,
161  std::ios_base::openmode which = std::ios_base::in) override {
162  if (dir == std::ios_base::cur)
163  gbump(off);
164  else if (dir == std::ios_base::end)
165  setg(eback(), egptr() + off, egptr());
166  else if (dir == std::ios_base::beg)
167  setg(eback(), eback() + off, egptr());
168  return gptr() - eback();
169  }
170 
171  pos_type seekpos(pos_type sp, std::ios_base::openmode which) override {
172  return seekoff(sp - pos_type(off_type(0)), std::ios_base::beg, which);
173  }
174 }; // struct CharBuffer
175 } // namespace utils
176 } // namespace minio
177 
178 #endif // #ifndef _MINIO_UTILS_H
Definition: utils.h:119
Definition: utils.h:97
Definition: utils.h:157
Definition: utils.h:144