fix(httpcommon): sonarqube warning cleanup (#3558)
Co-authored-by: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com>
This commit is contained in:
@@ -54,17 +54,14 @@ namespace http {
|
|||||||
config::nvhttp.pkey = (dir / ("pkey-"s + unique_id)).string();
|
config::nvhttp.pkey = (dir / ("pkey-"s + unique_id)).string();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) {
|
if ((!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) &&
|
||||||
if (create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
|
create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (user_creds_exist(config::sunshine.credentials_file)) {
|
if (!user_creds_exist(config::sunshine.credentials_file)) {
|
||||||
if (reload_user_creds(config::sunshine.credentials_file)) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
BOOST_LOG(info) << "Open the Web UI to set your new username and password and getting started";
|
BOOST_LOG(info) << "Open the Web UI to set your new username and password and getting started";
|
||||||
|
} else if (reload_user_creds(config::sunshine.credentials_file)) {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -179,19 +176,15 @@ namespace http {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool download_file(const std::string &url, const std::string &file) {
|
bool download_file(const std::string &url, const std::string &file, long ssl_version) {
|
||||||
CURL *curl = curl_easy_init();
|
// sonar complains about weak ssl and tls versions; however sonar cannot detect the fix
|
||||||
if (curl) {
|
CURL *curl = curl_easy_init(); // NOSONAR
|
||||||
// sonar complains about weak ssl and tls versions
|
if (!curl) {
|
||||||
// ideally, the setopts should go after the early returns; however sonar cannot detect the fix
|
|
||||||
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
|
|
||||||
} else {
|
|
||||||
BOOST_LOG(error) << "Couldn't create CURL instance";
|
BOOST_LOG(error) << "Couldn't create CURL instance";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string file_dir = file_handler::get_parent_directory(file);
|
if (std::string file_dir = file_handler::get_parent_directory(file); !file_handler::make_directory(file_dir)) {
|
||||||
if (!file_handler::make_directory(file_dir)) {
|
|
||||||
BOOST_LOG(error) << "Couldn't create directory ["sv << file_dir << ']';
|
BOOST_LOG(error) << "Couldn't create directory ["sv << file_dir << ']';
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
return false;
|
return false;
|
||||||
@@ -204,6 +197,7 @@ namespace http {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
curl_easy_setopt(curl, CURLOPT_SSLVERSION, ssl_version); // NOSONAR
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
|
||||||
@@ -219,17 +213,15 @@ namespace http {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string url_escape(const std::string &url) {
|
std::string url_escape(const std::string &url) {
|
||||||
CURL *curl = curl_easy_init();
|
char *string = curl_easy_escape(nullptr, url.c_str(), static_cast<int>(url.length()));
|
||||||
char *string = curl_easy_escape(curl, url.c_str(), url.length());
|
|
||||||
std::string result(string);
|
std::string result(string);
|
||||||
curl_free(string);
|
curl_free(string);
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string url_get_host(const std::string &url) {
|
std::string url_get_host(const std::string &url) {
|
||||||
CURLU *curlu = curl_url();
|
CURLU *curlu = curl_url();
|
||||||
curl_url_set(curlu, CURLUPART_URL, url.c_str(), url.length());
|
curl_url_set(curlu, CURLUPART_URL, url.c_str(), static_cast<unsigned int>(url.length()));
|
||||||
char *host;
|
char *host;
|
||||||
if (curl_url_get(curlu, CURLUPART_HOST, &host, 0) != CURLUE_OK) {
|
if (curl_url_get(curlu, CURLUPART_HOST, &host, 0) != CURLUE_OK) {
|
||||||
curl_url_cleanup(curlu);
|
curl_url_cleanup(curlu);
|
||||||
@@ -240,5 +232,4 @@ namespace http {
|
|||||||
curl_url_cleanup(curlu);
|
curl_url_cleanup(curlu);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace http
|
} // namespace http
|
||||||
|
|||||||
@@ -4,6 +4,9 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// lib includes
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
// local includes
|
// local includes
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "thread_safe.h"
|
#include "thread_safe.h"
|
||||||
@@ -20,7 +23,7 @@ namespace http {
|
|||||||
);
|
);
|
||||||
|
|
||||||
int reload_user_creds(const std::string &file);
|
int reload_user_creds(const std::string &file);
|
||||||
bool download_file(const std::string &url, const std::string &file);
|
bool download_file(const std::string &url, const std::string &file, long ssl_version = CURL_SSLVERSION_TLSv1_3);
|
||||||
std::string url_escape(const std::string &url);
|
std::string url_escape(const std::string &url);
|
||||||
std::string url_get_host(const std::string &url);
|
std::string url_get_host(const std::string &url);
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,19 @@
|
|||||||
* @file tests/unit/test_httpcommon.cpp
|
* @file tests/unit/test_httpcommon.cpp
|
||||||
* @brief Test src/httpcommon.*.
|
* @brief Test src/httpcommon.*.
|
||||||
*/
|
*/
|
||||||
|
// test imports
|
||||||
#include "../tests_common.h"
|
#include "../tests_common.h"
|
||||||
|
|
||||||
|
// lib imports
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
// local imports
|
||||||
#include <src/httpcommon.h>
|
#include <src/httpcommon.h>
|
||||||
|
|
||||||
struct UrlEscapeTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
struct UrlEscapeTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
||||||
|
|
||||||
TEST_P(UrlEscapeTest, Run) {
|
TEST_P(UrlEscapeTest, Run) {
|
||||||
auto [input, expected] = GetParam();
|
const auto &[input, expected] = GetParam();
|
||||||
ASSERT_EQ(http::url_escape(input), expected);
|
ASSERT_EQ(http::url_escape(input), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +31,7 @@ INSTANTIATE_TEST_SUITE_P(
|
|||||||
struct UrlGetHostTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
struct UrlGetHostTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
||||||
|
|
||||||
TEST_P(UrlGetHostTest, Run) {
|
TEST_P(UrlGetHostTest, Run) {
|
||||||
auto [input, expected] = GetParam();
|
const auto &[input, expected] = GetParam();
|
||||||
ASSERT_EQ(http::url_get_host(input), expected);
|
ASSERT_EQ(http::url_get_host(input), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,10 +48,10 @@ INSTANTIATE_TEST_SUITE_P(
|
|||||||
struct DownloadFileTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
struct DownloadFileTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
||||||
|
|
||||||
TEST_P(DownloadFileTest, Run) {
|
TEST_P(DownloadFileTest, Run) {
|
||||||
auto [url, filename] = GetParam();
|
const auto &[url, filename] = GetParam();
|
||||||
const std::string test_dir = platf::appdata().string() + "/tests/";
|
const std::string test_dir = platf::appdata().string() + "/tests/";
|
||||||
std::basic_string path = test_dir + filename;
|
std::string path = test_dir + filename;
|
||||||
ASSERT_TRUE(http::download_file(url, path));
|
ASSERT_TRUE(http::download_file(url, path, CURL_SSLVERSION_TLSv1_0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SUNSHINE_BUILD_FLATPAK
|
#ifdef SUNSHINE_BUILD_FLATPAK
|
||||||
|
|||||||
Reference in New Issue
Block a user