Implement SetHTTPRequestRawPostBodyFromFile (#24)

* Implement SetHTTPRequestRawPostBodyFromFile

* Made sure that retval of fread() matches filesize

* Minor code cleanup

* Moved sm_SetHTTPRequestRawPostBodyFromFile to its appropriate place in the file

* style
This commit is contained in:
Sikari
2018-09-02 13:03:18 -07:00
committed by Kyle Sanderson
parent df97c60298
commit 87ba54f237
2 changed files with 51 additions and 0 deletions
+49
View File
@@ -458,6 +458,54 @@ static cell_t sm_SetHTTPRequestRawPostBody(IPluginContext *pContext, const cell_
return pHTTP->SetHTTPRequestRawPostBody(pRequest->request, pName, reinterpret_cast<uint8_t *>(pBuffer), params[4]) ? 1 : 0;
}
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, *pFilePath;
pContext->LocalToString(params[2], &pContentType);
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);
}
fseek(pInputFile, 0, SEEK_END);
uint32_t size = ftell(pInputFile);
fseek(pInputFile, 0, SEEK_SET);
if (size <= 0)
{
fclose(pInputFile);
return 0;
}
char *pBuffer = new char[size + 1];
uint32_t itemsRead = fread(pBuffer, sizeof(char), size, pInputFile);
fclose(pInputFile);
if (itemsRead != size)
{
delete [] pBuffer;
return 0;
}
cell_t result = pHTTP->SetHTTPRequestRawPostBody(pRequest->request, pContentType, reinterpret_cast<uint8_t *>(pBuffer), size) ? 1 : 0;
delete [] pBuffer;
return result;
}
static cell_t sm_GetHTTPResponseBodyCallback(IPluginContext *pContext, const cell_t *params)
{
ISteamHTTP *pHTTP;
@@ -644,6 +692,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},