Skip to content

Commit 64544e7

Browse files
fix(httpcommon): sonarqube warning cleanup (#3558)
Co-authored-by: ReenigneArcher <[email protected]>
1 parent 2a31ee5 commit 64544e7

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

src/httpcommon.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,14 @@ namespace http {
5454
config::nvhttp.pkey = (dir / ("pkey-"s + unique_id)).string();
5555
}
5656

57-
if (!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) {
58-
if (create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
59-
return -1;
60-
}
57+
if ((!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) &&
58+
create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
59+
return -1;
6160
}
62-
if (user_creds_exist(config::sunshine.credentials_file)) {
63-
if (reload_user_creds(config::sunshine.credentials_file)) {
64-
return -1;
65-
}
66-
} else {
61+
if (!user_creds_exist(config::sunshine.credentials_file)) {
6762
BOOST_LOG(info) << "Open the Web UI to set your new username and password and getting started";
63+
} else if (reload_user_creds(config::sunshine.credentials_file)) {
64+
return -1;
6865
}
6966
return 0;
7067
}
@@ -179,19 +176,15 @@ namespace http {
179176
return 0;
180177
}
181178

182-
bool download_file(const std::string &url, const std::string &file) {
183-
CURL *curl = curl_easy_init();
184-
if (curl) {
185-
// sonar complains about weak ssl and tls versions
186-
// ideally, the setopts should go after the early returns; however sonar cannot detect the fix
187-
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
188-
} else {
179+
bool download_file(const std::string &url, const std::string &file, long ssl_version) {
180+
// sonar complains about weak ssl and tls versions; however sonar cannot detect the fix
181+
CURL *curl = curl_easy_init(); // NOSONAR
182+
if (!curl) {
189183
BOOST_LOG(error) << "Couldn't create CURL instance";
190184
return false;
191185
}
192186

193-
std::string file_dir = file_handler::get_parent_directory(file);
194-
if (!file_handler::make_directory(file_dir)) {
187+
if (std::string file_dir = file_handler::get_parent_directory(file); !file_handler::make_directory(file_dir)) {
195188
BOOST_LOG(error) << "Couldn't create directory ["sv << file_dir << ']';
196189
curl_easy_cleanup(curl);
197190
return false;
@@ -204,6 +197,7 @@ namespace http {
204197
return false;
205198
}
206199

200+
curl_easy_setopt(curl, CURLOPT_SSLVERSION, ssl_version); // NOSONAR
207201
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
208202
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
209203
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
@@ -219,17 +213,15 @@ namespace http {
219213
}
220214

221215
std::string url_escape(const std::string &url) {
222-
CURL *curl = curl_easy_init();
223-
char *string = curl_easy_escape(curl, url.c_str(), url.length());
216+
char *string = curl_easy_escape(nullptr, url.c_str(), static_cast<int>(url.length()));
224217
std::string result(string);
225218
curl_free(string);
226-
curl_easy_cleanup(curl);
227219
return result;
228220
}
229221

230222
std::string url_get_host(const std::string &url) {
231223
CURLU *curlu = curl_url();
232-
curl_url_set(curlu, CURLUPART_URL, url.c_str(), url.length());
224+
curl_url_set(curlu, CURLUPART_URL, url.c_str(), static_cast<unsigned int>(url.length()));
233225
char *host;
234226
if (curl_url_get(curlu, CURLUPART_HOST, &host, 0) != CURLUE_OK) {
235227
curl_url_cleanup(curlu);
@@ -240,5 +232,4 @@ namespace http {
240232
curl_url_cleanup(curlu);
241233
return result;
242234
}
243-
244235
} // namespace http

src/httpcommon.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*/
55
#pragma once
66

7+
// lib includes
8+
#include <curl/curl.h>
9+
710
// local includes
811
#include "network.h"
912
#include "thread_safe.h"
@@ -20,7 +23,7 @@ namespace http {
2023
);
2124

2225
int reload_user_creds(const std::string &file);
23-
bool download_file(const std::string &url, const std::string &file);
26+
bool download_file(const std::string &url, const std::string &file, long ssl_version = CURL_SSLVERSION_TLSv1_3);
2427
std::string url_escape(const std::string &url);
2528
std::string url_get_host(const std::string &url);
2629

tests/unit/test_httpcommon.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
* @file tests/unit/test_httpcommon.cpp
33
* @brief Test src/httpcommon.*.
44
*/
5+
// test imports
56
#include "../tests_common.h"
67

8+
// lib imports
9+
#include <curl/curl.h>
10+
11+
// local imports
712
#include <src/httpcommon.h>
813

914
struct UrlEscapeTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
1015

1116
TEST_P(UrlEscapeTest, Run) {
12-
auto [input, expected] = GetParam();
17+
const auto &[input, expected] = GetParam();
1318
ASSERT_EQ(http::url_escape(input), expected);
1419
}
1520

@@ -26,7 +31,7 @@ INSTANTIATE_TEST_SUITE_P(
2631
struct UrlGetHostTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
2732

2833
TEST_P(UrlGetHostTest, Run) {
29-
auto [input, expected] = GetParam();
34+
const auto &[input, expected] = GetParam();
3035
ASSERT_EQ(http::url_get_host(input), expected);
3136
}
3237

@@ -43,10 +48,10 @@ INSTANTIATE_TEST_SUITE_P(
4348
struct DownloadFileTest: testing::TestWithParam<std::tuple<std::string, std::string>> {};
4449

4550
TEST_P(DownloadFileTest, Run) {
46-
auto [url, filename] = GetParam();
51+
const auto &[url, filename] = GetParam();
4752
const std::string test_dir = platf::appdata().string() + "/tests/";
48-
std::basic_string path = test_dir + filename;
49-
ASSERT_TRUE(http::download_file(url, path));
53+
std::string path = test_dir + filename;
54+
ASSERT_TRUE(http::download_file(url, path, CURL_SSLVERSION_TLSv1_0));
5055
}
5156

5257
#ifdef SUNSHINE_BUILD_FLATPAK

0 commit comments

Comments
 (0)