size_t http_download_park(const char * url, void * * outData) { // Download park to buffer in memory HttpRequest2 request; request.Url = url; request.Method = "GET"; request.Type = HTTP_DATA_NONE; http_response_t *response = http_request(request); if (response == NULL || response->status_code != 200) { Console::Error::WriteLine("Failed to download '%s'", request.Url.c_str()); if (response != NULL) { http_request_dispose(response); } *outData = NULL; return 0; } size_t dataSize = response->size - 1; void * data = malloc(dataSize); if (data == NULL) { dataSize = 0; Console::Error::WriteLine("Failed to allocate memory for downloaded park."); } else { memcpy(data, response->body, dataSize); } http_request_dispose(response); *outData = data; return dataSize; }
bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10]) { // Download park to buffer in memory http_request_t request; request.url = url; request.method = "GET"; request.type = HTTP_DATA_NONE; http_response_t *response = http_request(&request); if (response == NULL || response->status_code != 200) { Console::Error::WriteLine("Failed to download '%s'", request.url); if (response != NULL) { http_request_dispose(response); } return false; } // Generate temporary filename that includes the original extension if (tmpnam(tmpPath) == NULL) { Console::Error::WriteLine("Failed to generate temporary filename for downloaded park '%s'", request.url); http_request_dispose(response); return false; } size_t remainingBytes = L_tmpnam + 10 - strlen(tmpPath); const char *ext = http_get_extension_from_url(request.url, ".sv6"); strncat(tmpPath, ext, remainingBytes); // Store park in temporary file and load it (discard ending NUL in response body) FILE* tmpFile = fopen(tmpPath, "wb"); if (tmpFile == NULL) { Console::Error::WriteLine("Failed to write downloaded park '%s' to temporary file", request.url); http_request_dispose(response); return false; } fwrite(response->body, 1, response->size - 1, tmpFile); fclose(tmpFile); http_request_dispose(response); return true; }