MinIO C++ SDK
s3_io.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 #ifndef _S3_IO_H
17 #define _S3_IO_H
18 
19 #include <cstddef>
20 #include <ostream>
21 #include <iostream>
22 #include <sstream>
23 #include <cstring>
24 #include <curlpp/Easy.hpp>
25 
26 #include "s3_headers.h"
27 
28 namespace Minio
29 {
30  typedef cURLpp::Easy S3Connection;
31 
32  // S3ClientIO objects specify data and headers to send,
33  // and collect the data and headers of the response.
34  struct S3ClientIO {
35  std::string httpDate;// Timestamp, set by S3Client::Submit()
36  Headers reqHeaders;// Headers for request
37 
38  std::string result;// Result code for response, minus the leading "HTTP/1.1"
39  int numResult;// Numeric result code for response
40  Headers respHeaders;// Headers from response
41 
42  std::ostringstream response;// default output stream, contains body of response
43  std::istream * istrm;
44  std::ostream * ostrm;
45 
46  size_t bytesToGet;// used only for progress reporting
47  size_t bytesReceived;
48  size_t bytesToPut;
49  size_t bytesSent;
50 
51  bool printProgress;
52  bool error;
53 
54  S3ClientIO() {Reset();}
55  S3ClientIO(std::istream * i) {Reset(i, NULL);}
56  S3ClientIO(std::ostream * o) {Reset(NULL, o);}
57  S3ClientIO(std::istream * i, std::ostream * o) {Reset(i, o);}
58 
59  void Reset(std::istream * i = NULL, std::ostream * o = NULL) {
60  reqHeaders.Clear();
61  respHeaders.Clear();
62  response.clear();
63  httpDate = "";
64  result = "";
65  numResult = 0;
66  istrm = NULL;
67  ostrm = (o == NULL)? &response : o;
68  bytesToGet = 0; bytesReceived = 0;
69  bytesToPut = 0; bytesSent = 0;
70  printProgress = false;
71  error = false;
72  }
73 
74  // "200 OK", or some other 20x message
75  bool Success() const {return result[0] == '2' && !error;}
76  bool Failure() const {return !Success();}
77 
78  // Called prior to performing action
79  virtual void WillStart();
80 
81  // Called after action is complete
82  virtual void DidFinish();
83 
84  // Handler for data received by libcurl
85  virtual size_t Write(char * buf, size_t size, size_t nmemb);
86 
87  // Handler for data requested by libcurl for transmission
88  virtual size_t Read(char * buf, size_t size, size_t nmemb);
89 
90  // Handler for headers: overrides must call if other functionality of
91  // S3ClientIO is to be used.
92  virtual size_t HandleHeader(char * buf, size_t size, size_t nmemb);
93 
94  friend std::ostream & operator<<(std::ostream & ostrm, S3ClientIO & io);
95  };
96 }
97 
98 #endif /* _S3_IO_H */
Minio::Headers
Definition: s3_headers.h:34
Minio::S3ClientIO
Definition: s3_io.h:34