MinIO C++ SDK
s3_headers.h
1 // MinIO C++ Library for Amazon S3 Compatible Cloud Storage
2 // Copyright 2021 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 #pragma once
17 
18 #include <iostream>
19 #include <string>
20 #include <map>
21 #include <cstdlib>
22 #include <exception>
23 #include <stdexcept>
24 
25 #include "s3_http.h"
26 
27 struct Dictionary_Error: public std::runtime_error {
28  Dictionary_Error(const std::string & msg = ""): std::runtime_error(msg) {}
29 };
30 
31 namespace Minio
32 {
33  // A simple string-to-string dictionary, with additional methods for conversion to doubles and integers.
34  class Headers {
35  Minio::Http::HeaderValueCollection entries;
36  public:
37  Headers() {}
38  typedef Minio::Http::HeaderValueCollection::iterator iterator;
39  typedef Minio::Http::HeaderValueCollection::const_iterator const_iterator;
40 
41  iterator begin() {return entries.begin();}
42  const_iterator begin() const {return entries.begin();}
43 
44  iterator end() {return entries.end();}
45  const_iterator end() const {return entries.end();}
46 
47  std::pair<iterator, iterator> equal_range(const std::string & key) {
48  return entries.equal_range(key);
49  }
50 
51  std::pair<const_iterator, const_iterator> equal_range(const std::string & key) const {
52  return entries.equal_range(key);
53  }
54 
55  void Clear() {entries.clear();}
56 
57  bool Exists(const std::string & key) const {return (entries.find(key) != entries.end());}
58 
59  // Get first value for key if one exists. Return true if a value found for key,
60  // return false otherwise.
61  bool Get(const std::string & key, std::string & value) const {
62  const_iterator val = entries.find(key);
63  if(val != entries.end()) value = val->second;
64  return (val != entries.end());
65  }
66 
67  bool Get(const std::string & key, double & value) const {
68  const_iterator val = entries.find(key);
69  if(val != entries.end()) value = strtod(val->second.c_str(), NULL);
70  return (val != entries.end());
71  }
72 
73  bool Get(const std::string & key, int & value) const {
74  const_iterator val = entries.find(key);
75  if(val != entries.end()) value = strtol(val->second.c_str(), NULL, 0);
76  return (val != entries.end());
77  }
78 
79  bool Get(const std::string & key, long & value) const {
80  const_iterator val = entries.find(key);
81  if(val != entries.end()) value = strtol(val->second.c_str(), NULL, 0);
82  return (val != entries.end());
83  }
84 
85  bool Get(const std::string & key, size_t & value) const {
86  const_iterator val = entries.find(key);
87  if(val != entries.end()) value = strtol(val->second.c_str(), NULL, 0);
88  return (val != entries.end());
89  }
90 
91  // Get first value for key if one exists. Return value if found for key,
92  // return defaultVal otherwise.
93  const std::string & GetWithDefault(const std::string & key, const std::string & defaultVal) const {
94  const_iterator val = entries.find(key);
95  return (val != entries.end())? val->second : defaultVal;
96  }
97 
98  double GetWithDefault(const std::string & key, double defaultVal) const {
99  const_iterator val = entries.find(key);
100  return (val != entries.end())? strtod(val->second.c_str(), NULL) : defaultVal;
101  }
102 
103  int GetWithDefault(const std::string & key, int defaultVal) const {
104  const_iterator val = entries.find(key);
105  return (val != entries.end())? strtol(val->second.c_str(), NULL, 0) : defaultVal;
106  }
107 
108  long GetWithDefault(const std::string & key, long defaultVal) const {
109  const_iterator val = entries.find(key);
110  return (val != entries.end())? strtol(val->second.c_str(), NULL, 0) : defaultVal;
111  }
112 
113  size_t GetWithDefault(const std::string & key, size_t defaultVal) const {
114  const_iterator val = entries.find(key);
115  return (val != entries.end())? strtol(val->second.c_str(), NULL, 0) : defaultVal;
116  }
117 
118  // Insert entry into dictionary, overwrites existing key values.
119  void Insert(const std::string & key, const std::string & value) {
120  entries.insert(std::make_pair(key, value));
121  }
122 
123  // Update value for existing key if possible, insert entry into dictionary if no value for key
124  void Update(const std::string & key, const std::string & value) {
125  iterator val = entries.find(key);
126  if(val == entries.end())
127  Insert(key, value);
128  else
129  val->second = value;
130  }
131  };
132 }
Dictionary_Error
Definition: s3_headers.h:27
Minio::Headers
Definition: s3_headers.h:34