MinIO C++ SDK
http.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_HTTP_H
17 #define _MINIO_HTTP_H
18 
19 #include <curlpp/Easy.hpp>
20 #include <curlpp/Options.hpp>
21 
22 #include "utils.h"
23 
24 namespace minio {
25 namespace http {
26 enum class Method { kGet, kHead, kPost, kPut, kDelete };
27 
28 // MethodToString converts http Method enum to string.
29 constexpr const char* MethodToString(Method& method) throw() {
30  switch (method) {
31  case Method::kGet:
32  return "GET";
33  case Method::kHead:
34  return "HEAD";
35  case Method::kPost:
36  return "POST";
37  case Method::kPut:
38  return "PUT";
39  case Method::kDelete:
40  return "DELETE";
41  default: {
42  std::cerr << "ABORT: Unimplemented HTTP method. This should not happen."
43  << std::endl;
44  std::terminate();
45  }
46  }
47  return NULL;
48 }
49 
50 // ExtractRegion extracts region value from AWS S3 host string.
51 std::string ExtractRegion(std::string host);
52 
53 struct BaseUrl {
54  std::string host;
55  bool is_https = true;
56  unsigned int port = 0;
57  std::string region;
58  bool aws_host = false;
59  bool accelerate_host = false;
60  bool dualstack_host = false;
61  bool virtual_style = false;
62 
63  error::Error SetHost(std::string hostvalue);
64  std::string GetHostHeaderValue();
65  error::Error BuildUrl(utils::Url& url, Method method, std::string region,
66  utils::Multimap query_params,
67  std::string bucket_name = "",
68  std::string object_name = "");
69  operator bool() const { return !host.empty(); }
70 }; // struct BaseUrl
71 
72 struct DataCallbackArgs;
73 
74 typedef size_t (*DataCallback)(DataCallbackArgs args);
75 
76 struct Response;
77 
79  curlpp::Easy* handle = NULL;
80  Response* response = NULL;
81  char* buffer = NULL;
82  size_t size = 0;
83  size_t length = 0;
84  void* user_arg = NULL;
85 }; // struct DataCallbackArgs
86 
87 struct Request {
88  Method method;
89  utils::Url url;
90  utils::Multimap headers;
91  std::string_view body = "";
92  DataCallback data_callback = NULL;
93  void* user_arg = NULL;
94  bool debug = false;
95  bool ignore_cert_check = false;
96 
97  Request(Method httpmethod, utils::Url httpurl);
98  Response Execute();
99  operator bool() const {
100  if (method < Method::kGet || method > Method::kDelete) return false;
101  return url;
102  }
103 
104  private:
105  Response execute();
106 }; // struct Request
107 
108 struct Response {
109  std::string error;
110  DataCallback data_callback = NULL;
111  void* user_arg = NULL;
112  int status_code = 0;
113  utils::Multimap headers;
114  std::string body;
115 
116  size_t ResponseCallback(curlpp::Easy* handle, char* buffer, size_t size,
117  size_t length);
118  operator bool() const {
119  return error.empty() && status_code >= 200 && status_code <= 299;
120  }
121 
122  private:
123  std::string response_;
124  bool continue100_ = false;
125  bool status_code_read_ = false;
126  bool headers_read_ = false;
127 
128  size_t ReadStatusCode(char* buffer, size_t size, size_t length);
129  size_t ReadHeaders(curlpp::Easy* handle, char* buffer, size_t size,
130  size_t length);
131 }; // struct Response
132 } // namespace http
133 } // namespace minio
134 #endif // #ifndef _MINIO_HTTP_H
Definition: error.h:23
Definition: utils.h:119
Definition: http.h:53
Definition: http.h:78
Definition: http.h:87
Definition: http.h:108
Definition: utils.h:144