forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpl_http.h
265 lines (221 loc) · 9.23 KB
/
cpl_http.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/******************************************************************************
* $Id$
*
* Project: Common Portability Library
* Purpose: Function wrapper for libcurl HTTP access.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 2006, Frank Warmerdam
* Copyright (c) 2009, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#ifndef CPL_HTTP_H_INCLUDED
#define CPL_HTTP_H_INCLUDED
#include "cpl_conv.h"
#include "cpl_string.h"
#include "cpl_progress.h"
#include "cpl_vsi.h"
/**
* \file cpl_http.h
*
* Interface for downloading HTTP, FTP documents
*/
/*! @cond Doxygen_Suppress */
#ifndef CPL_HTTP_MAX_RETRY
#define CPL_HTTP_MAX_RETRY 0
#endif
#ifndef CPL_HTTP_RETRY_DELAY
#define CPL_HTTP_RETRY_DELAY 30.0
#endif
/*! @endcond */
CPL_C_START
/*! Describe a part of a multipart message */
typedef struct
{
/*! NULL terminated array of headers */ char **papszHeaders;
/*! Buffer with data of the part */ GByte *pabyData;
/*! Buffer length */ int nDataLen;
} CPLMimePart;
/*! Describe the result of a CPLHTTPFetch() call */
typedef struct
{
/*! cURL error code : 0=success, non-zero if request failed */
int nStatus;
/*! Content-Type of the response */
char *pszContentType;
/*! Error message from curl, or NULL */
char *pszErrBuf;
/*! Length of the pabyData buffer */
int nDataLen;
/*! Allocated size of the pabyData buffer */
int nDataAlloc;
/*! Buffer with downloaded data */
GByte *pabyData;
/*! Headers returned */
char **papszHeaders;
/*! Number of parts in a multipart message */
int nMimePartCount;
/*! Array of parts (resolved by CPLHTTPParseMultipartMime()) */
CPLMimePart *pasMimePart;
} CPLHTTPResult;
/*! @cond Doxygen_Suppress */
typedef size_t (*CPLHTTPFetchWriteFunc)(void *pBuffer, size_t nSize,
size_t nMemb, void *pWriteArg);
/*! @endcond */
int CPL_DLL CPLHTTPEnabled(void);
CPLHTTPResult CPL_DLL *CPLHTTPFetch(const char *pszURL,
CSLConstList papszOptions);
CPLHTTPResult CPL_DLL *
CPLHTTPFetchEx(const char *pszURL, CSLConstList papszOptions,
GDALProgressFunc pfnProgress, void *pProgressArg,
CPLHTTPFetchWriteFunc pfnWrite, void *pWriteArg);
CPLHTTPResult CPL_DLL **CPLHTTPMultiFetch(const char *const *papszURL,
int nURLCount, int nMaxSimultaneous,
CSLConstList papszOptions);
void CPL_DLL CPLHTTPCleanup(void);
void CPL_DLL CPLHTTPDestroyResult(CPLHTTPResult *psResult);
void CPL_DLL CPLHTTPDestroyMultiResult(CPLHTTPResult **papsResults, int nCount);
int CPL_DLL CPLHTTPParseMultipartMime(CPLHTTPResult *psResult);
void CPL_DLL CPLHTTPSetDefaultUserAgent(const char *pszUserAgent);
/* -------------------------------------------------------------------- */
/* To install an alternate network layer to the default Curl one */
/* -------------------------------------------------------------------- */
/** Callback function to process network requests.
*
* If CLOSE_PERSISTENT is found in papszOptions, no network request should be
* issued, but a dummy non-null CPLHTTPResult* should be returned by the
* callback.
*
* Its first arguments are the same as CPLHTTPFetchEx()
* @param pszURL See CPLHTTPFetchEx()
* @param papszOptions See CPLHTTPFetchEx()
* @param pfnProgress See CPLHTTPFetchEx()
* @param pProgressArg See CPLHTTPFetchEx()
* @param pfnWrite See CPLHTTPFetchEx()
* @param pWriteArg See CPLHTTPFetchEx()
* @param pUserData user data value that was passed during
* CPLHTTPPushFetchCallback()
* @return nullptr if the request cannot be processed, in which case the
* previous handler will be used.
*/
typedef CPLHTTPResult *(*CPLHTTPFetchCallbackFunc)(
const char *pszURL, CSLConstList papszOptions, GDALProgressFunc pfnProgress,
void *pProgressArg, CPLHTTPFetchWriteFunc pfnWrite, void *pWriteArg,
void *pUserData);
void CPL_DLL CPLHTTPSetFetchCallback(CPLHTTPFetchCallbackFunc pFunc,
void *pUserData);
int CPL_DLL CPLHTTPPushFetchCallback(CPLHTTPFetchCallbackFunc pFunc,
void *pUserData);
int CPL_DLL CPLHTTPPopFetchCallback(void);
/* -------------------------------------------------------------------- */
/* The following is related to OAuth2 authorization around */
/* google services like fusion tables, and potentially others */
/* in the future. Code in cpl_google_oauth2.cpp. */
/* */
/* These services are built on CPL HTTP services. */
/* -------------------------------------------------------------------- */
char CPL_DLL *GOA2GetAuthorizationURL(const char *pszScope);
char CPL_DLL *GOA2GetRefreshToken(const char *pszAuthToken,
const char *pszScope);
char CPL_DLL *GOA2GetAccessToken(const char *pszRefreshToken,
const char *pszScope);
char CPL_DLL **GOA2GetAccessTokenFromServiceAccount(
const char *pszPrivateKey, const char *pszClientEmail, const char *pszScope,
CSLConstList papszAdditionalClaims, CSLConstList papszOptions);
char CPL_DLL **GOA2GetAccessTokenFromCloudEngineVM(CSLConstList papszOptions);
CPL_C_END
#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
/*! @cond Doxygen_Suppress */
// Not sure if this belong here, used in cpl_http.cpp, cpl_vsil_curl.cpp and
// frmts/wms/gdalhttp.cpp
void CPL_DLL *CPLHTTPSetOptions(void *pcurl, const char *pszURL,
const char *const *papszOptions);
char **CPLHTTPGetOptionsFromEnv(const char *pszFilename);
double CPLHTTPGetNewRetryDelay(int response_code, double dfOldDelay,
const char *pszErrBuf, const char *pszCurlError);
void CPL_DLL *CPLHTTPIgnoreSigPipe();
void CPL_DLL CPLHTTPRestoreSigPipeHandler(void *old_handler);
bool CPLMultiPerformWait(void *hCurlMultiHandle, int &repeats);
/*! @endcond */
bool CPL_DLL CPLIsMachinePotentiallyGCEInstance();
bool CPLIsMachineForSureGCEInstance();
/** Manager of Google OAuth2 authentication.
*
* This class handles different authentication methods and handles renewal
* of access token.
*
* @since GDAL 2.3
*/
class GOA2Manager
{
public:
GOA2Manager();
/** Authentication method */
typedef enum
{
NONE,
GCE,
ACCESS_TOKEN_FROM_REFRESH,
SERVICE_ACCOUNT
} AuthMethod;
bool SetAuthFromGCE(CSLConstList papszOptions);
bool SetAuthFromRefreshToken(const char *pszRefreshToken,
const char *pszClientId,
const char *pszClientSecret,
CSLConstList papszOptions);
bool SetAuthFromServiceAccount(const char *pszPrivateKey,
const char *pszClientEmail,
const char *pszScope,
CSLConstList papszAdditionalClaims,
CSLConstList papszOptions);
/** Returns the authentication method. */
AuthMethod GetAuthMethod() const
{
return m_eMethod;
}
const char *GetBearer() const;
/** Returns private key for SERVICE_ACCOUNT method */
const CPLString &GetPrivateKey() const
{
return m_osPrivateKey;
}
/** Returns client email for SERVICE_ACCOUNT method */
const CPLString &GetClientEmail() const
{
return m_osClientEmail;
}
private:
mutable CPLString m_osCurrentBearer{};
mutable time_t m_nExpirationTime = 0;
AuthMethod m_eMethod = NONE;
// for ACCESS_TOKEN_FROM_REFRESH
CPLString m_osClientId{};
CPLString m_osClientSecret{};
CPLString m_osRefreshToken{};
// for SERVICE_ACCOUNT
CPLString m_osPrivateKey{};
CPLString m_osClientEmail{};
CPLString m_osScope{};
CPLStringList m_aosAdditionalClaims{};
CPLStringList m_aosOptions{};
};
#endif // __cplusplus
#endif /* ndef CPL_HTTP_H_INCLUDED */