From 1a6558a4f3c482a98581919106b83f071162a093 Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Sat, 27 Jan 2024 17:38:46 +0800 Subject: [PATCH] Add cookie support for UtilHttpClient. --- .../UtilHTTPClient_SteamAPI.cpp | 48 +++++++++++++++---- Plugins/SCModelDownloader/UtilHTTPClient.cpp | 3 +- include/Interface/IUtilHTTPClient.h | 18 ++++++- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/PluginLibs/UtilHTTPClient_SteamAPI/UtilHTTPClient_SteamAPI.cpp b/PluginLibs/UtilHTTPClient_SteamAPI/UtilHTTPClient_SteamAPI.cpp index 540e1792..a534e6bc 100644 --- a/PluginLibs/UtilHTTPClient_SteamAPI/UtilHTTPClient_SteamAPI.cpp +++ b/PluginLibs/UtilHTTPClient_SteamAPI/UtilHTTPClient_SteamAPI.cpp @@ -211,7 +211,8 @@ class CUtilHTTPRequest : public IUtilHTTPRequest unsigned short port, bool secure, const std::string& target, - IUtilHTTPCallbacks* callbacks) : + IUtilHTTPCallbacks* callbacks, + HTTPCookieContainerHandle hCookieHandle) : m_Callbacks(callbacks), m_pResponse(new CUtilHTTPResponse()) { @@ -230,6 +231,8 @@ class CUtilHTTPRequest : public IUtilHTTPRequest m_RequestHandle = SteamHTTP()->CreateHTTPRequest(UTIL_ConvertUtilHTTPMethodToSteamHTTPMethod(method), url.c_str()); + SteamHTTP()->SetHTTPRequestCookieContainer(m_RequestHandle, hCookieHandle); + SetField(UtilHTTPField::host, field_host.c_str()); SetField(UtilHTTPField::user_agent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"); } @@ -442,8 +445,9 @@ class CUtilHTTPSyncRequest : public CUtilHTTPRequest unsigned short port, bool secure, const std::string& target, - IUtilHTTPCallbacks* callbacks) : - CUtilHTTPRequest(method, host, port, secure, target, callbacks) + IUtilHTTPCallbacks* callbacks, + HTTPCookieContainerHandle hCookieHandle) : + CUtilHTTPRequest(method, host, port, secure, target, callbacks, hCookieHandle) { m_hResponseEvent = CreateEventA(NULL, FALSE, FALSE, NULL); } @@ -509,8 +513,9 @@ class CUtilHTTPAsyncRequest : public CUtilHTTPRequest unsigned short port, bool secure, const std::string& target, - IUtilHTTPCallbacks* callbacks) : - CUtilHTTPRequest(method, host, port, secure, target, callbacks) + IUtilHTTPCallbacks* callbacks, + HTTPCookieContainerHandle hCookieHandle) : + CUtilHTTPRequest(method, host, port, secure, target, callbacks, hCookieHandle) { } @@ -547,11 +552,21 @@ class CUtilHTTPClient : public IUtilHTTPClient std::mutex m_RequestHandleLock; UtilHTTPRequestId_t m_RequestUsedId{ UTILHTTP_REQUEST_START_ID }; std::unordered_map m_RequestPool; + HTTPCookieContainerHandle m_CookieHandle{ INVALID_HTTPCOOKIE_HANDLE }; public: CUtilHTTPClient() { - + + } + + ~CUtilHTTPClient() + { + if (m_CookieHandle) + { + SteamHTTP()->ReleaseCookieContainer(m_CookieHandle); + m_CookieHandle = INVALID_HTTPCOOKIE_HANDLE; + } } void Destroy() override @@ -559,9 +574,12 @@ class CUtilHTTPClient : public IUtilHTTPClient delete this; } - void Init() override + void Init(CUtilHTTPClientCreationContext* context) override { - + if (context->m_bUseCookieContainer) + { + m_CookieHandle = SteamHTTP()->CreateCookieContainer(context->m_bAllowResponseToModifyCookie); + } } void Shutdown() override @@ -679,12 +697,12 @@ class CUtilHTTPClient : public IUtilHTTPClient IUtilHTTPRequest* CreateSyncRequestEx(const char* host, unsigned short port_us, const char* target, bool secure, const UtilHTTPMethod method, IUtilHTTPCallbacks* callback) { - return new CUtilHTTPSyncRequest(method, host, port_us, secure, target, callback); + return new CUtilHTTPSyncRequest(method, host, port_us, secure, target, callback, m_CookieHandle); } IUtilHTTPRequest* CreateAsyncRequestEx(const char * host, unsigned short port_us, const char* target, bool secure, const UtilHTTPMethod method, IUtilHTTPCallbacks* callback) { - return new CUtilHTTPAsyncRequest(method, host, port_us, secure, target, callback); + return new CUtilHTTPAsyncRequest(method, host, port_us, secure, target, callback, m_CookieHandle); } IUtilHTTPRequest* CreateSyncRequest(const char* url, const UtilHTTPMethod method, IUtilHTTPCallbacks* callbacks) override @@ -740,6 +758,16 @@ class CUtilHTTPClient : public IUtilHTTPClient return false; } + + bool SetCookie(const char* host, const char* url, const char* cookie) override + { + if (m_CookieHandle != INVALID_HTTPCOOKIE_HANDLE) + { + return SteamHTTP()->SetCookie(m_CookieHandle, host, url, cookie); + } + + return false; + } }; EXPOSE_INTERFACE(CUtilHTTPClient, IUtilHTTPClient, UTIL_HTTPCLIENT_STEAMAPI_INTERFACE_VERSION); \ No newline at end of file diff --git a/Plugins/SCModelDownloader/UtilHTTPClient.cpp b/Plugins/SCModelDownloader/UtilHTTPClient.cpp index 274cd2c2..1000cae9 100644 --- a/Plugins/SCModelDownloader/UtilHTTPClient.cpp +++ b/Plugins/SCModelDownloader/UtilHTTPClient.cpp @@ -38,7 +38,8 @@ void UtilHTTPClient_Init() if (g_pUtilHTTPClient) { - g_pUtilHTTPClient->Init(); + CUtilHTTPClientCreationContext context; + g_pUtilHTTPClient->Init(&context); } } diff --git a/include/Interface/IUtilHTTPClient.h b/include/Interface/IUtilHTTPClient.h index bf886769..d9ebb892 100644 --- a/include/Interface/IUtilHTTPClient.h +++ b/include/Interface/IUtilHTTPClient.h @@ -121,11 +121,24 @@ class IURLParsedResult : public IBaseInterface virtual void SetSecure(bool b) = 0; }; +class CUtilHTTPClientCreationContext +{ +public: + CUtilHTTPClientCreationContext() + { + m_bUseCookieContainer = false; + m_bAllowResponseToModifyCookie = false; + } + + bool m_bUseCookieContainer; + bool m_bAllowResponseToModifyCookie{}; +}; + class IUtilHTTPClient : public IBaseInterface { public: virtual void Destroy() = 0; - virtual void Init() = 0; + virtual void Init(CUtilHTTPClientCreationContext *context) = 0; virtual void Shutdown() = 0; virtual void RunFrame() = 0; virtual bool ParseUrlEx(const char* url, IURLParsedResult* result) = 0; @@ -134,8 +147,9 @@ class IUtilHTTPClient : public IBaseInterface virtual IUtilHTTPRequest* CreateAsyncRequest(const char* url, const UtilHTTPMethod method, IUtilHTTPCallbacks* callbacks) = 0; virtual IUtilHTTPRequest* GetRequestById(UtilHTTPRequestId_t id) = 0; virtual bool DestroyRequestById(UtilHTTPRequestId_t id) = 0; + virtual bool SetCookie(const char *host, const char *url, const char* cookie) = 0; }; IUtilHTTPClient* UtilHTTPClient(); -#define UTIL_HTTPCLIENT_STEAMAPI_INTERFACE_VERSION "UtilHTTPClient_SteamAPI_001" \ No newline at end of file +#define UTIL_HTTPCLIENT_STEAMAPI_INTERFACE_VERSION "UtilHTTPClient_SteamAPI_002" \ No newline at end of file