Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
11 KiB

  1. //====== Copyright © 1996-2009, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: interface to http client
  4. //
  5. //=============================================================================
  6. #ifndef ISTEAMHTTP_H
  7. #define ISTEAMHTTP_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "isteamclient.h"
  12. #include "steamhttpenums.h"
  13. // Handle to a HTTP Request handle
  14. typedef uint32 HTTPRequestHandle;
  15. #define INVALID_HTTPREQUEST_HANDLE 0
  16. typedef uint32 HTTPCookieContainerHandle;
  17. #define INVALID_HTTPCOOKIE_HANDLE 0
  18. //-----------------------------------------------------------------------------
  19. // Purpose: interface to http client
  20. //-----------------------------------------------------------------------------
  21. class ISteamHTTP
  22. {
  23. public:
  24. // Initializes a new HTTP request, returning a handle to use in further operations on it. Requires
  25. // the method (GET or POST) and the absolute URL for the request. Both http and https are supported,
  26. // so this string must start with http:// or https:// and should look like http://store.steampowered.com/app/250/
  27. // or such.
  28. virtual HTTPRequestHandle CreateHTTPRequest( EHTTPMethod eHTTPRequestMethod, const char *pchAbsoluteURL ) = 0;
  29. // Set a context value for the request, which will be returned in the HTTPRequestCompleted_t callback after
  30. // sending the request. This is just so the caller can easily keep track of which callbacks go with which request data.
  31. virtual bool SetHTTPRequestContextValue( HTTPRequestHandle hRequest, uint64 ulContextValue ) = 0;
  32. // Set a timeout in seconds for the HTTP request, must be called prior to sending the request. Default
  33. // timeout is 60 seconds if you don't call this. Returns false if the handle is invalid, or the request
  34. // has already been sent.
  35. virtual bool SetHTTPRequestNetworkActivityTimeout( HTTPRequestHandle hRequest, uint32 unTimeoutSeconds ) = 0;
  36. // Set a request header value for the request, must be called prior to sending the request. Will
  37. // return false if the handle is invalid or the request is already sent.
  38. virtual bool SetHTTPRequestHeaderValue( HTTPRequestHandle hRequest, const char *pchHeaderName, const char *pchHeaderValue ) = 0;
  39. // Set a GET or POST parameter value on the request, which is set will depend on the EHTTPMethod specified
  40. // when creating the request. Must be called prior to sending the request. Will return false if the
  41. // handle is invalid or the request is already sent.
  42. virtual bool SetHTTPRequestGetOrPostParameter( HTTPRequestHandle hRequest, const char *pchParamName, const char *pchParamValue ) = 0;
  43. // Sends the HTTP request, will return false on a bad handle, otherwise use SteamCallHandle to wait on
  44. // asynchronous response via callback.
  45. //
  46. // Note: If the user is in offline mode in Steam, then this will add a only-if-cached cache-control
  47. // header and only do a local cache lookup rather than sending any actual remote request.
  48. virtual bool SendHTTPRequest( HTTPRequestHandle hRequest, SteamAPICall_t *pCallHandle ) = 0;
  49. // Sends the HTTP request, will return false on a bad handle, otherwise use SteamCallHandle to wait on
  50. // asynchronous response via callback for completion, and listen for HTTPRequestHeadersReceived_t and
  51. // HTTPRequestDataReceived_t callbacks while streaming.
  52. virtual bool SendHTTPRequestAndStreamResponse( HTTPRequestHandle hRequest, SteamAPICall_t *pCallHandle ) = 0;
  53. // Defers a request you have sent, the actual HTTP client code may have many requests queued, and this will move
  54. // the specified request to the tail of the queue. Returns false on invalid handle, or if the request is not yet sent.
  55. virtual bool DeferHTTPRequest( HTTPRequestHandle hRequest ) = 0;
  56. // Prioritizes a request you have sent, the actual HTTP client code may have many requests queued, and this will move
  57. // the specified request to the head of the queue. Returns false on invalid handle, or if the request is not yet sent.
  58. virtual bool PrioritizeHTTPRequest( HTTPRequestHandle hRequest ) = 0;
  59. // Checks if a response header is present in a HTTP response given a handle from HTTPRequestCompleted_t, also
  60. // returns the size of the header value if present so the caller and allocate a correctly sized buffer for
  61. // GetHTTPResponseHeaderValue.
  62. virtual bool GetHTTPResponseHeaderSize( HTTPRequestHandle hRequest, const char *pchHeaderName, uint32 *unResponseHeaderSize ) = 0;
  63. // Gets header values from a HTTP response given a handle from HTTPRequestCompleted_t, will return false if the
  64. // header is not present or if your buffer is too small to contain it's value. You should first call
  65. // BGetHTTPResponseHeaderSize to check for the presence of the header and to find out the size buffer needed.
  66. virtual bool GetHTTPResponseHeaderValue( HTTPRequestHandle hRequest, const char *pchHeaderName, uint8 *pHeaderValueBuffer, uint32 unBufferSize ) = 0;
  67. // Gets the size of the body data from a HTTP response given a handle from HTTPRequestCompleted_t, will return false if the
  68. // handle is invalid.
  69. virtual bool GetHTTPResponseBodySize( HTTPRequestHandle hRequest, uint32 *unBodySize ) = 0;
  70. // Gets the body data from a HTTP response given a handle from HTTPRequestCompleted_t, will return false if the
  71. // handle is invalid or is to a streaming response, or if the provided buffer is not the correct size. Use BGetHTTPResponseBodySize first to find out
  72. // the correct buffer size to use.
  73. virtual bool GetHTTPResponseBodyData( HTTPRequestHandle hRequest, uint8 *pBodyDataBuffer, uint32 unBufferSize ) = 0;
  74. // Gets the body data from a streaming HTTP response given a handle from HTTPRequestDataReceived_t. Will return false if the
  75. // handle is invalid or is to a non-streaming response (meaning it wasn't sent with SendHTTPRequestAndStreamResponse), or if the buffer size and offset
  76. // do not match the size and offset sent in HTTPRequestDataReceived_t.
  77. virtual bool GetHTTPStreamingResponseBodyData( HTTPRequestHandle hRequest, uint32 cOffset, uint8 *pBodyDataBuffer, uint32 unBufferSize ) = 0;
  78. // Releases an HTTP response handle, should always be called to free resources after receiving a HTTPRequestCompleted_t
  79. // callback and finishing using the response.
  80. virtual bool ReleaseHTTPRequest( HTTPRequestHandle hRequest ) = 0;
  81. // Gets progress on downloading the body for the request. This will be zero unless a response header has already been
  82. // received which included a content-length field. For responses that contain no content-length it will report
  83. // zero for the duration of the request as the size is unknown until the connection closes.
  84. virtual bool GetHTTPDownloadProgressPct( HTTPRequestHandle hRequest, float *pflPercentOut ) = 0;
  85. // Sets the body for an HTTP Post request. Will fail and return false on a GET request, and will fail if POST params
  86. // have already been set for the request. Setting this raw body makes it the only contents for the post, the pchContentType
  87. // parameter will set the content-type header for the request so the server may know how to interpret the body.
  88. virtual bool SetHTTPRequestRawPostBody( HTTPRequestHandle hRequest, const char *pchContentType, uint8 *pubBody, uint32 unBodyLen ) = 0;
  89. // Creates a cookie container handle which you must later free with ReleaseCookieContainer(). If bAllowResponsesToModify=true
  90. // than any response to your requests using this cookie container may add new cookies which may be transmitted with
  91. // future requests. If bAllowResponsesToModify=false than only cookies you explicitly set will be sent. This API is just for
  92. // during process lifetime, after steam restarts no cookies are persisted and you have no way to access the cookie container across
  93. // repeat executions of your process.
  94. virtual HTTPCookieContainerHandle CreateCookieContainer( bool bAllowResponsesToModify ) = 0;
  95. // Release a cookie container you are finished using, freeing it's memory
  96. virtual bool ReleaseCookieContainer( HTTPCookieContainerHandle hCookieContainer ) = 0;
  97. // Adds a cookie to the specified cookie container that will be used with future requests.
  98. virtual bool SetCookie( HTTPCookieContainerHandle hCookieContainer, const char *pchHost, const char *pchUrl, const char *pchCookie ) = 0;
  99. // Set the cookie container to use for a HTTP request
  100. virtual bool SetHTTPRequestCookieContainer( HTTPRequestHandle hRequest, HTTPCookieContainerHandle hCookieContainer ) = 0;
  101. // Set the extra user agent info for a request, this doesn't clobber the normal user agent, it just adds the extra info on the end
  102. virtual bool SetHTTPRequestUserAgentInfo( HTTPRequestHandle hRequest, const char *pchUserAgentInfo ) = 0;
  103. // Set that https request should require verified SSL certificate via machines certificate trust store
  104. virtual bool SetHTTPRequestRequiresVerifiedCertificate( HTTPRequestHandle hRequest, bool bRequireVerifiedCertificate ) = 0;
  105. // Set an absolute timeout on the HTTP request, this is just a total time timeout different than the network activity timeout
  106. // which can bump everytime we get more data
  107. virtual bool SetHTTPRequestAbsoluteTimeoutMS( HTTPRequestHandle hRequest, uint32 unMilliseconds ) = 0;
  108. // Check if the reason the request failed was because we timed it out (rather than some harder failure)
  109. virtual bool GetHTTPRequestWasTimedOut( HTTPRequestHandle hRequest, bool *pbWasTimedOut ) = 0;
  110. };
  111. #define STEAMHTTP_INTERFACE_VERSION "STEAMHTTP_INTERFACE_VERSION002"
  112. // callbacks
  113. #if defined( VALVE_CALLBACK_PACK_SMALL )
  114. #pragma pack( push, 4 )
  115. #elif defined( VALVE_CALLBACK_PACK_LARGE )
  116. #pragma pack( push, 8 )
  117. #else
  118. #error isteamclient.h must be included
  119. #endif
  120. struct HTTPRequestCompleted_t
  121. {
  122. enum { k_iCallback = k_iClientHTTPCallbacks + 1 };
  123. // Handle value for the request that has completed.
  124. HTTPRequestHandle m_hRequest;
  125. // Context value that the user defined on the request that this callback is associated with, 0 if
  126. // no context value was set.
  127. uint64 m_ulContextValue;
  128. // This will be true if we actually got any sort of response from the server (even an error).
  129. // It will be false if we failed due to an internal error or client side network failure.
  130. bool m_bRequestSuccessful;
  131. // Will be the HTTP status code value returned by the server, k_EHTTPStatusCode200OK is the normal
  132. // OK response, if you get something else you probably need to treat it as a failure.
  133. EHTTPStatusCode m_eStatusCode;
  134. uint32 m_unBodySize; // Same as GetHTTPResponseBodySize()
  135. };
  136. struct HTTPRequestHeadersReceived_t
  137. {
  138. enum { k_iCallback = k_iClientHTTPCallbacks + 2 };
  139. // Handle value for the request that has received headers.
  140. HTTPRequestHandle m_hRequest;
  141. // Context value that the user defined on the request that this callback is associated with, 0 if
  142. // no context value was set.
  143. uint64 m_ulContextValue;
  144. };
  145. struct HTTPRequestDataReceived_t
  146. {
  147. enum { k_iCallback = k_iClientHTTPCallbacks + 3 };
  148. // Handle value for the request that has received data.
  149. HTTPRequestHandle m_hRequest;
  150. // Context value that the user defined on the request that this callback is associated with, 0 if
  151. // no context value was set.
  152. uint64 m_ulContextValue;
  153. // Offset to provide to GetHTTPStreamingResponseBodyData to get this chunk of data
  154. uint32 m_cOffset;
  155. // Size to provide to GetHTTPStreamingResponseBodyData to get this chunk of data
  156. uint32 m_cBytesReceived;
  157. };
  158. #pragma pack( pop )
  159. #endif // ISTEAMHTTP_H