From 52ccd1ec3887d72dd54e0c6eece3d46cce53f013 Mon Sep 17 00:00:00 2001 From: Sikari Date: Fri, 10 Aug 2018 08:02:53 +0300 Subject: [PATCH 1/5] Implement SetHTTPRequestRawPostBodyFromFile --- Extension/swhttprequest.cpp | 53 ++++++++++++++++++++++++++++++++++++ Pawn/includes/SteamWorks.inc | 2 ++ 2 files changed, 55 insertions(+) diff --git a/Extension/swhttprequest.cpp b/Extension/swhttprequest.cpp index 94c96daf..7fb58404 100644 --- a/Extension/swhttprequest.cpp +++ b/Extension/swhttprequest.cpp @@ -511,6 +511,58 @@ static cell_t sm_GetHTTPResponseBodyCallback(IPluginContext *pContext, const cel return 1; } +static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, const cell_t *params) +{ + ISteamHTTP *pHTTP; + SteamWorksHTTPRequest *pRequest = GetRequestPointer(pHTTP, pContext, params[1]); + + if (pRequest == NULL) + { + return 0; + } + + char *pContentType; + pContext->LocalToString(params[2], &pContentType); + + char *pFilePath; + pContext->LocalToString(params[3], &pFilePath); + + char szFinalPath[PLATFORM_MAX_PATH]; + smutils->BuildPath(Path_Game, szFinalPath, sizeof(szFinalPath), "%s", pFilePath); + + FILE *pInputFile = fopen(szFinalPath, "rb"); + + if (!pInputFile) + { + return pContext->ThrowNativeError("Unable to open %s for reading. errno: %d", szFinalPath, errno); + } + + uint32_t size; + + fseek(pInputFile, 0, SEEK_END); + size = ftell(pInputFile); + fseek(pInputFile, 0, SEEK_SET); + + if (size <= 0) + { + fclose(pInputFile); + return 0; + } + + char *pBuffer = new char[size + 1]; + fread(pBuffer, sizeof(char), size, pInputFile); + fclose(pInputFile); + + if (pHTTP->SetHTTPRequestRawPostBody(pRequest->request, pContentType, reinterpret_cast(pBuffer), size) == false) + { + delete [] pBuffer; + return 0; + } + + delete [] pBuffer; + return 1; +} + static cell_t sm_WriteHTTPResponseBodyToFile(IPluginContext *pContext, const cell_t *params) { ISteamHTTP *pHTTP; @@ -644,6 +696,7 @@ static sp_nativeinfo_t httpnatives[] = { {"SteamWorks_GetHTTPDownloadProgressPct", sm_GetHTTPDownloadProgressPct}, {"SteamWorks_GetHTTPRequestWasTimedOut", sm_GetHTTPRequestWasTimedOut}, {"SteamWorks_SetHTTPRequestRawPostBody", sm_SetHTTPRequestRawPostBody}, + {"SteamWorks_SetHTTPRequestRawPostBodyFromFile", sm_SetHTTPRequestRawPostBodyFromFile}, {"SteamWorks_GetHTTPResponseBodyCallback", sm_GetHTTPResponseBodyCallback}, {"SteamWorks_WriteHTTPResponseBodyToFile", sm_WriteHTTPResponseBodyToFile}, {"SteamWorks_SendHTTPRequestAndStreamResponse", sm_SendHTTPRequestAndStreamResponse}, diff --git a/Pawn/includes/SteamWorks.inc b/Pawn/includes/SteamWorks.inc index ecae2478..94913dea 100644 --- a/Pawn/includes/SteamWorks.inc +++ b/Pawn/includes/SteamWorks.inc @@ -281,6 +281,7 @@ native bool:SteamWorks_GetHTTPStreamingResponseBodyData(Handle:hRequest, cOffset native bool:SteamWorks_GetHTTPDownloadProgressPct(Handle:hRequest, &Float:percent); native bool:SteamWorks_GetHTTPRequestWasTimedOut(Handle:hRequest, &bool:bWasTimedOut); native bool:SteamWorks_SetHTTPRequestRawPostBody(Handle:hRequest, const String:sContentType[], const String:sBody[], bodylen); +native bool:SteamWorks_SetHTTPRequestRawPostBodyFromFile(Handle:hRequest, const String:sContentType[], const String:sFileName[]); funcenum SteamWorksHTTPBodyCallback { @@ -372,6 +373,7 @@ public __ext_SteamWorks_SetNTVOptional() MarkNativeAsOptional("SteamWorks_GetHTTPStreamingResponseBodyData"); MarkNativeAsOptional("SteamWorks_GetHTTPDownloadProgressPct"); MarkNativeAsOptional("SteamWorks_SetHTTPRequestRawPostBody"); + MarkNativeAsOptional("SteamWorks_SetHTTPRequestRawPostBodyFromFile"); MarkNativeAsOptional("SteamWorks_GetHTTPResponseBodyCallback"); MarkNativeAsOptional("SteamWorks_WriteHTTPResponseBodyToFile"); From efc8a656a692f39e23fba01c44716d9c5f55d05e Mon Sep 17 00:00:00 2001 From: Sikari Date: Fri, 10 Aug 2018 09:42:21 +0300 Subject: [PATCH 2/5] Made sure that retval of fread() matches filesize --- Extension/swhttprequest.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Extension/swhttprequest.cpp b/Extension/swhttprequest.cpp index 7fb58404..8bb306ed 100644 --- a/Extension/swhttprequest.cpp +++ b/Extension/swhttprequest.cpp @@ -531,13 +531,13 @@ static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, con smutils->BuildPath(Path_Game, szFinalPath, sizeof(szFinalPath), "%s", pFilePath); FILE *pInputFile = fopen(szFinalPath, "rb"); - if (!pInputFile) { return pContext->ThrowNativeError("Unable to open %s for reading. errno: %d", szFinalPath, errno); } uint32_t size; + uint32_t itemsRead; fseek(pInputFile, 0, SEEK_END); size = ftell(pInputFile); @@ -550,17 +550,19 @@ static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, con } char *pBuffer = new char[size + 1]; - fread(pBuffer, sizeof(char), size, pInputFile); + itemsRead = fread(pBuffer, sizeof(char), size, pInputFile); fclose(pInputFile); - if (pHTTP->SetHTTPRequestRawPostBody(pRequest->request, pContentType, reinterpret_cast(pBuffer), size) == false) + if (itemsRead != size) { delete [] pBuffer; return 0; } + cell_t result = pHTTP->SetHTTPRequestRawPostBody(pRequest->request, pContentType, reinterpret_cast(pBuffer), size) ? 1 : 0; + delete [] pBuffer; - return 1; + return result; } static cell_t sm_WriteHTTPResponseBodyToFile(IPluginContext *pContext, const cell_t *params) From 853236df2aa382946bfc6387f530932e79eea0f9 Mon Sep 17 00:00:00 2001 From: Sikari Date: Thu, 16 Aug 2018 19:05:21 +0300 Subject: [PATCH 3/5] Minor code cleanup --- Extension/swhttprequest.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Extension/swhttprequest.cpp b/Extension/swhttprequest.cpp index 8bb306ed..c975c46b 100644 --- a/Extension/swhttprequest.cpp +++ b/Extension/swhttprequest.cpp @@ -515,16 +515,13 @@ static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, con { ISteamHTTP *pHTTP; SteamWorksHTTPRequest *pRequest = GetRequestPointer(pHTTP, pContext, params[1]); - if (pRequest == NULL) { return 0; } - char *pContentType; + char *pContentType, *pFilePath; pContext->LocalToString(params[2], &pContentType); - - char *pFilePath; pContext->LocalToString(params[3], &pFilePath); char szFinalPath[PLATFORM_MAX_PATH]; @@ -537,8 +534,6 @@ static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, con } uint32_t size; - uint32_t itemsRead; - fseek(pInputFile, 0, SEEK_END); size = ftell(pInputFile); fseek(pInputFile, 0, SEEK_SET); @@ -549,6 +544,7 @@ static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, con return 0; } + uint32_t itemsRead; char *pBuffer = new char[size + 1]; itemsRead = fread(pBuffer, sizeof(char), size, pInputFile); fclose(pInputFile); From 86ffd47fe2b3d33d3414aa6228f445b517b69f8a Mon Sep 17 00:00:00 2001 From: Sikari Date: Thu, 16 Aug 2018 19:09:49 +0300 Subject: [PATCH 4/5] Moved sm_SetHTTPRequestRawPostBodyFromFile to its appropriate place in the file --- Extension/swhttprequest.cpp | 94 ++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/Extension/swhttprequest.cpp b/Extension/swhttprequest.cpp index c975c46b..1fb91108 100644 --- a/Extension/swhttprequest.cpp +++ b/Extension/swhttprequest.cpp @@ -458,7 +458,7 @@ static cell_t sm_SetHTTPRequestRawPostBody(IPluginContext *pContext, const cell_ return pHTTP->SetHTTPRequestRawPostBody(pRequest->request, pName, reinterpret_cast(pBuffer), params[4]) ? 1 : 0; } -static cell_t sm_GetHTTPResponseBodyCallback(IPluginContext *pContext, const cell_t *params) +static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, const cell_t *params) { ISteamHTTP *pHTTP; SteamWorksHTTPRequest *pRequest = GetRequestPointer(pHTTP, pContext, params[1]); @@ -467,51 +467,48 @@ static cell_t sm_GetHTTPResponseBodyCallback(IPluginContext *pContext, const cel return 0; } - IPlugin *pPlugin; - if (params[4] == BAD_HANDLE) - { - pPlugin = plsys->FindPluginByContext(pContext->GetContext()); - } else { - HandleError err; - pPlugin = plsys->PluginFromHandle(params[4], &err); + char *pContentType, *pFilePath; + pContext->LocalToString(params[2], &pContentType); + pContext->LocalToString(params[3], &pFilePath); - if (!pPlugin) - { - return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", params[4], err); - } - } + char szFinalPath[PLATFORM_MAX_PATH]; + smutils->BuildPath(Path_Game, szFinalPath, sizeof(szFinalPath), "%s", pFilePath); - IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]); - if (!pFunction) + FILE *pInputFile = fopen(szFinalPath, "rb"); + if (!pInputFile) { - return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); + return pContext->ThrowNativeError("Unable to open %s for reading. errno: %d", szFinalPath, errno); } uint32_t size; - if (pHTTP->GetHTTPResponseBodySize(pRequest->request, &size) == false) + fseek(pInputFile, 0, SEEK_END); + size = ftell(pInputFile); + fseek(pInputFile, 0, SEEK_SET); + + if (size <= 0) { + fclose(pInputFile); return 0; } - char *pBuffer = new char[size+1]; + uint32_t itemsRead; + char *pBuffer = new char[size + 1]; + itemsRead = fread(pBuffer, sizeof(char), size, pInputFile); + fclose(pInputFile); - if (pHTTP->GetHTTPResponseBodyData(pRequest->request, reinterpret_cast(pBuffer), size) == false) + if (itemsRead != size) { delete [] pBuffer; return 0; } - pBuffer[size] = '\0'; /* Incase users do something bad; we want to protect userspace; kind of. */ - pFunction->PushStringEx(pBuffer, size + 1, SM_PARAM_STRING_BINARY | SM_PARAM_STRING_COPY, 0); - pFunction->PushCell(params[3]); - pFunction->PushCell(size); - pFunction->Execute(NULL); + cell_t result = pHTTP->SetHTTPRequestRawPostBody(pRequest->request, pContentType, reinterpret_cast(pBuffer), size) ? 1 : 0; delete [] pBuffer; - return 1; + return result; } -static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, const cell_t *params) +static cell_t sm_GetHTTPResponseBodyCallback(IPluginContext *pContext, const cell_t *params) { ISteamHTTP *pHTTP; SteamWorksHTTPRequest *pRequest = GetRequestPointer(pHTTP, pContext, params[1]); @@ -520,45 +517,48 @@ static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, con return 0; } - char *pContentType, *pFilePath; - pContext->LocalToString(params[2], &pContentType); - pContext->LocalToString(params[3], &pFilePath); + IPlugin *pPlugin; + if (params[4] == BAD_HANDLE) + { + pPlugin = plsys->FindPluginByContext(pContext->GetContext()); + } else { + HandleError err; + pPlugin = plsys->PluginFromHandle(params[4], &err); - char szFinalPath[PLATFORM_MAX_PATH]; - smutils->BuildPath(Path_Game, szFinalPath, sizeof(szFinalPath), "%s", pFilePath); + if (!pPlugin) + { + return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", params[4], err); + } + } - FILE *pInputFile = fopen(szFinalPath, "rb"); - if (!pInputFile) + IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]); + if (!pFunction) { - return pContext->ThrowNativeError("Unable to open %s for reading. errno: %d", szFinalPath, errno); + return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); } uint32_t size; - fseek(pInputFile, 0, SEEK_END); - size = ftell(pInputFile); - fseek(pInputFile, 0, SEEK_SET); - - if (size <= 0) + if (pHTTP->GetHTTPResponseBodySize(pRequest->request, &size) == false) { - fclose(pInputFile); return 0; } - uint32_t itemsRead; - char *pBuffer = new char[size + 1]; - itemsRead = fread(pBuffer, sizeof(char), size, pInputFile); - fclose(pInputFile); + char *pBuffer = new char[size+1]; - if (itemsRead != size) + if (pHTTP->GetHTTPResponseBodyData(pRequest->request, reinterpret_cast(pBuffer), size) == false) { delete [] pBuffer; return 0; } - cell_t result = pHTTP->SetHTTPRequestRawPostBody(pRequest->request, pContentType, reinterpret_cast(pBuffer), size) ? 1 : 0; + pBuffer[size] = '\0'; /* Incase users do something bad; we want to protect userspace; kind of. */ + pFunction->PushStringEx(pBuffer, size + 1, SM_PARAM_STRING_BINARY | SM_PARAM_STRING_COPY, 0); + pFunction->PushCell(params[3]); + pFunction->PushCell(size); + pFunction->Execute(NULL); delete [] pBuffer; - return result; + return 1; } static cell_t sm_WriteHTTPResponseBodyToFile(IPluginContext *pContext, const cell_t *params) From 5ba09f5cb67647c05ffb1bfe39465754eb9c7ffb Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Sun, 2 Sep 2018 13:02:20 -0700 Subject: [PATCH 5/5] style --- Extension/swhttprequest.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Extension/swhttprequest.cpp b/Extension/swhttprequest.cpp index 1fb91108..46a949d1 100644 --- a/Extension/swhttprequest.cpp +++ b/Extension/swhttprequest.cpp @@ -480,9 +480,8 @@ static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, con return pContext->ThrowNativeError("Unable to open %s for reading. errno: %d", szFinalPath, errno); } - uint32_t size; fseek(pInputFile, 0, SEEK_END); - size = ftell(pInputFile); + uint32_t size = ftell(pInputFile); fseek(pInputFile, 0, SEEK_SET); if (size <= 0) @@ -491,9 +490,8 @@ static cell_t sm_SetHTTPRequestRawPostBodyFromFile(IPluginContext *pContext, con return 0; } - uint32_t itemsRead; char *pBuffer = new char[size + 1]; - itemsRead = fread(pBuffer, sizeof(char), size, pInputFile); + uint32_t itemsRead = fread(pBuffer, sizeof(char), size, pInputFile); fclose(pInputFile); if (itemsRead != size)