Skip to content

Commit

Permalink
Add cookie support for UtilHttpClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
hzqst committed Jan 27, 2024
1 parent 6cb8574 commit 1a6558a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
48 changes: 38 additions & 10 deletions PluginLibs/UtilHTTPClient_SteamAPI/UtilHTTPClient_SteamAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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");
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
{

}
Expand Down Expand Up @@ -547,21 +552,34 @@ class CUtilHTTPClient : public IUtilHTTPClient
std::mutex m_RequestHandleLock;
UtilHTTPRequestId_t m_RequestUsedId{ UTILHTTP_REQUEST_START_ID };
std::unordered_map<UtilHTTPRequestId_t, IUtilHTTPRequest*> 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
{
delete this;
}

void Init() override
void Init(CUtilHTTPClientCreationContext* context) override
{

if (context->m_bUseCookieContainer)
{
m_CookieHandle = SteamHTTP()->CreateCookieContainer(context->m_bAllowResponseToModifyCookie);
}
}

void Shutdown() override
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
3 changes: 2 additions & 1 deletion Plugins/SCModelDownloader/UtilHTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ void UtilHTTPClient_Init()

if (g_pUtilHTTPClient)
{
g_pUtilHTTPClient->Init();
CUtilHTTPClientCreationContext context;
g_pUtilHTTPClient->Init(&context);
}
}

Expand Down
18 changes: 16 additions & 2 deletions include/Interface/IUtilHTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"
#define UTIL_HTTPCLIENT_STEAMAPI_INTERFACE_VERSION "UtilHTTPClient_SteamAPI_002"

0 comments on commit 1a6558a

Please sign in to comment.