GDAL
cpl_aws.h
1 /**********************************************************************
2  * $Id: cpl_aws.h 41004 2017-12-11 19:08:47Z rouault $
3  *
4  * Name: cpl_aws.h
5  * Project: CPL - Common Portability Library
6  * Purpose: Amazon Web Services routines
7  * Author: Even Rouault <even.rouault at spatialys.com>
8  *
9  **********************************************************************
10  * Copyright (c) 2015, Even Rouault <even.rouault at spatialys.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  ****************************************************************************/
30 
31 #ifndef CPL_AWS_INCLUDED_H
32 #define CPL_AWS_INCLUDED_H
33 
34 #ifndef DOXYGEN_SKIP
35 
36 #ifdef HAVE_CURL
37 
38 #include <cstddef>
39 
40 #include "cpl_string.h"
41 
42 #include <curl/curl.h>
43 #include <map>
44 
45 CPLString CPLGetLowerCaseHexSHA256( const void *pabyData, size_t nBytes );
46 CPLString CPLGetLowerCaseHexSHA256( const CPLString& osStr );
47 
48 CPLString CPLGetAWS_SIGN4_Timestamp();
49 
50 CPLString CPLAWSURLEncode(const CPLString& osURL, bool bEncodeSlash = true);
51 
52 CPLString CPLAWSGetHeaderVal(const struct curl_slist* psExistingHeaders,
53  const char* pszKey);
54 
55 CPLString CPLGetAWS_SIGN4_Authorization(const CPLString& osSecretAccessKey,
56  const CPLString& osAccessKeyId,
57  const CPLString& osAccessToken,
58  const CPLString& osRegion,
59  const CPLString& osRequestPayer,
60  const CPLString& osService,
61  const CPLString& osVerb,
62  const struct curl_slist* psExistingHeaders,
63  const CPLString& osHost,
64  const CPLString& osCanonicalURI,
65  const CPLString& osCanonicalQueryString,
66  const CPLString& osXAMZContentSHA256,
67  const CPLString& osTimestamp);
68 
69 class IVSIS3LikeHandleHelper
70 {
71 protected:
72  std::map<CPLString, CPLString> m_oMapQueryParameters;
73 
74  virtual void RebuildURL() = 0;
75  CPLString GetQueryString() const;
76 
77 public:
78  IVSIS3LikeHandleHelper() {}
79  virtual ~IVSIS3LikeHandleHelper() {}
80 
81  void ResetQueryParameters();
82  void AddQueryParameter(const CPLString& osKey, const CPLString& osValue);
83 
84  virtual struct curl_slist* GetCurlHeaders(const CPLString& osVerb,
85  const struct curl_slist* psExistingHeaders,
86  const void *pabyDataContent = nullptr,
87  size_t nBytesContent = 0) const = 0;
88 
89  virtual bool AllowAutomaticRedirection() { return true; }
90  virtual bool CanRestartOnError(const char*, const char* /* pszHeaders*/,
91  bool /*bSetError*/, bool* /*pbUpdateMap*/ = nullptr) { return false;}
92 
93  virtual const CPLString& GetURL() const = 0;
94 
95  static bool GetBucketAndObjectKey(const char* pszURI,
96  const char* pszFSPrefix,
97  bool bAllowNoObject,
98  CPLString &osBucketOut,
99  CPLString &osObjectKeyOut);
100 
101  static CPLString BuildCanonicalizedHeaders(
102  std::map<CPLString, CPLString>& oSortedMapHeaders,
103  const struct curl_slist* psExistingHeaders,
104  const char* pszHeaderPrefix);
105 };
106 
107 class VSIS3HandleHelper: public IVSIS3LikeHandleHelper
108 {
109  CPLString m_osURL;
110  CPLString m_osSecretAccessKey;
111  CPLString m_osAccessKeyId;
112  CPLString m_osSessionToken;
113  CPLString m_osEndpoint;
114  CPLString m_osRegion;
115  CPLString m_osRequestPayer;
116  CPLString m_osBucket;
117  CPLString m_osObjectKey;
118  bool m_bUseHTTPS;
119  bool m_bUseVirtualHosting;
120 
121  virtual void RebuildURL() CPL_OVERRIDE;
122 
123  static bool GetConfigurationFromEC2(CPLString& osSecretAccessKey,
124  CPLString& osAccessKeyId,
125  CPLString& osSessionToken);
126 
127  static bool GetConfigurationFromAWSConfigFiles(
128  CPLString& osSecretAccessKey,
129  CPLString& osAccessKeyId,
130  CPLString& osSessionToken,
131  CPLString& osRegion,
132  CPLString& osCredentials);
133  protected:
134 
135  public:
136  VSIS3HandleHelper(const CPLString& osSecretAccessKey,
137  const CPLString& osAccessKeyId,
138  const CPLString& osSessionToken,
139  const CPLString& osEndpoint,
140  const CPLString& osRegion,
141  const CPLString& osRequestPayer,
142  const CPLString& osBucket,
143  const CPLString& osObjectKey,
144  bool bUseHTTPS, bool bUseVirtualHosting);
145  ~VSIS3HandleHelper();
146 
147  static VSIS3HandleHelper* BuildFromURI(const char* pszURI, const char* pszFSPrefix,
148  bool bAllowNoObject);
149  static CPLString BuildURL(const CPLString& osEndpoint,
150  const CPLString& osBucket,
151  const CPLString& osObjectKey,
152  bool bUseHTTPS, bool bUseVirtualHosting);
153 
154  struct curl_slist* GetCurlHeaders(const CPLString& osVerb,
155  const struct curl_slist* psExistingHeaders,
156  const void *pabyDataContent = nullptr,
157  size_t nBytesContent = 0) const CPL_OVERRIDE;
158 
159  bool AllowAutomaticRedirection() CPL_OVERRIDE { return false; }
160  bool CanRestartOnError(const char*, const char* pszHeaders,
161  bool bSetError, bool* pbUpdateMap = nullptr) CPL_OVERRIDE;
162 
163  const CPLString& GetURL() const CPL_OVERRIDE { return m_osURL; }
164  const CPLString& GetBucket() const { return m_osBucket; }
165  const CPLString& GetObjectKey() const { return m_osObjectKey; }
166  const CPLString& GetEndpoint()const { return m_osEndpoint; }
167  const CPLString& GetRegion() const { return m_osRegion; }
168  const CPLString& GetRequestPayer() const { return m_osRequestPayer; }
169  bool GetVirtualHosting() const { return m_bUseVirtualHosting; }
170  void SetEndpoint(const CPLString &osStr);
171  void SetRegion(const CPLString &osStr);
172  void SetRequestPayer(const CPLString &osStr);
173  void SetVirtualHosting(bool b);
174 
175  static bool GetConfiguration(CPLString& osSecretAccessKey,
176  CPLString& osAccessKeyId,
177  CPLString& osSessionToken,
178  CPLString& osRegion);
179  static void CleanMutex();
180  static void ClearCache();
181 };
182 
183 class VSIS3UpdateParams
184 {
185  public:
186  CPLString m_osRegion;
187  CPLString m_osEndpoint;
188  CPLString m_osRequestPayer;
189  bool m_bUseVirtualHosting;
190 
191  VSIS3UpdateParams() :
192  m_bUseVirtualHosting(false) {}
193 
194  explicit VSIS3UpdateParams(const VSIS3HandleHelper* poHelper) :
195  m_osRegion(poHelper->GetRegion()),
196  m_osEndpoint(poHelper->GetEndpoint()),
197  m_osRequestPayer(poHelper->GetRequestPayer()),
198  m_bUseVirtualHosting(poHelper->GetVirtualHosting()) {}
199 
200  void UpdateHandlerHelper(VSIS3HandleHelper* poHelper) {
201  poHelper->SetRegion(m_osRegion);
202  poHelper->SetEndpoint(m_osEndpoint);
203  poHelper->SetRequestPayer(m_osRequestPayer);
204  poHelper->SetVirtualHosting(m_bUseVirtualHosting);
205  }
206 };
207 
208 #endif /* HAVE_CURL */
209 
210 #endif /* #ifndef DOXYGEN_SKIP */
211 
212 #endif /* CPL_AWS_INCLUDED_H */
#define CPL_OVERRIDE
To be used in public headers only.
Definition: cpl_port.h:989
Convenient string class based on std::string.
Definition: cpl_string.h:338
Various convenience functions for working with strings and string lists.

Generated for GDAL by doxygen 1.8.8.