1 /**********************************************************************
2  * $Id: cpl_alibaba_oss.h 5e20a8bb8da9ffbe2b8c850a885b29f9bff6d70c 2019-11-28 15:35:44 +0100 Even Rouault $
3  *
4  * Name:     cpl_alibaba_oss.h
5  * Project:  CPL - Common Portability Library
6  * Purpose:  Alibaba Cloud Object Storage Service
7  * Author:   Even Rouault <even.rouault at spatialys.com>
8  *
9  **********************************************************************
10  * Copyright (c) 2017, 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_ALIBABA_OSS_INCLUDED_H
32 #define CPL_ALIBABA_OSS_INCLUDED_H
33 
34 #ifndef DOXYGEN_SKIP
35 
36 #include <cstddef>
37 
38 #include "cpl_string.h"
39 
40 #ifdef HAVE_CURL
41 
42 #include <curl/curl.h>
43 #include <map>
44 #include "cpl_aws.h"
45 
46 class VSIOSSHandleHelper final: public IVSIS3LikeHandleHelper
47 {
CPL_DISALLOW_COPY_ASSIGN(VSIOSSHandleHelper)48         CPL_DISALLOW_COPY_ASSIGN(VSIOSSHandleHelper)
49 
50         CPLString m_osURL{};
51         CPLString m_osSecretAccessKey{};
52         CPLString m_osAccessKeyId{};
53         CPLString m_osEndpoint{};
54         CPLString m_osBucket{};
55         CPLString m_osObjectKey{};
56         bool m_bUseHTTPS = false;
57         bool m_bUseVirtualHosting = false;
58 
59         void RebuildURL() override;
60 
61         static bool GetConfiguration(CSLConstList papszOptions,
62                                      CPLString& osSecretAccessKey,
63                                      CPLString& osAccessKeyId);
64 
65   protected:
66 
67     public:
68         VSIOSSHandleHelper(const CPLString& osSecretAccessKey,
69                     const CPLString& osAccessKeyId,
70                     const CPLString& osEndpoint,
71                     const CPLString& osBucket,
72                     const CPLString& osObjectKey,
73                     bool bUseHTTPS, bool bUseVirtualHosting);
74        ~VSIOSSHandleHelper();
75 
76         static VSIOSSHandleHelper* BuildFromURI(const char* pszURI,
77                                                 const char* pszFSPrefix,
78                                                 bool bAllowNoObject,
79                                                 CSLConstList papszOptions = nullptr);
80         static CPLString BuildURL(const CPLString& osEndpoint,
81                                   const CPLString& osBucket,
82                                   const CPLString& osObjectKey,
83                                   bool bUseHTTPS, bool bUseVirtualHosting);
84 
85         struct curl_slist* GetCurlHeaders(
86             const CPLString& osVerb,
87             const struct curl_slist* psExistingHeaders,
88             const void *pabyDataContent = nullptr,
89             size_t nBytesContent = 0 ) const override;
90 
91         bool CanRestartOnError(const char*, const char* pszHeaders,
92                                bool bSetError,
93                                bool* pbUpdateMap = nullptr) override;
94 
GetURL()95         const CPLString& GetURL() const override { return m_osURL; }
GetBucket()96         const CPLString& GetBucket() const { return m_osBucket; }
GetObjectKey()97         const CPLString& GetObjectKey() const { return m_osObjectKey; }
GetEndpoint()98         const CPLString& GetEndpoint()const  { return m_osEndpoint; }
GetVirtualHosting()99         bool GetVirtualHosting() const { return m_bUseVirtualHosting; }
100 
GetCopySourceHeader()101         CPLString GetCopySourceHeader() const override { return "x-oss-copy-source"; }
102 
103         void SetEndpoint(const CPLString &osStr);
104         void SetVirtualHosting(bool b);
105 
106         CPLString GetSignedURL(CSLConstList papszOptions);
107 };
108 
109 class VSIOSSUpdateParams
110 {
111     public:
112         CPLString m_osEndpoint{};
113 
114         VSIOSSUpdateParams() = default;
115 
VSIOSSUpdateParams(const VSIOSSHandleHelper * poHelper)116         explicit VSIOSSUpdateParams(const VSIOSSHandleHelper* poHelper) :
117             m_osEndpoint(poHelper->GetEndpoint()) {}
118 
UpdateHandlerHelper(VSIOSSHandleHelper * poHelper)119         void UpdateHandlerHelper(VSIOSSHandleHelper* poHelper) {
120             poHelper->SetEndpoint(m_osEndpoint);
121         }
122 };
123 
124 #endif /* HAVE_CURL */
125 
126 #endif /* #ifndef DOXYGEN_SKIP */
127 
128 #endif /* CPL_ALIBABA_OSS_INCLUDED_H */
129