Skip to content

Commit 0c035f0

Browse files
committed
update(HTTP): cURL -> SteamHTTP
1 parent c458fa6 commit 0c035f0

34 files changed

+227
-11400
lines changed

.vscode/c_cpp_properties.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"${workspaceFolder}/vendor/msgpack/include/**",
1616
"${workspaceFolder}/vendor/funchook/include/**",
1717
"${workspaceFolder}/vendor/texttable/**",
18-
"${workspaceFolder}/vendor/curl/win64/include/**",
1918
"${workspaceFolder}/vendor/mysql/win64/include/**",
2019
"${workspaceFolder}/vendor/asmjit/src/**",
2120
"${workspaceFolder}/vendor/dynohook/win64/include/**",
@@ -29,7 +28,6 @@
2928
"WIN32",
3029
"COMPILER_GCC",
3130
"CPPHTTPLIB_OPENSSL_SUPPORT",
32-
"CURL_STATICLIB",
3331
"MSGPACK_NO_BOOST"
3432
],
3533
"cStandard": "c17",
@@ -51,7 +49,6 @@
5149
"${workspaceFolder}/vendor/msgpack/include/**",
5250
"${workspaceFolder}/vendor/funchook/include/**",
5351
"${workspaceFolder}/vendor/texttable/**",
54-
"${workspaceFolder}/vendor/curl/linuxsteamrt64/include/**",
5552
"${workspaceFolder}/vendor/mysql/linuxsteamrt64/include/**",
5653
"${workspaceFolder}/vendor/asmjit/src/**",
5754
"${workspaceFolder}/vendor/dynohook/linuxsteamrt64/include/**",
@@ -64,8 +61,7 @@
6461
"_UNICODE",
6562
"POSIX",
6663
"COMPILER_GCC",
67-
"CPPHTTPLIB_OPENSSL_SUPPORT",
68-
"CURL_STATICLIB"
64+
"CPPHTTPLIB_OPENSSL_SUPPORT"
6965
],
7066
"cStandard": "c17",
7167
"cppStandard": "c++17",

AMBuilder

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ for sdk_target in MMSPlugin.sdk_targets:
6666
binary.compiler.postlink += [
6767
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'libfunchook.a'),
6868
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'libdistorm.a'),
69-
os.path.join(builder.sourcePath, 'vendor', 'curl', 'linuxsteamrt64', 'lib', 'libcurl.a'),
7069
os.path.join(builder.sourcePath, 'vendor', 'mysql', 'linuxsteamrt64', 'lib', 'libmysqlclient.a'),
7170
os.path.join(builder.sourcePath, 'vendor', 'dynohook', 'linuxsteamrt64', 'lib', 'libdynohook.a'),
7271
os.path.join(builder.sourcePath, 'vendor', 'zydis', 'linuxsteamrt64', 'libZydis.a'),
@@ -117,7 +116,6 @@ for sdk_target in MMSPlugin.sdk_targets:
117116
binary.compiler.postlink += [
118117
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'funchook.lib'),
119118
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'distorm.lib'),
120-
os.path.join(builder.sourcePath, 'vendor', 'curl', 'win64', 'lib', 'libcurl_a.lib'),
121119
os.path.join(builder.sourcePath, 'vendor', 'mysql', 'win64', 'lib', 'mysqlclient.lib'),
122120
os.path.join(builder.sourcePath, 'vendor', 'dynohook', 'win64', 'lib', 'dynohook.lib'),
123121
os.path.join(builder.sourcePath, 'vendor', 'zydis', 'win64', 'Zydis.lib'),
@@ -172,7 +170,6 @@ for sdk_target in MMSPlugin.sdk_targets:
172170
"GITHUB_SHA=\"" + (os.getenv('GITHUB_SHA') == None and "LOCAL" or os.getenv('GITHUB_SHA')) + "\"",
173171
"HAVE_STRUCT_TIMESPEC",
174172
"BUILDING",
175-
"CURL_STATICLIB",
176173
"MSGPACK_NO_BOOST",
177174
]
178175

