GDAL
cpl_http.h
Go to the documentation of this file.
1/******************************************************************************
2 * $Id: cpl_http.h 78ebfa67fe512cc98e3452fc307a0a17e88d0b68 2019-06-21 00:26:51 +0200 Even Rouault $
3 *
4 * Project: Common Portability Library
5 * Purpose: Function wrapper for libcurl HTTP access.
6 * Author: Frank Warmerdam, warmerdam@pobox.com
7 *
8 ******************************************************************************
9 * Copyright (c) 2006, Frank Warmerdam
10 * Copyright (c) 2009, Even Rouault <even dot rouault at mines-paris dot org>
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_HTTP_H_INCLUDED
32#define CPL_HTTP_H_INCLUDED
33
34#include "cpl_conv.h"
35#include "cpl_string.h"
36#include "cpl_progress.h"
37#include "cpl_vsi.h"
38
46#ifndef CPL_HTTP_MAX_RETRY
47#define CPL_HTTP_MAX_RETRY 0
48#endif
49
50#ifndef CPL_HTTP_RETRY_DELAY
51#define CPL_HTTP_RETRY_DELAY 30.0
52#endif
56
58typedef struct { char **papszHeaders;
60 GByte *pabyData; int nDataLen;
64
66typedef struct {
69
72
74 char *pszErrBuf;
75
80
83
86
89
92
94
96typedef size_t (*CPLHTTPFetchWriteFunc)(void *pBuffer, size_t nSize, size_t nMemb, void *pWriteArg);
99int CPL_DLL CPLHTTPEnabled( void );
100CPLHTTPResult CPL_DLL *CPLHTTPFetch( const char *pszURL, CSLConstList papszOptions);
101CPLHTTPResult CPL_DLL *CPLHTTPFetchEx( const char *pszURL,CSLConstList papszOptions,
102 GDALProgressFunc pfnProgress,
103 void *pProgressArg,
104 CPLHTTPFetchWriteFunc pfnWrite,
105 void *pWriteArg);
106CPLHTTPResult CPL_DLL **CPLHTTPMultiFetch( const char * const * papszURL,
107 int nURLCount,
108 int nMaxSimultaneous,
109 CSLConstList papszOptions);
110
111void CPL_DLL CPLHTTPCleanup( void );
112void CPL_DLL CPLHTTPDestroyResult( CPLHTTPResult *psResult );
113void CPL_DLL CPLHTTPDestroyMultiResult( CPLHTTPResult **papsResults, int nCount );
114int CPL_DLL CPLHTTPParseMultipartMime( CPLHTTPResult *psResult );
115
116/* -------------------------------------------------------------------- */
117/* The following is related to OAuth2 authorization around */
118/* google services like fusion tables, and potentially others */
119/* in the future. Code in cpl_google_oauth2.cpp. */
120/* */
121/* These services are built on CPL HTTP services. */
122/* -------------------------------------------------------------------- */
123
124char CPL_DLL *GOA2GetAuthorizationURL( const char *pszScope );
125char CPL_DLL *GOA2GetRefreshToken( const char *pszAuthToken,
126 const char *pszScope );
127char CPL_DLL *GOA2GetAccessToken( const char *pszRefreshToken,
128 const char *pszScope );
129
131 const char* pszPrivateKey,
132 const char* pszClientEmail,
133 const char* pszScope,
134 CSLConstList papszAdditionalClaims,
135 CSLConstList papszOptions);
136
137char CPL_DLL **GOA2GetAccessTokenFromCloudEngineVM( CSLConstList papszOptions );
138
140
141#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
143// Not sure if this belong here, used in cpl_http.cpp, cpl_vsil_curl.cpp and frmts/wms/gdalhttp.cpp
144void* CPLHTTPSetOptions(void *pcurl, const char *pszURL, const char * const* papszOptions);
145char** CPLHTTPGetOptionsFromEnv();
146double CPLHTTPGetNewRetryDelay(int response_code, double dfOldDelay,
147 const char* pszErrBuf, const char* pszCurlError);
148void* CPLHTTPIgnoreSigPipe();
149void CPLHTTPRestoreSigPipeHandler(void* old_handler);
150bool CPLMultiPerformWait(void* hCurlMultiHandle, int& repeats);
155
164{
165 public:
166
168
170 typedef enum
171 {
172 NONE,
173 GCE,
174 ACCESS_TOKEN_FROM_REFRESH,
175 SERVICE_ACCOUNT
176 } AuthMethod;
177
178 bool SetAuthFromGCE( CSLConstList papszOptions );
179 bool SetAuthFromRefreshToken( const char* pszRefreshToken,
180 const char* pszClientId,
181 const char* pszClientSecret,
182 CSLConstList papszOptions );
183 bool SetAuthFromServiceAccount(const char* pszPrivateKey,
184 const char* pszClientEmail,
185 const char* pszScope,
186 CSLConstList papszAdditionalClaims,
187 CSLConstList papszOptions );
188
190 AuthMethod GetAuthMethod() const { return m_eMethod; }
191
192 const char* GetBearer() const;
193
195 const CPLString& GetPrivateKey() const { return m_osPrivateKey; }
196
198 const CPLString& GetClientEmail() const { return m_osClientEmail; }
199
200 private:
201
202 mutable CPLString m_osCurrentBearer{};
203 mutable time_t m_nExpirationTime = 0;
204 AuthMethod m_eMethod = NONE;
205
206 // for ACCESS_TOKEN_FROM_REFRESH
207 CPLString m_osClientId{};
208 CPLString m_osClientSecret{};
209 CPLString m_osRefreshToken{};
210
211 // for SERVICE_ACCOUNT
212 CPLString m_osPrivateKey{};
213 CPLString m_osClientEmail{};
214 CPLString m_osScope{};
215 CPLStringList m_aosAdditionalClaims{};
216
217 CPLStringList m_aosOptions{};
218};
219
220
221#endif // __cplusplus
222
223#endif /* ndef CPL_HTTP_H_INCLUDED */
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:439
Convenient string class based on std::string.
Definition: cpl_string.h:330
Manager of Google OAuth2 authentication.
Definition: cpl_http.h:164
const CPLString & GetClientEmail() const
Returns client email for SERVICE_ACCOUNT method.
Definition: cpl_http.h:198
GOA2Manager()
Constructor.
AuthMethod GetAuthMethod() const
Returns the authentication method.
Definition: cpl_http.h:190
bool SetAuthFromRefreshToken(const char *pszRefreshToken, const char *pszClientId, const char *pszClientSecret, CSLConstList papszOptions)
Specifies that the authentication will be done using the OAuth2 client id method.
Definition: cpl_google_oauth2.cpp:595
bool SetAuthFromServiceAccount(const char *pszPrivateKey, const char *pszClientEmail, const char *pszScope, CSLConstList papszAdditionalClaims, CSLConstList papszOptions)
Specifies that the authentication will be done using the OAuth2 service account method.
Definition: cpl_google_oauth2.cpp:631
const CPLString & GetPrivateKey() const
Returns private key for SERVICE_ACCOUNT method.
Definition: cpl_http.h:195
const char * GetBearer() const
Return the access token.
Definition: cpl_google_oauth2.cpp:677
bool SetAuthFromGCE(CSLConstList papszOptions)
Specifies that the authentication will be done using the local credentials of the current Google Comp...
Definition: cpl_google_oauth2.cpp:570
AuthMethod
Authentication method.
Definition: cpl_http.h:171
Various convenience functions for CPL.
CPLHTTPResult ** CPLHTTPMultiFetch(const char *const *papszURL, int nURLCount, int nMaxSimultaneous, CSLConstList papszOptions)
Fetch several documents at once.
Definition: cpl_http.cpp:1219
char * GOA2GetRefreshToken(const char *pszAuthToken, const char *pszScope)
Turn Auth Token into a Refresh Token.
Definition: cpl_google_oauth2.cpp:164
void CPLHTTPCleanup(void)
Cleanup function to call at application termination.
Definition: cpl_http.cpp:2018
char ** GOA2GetAccessTokenFromServiceAccount(const char *pszPrivateKey, const char *pszClientEmail, const char *pszScope, CSLConstList papszAdditionalClaims, CSLConstList papszOptions)
Fetch access token using Service Account OAuth2.
Definition: cpl_google_oauth2.cpp:459
bool CPLIsMachinePotentiallyGCEInstance()
Returns whether the current machine is potentially a Google Compute Engine instance.
Definition: cpl_google_cloud.cpp:110
CPLHTTPResult * CPLHTTPFetch(const char *pszURL, CSLConstList papszOptions)
Fetch a document from an url and return in a string.
Definition: cpl_http.cpp:648
char ** GOA2GetAccessTokenFromCloudEngineVM(CSLConstList papszOptions)
Fetch access token using Cloud Engine internal REST API.
Definition: cpl_google_oauth2.cpp:418
bool CPLIsMachineForSureGCEInstance()
Returns whether the current machine is surely a Google Compute Engine instance.
Definition: cpl_google_cloud.cpp:58
char * GOA2GetAuthorizationURL(const char *pszScope)
Return authorization url for a given scope.
Definition: cpl_google_oauth2.cpp:127
CPLHTTPResult * CPLHTTPFetchEx(const char *pszURL, CSLConstList papszOptions, GDALProgressFunc pfnProgress, void *pProgressArg, CPLHTTPFetchWriteFunc pfnWrite, void *pWriteArg)
Fetch a document from an url and return in a string.
Definition: cpl_http.cpp:664
char * GOA2GetAccessToken(const char *pszRefreshToken, const char *pszScope)
Fetch access token using refresh token.
Definition: cpl_google_oauth2.cpp:387
int CPLHTTPEnabled(void)
Return if CPLHTTP services can be useful.
Definition: cpl_http.cpp:2001
void CPLHTTPDestroyResult(CPLHTTPResult *psResult)
Clean the memory associated with the return value of CPLHTTPFetch()
Definition: cpl_http.cpp:2080
void CPLHTTPDestroyMultiResult(CPLHTTPResult **papsResults, int nCount)
Clean the memory associated with the return value of CPLHTTPMultiFetch()
Definition: cpl_http.cpp:1466
int CPLHTTPParseMultipartMime(CPLHTTPResult *psResult)
Parses a MIME multipart message.
Definition: cpl_http.cpp:2113
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:339
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:337
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1194
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:215
Various convenience functions for working with strings and string lists.
Standard C Covers.
Definition: cpl_http.h:66
GByte * pabyData
Definition: cpl_http.h:82
int nStatus
Definition: cpl_http.h:68
CPLMimePart * pasMimePart
Definition: cpl_http.h:91
char * pszContentType
Definition: cpl_http.h:71
int nDataAlloc
Definition: cpl_http.h:79
char ** papszHeaders
Definition: cpl_http.h:85
char * pszErrBuf
Definition: cpl_http.h:74
int nMimePartCount
Definition: cpl_http.h:88
int nDataLen
Definition: cpl_http.h:77
Definition: cpl_http.h:58

Generated for GDAL by doxygen 1.9.4.