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.

210 lines
7.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "stdafx.h"
  3. #include "enumutils.h"
  4. // memdbgon must be the last include file in a .cpp file!!!
  5. #include "tier0/memdbgon.h"
  6. namespace GCSDK
  7. {
  8. ENUMSTRINGS_START( EGCWebApiPrivilege )
  9. { k_EGCWebApiPriv_None, "None" },
  10. { k_EGCWebApiPriv_Account, "Account" },
  11. { k_EGCWebApiPriv_Approved, "Approved" },
  12. { k_EGCWebApiPriv_Session, "Session" },
  13. { k_EGCWebApiPriv_Support, "Support" },
  14. { k_EGCWebApiPriv_Admin, "Admin" },
  15. { k_EGCWebApiPriv_EditApp, "EditApp" },
  16. { k_EGCWebApiPriv_MemberPublisher, "MemberPublisher" },
  17. { k_EGCWebApiPriv_EditPublisher, "EditPublisher" },
  18. { k_EGCWebApiPriv_AccountOptional, "AccountOptional" },
  19. ENUMSTRINGS_END( EGCWebApiPrivilege );
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Singleton accessor to registered WG jobs
  22. //-----------------------------------------------------------------------------
  23. CUtlDict< const JobCreationFunc_t* > &GMapGCWGJobCreationFuncs()
  24. {
  25. static CUtlDict< const JobCreationFunc_t* > s_MapWGJobCreationFuncs;
  26. return s_MapWGJobCreationFuncs;
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Purpose: Singleton accessor to registered WG jobs
  30. //-----------------------------------------------------------------------------
  31. CUtlDict< const WebApiFunc_t* > &GMapGCWGRequestInfo()
  32. {
  33. static CUtlDict< const WebApiFunc_t* > s_MapWGRequestInfo;
  34. return s_MapWGRequestInfo;
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose: Constructor
  38. //-----------------------------------------------------------------------------
  39. CGCWGJobMgr::CGCWGJobMgr( )
  40. {
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: Destructor
  44. //-----------------------------------------------------------------------------
  45. CGCWGJobMgr::~CGCWGJobMgr()
  46. {
  47. }
  48. //-----------------------------------------------------------------------------
  49. // Purpose: Hub has receieved a k_EMsgWGRequest message, find & dispatch WG Job
  50. //-----------------------------------------------------------------------------
  51. bool CGCWGJobMgr::BHandleMsg( IMsgNetPacket *pNetPacket )
  52. {
  53. CGCMsg<MsgGCWGRequest_t> msg( pNetPacket );
  54. AssertMsg( msg.GetEMsg() == k_EGCMsgWGRequest, "GCWGJobMgr asked to route a message, but it is not a MsgWGRequest" );
  55. CUtlString strRequestName;
  56. msg.BReadStr( &strRequestName );
  57. int iJobType = GMapGCWGRequestInfo().Find( strRequestName.Get() );
  58. if ( !GMapGCWGRequestInfo().IsValidIndex( iJobType ) )
  59. {
  60. EmitError( SPEW_GC, "Unable to find GC WG handler %s", strRequestName.Get() );
  61. SendErrorMessage( msg, "Unknown method", k_EResultInvalidParam );
  62. return false;
  63. }
  64. const WebApiFunc_t * pFunc = GMapGCWGRequestInfo()[iJobType];
  65. if( !BVerifyPrivileges( msg, pFunc ) )
  66. return false;
  67. CGCWGJob *job = (CGCWGJob *)(*((GMapGCWGJobCreationFuncs())[iJobType]))( GGCBase(), NULL );
  68. Assert( job );
  69. job->SetWebApiFunc( pFunc );
  70. job->StartJobFromNetworkMsg( pNetPacket, msg.Hdr().m_JobIDSource );
  71. return true;
  72. }
  73. bool CGCWGJobMgr::BVerifyPrivileges( const CGCMsg<MsgGCWGRequest_t> & msg, const WebApiFunc_t * pFunc )
  74. {
  75. if(msg.Body().m_unPrivilege != (uint32)pFunc->m_eRequiredPrivilege )
  76. {
  77. SendErrorMessage( msg,
  78. CFmtStr( "Privilege mismatch in %s gc call. Expected: %s, actual: %s", pFunc->m_pchRequestName, PchNameFromEGCWebApiPrivilege( pFunc->m_eRequiredPrivilege ), PchNameFromEGCWebApiPrivilege( (EGCWebApiPrivilege)msg.Body().m_unPrivilege ) ),
  79. k_EResultAccessDenied );
  80. return false;
  81. }
  82. else
  83. {
  84. return true;
  85. }
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose: Sends an error message in response to a WG request
  89. // Inputs: msg - The message we're responding to. This causes the error to
  90. // be routed to the right place and eventually back to the right
  91. // browser.
  92. // pchErrorMsg - The message to display
  93. // nResult - The error code to return
  94. //-----------------------------------------------------------------------------
  95. void CGCWGJobMgr::SendErrorMessage( const CGCMsg<MsgGCWGRequest_t> & msg, const char *pchErrorMsg, int32 nResult )
  96. {
  97. KeyValuesAD pkvErr( "error" );
  98. pkvErr->SetString( "error", pchErrorMsg );
  99. pkvErr->SetInt( "success", nResult );
  100. SendResponse( msg, pkvErr, false );
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose: Sets an error message in a response to a WG request
  104. // Inputs: pkvResponse - The response KV block to set the error in
  105. // pchErrorMsg - The message to display
  106. // nResult - The error code to return
  107. //-----------------------------------------------------------------------------
  108. void CGCWGJobMgr::SetErrorMessage( KeyValues *pkvErr, const char *pchErrorMsg, int32 nResult )
  109. {
  110. pkvErr->SetString( "error", pchErrorMsg );
  111. pkvErr->SetInt( "success", nResult );
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose: Sends a message in response to a WG request
  115. // Inputs: msg - The message we're responding to. This causes the error to
  116. // be routed to the right place and eventually back to the right
  117. // browser.
  118. // pkvResponse - The KeyValues containing the response data
  119. //-----------------------------------------------------------------------------
  120. void CGCWGJobMgr::SendResponse( const CGCMsg<MsgGCWGRequest_t> & msg, KeyValues *pkvResponse, bool bResult )
  121. {
  122. //prepare response msg
  123. CGCMsg<MsgGCWGResponse_t> msgResponse( k_EGCMsgWGResponse, msg );
  124. CUtlBuffer bufResponse;
  125. KVPacker packer;
  126. if ( packer.WriteAsBinary( pkvResponse, bufResponse ) )
  127. {
  128. msgResponse.AddVariableLenData( bufResponse.Base(), bufResponse.TellPut() );
  129. msgResponse.Body().m_cubKeyValues = bufResponse.TellPut();
  130. msgResponse.Body().m_bResult = bResult;
  131. }
  132. else
  133. {
  134. msgResponse.Body().m_cubKeyValues = 0;
  135. AssertMsg( false, "Failed to serialize WG response" );
  136. }
  137. msgResponse.Hdr().m_JobIDTarget = msg.Hdr().m_JobIDSource;
  138. GGCBase()->BSendSystemMessage( msgResponse );
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose: Adds a WG job handler to the global list
  142. //-----------------------------------------------------------------------------
  143. void CGCWGJobMgr::RegisterWGJob( const WebApiFunc_t *pWGJobType, const JobType_t *pJobCreationFunc )
  144. {
  145. const char *pchRequestName = pWGJobType->m_pchRequestName;
  146. GMapGCWGJobCreationFuncs().Insert( pchRequestName, &(pJobCreationFunc->m_pJobFactory) );
  147. GMapGCWGRequestInfo().Insert( pchRequestName, pWGJobType );
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Purpose: Get the list of registered WG jobs
  151. //-----------------------------------------------------------------------------
  152. CUtlDict< const WebApiFunc_t* > &CGCWGJobMgr::GetWGRequestMap()
  153. {
  154. return GMapGCWGRequestInfo();
  155. }
  156. #ifdef DBGFLAG_VALIDATE
  157. void CGCWGJobMgr::Validate( CValidator &validator, const char *pchName )
  158. {
  159. }
  160. void CGCWGJobMgr::ValidateStatics( CValidator &validator )
  161. {
  162. ValidateObj( GMapGCWGJobCreationFuncs() );
  163. ValidateObj( GMapGCWGRequestInfo() );
  164. }
  165. #endif // DBGFLAG_VALIDATE
  166. } // namespace GCSDK