Team Fortress 2 Source Code as on 22/4/2020
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.

228 lines
11 KiB

  1. //====== Copyright � 1996-2010, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: HTTP related enums and objects, stuff that both clients and server use should go here
  4. //
  5. //=============================================================================
  6. #ifndef HTTP_H
  7. #define HTTP_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "steam/steamhttpenums.h"
  12. #include "tier1/KeyValues.h"
  13. #include "tier1/netadr.h"
  14. #include "gcsdk/bufferpool.h"
  15. class CMsgHttpRequest;
  16. class CMsgHttpResponse;
  17. #include "tier0/memdbgon.h"
  18. namespace GCSDK
  19. {
  20. // Container class for useful parsing methods and other utility code used by client or server http code
  21. class CHTTPUtil
  22. {
  23. public:
  24. // Check if a status code allows a body to exist
  25. static bool BStatusCodeAllowsBody( EHTTPStatusCode eHTTPStatus );
  26. };
  27. class CHTTPRequest;
  28. class CHTTPResponse;
  29. class CHTTPServerClientConnection;
  30. class CHTTPRequest
  31. {
  32. public:
  33. CHTTPRequest();
  34. CHTTPRequest( CMsgHttpRequest* pProto );
  35. CHTTPRequest( EHTTPMethod eMethod, const char *pchHost, const char *pchRelativeURL );
  36. CHTTPRequest( EHTTPMethod eMethod, const char *pchAbsoluteURL );
  37. ~CHTTPRequest();
  38. // Get the method type for the request (ie, GET, POST, etc)
  39. EHTTPMethod GetEHTTPMethod() const { return ( EHTTPMethod )m_pProto->request_method(); }
  40. // Get the relative URL for the request
  41. const char *GetURL() const { return m_pProto->url().c_str(); }
  42. // Get the value of a GET parameter, using the default value if not set. This is case-insensitive by default.
  43. const CMsgHttpRequest_QueryParam *GetGETParam( const char *pchGetParamName, bool bMatchCase = false ) const;
  44. const char *GetGETParamString( const char *pchGetParamName, const char *pchDefault, bool bMatchCase = false ) const;
  45. bool GetGETParamBool( const char *pchGetParamName, bool bDefault, bool bMatchCase = false ) const;
  46. int32 GetGETParamInt32( const char *pchGetParamName, int32 nDefault, bool bMatchCase = false ) const;
  47. uint32 GetGETParamUInt32( const char *pchGetParamName, uint32 unDefault, bool bMatchCase = false ) const;
  48. int64 GetGETParamInt64( const char *pchGetParamName, int64 nDefault, bool bMatchCase = false ) const;
  49. uint64 GetGETParamUInt64( const char *pchGetParamName, uint64 unDefault, bool bMatchCase = false ) const;
  50. float GetGETParamFloat( const char *pchGetParamName, float fDefault, bool bMatchCase = false ) const;
  51. // Get the value of a POST parameter, using the default value if not set. This is case-insensitive by default.
  52. const CMsgHttpRequest_QueryParam *GetPOSTParam( const char *pchPostParamName, bool bMatchCase = false ) const;
  53. const char *GetPOSTParamString( const char *pchGetParamName, const char *pchDefault, bool bMatchCase = false ) const;
  54. bool GetPOSTParamBool( const char *pchGetParamName, bool bDefault, bool bMatchCase = false ) const;
  55. int32 GetPOSTParamInt32( const char *pchGetParamName, int32 nDefault, bool bMatchCase = false ) const;
  56. uint32 GetPOSTParamUInt32( const char *pchGetParamName, uint32 unDefault, bool bMatchCase = false ) const;
  57. int64 GetPOSTParamInt64( const char *pchGetParamName, int64 nDefault, bool bMatchCase = false ) const;
  58. uint64 GetPOSTParamUInt64( const char *pchGetParamName, uint64 unDefault, bool bMatchCase = false ) const;
  59. float GetPOSTParamFloat( const char *pchGetParamName, float fDefault, bool bMatchCase = false ) const;
  60. // Add a GET param to the request
  61. void SetGETParamString( const char *pchGetParamName, const char *pString ) { SetGETParamRaw( pchGetParamName, (uint8*)pString, Q_strlen(pString) ); }
  62. void SetGETParamBool( const char *pchPostParamName, bool bValue ) { SetGETParamRaw( pchPostParamName, (uint8*)(bValue ? "1" : "0"), 1 ); }
  63. void SetGETParamInt32( const char *pchPostParamName, int32 nValue ) { CNumStr str( nValue ); SetGETParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  64. void SetGETParamUInt32( const char *pchPostParamName, uint32 unValue ) { CNumStr str( unValue ); SetGETParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  65. void SetGETParamInt64( const char *pchPostParamName, int64 nValue ) { CNumStr str( nValue ); SetGETParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  66. void SetGETParamUInt64( const char *pchPostParamName, uint64 unValue ) { CNumStr str( unValue ); SetGETParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  67. void SetGETParamFloat( const char *pchPostParamName, float fValue ) { CNumStr str( fValue ); SetGETParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  68. // Add a POST param to the request given a string for the name and value
  69. void SetPOSTParamString( const char *pchPostParamName, const char *pString ) { SetPOSTParamRaw( pchPostParamName, (uint8*)pString, Q_strlen(pString) ); }
  70. void SetPOSTParamBool( const char *pchPostParamName, bool bValue ) { SetPOSTParamRaw( pchPostParamName, (uint8*)(bValue ? "1" : "0"), 1 ); }
  71. void SetPOSTParamInt32( const char *pchPostParamName, int32 nValue ) { CNumStr str( nValue ); SetPOSTParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  72. void SetPOSTParamUInt32( const char *pchPostParamName, uint32 unValue ) { CNumStr str( unValue ); SetPOSTParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  73. void SetPOSTParamInt64( const char *pchPostParamName, int64 nValue ) { CNumStr str( nValue ); SetPOSTParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  74. void SetPOSTParamUInt64( const char *pchPostParamName, uint64 unValue ) { CNumStr str( unValue ); SetPOSTParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  75. void SetPOSTParamFloat( const char *pchPostParamName, float fValue ) { CNumStr str( fValue ); SetPOSTParamRaw( pchPostParamName, (uint8*)str.String(), Q_strlen( str ) ); }
  76. // Adds a POST param containing raw data to the request. If you are using the Web API, you probably do not want this function
  77. void SetPOSTParamRaw( const char *pchPostParamName, uint8 *pData, uint32 cubDataLen );
  78. // Raw iteration support for GET and POST parameters
  79. uint32 GetPOSTParamCount() const { return m_pProto->post_params_size(); }
  80. const char* GetPOSTParamName( uint32 i ) const { return m_pProto->post_params( i ).name().c_str(); }
  81. const char* GetPOSTParamValue( uint32 i ) const { return m_pProto->post_params( i ).value().c_str(); }
  82. uint32 GetGETParamCount() const { return m_pProto->get_params_size(); }
  83. const char* GetGETParamName( uint32 i ) const { return m_pProto->get_params( i ).name().c_str(); }
  84. const char* GetGETParamValue( uint32 i ) const { return m_pProto->get_params( i ).value().c_str(); }
  85. // Fetch a request header by header name and convert it to a time value
  86. RTime32 GetRequestHeaderTimeValue( const char *pchRequestHeaderName, RTime32 rtDefault = 0 ) const;
  87. // Fetch a request headers value by header name
  88. const char *GetRequestHeaderValue( const char *pchRequestHeaderName, const char *pchDefault = NULL ) const;
  89. // Set a header field for the request
  90. void SetRequestHeaderValue( const char *pchHeaderName, const char *pchHeaderString );
  91. // Set the method for the request object
  92. void SetEHTTPMethod( EHTTPMethod eMethod ) { m_pProto->set_request_method( eMethod ); }
  93. // Set the relative URL for the request
  94. void SetURL( const char *pchURL )
  95. {
  96. AssertMsg( pchURL && pchURL[0] == '/', "URLs must start with the slash (/) character. Param: %s", pchURL );
  97. m_pProto->set_url( pchURL );
  98. }
  99. void SetURLDirect( const char *pchURL, size_t size )
  100. {
  101. AssertMsg( pchURL && pchURL[0] == '/', "URLs must start with the slash (/) character. Param: %s", pchURL );
  102. m_pProto->set_url( pchURL, size );
  103. }
  104. // Set body data
  105. void SetBodyData( const void *pubData, uint32 cubData );
  106. bool BHasBodyData() { return m_pProto->has_body(); }
  107. uint GetBodyDataSize() const { return (uint)m_pProto->body().size(); }
  108. const char* GetBodyData() const { return m_pProto->body().c_str(); }
  109. // Set hostname for request to target (or host it was received on)
  110. void SetHostname( const char *pchHost ) { m_pProto->set_hostname( pchHost ); }
  111. void SetHostnameDirect( const char *pchHost, uint32 unLength ) { m_pProto->set_hostname( pchHost, unLength ); }
  112. const char *GetHostname() const { return m_pProto->hostname().c_str(); }
  113. // Set the overall timeout in seconds for this request
  114. void SetAbsoluteTimeoutSeconds( uint32 unSeconds ) { m_pProto->set_absolute_timeout( unSeconds ); }
  115. // Get the total absolute timeout value for the request
  116. uint32 GetAbsoluteTimeoutSeconds() { return m_pProto->absolute_timeout(); }
  117. //accesses the HTTP message
  118. const CMsgHttpRequest& GetProtoObj() const { return *m_pProto; }
  119. static void RetrieveURLEncodedData( const char *pchQueryString, int nQueryStrLen, CUtlVector<CMsgHttpRequest_QueryParam> &vecParams );
  120. protected:
  121. // Adds a GET param containing raw data to the request. If you are using the Web API, you probably do not want this function
  122. void SetGETParamRaw( const char *pchGetParamName, uint8 *pData, uint32 cubDataLen );
  123. // Common initialization code
  124. void Init( CMsgHttpRequest* pProto );
  125. //the protobuf object that stores all of our contents
  126. CMsgHttpRequest* m_pProto;
  127. //whether or not we own this proto and should free it when the object is destroyed
  128. bool m_bOwnProto;
  129. };
  130. //-----------------------------------------------------------------------------
  131. // Purpose: A wrapper to CHTTPRequest for Steam WebAPIs. Host, mode, and API key
  132. // are set automatically
  133. //-----------------------------------------------------------------------------
  134. class CSteamAPIRequest : public CHTTPRequest
  135. {
  136. public:
  137. CSteamAPIRequest( EHTTPMethod eMethod, const char *pchInterface, const char *pchMethod, int nVersion );
  138. };
  139. class CHTTPResponse
  140. {
  141. public:
  142. CHTTPResponse();
  143. virtual ~CHTTPResponse();
  144. // Get a specific headers data
  145. const char *GetResponseHeaderValue( const char *pchName, const char *pchDefault = NULL ) const { return m_pkvResponseHeaders->GetString( pchName, pchDefault ); }
  146. // Set a specific headers data, will clobber any existing value for that header
  147. virtual void SetResponseHeaderValue( const char *pchName, const char *pchValue ) { m_pkvResponseHeaders->SetString( pchName, pchValue ); }
  148. // Set status code for response
  149. virtual void SetStatusCode( EHTTPStatusCode eStatusCode ) { m_eStatusCode = eStatusCode; }
  150. // Set the expiration header based on a time delta from now
  151. void SetExpirationHeaderDeltaFromNow( int32 nSecondsFromNow );
  152. // Set the expiration header based on a time delta from now
  153. void SetHeaderTimeValue( const char *pchHeaderName, RTime32 rtTimestamp );
  154. // Get the entire headers KV (in case you want to iterate them all or such)
  155. KeyValues *GetResponseHeadersKV() const { return m_pkvResponseHeaders; }
  156. // Accessors to the body data in the response
  157. const uint8 *GetPubBody() const { return (uint8*)m_pbufBody->Base(); }
  158. uint32 GetCubBody() const { return m_pbufBody->TellPut(); }
  159. const CUtlBuffer *GetBodyBuffer() const { return m_pbufBody; }
  160. CUtlBuffer *GetBodyBuffer() { return m_pbufBody; }
  161. // Accessor
  162. EHTTPStatusCode GetStatusCode() const { return m_eStatusCode; }
  163. // writes the response into a protobuf for use in messages
  164. void SerializeIntoProtoBuf( CMsgHttpResponse & response ) const;
  165. // reads the response out of a protobuf from a message
  166. void DeserializeFromProtoBuf( const CMsgHttpResponse & response );
  167. protected:
  168. static CBufferPool &GetBufferPool();
  169. EHTTPStatusCode m_eStatusCode;
  170. CUtlBuffer *m_pbufBody;
  171. KeyValues *m_pkvResponseHeaders;
  172. };
  173. }
  174. #include "tier0/memdbgoff.h"
  175. #endif // HTTP_H