@@ -195,7 +192,6 @@ for sdk_target in MMSPlugin.sdk_targets:
195192
os.path.join(builder.sourcePath, 'vendor', 'json', 'include'),
196193
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'include'),
197194
os.path.join(builder.sourcePath, 'vendor', 'texttable'),
198-
os.path.join(builder.sourcePath, 'vendor', 'curl', ('win64' if binary.compiler.target.platform == 'windows' else 'linuxsteamrt64'), 'include'),
199195
os.path.join(builder.sourcePath, 'vendor', 'mysql', ('win64' if binary.compiler.target.platform == 'windows' else 'linuxsteamrt64'), 'include'),
200196
os.path.join(builder.sourcePath, 'vendor', 'asmjit', 'src'),
201197
os.path.join(builder.sourcePath, 'vendor', 'dynohook', ('win64' if binary.compiler.target.platform == 'windows' else 'linuxsteamrt64'), 'include'),

src/entrypoint.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "addons/addons.h"
1212
#include "encoders/msgpack.h"
1313
#include "addons/clients.h"
14+
#include "http/HTTPManager.h"
1415
#include "configuration/Configuration.h"
1516
#include "commands/CommandsManager.h"
1617
#include "crashreporter/CallStack.h"
@@ -41,6 +42,7 @@ SH_DECL_HOOK6_void(IServerGameClients, OnClientConnected, SH_NOATTRIB, 0, CPlaye
4142
SH_DECL_HOOK6(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, CPlayerSlot, const char*, uint64, const char*, bool, CBufferString*);
4243
SH_DECL_HOOK3_void(ICvar, DispatchConCommand, SH_NOATTRIB, 0, ConCommandHandle, const CCommandContext&, const CCommand&);
4344
SH_DECL_HOOK0_void(IServerGameDLL, GameServerSteamAPIActivated, SH_NOATTRIB, 0);
45+
SH_DECL_HOOK0_void(IServerGameDLL, GameServerSteamAPIDeactivated, SH_NOATTRIB, 0);
4446
SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, CPlayerSlot, const CCommand&);
4547

4648
#ifdef _WIN32
@@ -69,6 +71,7 @@ IGameEventManager2* g_gameEventManager = nullptr;
6971
IGameEventSystem* g_pGameEventSystem = nullptr;
7072
CEntityListener g_entityListener;
7173
CSteamGameServerAPIContext g_SteamAPI;
74+
ISteamHTTP* g_http = nullptr;
7275
CSchemaSystem* g_pSchemaSystem2 = nullptr;
7376
CCSGameRules* gameRules = nullptr;
7477

@@ -109,6 +112,7 @@ ResourceMonitor* g_ResourceMonitor = nullptr;
109112
Patches* g_Patches = nullptr;
110113
CallStack* g_callStack = nullptr;
111114
EventManager* eventManager = nullptr;
115+
HTTPManager* g_httpManager = nullptr;
112116
VoiceManager g_voiceManager;
113117

114118
//////////////////////////////////////////////////////////////
@@ -143,6 +147,7 @@ bool Swiftly::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool
143147
SH_ADD_HOOK_MEMFUNC(INetworkServerService, StartupServer, g_pNetworkServerService, this, &Swiftly::Hook_StartupServer, true);
144148
SH_ADD_HOOK_MEMFUNC(ICvar, DispatchConCommand, icvar, this, &Swiftly::Hook_DispatchConCommand, false);
145149
SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameServerSteamAPIActivated, server, this, &Swiftly::Hook_GameServerSteamAPIActivated, false);
150+
SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameServerSteamAPIDeactivated, server, this, &Swiftly::Hook_GameServerSteamAPIDeactivated, false);
146151

147152
g_pCVar = icvar;
148153

@@ -151,6 +156,7 @@ bool Swiftly::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool
151156
if (!BeginCrashListener())
152157
PRINTRET("Crash Reporter failed to initialize.\n", false);
153158

