Skip to content

Commit

Permalink
fix request memory leak and check for http status codes + added debug…
Browse files Browse the repository at this point in the history
… logging to apistats when no api key was provided
  • Loading branch information
Apfelwurm committed Oct 25, 2022
1 parent 22b7239 commit 2ba1c89
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
8 changes: 8 additions & 0 deletions scripting/get5/natives.sp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max
CreateNative("Get5_AddLiveCvar", Native_AddLiveCvar);
CreateNative("Get5_IncreasePlayerStat", Native_IncreasePlayerStat);
CreateNative("Get5_GetMatchStats", Native_GetMatchStats);
CreateNative("Get5_CreateGet5HTTPRequest", Native_CreateGet5HTTPRequest);
RegPluginLibrary("get5");
return APLRes_Success;
}
Expand Down Expand Up @@ -251,3 +252,10 @@ public int Native_GetMatchStats(Handle plugin, int numParams) {
return view_as<int>(true);
}
}

public int Native_CreateGet5HTTPRequest(Handle plugin, int numParams) {
EHTTPMethod method = view_as<EHTTPMethod>(GetNativeCell(1));
char url[1024];
GetNativeString(2, url, sizeof(url));
return view_as<int>(CreateGet5HTTPRequest(method, url));
}
76 changes: 53 additions & 23 deletions scripting/get5_apistats.sp
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,20 @@ void ApiInfoChanged(ConVar convar, const char[] oldValue, const char[] newValue)
}

static Handle CreateRequest(EHTTPMethod httpMethod, const char[] apiMethod, any:...) {
char url[1024];
FormatEx(url, sizeof(url), "%s%s", g_APIURL, apiMethod);

char formattedUrl[1024];
VFormat(formattedUrl, sizeof(formattedUrl), url, 3);

LogDebug("Trying to create request to url %s", formattedUrl);

Handle req = SteamWorks_CreateHTTPRequest(httpMethod, formattedUrl);
if (StrEqual(g_APIKey, "")) {
// Not using a web interface.
LogError("Failed to create request because get5_web_api_key was not provided");
return INVALID_HANDLE;
}

} else if (req == INVALID_HANDLE) {
LogError("Failed to create request to %s", formattedUrl);
char url[1024];
char formattedUrl[1024];
FormatEx(url, sizeof(url), "%s%s", g_APIURL, apiMethod);
VFormat(formattedUrl, sizeof(formattedUrl), url, 3);
LogDebug("Trying to create request to url %s", formattedUrl);
Handle req = Get5_CreateGet5HTTPRequest(httpMethod, formattedUrl);
if (req == INVALID_HANDLE) {
return INVALID_HANDLE;

} else {
Expand All @@ -136,15 +135,28 @@ static Handle CreateRequest(EHTTPMethod httpMethod, const char[] apiMethod, any:
}
}

int RequestCallback(Handle request, bool failure, bool requestSuccessful,
static int RequestCallback(Handle request, bool failure, bool requestSuccessful,
EHTTPStatusCode statusCode) {

if (failure || !requestSuccessful) {
LogError("API request failed, HTTP status code = %d", statusCode);
char response[1024];
SteamWorks_GetHTTPResponseBodyData(request, response, sizeof(response));
LogError(response);
LogError("Network connection failed for the API request");
delete request;
return;
}

int status = view_as<int>(statusCode);
if (status >= 300 || status < 200) {
LogError("API request failed with HTTP status code: %d.", statusCode);
int responseSize;
SteamWorks_GetHTTPResponseBodySize(request, responseSize);
char[] response = new char[responseSize];
if (SteamWorks_GetHTTPResponseBodyData(request, response, responseSize)) {
LogError("Response body: %s", response);
} else {
LogError("Failed to read response body.");
}
}
delete request;
}

public void Get5_OnSeriesInit(const Get5SeriesStartedEvent event) {
Expand Down Expand Up @@ -196,17 +208,31 @@ static void CheckForLogo(const char[] logo) {
}
}

static int LogoCallback(Handle request, bool failure, bool successful, EHTTPStatusCode status,
int data) {
static int LogoCallback(Handle request, bool failure, bool successful, EHTTPStatusCode statusCode,
DataPack data) {
if (failure || !successful) {
LogError("Logo request failed, status code = %d", status);
LogError("Network connection failed for the logo request");
delete request;
return;
}

DataPack pack = view_as<DataPack>(data);
pack.Reset();
int status = view_as<int>(statusCode);
if (status >= 300 || status < 200) {
LogError("Logo request failed with HTTP status code: %d.", statusCode);
int responseSize;
SteamWorks_GetHTTPResponseBodySize(request, responseSize);
char[] response = new char[responseSize];
if (SteamWorks_GetHTTPResponseBodyData(request, response, responseSize)) {
LogError("Response body: %s", response);
} else {
LogError("Failed to read response body.");
}
delete request;
}

data.Reset();
char logo[32];
pack.ReadString(logo, sizeof(logo));
data.ReadString(logo, sizeof(logo));

char logoPath[PLATFORM_MAX_PATH + 1];
if (g_UseSVGCvar.BoolValue) {
Expand All @@ -215,8 +241,12 @@ static int LogoCallback(Handle request, bool failure, bool successful, EHTTPStat
FormatEx(logoPath, sizeof(logoPath), "%s/%s.png", g_LogoBasePath, logo);
}

LogMessage("Saved logo for %s to %s", logo, logoPath);
SteamWorks_WriteHTTPResponseBodyToFile(request, logoPath);
if (SteamWorks_WriteHTTPResponseBodyToFile(request, logoPath)) {
LogMessage("Saved logo for %s to %s", logo, logoPath);
} else {
LogError("Failed to write logo %s to file %s.", logo, logoPath);
}
delete request;
}

public void Get5_OnGoingLive(const Get5GoingLiveEvent event) {
Expand Down
3 changes: 3 additions & 0 deletions scripting/include/get5.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <json> // github.com/clugg/sm-json
#include <cstrike>
#include <SteamWorks>

enum Get5Side {
Get5Side_None = CS_TEAM_NONE,
Expand Down Expand Up @@ -135,6 +136,8 @@ native bool Get5_GetMatchStats(KeyValues kv);
// Increases an (integer-typed) player statistic in the plugin's stats keyvalue structure.
native int Get5_IncreasePlayerStat(int client, const char[] statName, int amount = 1);

// Creates a Steamworks http(s) request with the get5 headers
native Handle Get5_CreateGet5HTTPRequest(const EHTTPMethod method, const char[] url);

methodmap Get5StatusTeam < JSON_Object {

Expand Down

0 comments on commit 2ba1c89

Please sign in to comment.