Skip to content

Commit

Permalink
update for cookie jar
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr.Abc committed Feb 11, 2025
1 parent 7ac37de commit ef4b6fa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Header/Utility/httpclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CHttpCookieJar {
void Save();
std::string Get();
void Set(const char* cookie);
void Set(const std::string& cookie);
size_t Size();
private:
std::string m_szPath;
Expand Down
44 changes: 31 additions & 13 deletions src/Source/Utility/httpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "httpclient.h"
#include <Controls.h>

#define MAX_COOKIE_LENGTH 2048

static HINTERFACEMODULE g_hUtilHTTPClient;
static IUtilHTTPClient* g_pUtilHTTPClient;
static CHttpClient g_pHttpClient;
Expand Down Expand Up @@ -111,28 +109,37 @@ CHttpClientItem::CHttpClientItem(httpContext_s* ctx) : IUtilHTTPCallbacks(){
else
m_pRequest = g_pUtilHTTPClient->CreateSyncRequest(m_hContext.url.c_str(), m_hContext.method, this);
if (m_pCookieJar)
m_pRequest->SetField("Set-Cookie", m_pCookieJar->Get().c_str());
m_pRequest->SetField("Cookie", m_pCookieJar->Get().c_str());
m_pRequest->SetAutoDestroyOnFinish(true);
}
CHttpClientItem* CHttpClientItem::Start(){
if (!m_pRequest)
return nullptr;
if (m_bAsync) {
if (m_pCookieJar)
m_pRequest->SetField("Cookie", m_pCookieJar->Get().c_str());
m_pRequest->Send();
return this;
}
return nullptr;
}
IUtilHTTPResponse* CHttpClientItem::StartSync(){
if (!m_bAsync) {
if (m_pCookieJar)
m_pRequest->SetField("Cookie", m_pCookieJar->Get().c_str());
m_pRequest->Send();
m_pRequest->WaitForComplete();
auto reb = m_pRequest->GetResponse();
if (m_pCookieJar) {
char cookiebuf[MAX_COOKIE_LENGTH];
memset(cookiebuf, 0, MAX_COOKIE_LENGTH);
if(reb->GetHeader("Set-Cookie", cookiebuf, MAX_COOKIE_LENGTH))
m_pCookieJar->Set(cookiebuf);
std::string cookiebuf;
size_t bufsize = 0;
if (reb->GetHeaderSize("Set-Cookie", &bufsize)) {
cookiebuf.resize(bufsize);
if (reb->GetHeader("Set-Cookie", cookiebuf.data(), bufsize)) {
m_pCookieJar->Set(cookiebuf);
m_pCookieJar->Save();
}
}
}
return reb;
}
Expand Down Expand Up @@ -190,9 +197,15 @@ void CHttpClientItem::OnResponseComplete(IUtilHTTPRequest* RequestInstance, IUti
if (m_pOnResponse) {
std::invoke(m_pOnResponse, ResponseInstance);
if (m_pCookieJar) {
char cookiebuf[MAX_COOKIE_LENGTH];
ResponseInstance->GetHeader("Set-Cookie", cookiebuf, MAX_COOKIE_LENGTH);
m_pCookieJar->Set(cookiebuf);
std::string cookiebuf;
size_t bufsize = 0;
if (ResponseInstance->GetHeaderSize("Set-Cookie", &bufsize)) {
cookiebuf.resize(bufsize);
if (ResponseInstance->GetHeader("Set-Cookie", cookiebuf.data(), bufsize)) {
m_pCookieJar->Set(cookiebuf);
m_pCookieJar->Save();
}
}
}
}
}
Expand Down Expand Up @@ -234,10 +247,11 @@ CHttpCookieJar::CHttpCookieJar(const char* path){
void CHttpCookieJar::Load(const char* path){
FileHandle_t file = vgui::filesystem()->Open(path, "r");
if (file) {
char buffer[MAX_COOKIE_LENGTH];
vgui::filesystem()->Read(buffer, MAX_COOKIE_LENGTH, file);
int filesize = vgui::filesystem()->Size(file);
std::string buffer;
buffer.resize(filesize);
vgui::filesystem()->Read(buffer.data(), filesize, file);
m_szCookie = buffer;
m_szCookie += '\0';
m_szPath = path;
}
vgui::filesystem()->Close(file);
Expand All @@ -257,6 +271,10 @@ void CHttpCookieJar::Set(const char* cookie){
m_szCookie = cookie;
}

void CHttpCookieJar::Set(const std::string& cookie){
m_szCookie = cookie;
}

size_t CHttpCookieJar::Size(){
return m_szCookie.size();
}

0 comments on commit ef4b6fa

Please sign in to comment.