159+
g_httpManager = new HTTPManager();
154160
g_pluginManager = new PluginManager();
155161
g_Config = new Configuration();
156162
g_conFilter = new ConsoleFilter();
@@ -205,6 +211,8 @@ bool Swiftly::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool
205211
if (late)
206212
{
207213
g_SteamAPI.Init();
214+
g_http = g_SteamAPI.SteamHTTP();
215+
g_httpManager->ProcessPendingHTTPRequests();
208216
m_CallbackDownloadItemResult.Register(this, &Swiftly::OnAddonDownloaded);
209217
}
210218

@@ -223,6 +231,8 @@ void Swiftly::Hook_GameServerSteamAPIActivated()
223231
return;
224232

225233
g_SteamAPI.Init();
234+
g_http = g_SteamAPI.SteamHTTP();
235+
g_httpManager->ProcessPendingHTTPRequests();
226236
m_CallbackDownloadItemResult.Register(this, &Swiftly::OnAddonDownloaded);
227237

228238
std::thread([&]() -> void
@@ -236,6 +246,13 @@ void Swiftly::Hook_GameServerSteamAPIActivated()
236246
RETURN_META(MRES_IGNORED);
237247
}
238248

249+
void Swiftly::Hook_GameServerSteamAPIDeactivated()
250+
{
251+
g_http = nullptr;
252+
253+
RETURN_META(MRES_IGNORED);
254+
}
255+
239256
void Swiftly::OnAddonDownloaded(DownloadItemResult_t* pResult)
240257
{
241258
g_addons.OnAddonDownloaded(pResult);
@@ -263,6 +280,7 @@ bool Swiftly::Unload(char* error, size_t maxlen)
263280
SH_REMOVE_HOOK_MEMFUNC(INetworkServerService, StartupServer, g_pNetworkServerService, this, &Swiftly::Hook_StartupServer, true);
264281
SH_REMOVE_HOOK_MEMFUNC(ICvar, DispatchConCommand, icvar, this, &Swiftly::Hook_DispatchConCommand, false);
265282
SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameServerSteamAPIActivated, server, this, &Swiftly::Hook_GameServerSteamAPIActivated, false);
283+
SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameServerSteamAPIDeactivated, server, this, &Swiftly::Hook_GameServerSteamAPIDeactivated, false);
266284

267285
g_pGameEntitySystem->RemoveListenerEntity(&g_entityListener);
268286

src/entrypoint.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Swiftly : public ISmmPlugin, public IMetamodListener
6565
bool Hook_ClientConnect(CPlayerSlot slot, const char* pszName, uint64 xuid, const char* pszNetworkID, bool unk1, CBufferString* pRejectReason);
6666
void Hook_DispatchConCommand(ConCommandHandle cmd, const CCommandContext& ctx, const CCommand& args);
6767
void Hook_GameServerSteamAPIActivated();
68+
void Hook_GameServerSteamAPIDeactivated();
6869
void Hook_OnClientCommand(CPlayerSlot slot, const CCommand& args);
6970

7071
public:

