2021-07-25 23:23:44 +01:00
|
|
|
/*
|
|
|
|
* http.c
|
|
|
|
*
|
2023-04-08 12:42:22 +01:00
|
|
|
* Copyright (c) 2020-2023, DarkMatterCore <pabloacurielz@gmail.com>.
|
2021-07-25 23:23:44 +01:00
|
|
|
*
|
|
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
|
|
*
|
|
|
|
* nxdumptool is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* nxdumptool is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nxdt_utils.h"
|
|
|
|
#include "http.h"
|
|
|
|
|
|
|
|
/* Global variables. */
|
|
|
|
|
|
|
|
static Mutex g_httpMutex = 0;
|
|
|
|
static bool g_httpInterfaceInit = false;
|
|
|
|
|
|
|
|
bool httpInitialize(void)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
SCOPED_LOCK(&g_httpMutex)
|
|
|
|
{
|
|
|
|
ret = g_httpInterfaceInit;
|
|
|
|
if (ret) break;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Initialize CURL. */
|
|
|
|
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
if (res != CURLE_OK)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("%s", curl_easy_strerror(res));
|
2021-07-25 23:23:44 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Update flags. */
|
|
|
|
ret = g_httpInterfaceInit = true;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void httpExit(void)
|
|
|
|
{
|
|
|
|
SCOPED_LOCK(&g_httpMutex)
|
|
|
|
{
|
|
|
|
/* Cleanup CURL. */
|
|
|
|
curl_global_cleanup();
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Update flag. */
|
|
|
|
g_httpInterfaceInit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t httpWriteFileCallback(char *buffer, size_t size, size_t nitems, void *outstream)
|
|
|
|
{
|
|
|
|
size_t total_size = (size * nitems);
|
|
|
|
return (total_size ? fwrite(buffer, 1, total_size, (FILE*)outstream) : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t httpWriteBufferCallback(char *buffer, size_t size, size_t nitems, void *outstream)
|
|
|
|
{
|
|
|
|
size_t total_size = (size * nitems);
|
|
|
|
HttpBuffer *http_buffer = (HttpBuffer*)outstream;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
if (!total_size) return 0;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
char *data_tmp = realloc(http_buffer->data, http_buffer->size + total_size);
|
|
|
|
if (!data_tmp) return 0;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
http_buffer->data = data_tmp;
|
|
|
|
data_tmp = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
memcpy(http_buffer->data + http_buffer->size, buffer, total_size);
|
|
|
|
http_buffer->size += total_size;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool httpPerformGetRequest(const char *url, bool force_https, size_t *outsize, HttpWriteCallback write_cb, void *write_ptr, HttpProgressCallback progress_cb, void *progress_ptr)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
SCOPED_LOCK(&g_httpMutex)
|
|
|
|
{
|
|
|
|
if (!g_httpInterfaceInit || !url || !*url)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Invalid parameters!");
|
2021-07-25 23:23:44 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
CURL *curl = NULL;
|
|
|
|
CURLcode res = CURLE_OK;
|
|
|
|
long http_code = 0;
|
|
|
|
curl_off_t download_size = 0, content_length = 0;
|
|
|
|
char curl_err_buf[CURL_ERROR_SIZE] = {0};
|
2021-07-29 08:50:17 +01:00
|
|
|
const char *error_str = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Start CURL session. */
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if (!curl)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to start CURL session for \"%s\"!", url);
|
2021-07-25 23:23:44 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Set CURL options. */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, HTTP_USER_AGENT);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_buf);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
|
2021-08-08 08:39:21 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, HTTP_LOW_SPEED_LIMIT);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, HTTP_LOW_SPEED_TIME);
|
2021-07-25 23:23:44 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, HTTP_BUFFER_SIZE);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)(force_https ? CURL_HTTP_VERSION_2TLS : CURL_HTTP_VERSION_1_1));
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
if (write_cb) curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
|
|
|
|
if (write_ptr) curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_ptr);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
if (progress_cb)
|
|
|
|
{
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_cb);
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
if (progress_ptr) curl_easy_setopt(curl, CURLOPT_XFERINFODATA, progress_ptr);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Perform GET request. */
|
|
|
|
res = curl_easy_perform(curl);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Get HTTP request properties. */
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD_T, &download_size);
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &content_length);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* End CURL session. */
|
|
|
|
curl_easy_cleanup(curl);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Update return value. */
|
|
|
|
ret = (res == CURLE_OK && http_code >= 200 && http_code <= 299 && (content_length <= 0 || download_size == content_length));
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
/* Update output size. */
|
|
|
|
if (outsize) *outsize = (size_t)download_size;
|
|
|
|
} else {
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("curl_easy_perform failed for \"%s\"! (res %d, HTTP code %ld, download %ld, length %ld).", url, res, http_code, download_size, content_length);
|
2021-07-25 23:23:44 +01:00
|
|
|
if (res != CURLE_OK)
|
|
|
|
{
|
|
|
|
/* Log CURL error info. */
|
|
|
|
if (*curl_err_buf)
|
|
|
|
{
|
|
|
|
size_t curl_err_buf_len = strlen(curl_err_buf);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
if (curl_err_buf[curl_err_buf_len - 1] == '\n') curl_err_buf[--curl_err_buf_len] = '\0';
|
|
|
|
if (curl_err_buf[curl_err_buf_len - 1] == '\r') curl_err_buf[--curl_err_buf_len] = '\0';
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-29 08:50:17 +01:00
|
|
|
error_str = curl_err_buf;
|
2021-07-25 23:23:44 +01:00
|
|
|
} else {
|
2021-07-29 08:50:17 +01:00
|
|
|
error_str = curl_easy_strerror(res);
|
2021-07-25 23:23:44 +01:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2022-07-12 17:34:49 +01:00
|
|
|
if (error_str) LOG_MSG_INFO("CURL error info: \"%s\".", error_str);
|
2021-07-25 23:23:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool httpDownloadFile(const char *path, const char *url, bool force_https, HttpProgressCallback progress_cb, void *progress_ptr)
|
|
|
|
{
|
|
|
|
if (!path || !*path)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Invalid parameters!");
|
2021-07-25 23:23:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
FILE *fd = NULL;
|
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Open output file. */
|
|
|
|
fd = fopen(path, "wb");
|
|
|
|
if (!fd)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to open \"%s\" for writing!", path);
|
2021-07-25 23:23:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Perform HTTP GET request. */
|
|
|
|
ret = httpPerformGetRequest(url, force_https, NULL, httpWriteFileCallback, fd, progress_cb, progress_ptr);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Close output file. */
|
|
|
|
fclose(fd);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Delete output file if the request failed. */
|
|
|
|
if (!ret) remove(path);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-29 17:48:32 +01:00
|
|
|
/* Commit SD card filesystem changes. */
|
|
|
|
utilsCommitSdCardFileSystemChanges();
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *httpDownloadData(size_t *outsize, const char *url, bool force_https, HttpProgressCallback progress_cb, void *progress_ptr)
|
|
|
|
{
|
|
|
|
if (!outsize)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Invalid parameters!");
|
2021-07-25 23:23:44 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
HttpBuffer http_buffer = {0};
|
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Perform HTTP GET request. */
|
|
|
|
ret = httpPerformGetRequest(url, force_https, outsize, httpWriteBufferCallback, &http_buffer, progress_cb, progress_ptr);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
/* Free output buffer if the request failed. */
|
|
|
|
if (!ret && http_buffer.data)
|
|
|
|
{
|
|
|
|
free(http_buffer.data);
|
|
|
|
http_buffer.data = NULL;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-25 23:23:44 +01:00
|
|
|
return http_buffer.data;
|
|
|
|
}
|