Skip to content

Commit

Permalink
fix(httpcommon): sonarqube warning cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyManess committed Jan 18, 2025
1 parent bc22cca commit 97b8a3b
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions src/httpcommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ namespace http {
config::nvhttp.pkey = (dir / ("pkey-"s + unique_id)).string();
}

if (!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) {
if (create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
return -1;
}
if ((!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) &&
create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
return -1;
}
if (user_creds_exist(config::sunshine.credentials_file)) {
if (reload_user_creds(config::sunshine.credentials_file)) return -1;
if (user_creds_exist(config::sunshine.credentials_file) && 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";
Expand Down Expand Up @@ -195,19 +194,14 @@ namespace http {

bool
download_file(const std::string &url, const std::string &file) {
CURL *curl = curl_easy_init();
if (curl) {
// sonar complains about weak ssl and tls versions
// 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 {
// sonar complains about weak ssl and tls versions; however sonar cannot detect the fix
CURL *curl = curl_easy_init(); // NOSONAR
if (!curl) {
BOOST_LOG(error) << "Couldn't create CURL instance";
return false;
}

std::string file_dir = file_handler::get_parent_directory(file);
if (!file_handler::make_directory(file_dir)) {
if (std::string file_dir = file_handler::get_parent_directory(file); !file_handler::make_directory(file_dir)) {
BOOST_LOG(error) << "Couldn't create directory ["sv << file_dir << ']';
curl_easy_cleanup(curl);
return false;
Expand All @@ -220,6 +214,7 @@ namespace http {
return false;
}

curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // NOSONAR
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
Expand All @@ -236,18 +231,18 @@ namespace http {

std::string
url_escape(const std::string &url) {
CURL *curl = curl_easy_init();
char *string = curl_easy_escape(curl, url.c_str(), url.length());
std::string result(string);
curl_free(string);
curl_easy_cleanup(curl);
return result;
if (char *string = curl_easy_escape(nullptr, url.c_str(), static_cast<int>(url.length()))) {
std::string result(string);
curl_free(string);
return result;
}
return "";
}

std::string
url_get_host(const std::string &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;
if (curl_url_get(curlu, CURLUPART_HOST, &host, 0) != CURLUE_OK) {
curl_url_cleanup(curlu);
Expand All @@ -258,5 +253,4 @@ namespace http {
curl_url_cleanup(curlu);
return result;
}

} // namespace http

0 comments on commit 97b8a3b

Please sign in to comment.