src/http/HTTPManager.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "HTTPManager.h"
2+
#include "../plugins/core/scripting.h"
3+
4+
extern ISteamHTTP* g_http;
5+
6+
TrackedRequest::TrackedRequest(HTTPRequestHandle hndl, SteamAPICall_t hCall, std::string requestID, RequestCB callback)
7+
{
8+
m_hHTTPReq = hndl;
9+
m_CallResult.SetGameserverFlag();
10+
m_CallResult.Set(hCall, this, &TrackedRequest::OnHTTPRequestCompleted);
11+
12+
m_callback = callback;
13+
m_requestID = requestID;
14+
15+
g_httpManager->m_PendingRequests.push_back(this);
16+
}
17+
18+
TrackedRequest::~TrackedRequest()
19+
{
20+
for (auto e = g_httpManager->m_PendingRequests.begin(); e != g_httpManager->m_PendingRequests.end(); ++e)
21+
{
22+
if (*e == this)
23+
{
24+
g_httpManager->m_PendingRequests.erase(e);
25+
break;
26+
}
27+
}
28+
}
29+
30+
void TrackedRequest::OnHTTPRequestCompleted(HTTPRequestCompleted_t* arg, bool bFailed)
31+
{
32+
int status = (bFailed ? 0 : arg->m_eStatusCode);
33+
std::string body = "";
34+
std::string error = "";
35+
uint8* response = nullptr;
36+
37+
if (bFailed) error = "Failed to create request.";
38+
else if (status < 200 || status > 299) error = std::to_string(arg->m_eStatusCode);
39+
else error = "Success (no errors)";
40+
41+
if (status >= 200 && status <= 299) {
42+
uint32_t bodySize;
43+
g_http->GetHTTPResponseBodySize(arg->m_hRequest, &bodySize);
44+
45+
response = new uint8[bodySize + 1];
46+
g_http->GetHTTPResponseBodyData(arg->m_hRequest, response, bodySize);
47+
response[bodySize] = 0;
48+
49+
body = (char*)response;
50+
}
51+
52+
this->m_callback(arg->m_hRequest, status, body, "[]", error, this->m_requestID);
53+
54+
if (response != nullptr) delete[] response;
55+
if (g_http) g_http->ReleaseHTTPRequest(arg->m_hRequest);
56+
57+
delete this;
58+
}
59+
60+
void HTTPManager::ProcessPendingHTTPRequests()
61+
{
62+
for (auto it = m_PendingHTTPRequests.begin(); it != m_PendingHTTPRequests.end(); ++it)
63+
{
64+
if (it->second.size() == 0) continue;
65+
66+
PluginHTTP h(it->first);
67+
for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2)
68+
h.PerformHTTPWithRequestID(it2->second, it2->first);
69+
}
70+
71+
m_PendingHTTPRequests.clear();
72+
}

src/http/HTTPManager.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef _http_manager_h
2+
#define _http_manager_h
3+
4+
#include <steam/steam_gameserver.h>
5+
#include <vector>
6+
#include <map>
7+
#include <string>
8+
#include <functional>
9+
10+
#define RequestCB std::function<void(HTTPRequestHandle, int, std::string, std::string, std::string, std::string)>
11+
12+
class TrackedRequest
13+
{
14+
public:
15+
TrackedRequest(const TrackedRequest& req) = delete;
16+
TrackedRequest(HTTPRequestHandle hndl, SteamAPICall_t hCall, std::string requestID, RequestCB callback);
17+
~TrackedRequest();
18+
private:
19+
void OnHTTPRequestCompleted(HTTPRequestCompleted_t* arg, bool bFailed);
20+
21+
HTTPRequestHandle m_hHTTPReq;
22+
CCallResult<TrackedRequest, HTTPRequestCompleted_t> m_CallResult;
23+
std::string m_requestID;
24+
25+
RequestCB m_callback;
26+
};
27+
28+
class HTTPManager
29+
{
30+
public:
31+
HTTPManager() = default;
32+
~HTTPManager() = default;
33+
34+
void ProcessPendingHTTPRequests();
35+
36+
std::vector<TrackedRequest*> m_PendingRequests;
37+
std::map<std::string, std::map<std::string, std::string>> m_PendingHTTPRequests;
38+
};
39+
40+
extern HTTPManager* g_httpManager;
41+
42+
#endif

src/plugins/core/scripting.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ class PluginPlayerManager
9999
///////////////// HTTP Client //////////////
100100
////////////////////////////////////////////////////////////
101101

102+
class HTTPHeader
103+
{
104+
public:
105+
HTTPHeader(std::string strName, std::string strValue)
106+
{
107+
m_strName = strName;
108+
m_strValue = strValue;
109+
}
110+
const char* GetName() { return m_strName.c_str(); }
111+
const char* GetValue() { return m_strValue.c_str(); }
112+
private:
113+
std::string m_strName;
114+
std::string m_strValue;
115+
};
116+
102117
class PluginHTTP
103118
{
104119
private:
@@ -108,6 +123,7 @@ class PluginHTTP
108123
PluginHTTP(std::string m_plugin_name);
109124

110125
std::string PerformHTTP(std::string receivedData);
126+
std::string PerformHTTPWithRequestID(std::string receivedData, std::string requestID);
111127
};
112128

113129
//////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)