From da1a73070c9dc7e5b64a41237fa0d66e70ef6f8e Mon Sep 17 00:00:00 2001 From: Fabian Sauter Date: Sun, 15 Feb 2026 12:22:18 +0100 Subject: [PATCH] cppcheck and clang-tidy fixes for Fedora43 --- .clang-tidy | 10 ++- cppcheck-suppressions.xml | 4 + cpr/async.cpp | 2 +- cpr/callback.cpp | 6 +- cpr/connection_pool.cpp | 23 +++--- cpr/cprtypes.cpp | 2 +- cpr/curlholder.cpp | 4 +- cpr/error.cpp | 143 +++++++++++++++++---------------- cpr/response.cpp | 6 +- cpr/session.cpp | 4 +- cpr/ssl_ctx.cpp | 2 +- cpr/threadpool.cpp | 33 ++++---- cpr/util.cpp | 4 +- include/cpr/accept_encoding.h | 6 +- include/cpr/async.h | 14 ++-- include/cpr/auth.h | 7 +- include/cpr/bearer.h | 3 +- include/cpr/body.h | 6 +- include/cpr/body_view.h | 12 +-- include/cpr/buffer.h | 4 +- include/cpr/callback.h | 25 ++---- include/cpr/cert_info.h | 8 +- include/cpr/connect_timeout.h | 2 - include/cpr/connection_pool.h | 16 ++-- include/cpr/cookies.h | 5 +- include/cpr/cprtypes.h | 42 ++++++++-- include/cpr/curl_container.h | 16 ++-- include/cpr/curlholder.h | 4 +- include/cpr/error.h | 128 ++++++++++++++--------------- include/cpr/file.h | 1 - include/cpr/filesystem.h | 2 +- include/cpr/http_version.h | 3 +- include/cpr/interceptor.h | 4 +- include/cpr/interface.h | 3 - include/cpr/limit_rate.h | 4 +- include/cpr/local_port.h | 1 - include/cpr/local_port_range.h | 1 - include/cpr/low_speed.h | 6 +- include/cpr/multipart.h | 6 +- include/cpr/multiperform.h | 6 +- include/cpr/proxies.h | 4 +- include/cpr/range.h | 12 +-- include/cpr/reserve_size.h | 4 +- include/cpr/response.h | 18 ++--- include/cpr/secure_string.h | 6 +- include/cpr/singleton.h | 6 +- include/cpr/sse.h | 3 +- include/cpr/ssl_options.h | 55 ++++--------- include/cpr/threadpool.h | 37 ++++----- include/cpr/timeout.h | 4 +- include/cpr/unix_socket.h | 3 +- include/cpr/user_agent.h | 7 +- include/cpr/util.h | 2 +- include/cpr/verbose.h | 1 - test/abstractServer.hpp | 2 +- test/connection_pool_tests.cpp | 20 ++--- test/curlholder_tests.cpp | 2 +- test/proxy_tests.cpp | 2 +- 58 files changed, 378 insertions(+), 388 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 55570bf18..34e684a65 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -38,8 +38,14 @@ Checks: '*, -cppcoreguidelines-avoid-do-while, -llvmlibc-inline-function-decl, -altera-struct-pack-align, --boost-use-ranges +-boost-use-ranges, +-cppcoreguidelines-special-member-functions, +-hicpp-special-member-functions, +-misc-header-include-cycle, +-cppcoreguidelines-avoid-const-or-ref-data-members, +-google-explicit-constructor, +-hicpp-explicit-conversions ' WarningsAsErrors: '*' -HeaderFilterRegex: 'src\/*.hpp' +HeaderFilterRegex: 'include\/cpr\/.*\.h(pp)?' FormatStyle: file diff --git a/cppcheck-suppressions.xml b/cppcheck-suppressions.xml index 8995984be..6376624d1 100644 --- a/cppcheck-suppressions.xml +++ b/cppcheck-suppressions.xml @@ -39,4 +39,8 @@ postfixOperator + + syntaxError + */cpr/util.cpp + \ No newline at end of file diff --git a/cpr/async.cpp b/cpr/async.cpp index e10d09e1f..b4adab778 100644 --- a/cpr/async.cpp +++ b/cpr/async.cpp @@ -3,6 +3,6 @@ namespace cpr { // NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables) -CPR_SINGLETON_IMPL(GlobalThreadPool) +CPR_SINGLETON_IMPL(GlobalThreadPool); } // namespace cpr diff --git a/cpr/callback.cpp b/cpr/callback.cpp index f2d257a2c..c08215eff 100644 --- a/cpr/callback.cpp +++ b/cpr/callback.cpp @@ -3,12 +3,12 @@ #include namespace cpr { - void CancellationCallback::SetProgressCallback(ProgressCallback& u_cb) { user_cb.emplace(std::reference_wrapper{u_cb}); } + bool CancellationCallback::operator()(cpr_pf_arg_t dltotal, cpr_pf_arg_t dlnow, cpr_pf_arg_t ultotal, cpr_pf_arg_t ulnow) const { - const bool cont_operation{!cancellation_state->load()}; - return user_cb ? (cont_operation && (*user_cb)(dltotal, dlnow, ultotal, ulnow)) : cont_operation; + const bool const_operation = !(cancellation_state->load()); + return user_cb ? (const_operation && (*user_cb)(dltotal, dlnow, ultotal, ulnow)) : const_operation; } } // namespace cpr diff --git a/cpr/connection_pool.cpp b/cpr/connection_pool.cpp index 6afb8643e..e645e82d8 100644 --- a/cpr/connection_pool.cpp +++ b/cpr/connection_pool.cpp @@ -7,33 +7,32 @@ namespace cpr { ConnectionPool::ConnectionPool() { CURLSH* curl_share = curl_share_init(); this->connection_mutex_ = std::make_shared(); - + auto lock_f = +[](CURL* /*handle*/, curl_lock_data /*data*/, curl_lock_access /*access*/, void* userptr) { std::mutex* lock = static_cast(userptr); lock->lock(); // cppcheck-suppress localMutex // False positive: mutex is used as callback for libcurl, not local scope }; - + auto unlock_f = +[](CURL* /*handle*/, curl_lock_data /*data*/, void* userptr) { std::mutex* lock = static_cast(userptr); lock->unlock(); }; - + curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT); curl_share_setopt(curl_share, CURLSHOPT_USERDATA, this->connection_mutex_.get()); curl_share_setopt(curl_share, CURLSHOPT_LOCKFUNC, lock_f); curl_share_setopt(curl_share, CURLSHOPT_UNLOCKFUNC, unlock_f); - - this->curl_sh_ = std::shared_ptr(curl_share, - [](CURLSH* ptr) { - // Make sure to reset callbacks before cleanup to avoid deadlocks - curl_share_setopt(ptr, CURLSHOPT_LOCKFUNC, nullptr); - curl_share_setopt(ptr, CURLSHOPT_UNLOCKFUNC, nullptr); - curl_share_cleanup(ptr); - }); + + this->curl_sh_ = std::shared_ptr(curl_share, [](CURLSH* ptr) { + // Make sure to reset callbacks before cleanup to avoid deadlocks + curl_share_setopt(ptr, CURLSHOPT_LOCKFUNC, nullptr); + curl_share_setopt(ptr, CURLSHOPT_UNLOCKFUNC, nullptr); + curl_share_cleanup(ptr); + }); } void ConnectionPool::SetupHandler(CURL* easy_handler) const { curl_easy_setopt(easy_handler, CURLOPT_SHARE, this->curl_sh_.get()); } -} // namespace cpr \ No newline at end of file +} // namespace cpr \ No newline at end of file diff --git a/cpr/cprtypes.cpp b/cpr/cprtypes.cpp index 43df13491..d3cb78d92 100644 --- a/cpr/cprtypes.cpp +++ b/cpr/cprtypes.cpp @@ -1,9 +1,9 @@ #include "cpr/cprtypes.h" - #include #include #include + namespace cpr { bool CaseInsensitiveCompare::operator()(const std::string& a, const std::string& b) const noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), [](unsigned char ac, unsigned char bc) { return std::tolower(ac) < std::tolower(bc); }); diff --git a/cpr/curlholder.cpp b/cpr/curlholder.cpp index 4c8084634..3c397864a 100644 --- a/cpr/curlholder.cpp +++ b/cpr/curlholder.cpp @@ -22,7 +22,7 @@ CurlHolder::CurlHolder() { assert(handle); } -CurlHolder::CurlHolder(CurlHolder&& old) noexcept : handle(old.handle), chunk(old.chunk), resolveCurlList(old.resolveCurlList), multipart(old.multipart), error(std::move(old.error)) { +CurlHolder::CurlHolder(CurlHolder&& old) noexcept : handle(old.handle), chunk(old.chunk), resolveCurlList(old.resolveCurlList), multipart(old.multipart), error(old.error) { // Avoid double free old.handle = nullptr; old.chunk = nullptr; @@ -49,7 +49,7 @@ CurlHolder& CurlHolder::operator=(CurlHolder&& old) noexcept { chunk = old.chunk; resolveCurlList = old.resolveCurlList; multipart = old.multipart; - error = std::move(old.error); + error = old.error; // Avoid double free old.handle = nullptr; diff --git a/cpr/error.cpp b/cpr/error.cpp index 4c533a670..3ce918fa6 100644 --- a/cpr/error.cpp +++ b/cpr/error.cpp @@ -4,220 +4,223 @@ #include namespace cpr { +// NOLINTBEGIN(bugprone-branch-clone) Fine in this case ErrorCode Error::getErrorCodeForCurlError(std::int32_t curl_code) { - switch (curl_code) - { + switch (curl_code) { case CURLE_OK: - return ErrorCode::OK; + return ErrorCode::OK; case CURLE_UNSUPPORTED_PROTOCOL: - return ErrorCode::UNSUPPORTED_PROTOCOL; + return ErrorCode::UNSUPPORTED_PROTOCOL; case CURLE_FAILED_INIT: - return ErrorCode::FAILED_INIT; + return ErrorCode::FAILED_INIT; case CURLE_URL_MALFORMAT: - return ErrorCode::URL_MALFORMAT; + return ErrorCode::URL_MALFORMAT; case CURLE_NOT_BUILT_IN: - return ErrorCode::NOT_BUILT_IN; + return ErrorCode::NOT_BUILT_IN; case CURLE_COULDNT_RESOLVE_PROXY: - return ErrorCode::COULDNT_RESOLVE_PROXY; + return ErrorCode::COULDNT_RESOLVE_PROXY; case CURLE_COULDNT_RESOLVE_HOST: - return ErrorCode::COULDNT_RESOLVE_HOST; + return ErrorCode::COULDNT_RESOLVE_HOST; case CURLE_COULDNT_CONNECT: - return ErrorCode::COULDNT_CONNECT; - + return ErrorCode::COULDNT_CONNECT; + // Name changed in curl >= 7.51.0. #if LIBCURL_VERSION_NUM >= 0x073300 case CURLE_WEIRD_SERVER_REPLY: - return ErrorCode::WEIRD_SERVER_REPLY; + return ErrorCode::WEIRD_SERVER_REPLY; #else case CURLE_FTP_WEIRD_SERVER_REPLY: - return ErrorCode::WEIRD_SERVER_REPLY; + return ErrorCode::WEIRD_SERVER_REPLY; #endif + case CURLE_REMOTE_ACCESS_DENIED: - return ErrorCode::REMOTE_ACCESS_DENIED; + return ErrorCode::REMOTE_ACCESS_DENIED; case CURLE_HTTP2: - return ErrorCode::HTTP2; + return ErrorCode::HTTP2; case CURLE_QUOTE_ERROR: - return ErrorCode::QUOTE_ERROR; + return ErrorCode::QUOTE_ERROR; case CURLE_HTTP_RETURNED_ERROR: - return ErrorCode::HTTP_RETURNED_ERROR; + return ErrorCode::HTTP_RETURNED_ERROR; case CURLE_WRITE_ERROR: - return ErrorCode::WRITE_ERROR; + return ErrorCode::WRITE_ERROR; case CURLE_UPLOAD_FAILED: - return ErrorCode::UPLOAD_FAILED; + return ErrorCode::UPLOAD_FAILED; case CURLE_READ_ERROR: - return ErrorCode::READ_ERROR; + return ErrorCode::READ_ERROR; case CURLE_OUT_OF_MEMORY: - return ErrorCode::OUT_OF_MEMORY; + return ErrorCode::OUT_OF_MEMORY; case CURLE_OPERATION_TIMEDOUT: - return ErrorCode::OPERATION_TIMEDOUT; + return ErrorCode::OPERATION_TIMEDOUT; case CURLE_RANGE_ERROR: - return ErrorCode::RANGE_ERROR; + return ErrorCode::RANGE_ERROR; case CURLE_HTTP_POST_ERROR: - return ErrorCode::HTTP_POST_ERROR; + return ErrorCode::HTTP_POST_ERROR; case CURLE_SSL_CONNECT_ERROR: - return ErrorCode::SSL_CONNECT_ERROR; + return ErrorCode::SSL_CONNECT_ERROR; case CURLE_BAD_DOWNLOAD_RESUME: - return ErrorCode::BAD_DOWNLOAD_RESUME; + return ErrorCode::BAD_DOWNLOAD_RESUME; case CURLE_FILE_COULDNT_READ_FILE: - return ErrorCode::FILE_COULDNT_READ_FILE; + return ErrorCode::FILE_COULDNT_READ_FILE; case CURLE_FUNCTION_NOT_FOUND: - return ErrorCode::FUNCTION_NOT_FOUND; + return ErrorCode::FUNCTION_NOT_FOUND; case CURLE_ABORTED_BY_CALLBACK: - return ErrorCode::ABORTED_BY_CALLBACK; + return ErrorCode::ABORTED_BY_CALLBACK; case CURLE_BAD_FUNCTION_ARGUMENT: - return ErrorCode::BAD_FUNCTION_ARGUMENT; + return ErrorCode::BAD_FUNCTION_ARGUMENT; case CURLE_INTERFACE_FAILED: - return ErrorCode::INTERFACE_FAILED; + return ErrorCode::INTERFACE_FAILED; case CURLE_TOO_MANY_REDIRECTS: - return ErrorCode::TOO_MANY_REDIRECTS; + return ErrorCode::TOO_MANY_REDIRECTS; case CURLE_UNKNOWN_OPTION: - return ErrorCode::UNKNOWN_OPTION; + return ErrorCode::UNKNOWN_OPTION; + // Added in curl 7.78.0. #if LIBCURL_VERSION_NUM >= 0x074E00 case CURLE_SETOPT_OPTION_SYNTAX: - return ErrorCode::SETOPT_OPTION_SYNTAX; + return ErrorCode::SETOPT_OPTION_SYNTAX; #endif + case CURLE_GOT_NOTHING: - return ErrorCode::GOT_NOTHING; + return ErrorCode::GOT_NOTHING; case CURLE_SSL_ENGINE_NOTFOUND: - return ErrorCode::SSL_ENGINE_NOTFOUND; + return ErrorCode::SSL_ENGINE_NOTFOUND; case CURLE_SSL_ENGINE_SETFAILED: - return ErrorCode::SSL_ENGINE_SETFAILED; + return ErrorCode::SSL_ENGINE_SETFAILED; case CURLE_SEND_ERROR: - return ErrorCode::SEND_ERROR; + return ErrorCode::SEND_ERROR; case CURLE_RECV_ERROR: - return ErrorCode::RECV_ERROR; + return ErrorCode::RECV_ERROR; case CURLE_SSL_CERTPROBLEM: - return ErrorCode::SSL_CERTPROBLEM; + return ErrorCode::SSL_CERTPROBLEM; case CURLE_SSL_CIPHER: - return ErrorCode::SSL_CIPHER; + return ErrorCode::SSL_CIPHER; case CURLE_PEER_FAILED_VERIFICATION: - return ErrorCode::PEER_FAILED_VERIFICATION; + return ErrorCode::PEER_FAILED_VERIFICATION; case CURLE_BAD_CONTENT_ENCODING: - return ErrorCode::BAD_CONTENT_ENCODING; + return ErrorCode::BAD_CONTENT_ENCODING; case CURLE_FILESIZE_EXCEEDED: - return ErrorCode::FILESIZE_EXCEEDED; + return ErrorCode::FILESIZE_EXCEEDED; case CURLE_USE_SSL_FAILED: - return ErrorCode::USE_SSL_FAILED; + return ErrorCode::USE_SSL_FAILED; case CURLE_SEND_FAIL_REWIND: - return ErrorCode::SEND_FAIL_REWIND; + return ErrorCode::SEND_FAIL_REWIND; case CURLE_SSL_ENGINE_INITFAILED: - return ErrorCode::SSL_ENGINE_INITFAILED; + return ErrorCode::SSL_ENGINE_INITFAILED; // Added in curl 7.13.1. #if LIBCURL_VERSION_NUM >= 0x070D01 case CURLE_LOGIN_DENIED: - return ErrorCode::LOGIN_DENIED; + return ErrorCode::LOGIN_DENIED; #endif // Added in curl 7.16.0. #if LIBCURL_VERSION_NUM >= 0x071000 case CURLE_SSL_CACERT_BADFILE: - return ErrorCode::SSL_CACERT_BADFILE; + return ErrorCode::SSL_CACERT_BADFILE; #endif // Added in curl 7.16.1. #if LIBCURL_VERSION_NUM >= 0x071001 case CURLE_SSL_SHUTDOWN_FAILED: - return ErrorCode::SSL_SHUTDOWN_FAILED; + return ErrorCode::SSL_SHUTDOWN_FAILED; #endif // Added in curl 7.18.2. #if LIBCURL_VERSION_NUM >= 0x071202 case CURLE_AGAIN: - return ErrorCode::AGAIN; + return ErrorCode::AGAIN; #endif // Added in curl 7.19.0. #if LIBCURL_VERSION_NUM >= 0x071300 case CURLE_SSL_CRL_BADFILE: - return ErrorCode::SSL_CRL_BADFILE; + return ErrorCode::SSL_CRL_BADFILE; case CURLE_SSL_ISSUER_ERROR: - return ErrorCode::SSL_ISSUER_ERROR; + return ErrorCode::SSL_ISSUER_ERROR; #endif // Added in curl 7.21.0. #if LIBCURL_VERSION_NUM >= 0x071500 case CURLE_CHUNK_FAILED: - return ErrorCode::CHUNK_FAILED; + return ErrorCode::CHUNK_FAILED; #endif // Added in curl 7.30.0. #if LIBCURL_VERSION_NUM >= 0x071E00 case CURLE_NO_CONNECTION_AVAILABLE: - return ErrorCode::NO_CONNECTION_AVAILABLE; + return ErrorCode::NO_CONNECTION_AVAILABLE; #endif // Added in curl 7.39.0. #if LIBCURL_VERSION_NUM >= 0x072700 case CURLE_SSL_PINNEDPUBKEYNOTMATCH: - return ErrorCode::SSL_PINNEDPUBKEYNOTMATCH; + return ErrorCode::SSL_PINNEDPUBKEYNOTMATCH; #endif // Added in curl 7.41.0. #if LIBCURL_VERSION_NUM >= 0x072900 case CURLE_SSL_INVALIDCERTSTATUS: - return ErrorCode::SSL_INVALIDCERTSTATUS; + return ErrorCode::SSL_INVALIDCERTSTATUS; #endif // Added in curl 7.49.0. #if LIBCURL_VERSION_NUM >= 0x073100 case CURLE_HTTP2_STREAM: - return ErrorCode::HTTP2_STREAM; + return ErrorCode::HTTP2_STREAM; #endif case CURLE_PARTIAL_FILE: - return ErrorCode::PARTIAL_FILE; + return ErrorCode::PARTIAL_FILE; // Added in curl 7.59.0. #if LIBCURL_VERSION_NUM >= 0x073B00 case CURLE_RECURSIVE_API_CALL: - return ErrorCode::RECURSIVE_API_CALL; + return ErrorCode::RECURSIVE_API_CALL; #endif // Added in curl 7.66.0. #if LIBCURL_VERSION_NUM >= 0x074200 case CURLE_AUTH_ERROR: - return ErrorCode::AUTH_ERROR; + return ErrorCode::AUTH_ERROR; #endif // Added in curl 7.68.0. #if LIBCURL_VERSION_NUM >= 0x074400 case CURLE_HTTP3: - return ErrorCode::HTTP3; + return ErrorCode::HTTP3; #endif // Added in curl 7.69.0. #if LIBCURL_VERSION_NUM >= 0x074500 case CURLE_QUIC_CONNECT_ERROR: - return ErrorCode::QUIC_CONNECT_ERROR; + return ErrorCode::QUIC_CONNECT_ERROR; #endif // Added in curl 7.73.0. #if LIBCURL_VERSION_NUM >= 0x074900 case CURLE_PROXY: - return ErrorCode::PROXY; + return ErrorCode::PROXY; #endif // Added in curl 7.77.0. #if LIBCURL_VERSION_NUM >= 0x074D00 case CURLE_SSL_CLIENTCERT: - return ErrorCode::SSL_CLIENTCERT; + return ErrorCode::SSL_CLIENTCERT; #endif // Added in curl 7.84.0. #if LIBCURL_VERSION_NUM >= 0x075400 case CURLE_UNRECOVERABLE_POLL: - return ErrorCode::UNRECOVERABLE_POLL; + return ErrorCode::UNRECOVERABLE_POLL; #endif // Added in curl 7.6.0. #if LIBCURL_VERSION_NUM >= 0x080600 case CURLE_TOO_LARGE: - return ErrorCode::TOO_LARGE; + return ErrorCode::TOO_LARGE; #endif default: - return ErrorCode::UNKNOWN_ERROR; - } + return ErrorCode::UNKNOWN_ERROR; + } } - +// NOLINTEND(bugprone-branch-clone) } // namespace cpr \ No newline at end of file diff --git a/cpr/response.cpp b/cpr/response.cpp index 4bfa3d5ed..19f75858e 100644 --- a/cpr/response.cpp +++ b/cpr/response.cpp @@ -21,7 +21,7 @@ Response::Response(std::shared_ptr curl, std::string&& p_text, std:: assert(curl_->handle); curl_easy_getinfo(curl_->handle, CURLINFO_RESPONSE_CODE, &status_code); curl_easy_getinfo(curl_->handle, CURLINFO_TOTAL_TIME, &elapsed); - char* url_string{nullptr}; + const char* url_string{nullptr}; curl_easy_getinfo(curl_->handle, CURLINFO_EFFECTIVE_URL, &url_string); url = Url(url_string); #if LIBCURL_VERSION_NUM >= 0x073700 // 7.55.0 @@ -36,7 +36,7 @@ Response::Response(std::shared_ptr curl, std::string&& p_text, std:: #endif curl_easy_getinfo(curl_->handle, CURLINFO_REDIRECT_COUNT, &redirect_count); #if LIBCURL_VERSION_NUM >= 0x071300 // 7.19.0 - char* ip_ptr{nullptr}; + const char* ip_ptr{nullptr}; if (curl_easy_getinfo(curl_->handle, CURLINFO_PRIMARY_IP, &ip_ptr) == CURLE_OK && ip_ptr) { primary_ip = ip_ptr; } @@ -54,7 +54,7 @@ Response::Response(std::shared_ptr curl, std::string&& p_text, std:: std::vector Response::GetCertInfos() const { assert(curl_); assert(curl_->handle); - curl_certinfo* ci{nullptr}; + const curl_certinfo* ci{nullptr}; curl_easy_getinfo(curl_->handle, CURLINFO_CERTINFO, &ci); std::vector cert_infos; diff --git a/cpr/session.cpp b/cpr/session.cpp index a62c8dd53..108ff2adf 100644 --- a/cpr/session.cpp +++ b/cpr/session.cpp @@ -143,7 +143,7 @@ void Session::SetBearer(const Bearer& token) { Session::Session() : curl_(new CurlHolder()) { // Set up some sensible defaults - curl_version_info_data* version_info = curl_version_info(CURLVERSION_NOW); + const curl_version_info_data* version_info = curl_version_info(CURLVERSION_NOW); const std::string version = "curl/" + std::string{version_info->version}; curl_easy_setopt(curl_->handle, CURLOPT_USERAGENT, version.c_str()); SetRedirect(Redirect()); @@ -1125,7 +1125,7 @@ void Session::SetOption(const LocalPortRange& local_port_range) { SetLocalPortRa void Session::SetOption(const HttpVersion& version) { SetHttpVersion(version); } void Session::SetOption(const Range& range) { SetRange(range); } void Session::SetOption(const MultiRange& multi_range) { SetMultiRange(multi_range); } -void Session::SetOption(const ReserveSize& reserve_size) { SetReserveSize(reserve_size.size); } +void Session::SetOption(const ReserveSize& reserve_size) { SetReserveSize(reserve_size); } void Session::SetOption(const AcceptEncoding& accept_encoding) { SetAcceptEncoding(accept_encoding); } void Session::SetOption(AcceptEncoding&& accept_encoding) { SetAcceptEncoding(std::move(accept_encoding)); } void Session::SetOption(const ConnectionPool& pool) { SetConnectionPool(pool); } diff --git a/cpr/ssl_ctx.cpp b/cpr/ssl_ctx.cpp index da5949270..60223edb8 100644 --- a/cpr/ssl_ctx.cpp +++ b/cpr/ssl_ctx.cpp @@ -67,7 +67,7 @@ CURLcode sslctx_function_load_ca_cert_from_buffer(CURL* /*curl*/, void* sslctx, // Create a memory BIO using the data of cert_buf // Note: It is assumed, that cert_buf is nul terminated and its length is determined by strlen - char* cert_buf = static_cast(raw_cert_buf); + const char* cert_buf = static_cast(raw_cert_buf); BIO* bio = BIO_new_mem_buf(cert_buf, -1); // Get a pointer to the current certificate verification storage diff --git a/cpr/threadpool.cpp b/cpr/threadpool.cpp index 105c02039..04a5adc94 100644 --- a/cpr/threadpool.cpp +++ b/cpr/threadpool.cpp @@ -16,10 +16,10 @@ ThreadPool::~ThreadPool() { } int ThreadPool::Start(size_t start_threads) { - if (status != STOP) { + if (status != Status::STOP) { return -1; } - status = RUNNING; + status = Status::RUNNING; start_threads = std::clamp(start_threads, min_thread_num, max_thread_num); for (size_t i = 0; i < start_threads; ++i) { CreateThread(); @@ -29,11 +29,11 @@ int ThreadPool::Start(size_t start_threads) { int ThreadPool::Stop() { const std::unique_lock status_lock(status_wait_mutex); - if (status == STOP) { + if (status == Status::STOP) { return -1; } - status = STOP; + status = Status::STOP; status_wait_cond.notify_all(); task_cond.notify_all(); @@ -50,29 +50,28 @@ int ThreadPool::Stop() { } int ThreadPool::Pause() { - if (status == RUNNING) { - status = PAUSE; + if (status == Status::RUNNING) { + status = Status::PAUSE; } return 0; } int ThreadPool::Resume() { const std::unique_lock status_lock(status_wait_mutex); - if (status == PAUSE) { - status = RUNNING; + if (status == Status::PAUSE) { + status = Status::RUNNING; status_wait_cond.notify_all(); } return 0; } -int ThreadPool::Wait() const { +void ThreadPool::Wait() { while (true) { - if (status == STOP || (tasks.empty() && idle_thread_num == cur_thread_num)) { + if (status == Status::STOP || (tasks.empty() && idle_thread_num == cur_thread_num)) { break; } std::this_thread::yield(); } - return 0; } bool ThreadPool::CreateThread() { @@ -81,7 +80,7 @@ bool ThreadPool::CreateThread() { } auto thread = std::make_shared([this] { bool initialRun = true; - while (status != STOP) { + while (status != Status::STOP) { { std::unique_lock status_lock(status_wait_mutex); status_wait_cond.wait(status_lock, [this]() { return status != Status::PAUSE; }); @@ -90,8 +89,8 @@ bool ThreadPool::CreateThread() { Task task; { std::unique_lock locker(task_mutex); - task_cond.wait_for(locker, std::chrono::milliseconds(max_idle_time), [this]() { return status == STOP || !tasks.empty(); }); - if (status == STOP) { + task_cond.wait_for(locker, std::chrono::milliseconds(max_idle_time), [this]() { return status == Status::STOP || !tasks.empty(); }); + if (status == Status::STOP) { return; } if (tasks.empty()) { @@ -124,7 +123,7 @@ void ThreadPool::AddThread(const std::shared_ptr& thread) { ThreadData data; data.thread = thread; data.id = thread->get_id(); - data.status = RUNNING; + data.status = Status::RUNNING; data.start_time = std::chrono::steady_clock::now(); data.stop_time = std::chrono::steady_clock::time_point::max(); threads.emplace_back(data); @@ -139,14 +138,14 @@ void ThreadPool::DelThread(std::thread::id id) { --idle_thread_num; auto iter = threads.begin(); while (iter != threads.end()) { - if (iter->status == STOP && now > iter->stop_time) { + if (iter->status == Status::STOP && now > iter->stop_time) { if (iter->thread->joinable()) { iter->thread->join(); iter = threads.erase(iter); continue; } } else if (iter->id == id) { - iter->status = STOP; + iter->status = Status::STOP; iter->stop_time = std::chrono::steady_clock::now(); } ++iter; diff --git a/cpr/util.cpp b/cpr/util.cpp index 8bf5426a2..a89552c4f 100644 --- a/cpr/util.cpp +++ b/cpr/util.cpp @@ -18,7 +18,7 @@ #include #include -#if defined(_Win32) +#ifdef _Win32 #include #else #ifdef __clang__ @@ -52,7 +52,7 @@ enum class CurlHTTPCookieField : uint8_t { Cookies parseCookies(curl_slist* raw_cookies) { const int CURL_HTTP_COOKIE_SIZE = static_cast(CurlHTTPCookieField::Value) + 1; Cookies cookies; - for (curl_slist* nc = raw_cookies; nc; nc = nc->next) { + for (const curl_slist* nc = raw_cookies; nc; nc = nc->next) { std::vector tokens = cpr::util::split(nc->data, '\t'); while (tokens.size() < CURL_HTTP_COOKIE_SIZE) { tokens.emplace_back(""); diff --git a/include/cpr/accept_encoding.h b/include/cpr/accept_encoding.h index 167d7c2cf..65a08e058 100644 --- a/include/cpr/accept_encoding.h +++ b/include/cpr/accept_encoding.h @@ -1,15 +1,17 @@ #ifndef CPR_ACCEPT_ENCODING_H #define CPR_ACCEPT_ENCODING_H +#include #include #include #include #include +#include #include namespace cpr { -enum class AcceptEncodingMethods { +enum class AcceptEncodingMethods : uint8_t { identity, deflate, zlib, @@ -23,9 +25,7 @@ static const std::map AcceptEncodingMethodsS class AcceptEncoding { public: AcceptEncoding() = default; - // NOLINTNEXTLINE(google-explicit-constructor) AcceptEncoding(const std::initializer_list& methods); - // NOLINTNEXTLINE(google-explicit-constructor) AcceptEncoding(const std::initializer_list& methods); [[nodiscard]] bool empty() const noexcept; diff --git a/include/cpr/async.h b/include/cpr/async.h index 03a35354b..01c29454a 100644 --- a/include/cpr/async.h +++ b/include/cpr/async.h @@ -24,13 +24,13 @@ class GlobalThreadPool : public ThreadPool { **/ template auto async(Fn&& fn, Args&&... args) { - std::future future = GlobalThreadPool::GetInstance()->Submit(std::forward(fn), std::forward(args)...); - using async_wrapper_t = AsyncWrapper; - if constexpr (isCancellable) { - return async_wrapper_t{std::move(future), std::make_shared(false)}; - } else { - return async_wrapper_t{std::move(future)}; - } + std::future future = GlobalThreadPool::GetInstance()->Submit(std::forward(fn), std::forward(args)...); + using async_wrapper_t = AsyncWrapper; + if constexpr (isCancellable) { + return async_wrapper_t{std::move(future), std::make_shared(false)}; + } else { + return async_wrapper_t{std::move(future)}; + } } class async { diff --git a/include/cpr/auth.h b/include/cpr/auth.h index 830b964a9..166d1850e 100644 --- a/include/cpr/auth.h +++ b/include/cpr/auth.h @@ -1,6 +1,7 @@ #ifndef CPR_AUTH_H #define CPR_AUTH_H +#include #include #include @@ -8,14 +9,14 @@ namespace cpr { -enum class AuthMode { BASIC, DIGEST, NTLM, NEGOTIATE, ANY, ANYSAFE }; +enum class AuthMode : uint8_t { BASIC, DIGEST, NTLM, NEGOTIATE, ANY, ANYSAFE }; class Authentication { public: Authentication(std::string_view username, std::string_view password, AuthMode auth_mode); - const char* GetAuthString() const noexcept; - AuthMode GetAuthMode() const noexcept; + [[nodiscard]] const char* GetAuthString() const noexcept; + [[nodiscard]] AuthMode GetAuthMode() const noexcept; private: util::SecureString auth_string_; diff --git a/include/cpr/bearer.h b/include/cpr/bearer.h index 7792dc704..1f448da0c 100644 --- a/include/cpr/bearer.h +++ b/include/cpr/bearer.h @@ -15,7 +15,6 @@ namespace cpr { #if LIBCURL_VERSION_NUM >= 0x073D00 class Bearer { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Bearer(std::string_view token) : token_string_{token} {} Bearer(const Bearer& other) = default; Bearer(Bearer&& old) noexcept = default; @@ -24,7 +23,7 @@ class Bearer { Bearer& operator=(Bearer&& old) noexcept = default; Bearer& operator=(const Bearer& other) = default; - virtual const char* GetToken() const noexcept { + [[nodiscard]] virtual const char* GetToken() const noexcept { return token_string_.c_str(); } diff --git a/include/cpr/body.h b/include/cpr/body.h index cea7cdff6..8febe3dcd 100644 --- a/include/cpr/body.h +++ b/include/cpr/body.h @@ -15,17 +15,13 @@ namespace cpr { class Body : public StringHolder { public: Body() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Body(std::string body) : StringHolder(std::move(body)) {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Body(std::string_view body) : StringHolder(body) {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Body(const char* body) : StringHolder(body) {} Body(const char* str, size_t len) : StringHolder(str, len) {} Body(const std::initializer_list args) : StringHolder(args) {} - // NOLINTNEXTLINE(google-explicit-constructor, cppcoreguidelines-pro-type-reinterpret-cast) + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) Body(const Buffer& buffer) : StringHolder(reinterpret_cast(buffer.data), static_cast(buffer.datalen)) {} - // NOLINTNEXTLINE(google-explicit-constructor) Body(const File& file) { std::ifstream is(file.filepath, std::ifstream::binary); if (!is) { diff --git a/include/cpr/body_view.h b/include/cpr/body_view.h index 3bbf73da0..20bd6f366 100644 --- a/include/cpr/body_view.h +++ b/include/cpr/body_view.h @@ -10,12 +10,10 @@ namespace cpr { class BodyView final { public: BodyView() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) BodyView(std::string_view body) : m_body(body) {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) BodyView(const char* body) : m_body(body) {} BodyView(const char* str, size_t len) : m_body(str, len) {} - // NOLINTNEXTLINE(google-explicit-constructor, cppcoreguidelines-pro-type-reinterpret-cast) + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) BodyView(const Buffer& buffer) : m_body(reinterpret_cast(buffer.data), static_cast(buffer.datalen)) {} BodyView(const BodyView& other) = default; @@ -25,10 +23,12 @@ class BodyView final { BodyView& operator=(BodyView&& old) noexcept = default; BodyView& operator=(const BodyView& other) = default; - [[nodiscard]] std::string_view str() const { return m_body; } + [[nodiscard]] std::string_view str() const { + return m_body; + } - private: - std::string_view m_body; + private: + std::string_view m_body; }; } // namespace cpr diff --git a/include/cpr/buffer.h b/include/cpr/buffer.h index 7b4ccfcf0..04a3e2bdc 100644 --- a/include/cpr/buffer.h +++ b/include/cpr/buffer.h @@ -12,7 +12,7 @@ struct Buffer { template Buffer(Iterator begin, Iterator end, fs::path&& p_filename) - // Ignored here since libcurl reqires a long. + // Ignored here since libcurl requires a long. // There is also no way around the reinterpret_cast. // NOLINTNEXTLINE(google-runtime-int, cppcoreguidelines-pro-type-reinterpret-cast) : data{reinterpret_cast(&(*begin))}, datalen{static_cast(std::distance(begin, end))}, filename(std::move(p_filename)) { @@ -21,7 +21,7 @@ struct Buffer { } template - typename std::enable_if::iterator_category, std::random_access_iterator_tag>::value>::type is_random_access_iterator(Iterator /* begin */, Iterator /* end */) {} + static std::enable_if_t::iterator_category, std::random_access_iterator_tag>> is_random_access_iterator(Iterator /* begin */, Iterator /* end */) {} data_t data; size_t datalen; diff --git a/include/cpr/callback.h b/include/cpr/callback.h index ababb87f7..64775f0be 100644 --- a/include/cpr/callback.h +++ b/include/cpr/callback.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -14,12 +15,10 @@ namespace cpr { class ReadCallback { public: ReadCallback() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) ReadCallback(std::function p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), size{-1}, callback{std::move(p_callback)} {} ReadCallback(cpr_off_t p_size, std::function p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), size{p_size}, callback{std::move(p_callback)} {} bool operator()(char* buffer, size_t& buffer_size) const { - if(!callback) - { + if (!callback) { return true; } return callback(buffer, buffer_size, userdata); @@ -33,11 +32,9 @@ class ReadCallback { class HeaderCallback { public: HeaderCallback() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) HeaderCallback(std::function p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {} bool operator()(std::string_view header) const { - if(!callback) - { + if (!callback) { return true; } return callback(header, userdata); @@ -50,11 +47,9 @@ class HeaderCallback { class WriteCallback { public: WriteCallback() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) WriteCallback(std::function p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {} bool operator()(std::string_view data) const { - if(!callback) - { + if (!callback) { return true; } return callback(data, userdata); @@ -67,11 +62,9 @@ class WriteCallback { class ProgressCallback { public: ProgressCallback() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) ProgressCallback(std::function p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {} bool operator()(cpr_pf_arg_t downloadTotal, cpr_pf_arg_t downloadNow, cpr_pf_arg_t uploadTotal, cpr_pf_arg_t uploadNow) const { - if(!callback) - { + if (!callback) { return true; } return callback(downloadTotal, downloadNow, uploadTotal, uploadNow, userdata); @@ -83,7 +76,7 @@ class ProgressCallback { class DebugCallback { public: - enum class InfoType { + enum class InfoType : uint8_t { TEXT = 0, HEADER_IN = 1, HEADER_OUT = 2, @@ -93,11 +86,9 @@ class DebugCallback { SSL_DATA_OUT = 6, }; DebugCallback() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) DebugCallback(std::function p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {} void operator()(InfoType type, std::string_view data) const { - if(!callback) - { + if (!callback) { return; } callback(type, data, userdata); @@ -122,7 +113,7 @@ class CancellationCallback { void SetProgressCallback(ProgressCallback& u_cb); private: - std::shared_ptr cancellation_state; + std::shared_ptr cancellation_state{nullptr}; std::optional> user_cb; }; diff --git a/include/cpr/cert_info.h b/include/cpr/cert_info.h index 4d957b026..62182a5b0 100644 --- a/include/cpr/cert_info.h +++ b/include/cpr/cert_info.h @@ -24,10 +24,10 @@ class CertInfo { std::string& operator[](const size_t& pos); iterator begin(); iterator end(); - const_iterator begin() const; - const_iterator end() const; - const_iterator cbegin() const; - const_iterator cend() const; + [[nodiscard]] const_iterator begin() const; + [[nodiscard]] const_iterator end() const; + [[nodiscard]] const_iterator cbegin() const; + [[nodiscard]] const_iterator cend() const; void emplace_back(const std::string& str); void push_back(const std::string& str); void pop_back(); diff --git a/include/cpr/connect_timeout.h b/include/cpr/connect_timeout.h index 546e8a585..d608b98d0 100644 --- a/include/cpr/connect_timeout.h +++ b/include/cpr/connect_timeout.h @@ -7,9 +7,7 @@ namespace cpr { class ConnectTimeout : public Timeout { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) ConnectTimeout(const std::chrono::milliseconds& duration) : Timeout{duration} {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) ConnectTimeout(const std::int32_t& milliseconds) : Timeout{milliseconds} {} }; diff --git a/include/cpr/connection_pool.h b/include/cpr/connection_pool.h index b4a758b4f..fc75d2767 100644 --- a/include/cpr/connection_pool.h +++ b/include/cpr/connection_pool.h @@ -18,11 +18,11 @@ namespace cpr { * ```cpp * // Create a connection pool * cpr::ConnectionPool pool; - * + * * // Use the pool with requests to reuse connections * cpr::Response r1 = cpr::Get(cpr::Url{"http://example.com/api/data"}, pool); * cpr::Response r2 = cpr::Get(cpr::Url{"http://example.com/api/more"}, pool); - * + * * // Or with async requests * auto future1 = cpr::GetAsync(cpr::Url{"http://example.com/api/data"}, pool); * auto future2 = cpr::GetAsync(cpr::Url{"http://example.com/api/more"}, pool); @@ -35,24 +35,24 @@ class ConnectionPool { * Initializes the underlying CURLSH handle and sets up thread-safe locking mechanisms. **/ ConnectionPool(); - + /** * Copy constructor - creates a new connection pool sharing the same connection state. * Multiple ConnectionPool instances can share the same underlying connection pool. **/ ConnectionPool(const ConnectionPool&) = default; - + /** * Copy assignment operator is deleted to prevent accidental copying. * Use the copy constructor if you need to share the connection pool. **/ ConnectionPool& operator=(const ConnectionPool&) = delete; - + /** * Configures a CURL easy handle to use this connection pool. * This method sets up the easy handle to participate in connection sharing * managed by this pool. - * + * * @param easy_handler The CURL easy handle to configure for connection sharing. **/ void SetupHandler(CURL* easy_handler) const; @@ -65,7 +65,7 @@ class ConnectionPool { * to ensure it's destroyed last, after the CURLSH handle that references it. **/ std::shared_ptr connection_mutex_; - + /** * Shared CURL handle (CURLSH) that manages the actual connection sharing. * This handle maintains the pool of reusable connections and is configured @@ -77,4 +77,4 @@ class ConnectionPool { std::shared_ptr curl_sh_; }; } // namespace cpr -#endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/include/cpr/cookies.h b/include/cpr/cookies.h index 052d9a8c0..53f0c0a29 100644 --- a/include/cpr/cookies.h +++ b/include/cpr/cookies.h @@ -42,7 +42,7 @@ class Cookie { /** * TODO: Update the implementation using `std::chrono::utc_clock` of C++20 **/ - std::chrono::system_clock::time_point expires_{}; + std::chrono::system_clock::time_point expires_; }; class Cookies { @@ -60,10 +60,8 @@ class Cookies { **/ bool encode{true}; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Cookies(bool p_encode = true) : encode{p_encode} {} Cookies(const std::initializer_list& cookies, bool p_encode = true) : encode{p_encode}, cookies_{cookies} {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Cookies(const cpr::Cookie& cookie, bool p_encode = true) : encode{p_encode}, cookies_{cookie} {} cpr::Cookie& operator[](size_t pos); @@ -86,7 +84,6 @@ class Cookies { private: std::vector cookies_; }; - } // namespace cpr #endif diff --git a/include/cpr/cprtypes.h b/include/cpr/cprtypes.h index 6692ffd7b..20a86ded5 100644 --- a/include/cpr/cprtypes.h +++ b/include/cpr/cprtypes.h @@ -1,11 +1,10 @@ -#ifndef CPR_CPR_TYPES_H -#define CPR_CPR_TYPES_H +#ifndef CPR_CPRTYPES_H +#define CPR_CPRTYPES_H #include #include #include #include -#include #include #include #include @@ -29,16 +28,40 @@ using cpr_pf_arg_t = cpr_off_t; template class StringHolder { public: + private: StringHolder() = default; + + public: + private: explicit StringHolder(std::string str) : str_(std::move(str)) {} + + public: + private: explicit StringHolder(std::string_view str) : str_(str) {} + + public: + private: explicit StringHolder(const char* str) : str_(str) {} + + public: + private: StringHolder(const char* str, size_t len) : str_(str, len) {} + + public: + private: StringHolder(const std::initializer_list args) { str_ = std::accumulate(args.begin(), args.end(), str_); } + + public: + private: StringHolder(const StringHolder& other) = default; + + public: + private: StringHolder(StringHolder&& old) noexcept = default; + + public: virtual ~StringHolder() = default; StringHolder& operator=(StringHolder&& old) noexcept = default; @@ -105,7 +128,15 @@ class StringHolder { } protected: - std::string str_{}; + std::string str_; + friend T; + friend T; + friend T; + friend T; + friend T; + friend T; + friend T; + friend T; }; template @@ -117,11 +148,8 @@ std::ostream& operator<<(std::ostream& os, const StringHolder& s) { class Url : public StringHolder { public: Url() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Url(std::string url) : StringHolder(std::move(url)) {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Url(std::string_view url) : StringHolder(url) {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Url(const char* url) : StringHolder(url) {} Url(const char* str, size_t len) : StringHolder(std::string(str, len)) {} Url(const std::initializer_list args) : StringHolder(args) {} diff --git a/include/cpr/curl_container.h b/include/cpr/curl_container.h index 3f80586a4..1be9e62ae 100644 --- a/include/cpr/curl_container.h +++ b/include/cpr/curl_container.h @@ -1,5 +1,5 @@ -#ifndef CURL_CONTAINER_H -#define CURL_CONTAINER_H +#ifndef CPR_CURL_CONTAINER_H +#define CPR_CURL_CONTAINER_H #include #include @@ -35,22 +35,22 @@ class CurlContainer { bool encode = true; CurlContainer() = default; - CurlContainer(const std::initializer_list&); + CurlContainer(const std::initializer_list& /*containerList*/); - void Add(const std::initializer_list&); - void Add(const T&); + void Add(const std::initializer_list& /*containerList*/); + void Add(const T& /*element*/); /** * Returns the URL using curl_easy_escape(...) for escaping the given parameters. * Requires `CurlHolder`. **/ - const std::string GetContent(const CurlHolder&) const; + [[nodiscard]] const std::string GetContent(const CurlHolder& /*holder*/) const; /** * Returns the URL while ignoring `encode`. This allows calling without * active `CurlHolder`. **/ - const std::string GetContent() const; + [[nodiscard]] const std::string GetContent() const; protected: std::vector containerList_; @@ -58,4 +58,4 @@ class CurlContainer { } // namespace cpr -#endif // +#endif // CPR_CURL_CONTAINER_H diff --git a/include/cpr/curlholder.h b/include/cpr/curlholder.h index 75aff10cd..35a3a8490 100644 --- a/include/cpr/curlholder.h +++ b/include/cpr/curlholder.h @@ -1,5 +1,5 @@ -#ifndef CPR_CURL_HOLDER_H -#define CPR_CURL_HOLDER_H +#ifndef CPR_CURLHOLDER_H +#define CPR_CURLHOLDER_H #include #include diff --git a/include/cpr/error.h b/include/cpr/error.h index 0548ba9e4..5cf7e6b9e 100644 --- a/include/cpr/error.h +++ b/include/cpr/error.h @@ -14,73 +14,73 @@ namespace cpr { * cpr error codes that match the ones found inside 'curl.h'. * These error codes only include relevant error codes meaning no support for e.g. FTP errors since cpr does only support HTTP. **/ -enum class ErrorCode { +enum class ErrorCode : uint16_t { /** * Everything is good and no error occurred. **/ OK = 0, - UNSUPPORTED_PROTOCOL, - FAILED_INIT, - URL_MALFORMAT, - NOT_BUILT_IN, - COULDNT_RESOLVE_PROXY, - COULDNT_RESOLVE_HOST, - COULDNT_CONNECT, - WEIRD_SERVER_REPLY, - REMOTE_ACCESS_DENIED, - HTTP2, - PARTIAL_FILE, - QUOTE_ERROR, - HTTP_RETURNED_ERROR, - WRITE_ERROR, - UPLOAD_FAILED, - READ_ERROR, - OUT_OF_MEMORY, - OPERATION_TIMEDOUT, - RANGE_ERROR, - HTTP_POST_ERROR, - SSL_CONNECT_ERROR, - BAD_DOWNLOAD_RESUME, - FILE_COULDNT_READ_FILE, - FUNCTION_NOT_FOUND, - ABORTED_BY_CALLBACK, - BAD_FUNCTION_ARGUMENT, - INTERFACE_FAILED, - TOO_MANY_REDIRECTS, - UNKNOWN_OPTION, - SETOPT_OPTION_SYNTAX, - GOT_NOTHING, - SSL_ENGINE_NOTFOUND, - SSL_ENGINE_SETFAILED, - SEND_ERROR, - RECV_ERROR, - SSL_CERTPROBLEM, - SSL_CIPHER, - PEER_FAILED_VERIFICATION, - BAD_CONTENT_ENCODING, - FILESIZE_EXCEEDED, - USE_SSL_FAILED, - SEND_FAIL_REWIND, - SSL_ENGINE_INITFAILED, - LOGIN_DENIED, - SSL_CACERT_BADFILE, - SSL_SHUTDOWN_FAILED, - AGAIN, - SSL_CRL_BADFILE, - SSL_ISSUER_ERROR, - CHUNK_FAILED, - NO_CONNECTION_AVAILABLE, - SSL_PINNEDPUBKEYNOTMATCH, - SSL_INVALIDCERTSTATUS, - HTTP2_STREAM, - RECURSIVE_API_CALL, - AUTH_ERROR, - HTTP3, - QUIC_CONNECT_ERROR, - PROXY, - SSL_CLIENTCERT, - UNRECOVERABLE_POLL, - TOO_LARGE, + UNSUPPORTED_PROTOCOL = 1, + FAILED_INIT = 2, + URL_MALFORMAT = 3, + NOT_BUILT_IN = 4, + COULDNT_RESOLVE_PROXY = 5, + COULDNT_RESOLVE_HOST = 6, + COULDNT_CONNECT = 7, + WEIRD_SERVER_REPLY = 8, + REMOTE_ACCESS_DENIED = 9, + HTTP2 = 10, + PARTIAL_FILE = 11, + QUOTE_ERROR = 12, + HTTP_RETURNED_ERROR = 13, + WRITE_ERROR = 14, + UPLOAD_FAILED = 15, + READ_ERROR = 16, + OUT_OF_MEMORY = 17, + OPERATION_TIMEDOUT = 18, + RANGE_ERROR = 19, + HTTP_POST_ERROR = 20, + SSL_CONNECT_ERROR = 21, + BAD_DOWNLOAD_RESUME = 22, + FILE_COULDNT_READ_FILE = 23, + FUNCTION_NOT_FOUND = 24, + ABORTED_BY_CALLBACK = 25, + BAD_FUNCTION_ARGUMENT = 26, + INTERFACE_FAILED = 27, + TOO_MANY_REDIRECTS = 28, + UNKNOWN_OPTION = 29, + SETOPT_OPTION_SYNTAX = 30, + GOT_NOTHING = 31, + SSL_ENGINE_NOTFOUND = 32, + SSL_ENGINE_SETFAILED = 33, + SEND_ERROR = 34, + RECV_ERROR = 35, + SSL_CERTPROBLEM = 36, + SSL_CIPHER = 37, + PEER_FAILED_VERIFICATION = 38, + BAD_CONTENT_ENCODING = 39, + FILESIZE_EXCEEDED = 40, + USE_SSL_FAILED = 41, + SEND_FAIL_REWIND = 42, + SSL_ENGINE_INITFAILED = 43, + LOGIN_DENIED = 44, + SSL_CACERT_BADFILE = 45, + SSL_SHUTDOWN_FAILED = 46, + AGAIN = 47, + SSL_CRL_BADFILE = 48, + SSL_ISSUER_ERROR = 49, + CHUNK_FAILED = 50, + NO_CONNECTION_AVAILABLE = 51, + SSL_PINNEDPUBKEYNOTMATCH = 52, + SSL_INVALIDCERTSTATUS = 53, + HTTP2_STREAM = 54, + RECURSIVE_API_CALL = 55, + AUTH_ERROR = 56, + HTTP3 = 57, + QUIC_CONNECT_ERROR = 58, + PROXY = 59, + SSL_CLIENTCERT = 60, + UNRECOVERABLE_POLL = 61, + TOO_LARGE = 62, /** * An unknown error inside curl occurred. * Please try to reproduce it and then report it to us. @@ -178,11 +178,13 @@ class Error { } // namespace cpr +// NOLINTBEGIN(cert-dcl58-cpp) required for to_string namespace std { inline std::string to_string(const cpr::ErrorCode& code) { return cpr::get_error_code_to_string_mapping().at(code); } } // namespace std +// NOLINTEND(cert-dcl58-cpp) required for to_string #endif diff --git a/include/cpr/file.h b/include/cpr/file.h index 9f1f18cf3..7eefcfdc3 100644 --- a/include/cpr/file.h +++ b/include/cpr/file.h @@ -23,7 +23,6 @@ struct File { class Files { public: Files() = default; - // NOLINTNEXTLINE(google-explicit-constructor) Files(const File& p_file) : files{p_file} {} Files(const Files& other) = default; diff --git a/include/cpr/filesystem.h b/include/cpr/filesystem.h index f296770cf..8a3bdafe0 100644 --- a/include/cpr/filesystem.h +++ b/include/cpr/filesystem.h @@ -13,7 +13,7 @@ namespace fs = boost::filesystem; #include namespace cpr { namespace fs = std::filesystem; -} +} // namespace cpr #elif __has_include("experimental/filesystem") #include namespace cpr { diff --git a/include/cpr/http_version.h b/include/cpr/http_version.h index 3d4d58c6c..39b9512f4 100644 --- a/include/cpr/http_version.h +++ b/include/cpr/http_version.h @@ -1,10 +1,11 @@ #ifndef CPR_HTTP_VERSION_H #define CPR_HTTP_VERSION_H +#include #include namespace cpr { -enum class HttpVersionCode { +enum class HttpVersionCode : uint8_t { /** * Let libcurl decide which version is the best. **/ diff --git a/include/cpr/interceptor.h b/include/cpr/interceptor.h index d05f4c9c2..4bef2f31f 100644 --- a/include/cpr/interceptor.h +++ b/include/cpr/interceptor.h @@ -9,7 +9,7 @@ namespace cpr { class Interceptor { public: - enum class ProceedHttpMethod { + enum class ProceedHttpMethod : uint8_t { GET_REQUEST = 0, POST_REQUEST, PUT_REQUEST, @@ -40,7 +40,7 @@ class Interceptor { class InterceptorMulti { public: - enum class ProceedHttpMethod { + enum class ProceedHttpMethod : uint8_t { GET_REQUEST = 0, POST_REQUEST, PUT_REQUEST, diff --git a/include/cpr/interface.h b/include/cpr/interface.h index b98940ec2..115dc912b 100644 --- a/include/cpr/interface.h +++ b/include/cpr/interface.h @@ -11,11 +11,8 @@ namespace cpr { class Interface : public StringHolder { public: Interface() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Interface(std::string iface) : StringHolder(std::move(iface)) {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Interface(std::string_view iface) : StringHolder(iface) {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Interface(const char* iface) : StringHolder(iface) {} Interface(const char* str, size_t len) : StringHolder(str, len) {} Interface(const std::initializer_list args) : StringHolder(args) {} diff --git a/include/cpr/limit_rate.h b/include/cpr/limit_rate.h index f25c09e5b..ac3c049e1 100644 --- a/include/cpr/limit_rate.h +++ b/include/cpr/limit_rate.h @@ -1,5 +1,5 @@ -#ifndef CPR_SPEED_LIMIT_H -#define CPR_SPEED_LIMIT_H +#ifndef CPR_LIMIT_RATE_H +#define CPR_LIMIT_RATE_H #include diff --git a/include/cpr/local_port.h b/include/cpr/local_port.h index a6efe7ecf..12cee9ec5 100644 --- a/include/cpr/local_port.h +++ b/include/cpr/local_port.h @@ -7,7 +7,6 @@ namespace cpr { class LocalPort { public: - // NOLINTNEXTLINE(google-explicit-constructor) LocalPort(const std::uint16_t p_localport) : localport_(p_localport) {} operator std::uint16_t() const { diff --git a/include/cpr/local_port_range.h b/include/cpr/local_port_range.h index e048b6e97..6a7fa964d 100644 --- a/include/cpr/local_port_range.h +++ b/include/cpr/local_port_range.h @@ -7,7 +7,6 @@ namespace cpr { class LocalPortRange { public: - // NOLINTNEXTLINE(google-explicit-constructor) LocalPortRange(const std::uint16_t p_localportrange) : localportrange_(p_localportrange) {} operator std::uint16_t() const { diff --git a/include/cpr/low_speed.h b/include/cpr/low_speed.h index 8f9eb86ab..c74ceceb5 100644 --- a/include/cpr/low_speed.h +++ b/include/cpr/low_speed.h @@ -1,16 +1,16 @@ #ifndef CPR_LOW_SPEED_H #define CPR_LOW_SPEED_H -#include #include +#include namespace cpr { class LowSpeed { public: - [[deprecated("Will be removed in CPR 2.x - Use the constructor with std::chrono::seconds instead of std::int32_t")]] - LowSpeed(const std::int32_t p_limit, const std::int32_t p_time) : limit(p_limit), time(std::chrono::seconds(p_time)) {} + LowSpeed(const std::int32_t p_limit, const std::int32_t p_time) + : limit(p_limit), time(std::chrono::seconds(p_time)) {} LowSpeed(const std::int32_t p_limit, const std::chrono::seconds p_time) : limit(p_limit), time(p_time) {} diff --git a/include/cpr/multipart.h b/include/cpr/multipart.h index 9ee62d5ab..991764243 100644 --- a/include/cpr/multipart.h +++ b/include/cpr/multipart.h @@ -16,7 +16,7 @@ struct Part { Part(const std::string& p_name, const std::string& p_value, const std::string& p_content_type = {}) : name{p_name}, value{p_value}, content_type{p_content_type}, is_file{false}, is_buffer{false} {} Part(const std::string& p_name, const std::int32_t& p_value, const std::string& p_content_type = {}) : name{p_name}, value{std::to_string(p_value)}, content_type{p_content_type}, is_file{false}, is_buffer{false} {} Part(const std::string& p_name, const Files& p_files, const std::string& p_content_type = {}) : name{p_name}, content_type{p_content_type}, is_file{true}, is_buffer{false}, files{p_files} {} - Part(const std::string& p_name, Files&& p_files, const std::string& p_content_type = {}) : name{p_name}, content_type{p_content_type}, is_file{true}, is_buffer{false}, files{p_files} {} + Part(const std::string& p_name, Files&& p_files, const std::string& p_content_type = {}) : name{p_name}, content_type{p_content_type}, is_file{true}, is_buffer{false}, files{std::move(p_files)} {} Part(const std::string& p_name, const Buffer& buffer, const std::string& p_content_type = {}) : name{p_name}, value{buffer.filename.string()}, content_type{p_content_type}, data{buffer.data}, datalen{buffer.datalen}, is_file{false}, is_buffer{true} {} std::string name; @@ -34,8 +34,8 @@ struct Part { class Multipart { public: Multipart(const std::initializer_list& parts); - Multipart(const std::vector& parts); - Multipart(const std::vector&& parts); + explicit Multipart(const std::vector& parts); + explicit Multipart(const std::vector&& parts); std::vector parts; }; diff --git a/include/cpr/multiperform.h b/include/cpr/multiperform.h index a48123792..e4ca75f92 100644 --- a/include/cpr/multiperform.h +++ b/include/cpr/multiperform.h @@ -16,7 +16,7 @@ class InterceptorMulti; class MultiPerform { public: - enum class HttpMethod { + enum class HttpMethod : uint8_t { UNDEFINED = 0, GET_REQUEST, POST_REQUEST, @@ -67,7 +67,7 @@ class MultiPerform { template void PrepareDownloadSessions(size_t sessions_index, CurrentDownloadArgType current_arg, DownloadArgTypes... args); template - void PrepareDownloadSessions(size_t sessions_index, CurrentDownloadArgType current_arg); + void PrepareDownloadSessions(size_t sessions_index, const CurrentDownloadArgType& current_arg); void PrepareDownloadSession(size_t sessions_index, std::ofstream& file); void PrepareDownloadSession(size_t sessions_index, const WriteCallback& write); @@ -102,7 +102,7 @@ class MultiPerform { }; template -void MultiPerform::PrepareDownloadSessions(size_t sessions_index, CurrentDownloadArgType current_arg) { +void MultiPerform::PrepareDownloadSessions(size_t sessions_index, const CurrentDownloadArgType& current_arg) { PrepareDownloadSession(sessions_index, current_arg); } diff --git a/include/cpr/proxies.h b/include/cpr/proxies.h index 6970442de..60108af7f 100644 --- a/include/cpr/proxies.h +++ b/include/cpr/proxies.h @@ -10,9 +10,9 @@ class Proxies { public: Proxies() = default; Proxies(const std::initializer_list>& hosts); - Proxies(const std::map& hosts); + explicit Proxies(const std::map& hosts); - bool has(const std::string& protocol) const; + [[nodiscard]] bool has(const std::string& protocol) const; const std::string& operator[](const std::string& protocol); private: diff --git a/include/cpr/range.h b/include/cpr/range.h index 2c5a145d5..c6ab6901a 100644 --- a/include/cpr/range.h +++ b/include/cpr/range.h @@ -8,7 +8,7 @@ namespace cpr { class Range { public: - Range(const std::optional p_resume_from = std::nullopt, const std::optional p_finish_at = std::nullopt) { + explicit Range(const std::optional p_resume_from = std::nullopt, const std::optional p_finish_at = std::nullopt) { resume_from = p_resume_from.value_or(0); finish_at = p_finish_at.value_or(-1); } @@ -16,9 +16,9 @@ class Range { std::int64_t resume_from; std::int64_t finish_at; - const std::string str() const { - std::string from_str = (resume_from < 0U) ? "" : std::to_string(resume_from); - std::string to_str = (finish_at < 0U) ? "" : std::to_string(finish_at); + [[nodiscard]] const std::string str() const { + std::string const from_str = (resume_from < 0U) ? "" : std::to_string(resume_from); + std::string const to_str = (finish_at < 0U) ? "" : std::to_string(finish_at); return from_str + "-" + to_str; } }; @@ -27,9 +27,9 @@ class MultiRange { public: MultiRange(std::initializer_list rs) : ranges{rs} {} - const std::string str() const { + [[nodiscard]] const std::string str() const { std::string multi_range_string{}; - for (Range range : ranges) { + for (Range const range : ranges) { multi_range_string += ((multi_range_string.empty()) ? "" : ", ") + range.str(); } return multi_range_string; diff --git a/include/cpr/reserve_size.h b/include/cpr/reserve_size.h index 5eae4c80a..93bb31e86 100644 --- a/include/cpr/reserve_size.h +++ b/include/cpr/reserve_size.h @@ -7,9 +7,9 @@ namespace cpr { class ReserveSize { public: - ReserveSize(const size_t _size) : size(_size) {} + ReserveSize(const std::size_t _size) : size(_size) {} - size_t size = 0; + std::size_t size = 0; }; } // namespace cpr diff --git a/include/cpr/response.h b/include/cpr/response.h index 5606253b9..1f85b3576 100644 --- a/include/cpr/response.h +++ b/include/cpr/response.h @@ -28,21 +28,21 @@ class Response { // Ignored here since libcurl uses a long for this. // NOLINTNEXTLINE(google-runtime-int) long status_code{}; - std::string text{}; - Header header{}; - Url url{}; + std::string text; + Header header; + Url url; double elapsed{}; - Cookies cookies{}; - Error error{}; - std::string raw_header{}; - std::string status_line{}; - std::string reason{}; + Cookies cookies; + Error error; + std::string raw_header; + std::string status_line; + std::string reason; cpr_off_t uploaded_bytes{}; cpr_off_t downloaded_bytes{}; // Ignored here since libcurl uses a long for this. // NOLINTNEXTLINE(google-runtime-int) long redirect_count{}; - std::string primary_ip{}; + std::string primary_ip; std::uint16_t primary_port{}; Response() = default; diff --git a/include/cpr/secure_string.h b/include/cpr/secure_string.h index 86c38c6b4..35a6cbb4b 100644 --- a/include/cpr/secure_string.h +++ b/include/cpr/secure_string.h @@ -16,11 +16,9 @@ struct SecureAllocator : private std::allocator { friend struct SecureAllocator; SecureAllocator() = default; template - // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions) SecureAllocator(const SecureAllocator& rhs) noexcept : std::allocator(static_cast&>(rhs)) {} template - // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions) - SecureAllocator(SecureAllocator&& rhs) noexcept : std::allocator(static_cast&&>(rhs)) {} + SecureAllocator(SecureAllocator&& rhs) noexcept : std::allocator(static_cast&&>(std::move(rhs))) {} template SecureAllocator& operator=(const SecureAllocator& rhs) noexcept { static_cast&>(*this) = static_cast&>(rhs); @@ -28,7 +26,7 @@ struct SecureAllocator : private std::allocator { } template SecureAllocator& operator=(SecureAllocator&& rhs) noexcept { - static_cast&>(*this) = static_cast&&>(rhs); + static_cast&>(*this) = static_cast&&>(std::move(rhs)); return *this; } diff --git a/include/cpr/singleton.h b/include/cpr/singleton.h index e52439bf1..ccff28483 100644 --- a/include/cpr/singleton.h +++ b/include/cpr/singleton.h @@ -4,6 +4,8 @@ #include #include +// NOLINTBEGIN(cppcoreguidelines-macro-usage, bugprone-macro-parentheses) + #ifndef CPR_DISABLE_COPY #define CPR_DISABLE_COPY(Class) \ Class(const Class&) = delete; \ @@ -41,4 +43,6 @@ } #endif -#endif +// NOLINTEND(cppcoreguidelines-macro-usage, bugprone-macro-parentheses) + +#endif \ No newline at end of file diff --git a/include/cpr/sse.h b/include/cpr/sse.h index 2a137b361..54746a341 100644 --- a/include/cpr/sse.h +++ b/include/cpr/sse.h @@ -3,10 +3,10 @@ #include #include -#include #include #include #include +#include namespace cpr { @@ -74,7 +74,6 @@ class ServerSentEventParser { class ServerSentEventCallback { public: ServerSentEventCallback() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) ServerSentEventCallback(std::function p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {} bool operator()(ServerSentEvent&& event) const { diff --git a/include/cpr/ssl_options.h b/include/cpr/ssl_options.h index d7d43d60c..338744ea9 100644 --- a/include/cpr/ssl_options.h +++ b/include/cpr/ssl_options.h @@ -1,5 +1,5 @@ -#ifndef CPR_SSLOPTIONS_H -#define CPR_SSLOPTIONS_H +#ifndef CPR_SSL_OPTIONS_H +#define CPR_SSL_OPTIONS_H #include #include @@ -79,7 +79,6 @@ namespace cpr { class VerifySsl { public: VerifySsl() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) VerifySsl(bool p_verify) : verify(p_verify) {} explicit operator bool() const { @@ -94,14 +93,13 @@ namespace ssl { // set SSL client certificate class CertFile { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) CertFile(fs::path&& p_filename) : filename(std::move(p_filename)) {} virtual ~CertFile() = default; const fs::path filename; - virtual const char* GetCertType() const { + [[nodiscard]] virtual const char* GetCertType() const { return "PEM"; } }; @@ -110,12 +108,11 @@ using PemCert = CertFile; class DerCert : public CertFile { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) DerCert(fs::path&& p_filename) : CertFile(std::move(p_filename)) {} ~DerCert() override = default; - const char* GetCertType() const override { + [[nodiscard]] const char* GetCertType() const override { return "DER"; } }; @@ -123,15 +120,14 @@ class DerCert : public CertFile { #if SUPPORT_CURLOPT_SSLCERT_BLOB class CertBlob { -public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) + public: CertBlob(std::string&& p_blob) : blob(std::move(p_blob)) {} virtual ~CertBlob() = default; std::string blob; - virtual const char* GetCertType() const { + [[nodiscard]] virtual const char* GetCertType() const { return "PEM"; } }; @@ -139,14 +135,14 @@ class CertBlob { using PemBlob = CertBlob; class DerBlob : public CertBlob { -public: + public: template - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) - DerBlob(BlobType&& p_blob) : CertBlob(std::move(p_blob)) {} + // NOLINTNEXTLINE(bugprone-forwarding-reference-overload) + DerBlob(BlobType&& p_blob) : CertBlob(std::forward(p_blob)) {} ~DerBlob() override = default; - const char* GetCertType() const override { + [[nodiscard]] const char* GetCertType() const override { return "DER"; } }; @@ -155,7 +151,6 @@ class DerBlob : public CertBlob { // specify private keyfile for TLS and SSL client cert class KeyFile { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) KeyFile(fs::path&& p_filename) : filename(std::move(p_filename)) {} template @@ -166,7 +161,7 @@ class KeyFile { fs::path filename; util::SecureString password; - virtual const char* GetKeyType() const { + [[nodiscard]] virtual const char* GetKeyType() const { return "PEM"; } }; @@ -174,7 +169,6 @@ class KeyFile { #if SUPPORT_CURLOPT_SSLKEY_BLOB class KeyBlob { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) KeyBlob(std::string&& p_blob) : blob(std::move(p_blob)) {} template @@ -185,7 +179,7 @@ class KeyBlob { std::string blob; util::SecureString password; - virtual const char* GetKeyType() const { + [[nodiscard]] virtual const char* GetKeyType() const { return "PEM"; } }; @@ -195,7 +189,6 @@ using PemKey = KeyFile; class DerKey : public KeyFile { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) DerKey(fs::path&& p_filename) : KeyFile(std::move(p_filename)) {} template @@ -203,14 +196,13 @@ class DerKey : public KeyFile { ~DerKey() override = default; - const char* GetKeyType() const override { + [[nodiscard]] const char* GetKeyType() const override { return "DER"; } }; class PinnedPublicKey { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) PinnedPublicKey(std::string&& p_pinned_public_key) : pinned_public_key(std::move(p_pinned_public_key)) {} const std::string pinned_public_key; @@ -222,7 +214,6 @@ class PinnedPublicKey { class ALPN { public: ALPN() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) ALPN(bool p_enabled) : enabled(p_enabled) {} explicit operator bool() const { @@ -239,7 +230,6 @@ class ALPN { class NPN { public: NPN() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) NPN(bool p_enabled) : enabled(p_enabled) {} explicit operator bool() const { @@ -255,7 +245,6 @@ class NPN { class VerifyHost { public: VerifyHost() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) VerifyHost(bool p_enabled) : enabled(p_enabled) {} explicit operator bool() const { @@ -269,7 +258,6 @@ class VerifyHost { class VerifyPeer { public: VerifyPeer() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) VerifyPeer(bool p_enabled) : enabled(p_enabled) {} explicit operator bool() const { @@ -283,7 +271,6 @@ class VerifyPeer { // "Certificate Status Request" TLS extension (aka. OCSP stapling). class VerifyStatus { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) VerifyStatus(bool p_enabled) : enabled(p_enabled) {} explicit operator bool() const { @@ -344,7 +331,6 @@ struct MaxTLSv1_3 {}; // path to Certificate Authority (CA) bundle class CaInfo { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) CaInfo(fs::path&& p_filename) : filename(std::move(p_filename)) {} fs::path filename; @@ -353,8 +339,7 @@ class CaInfo { #if SUPPORT_CURLOPT_CAINFO_BLOB // Certificate Authority (CA) bundle as blob class CaInfoBlob { -public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) + public: CaInfoBlob(std::string&& p_blob) : blob(std::move(p_blob)) {} std::string blob; @@ -364,7 +349,6 @@ class CaInfoBlob { // specify directory holding CA certificates class CaPath { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) CaPath(fs::path&& p_filename) : filename(std::move(p_filename)) {} fs::path filename; @@ -373,7 +357,6 @@ class CaPath { #if SUPPORT_CURLOPT_SSL_CTX_FUNCTION class CaBuffer { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) CaBuffer(std::string&& p_buffer) : buffer(std::move(p_buffer)) {} const std::string buffer; @@ -383,7 +366,6 @@ class CaBuffer { // specify a Certificate Revocation List file class Crl { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Crl(fs::path&& p_filename) : filename(std::move(p_filename)) {} fs::path filename; @@ -392,7 +374,6 @@ class Crl { // specify ciphers to use for TLS class Ciphers { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Ciphers(std::string&& p_ciphers) : ciphers(std::move(p_ciphers)) {} std::string ciphers; @@ -402,7 +383,6 @@ class Ciphers { // specify ciphers suites to use for TLS 1.3 class TLS13_Ciphers { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) TLS13_Ciphers(std::string&& p_ciphers) : ciphers(std::move(p_ciphers)) {} std::string ciphers; @@ -414,7 +394,6 @@ class TLS13_Ciphers { class SessionIdCache { public: SessionIdCache() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) SessionIdCache(bool p_enabled) : enabled(p_enabled) {} explicit operator bool() const { @@ -429,7 +408,6 @@ class SessionIdCache { class SslFastStart { public: SslFastStart() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) SslFastStart(bool p_enabled) : enabled(p_enabled) {} explicit operator bool() const { @@ -443,7 +421,6 @@ class SslFastStart { class NoRevoke { public: NoRevoke() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) NoRevoke(bool p_enabled) : enabled(p_enabled) {} explicit operator bool() const { @@ -658,7 +635,7 @@ void set_ssl_option(SslOptions& opts, T&& t) { template void set_ssl_option(SslOptions& opts, T&& t, Ts&&... ts) { set_ssl_option(opts, std::forward(t)); - set_ssl_option(opts, std::move(ts)...); + set_ssl_option(opts, std::forward(ts)...); } } // namespace priv @@ -666,7 +643,7 @@ void set_ssl_option(SslOptions& opts, T&& t, Ts&&... ts) { template SslOptions Ssl(Ts&&... ts) { SslOptions opts; - priv::set_ssl_option(opts, std::move(ts)...); + priv::set_ssl_option(opts, std::forward(ts)...); return opts; } diff --git a/include/cpr/threadpool.h b/include/cpr/threadpool.h index 346ff025c..0532a5bed 100644 --- a/include/cpr/threadpool.h +++ b/include/cpr/threadpool.h @@ -1,9 +1,10 @@ -#ifndef CPR_THREAD_POOL_H -#define CPR_THREAD_POOL_H +#ifndef CPR_THREADPOOL_H +#define CPR_THREADPOOL_H #include #include #include +#include #include #include #include @@ -53,19 +54,19 @@ class ThreadPool { return idle_thread_num; } - bool IsStarted() const { - return status != STOP; + [[nodiscard]] bool IsStarted() const { + return status != Status::STOP; } - bool IsStopped() const { - return status == STOP; + [[nodiscard]] bool IsStopped() const { + return status == Status::STOP; } int Start(size_t start_threads = 0); int Stop(); int Pause(); int Resume(); - int Wait() const; + void Wait(); /** * Return a future, calling future.get() will wait task done and return RetType. @@ -75,7 +76,7 @@ class ThreadPool { **/ template auto Submit(Fn&& fn, Args&&... args) { - if (status == STOP) { + if (status == Status::STOP) { Start(); } if (idle_thread_num <= 0 && cur_thread_num < max_thread_num) { @@ -85,7 +86,7 @@ class ThreadPool { auto task = std::make_shared>([fn = std::forward(fn), args...]() mutable { return std::invoke(fn, args...); }); std::future future = task->get_future(); { - std::lock_guard locker(task_mutex); + std::scoped_lock const locker(task_mutex); tasks.emplace([task] { (*task)(); }); } @@ -104,7 +105,7 @@ class ThreadPool { std::chrono::milliseconds max_idle_time; private: - enum Status { + enum class Status : uint8_t { STOP, RUNNING, PAUSE, @@ -113,24 +114,24 @@ class ThreadPool { struct ThreadData { std::shared_ptr thread; std::thread::id id; - Status status; + Status status{Status::STOP}; std::chrono::steady_clock::time_point start_time; std::chrono::steady_clock::time_point stop_time; }; std::atomic status{Status::STOP}; - std::condition_variable status_wait_cond{}; - std::mutex status_wait_mutex{}; + std::condition_variable status_wait_cond; + std::mutex status_wait_mutex; std::atomic cur_thread_num{0}; std::atomic idle_thread_num{0}; - std::list threads{}; - std::mutex thread_mutex{}; + std::list threads; + std::mutex thread_mutex; - std::queue tasks{}; - std::mutex task_mutex{}; - std::condition_variable task_cond{}; + std::queue tasks; + std::mutex task_mutex; + std::condition_variable task_cond; }; } // namespace cpr diff --git a/include/cpr/timeout.h b/include/cpr/timeout.h index af80a1b7a..e2464803d 100644 --- a/include/cpr/timeout.h +++ b/include/cpr/timeout.h @@ -9,16 +9,14 @@ namespace cpr { class Timeout { public: // Template constructor to accept any chrono duration type and convert it to milliseconds - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) template Timeout(const std::chrono::duration& duration) : ms{std::chrono::duration_cast(duration)} {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Timeout(const std::int32_t& milliseconds) : Timeout{std::chrono::milliseconds(milliseconds)} {} // No way around since curl uses a long here. // NOLINTNEXTLINE(google-runtime-int) - long Milliseconds() const; + [[nodiscard]] long Milliseconds() const; std::chrono::milliseconds ms; }; diff --git a/include/cpr/unix_socket.h b/include/cpr/unix_socket.h index 152caf0ca..4b8701634 100644 --- a/include/cpr/unix_socket.h +++ b/include/cpr/unix_socket.h @@ -7,10 +7,9 @@ namespace cpr { class UnixSocket { public: - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) UnixSocket(std::string unix_socket) : unix_socket_(std::move(unix_socket)) {} - const char* GetUnixSocketString() const noexcept; + [[nodiscard]] const char* GetUnixSocketString() const noexcept; private: const std::string unix_socket_; diff --git a/include/cpr/user_agent.h b/include/cpr/user_agent.h index a3cc1293d..5cb04ea7c 100644 --- a/include/cpr/user_agent.h +++ b/include/cpr/user_agent.h @@ -1,5 +1,5 @@ -#ifndef CPR_USERAGENT_H -#define CPR_USERAGENT_H +#ifndef CPR_USER_AGENT_H +#define CPR_USER_AGENT_H #include #include @@ -10,11 +10,8 @@ namespace cpr { class UserAgent : public StringHolder { public: UserAgent() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) UserAgent(std::string useragent) : StringHolder(std::move(useragent)) {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) UserAgent(std::string_view useragent) : StringHolder(useragent) {} - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) UserAgent(const char* useragent) : StringHolder(useragent) {} UserAgent(const char* str, size_t len) : StringHolder(str, len) {} UserAgent(const std::initializer_list args) : StringHolder(args) {} diff --git a/include/cpr/util.h b/include/cpr/util.h index 4ffd02df6..e210b19b7 100644 --- a/include/cpr/util.h +++ b/include/cpr/util.h @@ -42,7 +42,7 @@ bool isTrue(const std::string& s); * Parses the given std::string into time_t (unix ms). * This parsing happens time_t size agnostic since time_t does not use the same underlying type on all systems/compilers. **/ -time_t sTimestampToT(const std::string&); +time_t sTimestampToT(const std::string& /*st*/); } // namespace cpr::util diff --git a/include/cpr/verbose.h b/include/cpr/verbose.h index 2bf0df8f0..46714200d 100644 --- a/include/cpr/verbose.h +++ b/include/cpr/verbose.h @@ -6,7 +6,6 @@ namespace cpr { class Verbose { public: Verbose() = default; - // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) Verbose(const bool p_verbose) : verbose{p_verbose} {} bool verbose = true; diff --git a/test/abstractServer.hpp b/test/abstractServer.hpp index 2365b51af..25c831d54 100644 --- a/test/abstractServer.hpp +++ b/test/abstractServer.hpp @@ -6,8 +6,8 @@ #include #include #include -#include #include +#include #include "cpr/cpr.h" #include "mongoose.h" diff --git a/test/connection_pool_tests.cpp b/test/connection_pool_tests.cpp index ba546a5cd..91b1ccaaf 100644 --- a/test/connection_pool_tests.cpp +++ b/test/connection_pool_tests.cpp @@ -1,9 +1,9 @@ #include +#include #include -#include #include -#include +#include #include @@ -48,22 +48,22 @@ TEST(MultipleGetTests, PoolAsyncGetMultipleTest) { server->ResetConnectionCount(); const size_t NUM_BATCHES = 2; - const size_t BATCH_SIZE = NUM_REQUESTS / 2; // 5 requests per batch + const size_t BATCH_SIZE = NUM_REQUESTS / 2; // 5 requests per batch // Without shared connection pool - two batches with 10ms sleep responses.reserve(NUM_REQUESTS); - + for (size_t batch = 0; batch < NUM_BATCHES; ++batch) { for (size_t i = 0; i < BATCH_SIZE; ++i) { responses.emplace_back(cpr::GetAsync(url)); } - + // Sleep between batches but not after the last batch if (batch != NUM_BATCHES - 1) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); } } - + // Wait for all responses for (AsyncResponse& future : responses) { Response response = future.get(); @@ -78,12 +78,12 @@ TEST(MultipleGetTests, PoolAsyncGetMultipleTest) { server->ResetConnectionCount(); responses.clear(); responses.reserve(NUM_REQUESTS); - + for (size_t batch = 0; batch < NUM_BATCHES; ++batch) { for (size_t i = 0; i < BATCH_SIZE; ++i) { responses.emplace_back(cpr::GetAsync(url, pool)); } - + // Sleep between batches but not after the last batch if (batch != NUM_BATCHES - 1) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); @@ -98,7 +98,7 @@ TEST(MultipleGetTests, PoolAsyncGetMultipleTest) { EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); EXPECT_EQ(200, response.status_code); } - + // With connection pooling, should use fewer connections than requests EXPECT_LT(server->GetConnectionCount(), NUM_REQUESTS); } @@ -107,4 +107,4 @@ int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::testing::AddGlobalTestEnvironment(server); return RUN_ALL_TESTS(); -} \ No newline at end of file +} \ No newline at end of file diff --git a/test/curlholder_tests.cpp b/test/curlholder_tests.cpp index c7cdc454a..2187cff4b 100644 --- a/test/curlholder_tests.cpp +++ b/test/curlholder_tests.cpp @@ -10,7 +10,7 @@ TEST(CurlholderTests, MoveOperator) { cpr::CurlHolder a; cpr::CurlHolder b; - + a = std::move(b); } diff --git a/test/proxy_tests.cpp b/test/proxy_tests.cpp index a5a821c6f..8d8423455 100644 --- a/test/proxy_tests.cpp +++ b/test/proxy_tests.cpp @@ -1,9 +1,9 @@ #include #include +#include #include #include -#include #include "cpr/cpr.h"