gooderp18绿色标准版
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

224 lines
8.3KB

  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. #ifndef CPL_HTTP_H_INCLUDED
  31. #define CPL_HTTP_H_INCLUDED
  32. #include "cpl_conv.h"
  33. #include "cpl_string.h"
  34. #include "cpl_progress.h"
  35. #include "cpl_vsi.h"
  36. /**
  37. * \file cpl_http.h
  38. *
  39. * Interface for downloading HTTP, FTP documents
  40. */
  41. /*! @cond Doxygen_Suppress */
  42. #ifndef CPL_HTTP_MAX_RETRY
  43. #define CPL_HTTP_MAX_RETRY 0
  44. #endif
  45. #ifndef CPL_HTTP_RETRY_DELAY
  46. #define CPL_HTTP_RETRY_DELAY 30.0
  47. #endif
  48. /*! @endcond */
  49. CPL_C_START
  50. /*! Describe a part of a multipart message */
  51. typedef struct {
  52. /*! NULL terminated array of headers */ char **papszHeaders;
  53. /*! Buffer with data of the part */ GByte *pabyData;
  54. /*! Buffer length */ int nDataLen;
  55. } CPLMimePart;
  56. /*! Describe the result of a CPLHTTPFetch() call */
  57. typedef struct {
  58. /*! cURL error code : 0=success, non-zero if request failed */
  59. int nStatus;
  60. /*! Content-Type of the response */
  61. char *pszContentType;
  62. /*! Error message from curl, or NULL */
  63. char *pszErrBuf;
  64. /*! Length of the pabyData buffer */
  65. int nDataLen;
  66. /*! Allocated size of the pabyData buffer */
  67. int nDataAlloc;
  68. /*! Buffer with downloaded data */
  69. GByte *pabyData;
  70. /*! Headers returned */
  71. char **papszHeaders;
  72. /*! Number of parts in a multipart message */
  73. int nMimePartCount;
  74. /*! Array of parts (resolved by CPLHTTPParseMultipartMime()) */
  75. CPLMimePart *pasMimePart;
  76. } CPLHTTPResult;
  77. /*! @cond Doxygen_Suppress */
  78. typedef size_t (*CPLHTTPFetchWriteFunc)(void *pBuffer, size_t nSize, size_t nMemb, void *pWriteArg);
  79. /*! @endcond */
  80. int CPL_DLL CPLHTTPEnabled( void );
  81. CPLHTTPResult CPL_DLL *CPLHTTPFetch( const char *pszURL, CSLConstList papszOptions);
  82. CPLHTTPResult CPL_DLL *CPLHTTPFetchEx( const char *pszURL,CSLConstList papszOptions,
  83. GDALProgressFunc pfnProgress,
  84. void *pProgressArg,
  85. CPLHTTPFetchWriteFunc pfnWrite,
  86. void *pWriteArg);
  87. CPLHTTPResult CPL_DLL **CPLHTTPMultiFetch( const char * const * papszURL,
  88. int nURLCount,
  89. int nMaxSimultaneous,
  90. CSLConstList papszOptions);
  91. void CPL_DLL CPLHTTPCleanup( void );
  92. void CPL_DLL CPLHTTPDestroyResult( CPLHTTPResult *psResult );
  93. void CPL_DLL CPLHTTPDestroyMultiResult( CPLHTTPResult **papsResults, int nCount );
  94. int CPL_DLL CPLHTTPParseMultipartMime( CPLHTTPResult *psResult );
  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. char CPL_DLL *GOA2GetAuthorizationURL( const char *pszScope );
  103. char CPL_DLL *GOA2GetRefreshToken( const char *pszAuthToken,
  104. const char *pszScope );
  105. char CPL_DLL *GOA2GetAccessToken( const char *pszRefreshToken,
  106. const char *pszScope );
  107. char CPL_DLL **GOA2GetAccessTokenFromServiceAccount(
  108. const char* pszPrivateKey,
  109. const char* pszClientEmail,
  110. const char* pszScope,
  111. CSLConstList papszAdditionalClaims,
  112. CSLConstList papszOptions);
  113. char CPL_DLL **GOA2GetAccessTokenFromCloudEngineVM( CSLConstList papszOptions );
  114. CPL_C_END
  115. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  116. /*! @cond Doxygen_Suppress */
  117. // Not sure if this belong here, used in cpl_http.cpp, cpl_vsil_curl.cpp and frmts/wms/gdalhttp.cpp
  118. void* CPLHTTPSetOptions(void *pcurl, const char *pszURL, const char * const* papszOptions);
  119. char** CPLHTTPGetOptionsFromEnv();
  120. double CPLHTTPGetNewRetryDelay(int response_code, double dfOldDelay,
  121. const char* pszErrBuf, const char* pszCurlError);
  122. void* CPLHTTPIgnoreSigPipe();
  123. void CPLHTTPRestoreSigPipeHandler(void* old_handler);
  124. bool CPLMultiPerformWait(void* hCurlMultiHandle, int& repeats);
  125. /*! @endcond */
  126. bool CPLIsMachinePotentiallyGCEInstance();
  127. bool CPLIsMachineForSureGCEInstance();
  128. /** Manager of Google OAuth2 authentication.
  129. *
  130. * This class handles different authentication methods and handles renewal
  131. * of access token.
  132. *
  133. * @since GDAL 2.3
  134. */
  135. class GOA2Manager
  136. {
  137. public:
  138. GOA2Manager();
  139. /** Authentication method */
  140. typedef enum
  141. {
  142. NONE,
  143. GCE,
  144. ACCESS_TOKEN_FROM_REFRESH,
  145. SERVICE_ACCOUNT
  146. } AuthMethod;
  147. bool SetAuthFromGCE( CSLConstList papszOptions );
  148. bool SetAuthFromRefreshToken( const char* pszRefreshToken,
  149. const char* pszClientId,
  150. const char* pszClientSecret,
  151. CSLConstList papszOptions );
  152. bool SetAuthFromServiceAccount(const char* pszPrivateKey,
  153. const char* pszClientEmail,
  154. const char* pszScope,
  155. CSLConstList papszAdditionalClaims,
  156. CSLConstList papszOptions );
  157. /** Returns the authentication method. */
  158. AuthMethod GetAuthMethod() const { return m_eMethod; }
  159. const char* GetBearer() const;
  160. /** Returns private key for SERVICE_ACCOUNT method */
  161. const CPLString& GetPrivateKey() const { return m_osPrivateKey; }
  162. /** Returns client email for SERVICE_ACCOUNT method */
  163. const CPLString& GetClientEmail() const { return m_osClientEmail; }
  164. private:
  165. mutable CPLString m_osCurrentBearer{};
  166. mutable time_t m_nExpirationTime = 0;
  167. AuthMethod m_eMethod = NONE;
  168. // for ACCESS_TOKEN_FROM_REFRESH
  169. CPLString m_osClientId{};
  170. CPLString m_osClientSecret{};
  171. CPLString m_osRefreshToken{};
  172. // for SERVICE_ACCOUNT
  173. CPLString m_osPrivateKey{};
  174. CPLString m_osClientEmail{};
  175. CPLString m_osScope{};
  176. CPLStringList m_aosAdditionalClaims{};
  177. CPLStringList m_aosOptions{};
  178. };
  179. #endif // __cplusplus
  180. #endif /* ndef CPL_HTTP_H_INCLUDED */
上海开阖软件有限公司 沪ICP备12045867号-1