GDAL
cpl_http.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id: cpl_http.h 40974 2017-12-07 18:13:41Z 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_vsi.h"
37 
45 #define CPL_HTTP_MAX_RETRY 0
46 #define CPL_HTTP_RETRY_DELAY 30.0
47 
50 
52 typedef struct { char **papszHeaders;
54  GByte *pabyData; int nDataLen;
57 } CPLMimePart;
58 
60 typedef struct {
62  int nStatus;
63 
66 
68  char *pszErrBuf;
69 
71  int nDataLen;
74 
77 
79  char **papszHeaders;
80 
83 
86 
88 
89 int CPL_DLL CPLHTTPEnabled( void );
90 CPLHTTPResult CPL_DLL *CPLHTTPFetch( const char *pszURL, char **papszOptions);
91 void CPL_DLL CPLHTTPCleanup( void );
92 void CPL_DLL CPLHTTPDestroyResult( CPLHTTPResult *psResult );
93 int CPL_DLL CPLHTTPParseMultipartMime( CPLHTTPResult *psResult );
94 
95 /* -------------------------------------------------------------------- */
96 /* The following is related to OAuth2 authorization around */
97 /* google services like fusion tables, and potentially others */
98 /* in the future. Code in cpl_google_oauth2.cpp. */
99 /* */
100 /* These services are built on CPL HTTP services. */
101 /* -------------------------------------------------------------------- */
102 
103 char CPL_DLL *GOA2GetAuthorizationURL( const char *pszScope );
104 char CPL_DLL *GOA2GetRefreshToken( const char *pszAuthToken,
105  const char *pszScope );
106 char CPL_DLL *GOA2GetAccessToken( const char *pszRefreshToken,
107  const char *pszScope );
108 
110  const char* pszPrivateKey,
111  const char* pszClientEmail,
112  const char* pszScope,
113  char** papszAdditionalClaims,
114  char** papszOptions);
115 
116 char CPL_DLL **GOA2GetAccessTokenFromCloudEngineVM( char** papszOptions );
117 
118 CPL_C_END
119 
120 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
121 
122 // Not sure if this belong here, used in cpl_http.cpp, cpl_vsil_curl.cpp and frmts/wms/gdalhttp.cpp
123 void* CPLHTTPSetOptions(void *pcurl, const char * const* papszOptions);
124 char** CPLHTTPGetOptionsFromEnv();
125 double CPLHTTPGetNewRetryDelay(int response_code, double dfOldDelay);
126 void* CPLHTTPIgnoreSigPipe();
127 void CPLHTTPRestoreSigPipeHandler(void* old_handler);
132 
141 {
142  public:
143 
144  GOA2Manager();
145 
147  typedef enum
148  {
149  NONE,
150  GCE,
151  ACCESS_TOKEN_FROM_REFRESH,
152  SERVICE_ACCOUNT
153  } AuthMethod;
154 
155  bool SetAuthFromGCE( char** papszOptions );
156  bool SetAuthFromRefreshToken( const char* pszRefreshToken,
157  const char* pszClientId,
158  const char* pszClientSecret,
159  char** papszOptions );
160  bool SetAuthFromServiceAccount(const char* pszPrivateKey,
161  const char* pszClientEmail,
162  const char* pszScope,
163  char** papszAdditionalClaims,
164  char** papszOptions );
165 
167  AuthMethod GetAuthMethod() const { return m_eMethod; }
168 
169  const char* GetBearer() const;
170 
171  private:
172 
173  mutable CPLString m_osCurrentBearer;
174  mutable time_t m_nExpirationTime;
175  AuthMethod m_eMethod;
176 
177  // for ACCESS_TOKEN_FROM_REFRESH
178  CPLString m_osClientId;
179  CPLString m_osClientSecret;
180  CPLString m_osRefreshToken;
181 
182  // for SERVICE_ACCOUNT
183  CPLString m_osPrivateKey;
184  CPLString m_osClientEmail;
185  CPLString m_osScope;
186  CPLStringList m_aosAdditionalClaims;
187 
188  CPLStringList m_aosOptions;
189 };
190 
191 
192 #endif // __cplusplus
193 
194 #endif /* ndef CPL_HTTP_H_INCLUDED */
CPLMimePart * pasMimePart
Definition: cpl_http.h:85
Standard C Covers.
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:338
bool CPLIsMachineForSureGCEInstance()
Returns whether the current machine is surely a Google Compute Engine instance.
Definition: cpl_google_cloud.cpp:56
void CPLHTTPCleanup(void)
Cleanup function to call at application termination.
Definition: cpl_http.cpp:1225
char ** GOA2GetAccessTokenFromServiceAccount(const char *pszPrivateKey, const char *pszClientEmail, const char *pszScope, char **papszAdditionalClaims, char **papszOptions)
Fetch access token using Service Account OAuth2.
Definition: cpl_google_oauth2.cpp:699
bool SetAuthFromRefreshToken(const char *pszRefreshToken, const char *pszClientId, const char *pszClientSecret, char **papszOptions)
Specifies that the authentication will be done using the OAuth2 client id method. ...
Definition: cpl_google_oauth2.cpp:839
char * pszContentType
Definition: cpl_http.h:65
int CPLHTTPParseMultipartMime(CPLHTTPResult *psResult)
Parses a MIME multipart message.
Definition: cpl_http.cpp:1301
void CPLHTTPDestroyResult(CPLHTTPResult *psResult)
Clean the memory associated with the return value of CPLHTTPFetch()
Definition: cpl_http.cpp:1268
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:213
char ** papszHeaders
Definition: cpl_http.h:79
const char * GetBearer() const
Return the access token.
Definition: cpl_google_oauth2.cpp:921
char ** GOA2GetAccessTokenFromCloudEngineVM(char **papszOptions)
Fetch access token using Cloud Engine internal REST API.
Definition: cpl_google_oauth2.cpp:448
Convenient string class based on std::string.
Definition: cpl_string.h:338
bool SetAuthFromServiceAccount(const char *pszPrivateKey, const char *pszClientEmail, const char *pszScope, char **papszAdditionalClaims, char **papszOptions)
Specifies that the authentication will be done using the OAuth2 service account method.
Definition: cpl_google_oauth2.cpp:875
char * GOA2GetAccessToken(const char *pszRefreshToken, const char *pszScope)
Fetch access token using refresh token.
Definition: cpl_google_oauth2.cpp:417
Various convenience functions for working with strings and string lists.
AuthMethod GetAuthMethod() const
Returns the authentication method.
Definition: cpl_http.h:167
int CPLHTTPEnabled(void)
Return if CPLHTTP services can be useful.
Definition: cpl_http.cpp:1208
int nMimePartCount
Definition: cpl_http.h:82
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:441
int nDataAlloc
Definition: cpl_http.h:73
CPLHTTPResult * CPLHTTPFetch(const char *pszURL, char **papszOptions)
Fetch a document from an url and return in a string.
Definition: cpl_http.cpp:383
int nDataLen
Definition: cpl_http.h:71
Various convenience functions for CPL.
AuthMethod
Authentication method.
Definition: cpl_http.h:147
bool CPLIsMachinePotentiallyGCEInstance()
Returns whether the current machine is potentially a Google Compute Engine instance.
Definition: cpl_google_cloud.cpp:113
Definition: cpl_http.h:60
int nStatus
Definition: cpl_http.h:62
Manager of Google OAuth2 authentication.
Definition: cpl_http.h:140
GByte * pabyData
Definition: cpl_http.h:76
Definition: cpl_http.h:52
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:340
bool SetAuthFromGCE(char **papszOptions)
Specifies that the authentication will be done using the local credentials of the current Google Comp...
Definition: cpl_google_oauth2.cpp:814
char * pszErrBuf
Definition: cpl_http.h:68
char * GOA2GetRefreshToken(const char *pszAuthToken, const char *pszScope)
Turn Auth Token into a Refresh Token.
Definition: cpl_google_oauth2.cpp:194
char * GOA2GetAuthorizationURL(const char *pszScope)
Return authorization url for a given scope.
Definition: cpl_google_oauth2.cpp:157
GOA2Manager()
Constructor.
Definition: cpl_google_oauth2.cpp:796

Generated for GDAL by doxygen 1.8.8.