Skip to content

Commit 64d84ae

Browse files
authored
Merge pull request #1120 from libcpr/feature/curl_error_code_version_check
Curl Minimum Supported Version 7.64.0
2 parents 225b745 + 88153bb commit 64d84ae

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ if(CPR_USE_SYSTEM_CURL)
183183
find_package(CURL COMPONENTS HTTP HTTPS)
184184
if(CURL_FOUND)
185185
message(STATUS "Curl ${CURL_VERSION_STRING} found on this system.")
186+
186187
# To be able to load certificates under Windows when using OpenSSL:
187188
if(CMAKE_USE_OPENSSL AND WIN32 AND (NOT (CURL_VERSION_STRING VERSION_GREATER_EQUAL "7.71.0")))
188189
message(FATAL_ERROR "Your system curl version (${CURL_VERSION_STRING}) is too old to support OpenSSL on Windows which requires curl >= 7.71.0. Update your curl version, use WinSSL, disable SSL or use the built-in version of curl.")
@@ -203,6 +204,11 @@ if(CPR_USE_SYSTEM_CURL)
203204
message(FATAL_ERROR "Curl not found on this system. To use the built-in version set CPR_USE_SYSTEM_CURL to OFF.")
204205
endif()
205206
endif()
207+
208+
# Check for the minimum supported curl version
209+
if(NOT (CURL_VERSION_STRING VERSION_GREATER_EQUAL "7.64.0"))
210+
message(FATAL_ERROR "Your system curl version (${CURL_VERSION_STRING}) is too old! curl >= 7.64.0 is required. Update your curl version, or use the build in curl version e.g. via `cmake .. -DCPR_USE_SYSTEM_CURL=OFF` during CMake configure.")
211+
endif()
206212
else()
207213
message(STATUS "Configuring built-in curl...")
208214

@@ -287,6 +293,15 @@ else()
287293
restore_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)
288294
endif()
289295

296+
# Depending on which version of libcurl we are using the CMake target is called differently
297+
if(TARGET libcurl)
298+
# Old curl CMake target name
299+
set(CURL_LIB libcurl)
300+
else()
301+
# New curl CMake target name
302+
set(CURL_LIB CURL::libcurl)
303+
endif()
304+
290305
# GTest configuration
291306
if(CPR_BUILD_TESTS)
292307
if(CPR_USE_SYSTEM_GTEST)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ The only explicit requirements are:
177177
* a `C++17` compatible compiler such as Clang or GCC. The minimum required version of GCC is unknown, so if anyone has trouble building this library with a specific version of GCC, do let us know
178178
* in case you only have a `C++11` compatible compiler available, all versions below cpr 1.9.x are for you. The 1.10.0 release of cpr switches to `C++17` as a requirement.
179179
* If you would like to perform https requests `OpenSSL` and its development libraries are required.
180+
* If you do not use the build in version of [curl](https://github.com/curl/curl) but instead use your systems version, make sure you use a version `>= 7.64.0`. Lower versions are not supported. This means you need Debian `>= 10` or Ubuntu `>= 20.04 LTS`.
180181

181182
## Building cpr - Using vcpkg
182183

cpr/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ add_library(cpr
3232

3333
add_library(cpr::cpr ALIAS cpr)
3434

35-
target_link_libraries(cpr PUBLIC CURL::libcurl) # todo should be private, but first dependencies in ssl_options need to be removed
35+
target_link_libraries(cpr PUBLIC ${CURL_LIB}) # todo should be private, but first dependencies in ssl_options need to be removed
3636

3737
# Fix missing OpenSSL includes for Windows since in 'ssl_ctx.cpp' we include OpenSSL directly
3838
if(SSL_BACKEND_USED STREQUAL "OpenSSL")

cpr/error.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ ErrorCode Error::getErrorCodeForCurlError(std::int32_t curl_code) {
2222
return ErrorCode::COULDNT_RESOLVE_HOST;
2323
case CURLE_COULDNT_CONNECT:
2424
return ErrorCode::COULDNT_CONNECT;
25+
// Name changed in curl >= 7.51.0.
26+
#if LIBCURL_VERSION_NUM >= 0x073300
2527
case CURLE_WEIRD_SERVER_REPLY:
28+
#else
29+
case CURLE_FTP_WEIRD_SERVER_REPLY:
30+
#endif
2631
return ErrorCode::WEIRD_SERVER_REPLY;
2732
case CURLE_REMOTE_ACCESS_DENIED:
2833
return ErrorCode::REMOTE_ACCESS_DENIED;
@@ -66,8 +71,11 @@ ErrorCode Error::getErrorCodeForCurlError(std::int32_t curl_code) {
6671
return ErrorCode::TOO_MANY_REDIRECTS;
6772
case CURLE_UNKNOWN_OPTION:
6873
return ErrorCode::UNKNOWN_OPTION;
74+
// Added in curl 7.78.0.
75+
#if LIBCURL_VERSION_NUM >= 0x074E00
6976
case CURLE_SETOPT_OPTION_SYNTAX:
7077
return ErrorCode::SETOPT_OPTION_SYNTAX;
78+
#endif
7179
case CURLE_GOT_NOTHING:
7280
return ErrorCode::GOT_NOTHING;
7381
case CURLE_SSL_ENGINE_NOTFOUND:
@@ -94,44 +102,98 @@ ErrorCode Error::getErrorCodeForCurlError(std::int32_t curl_code) {
94102
return ErrorCode::SEND_FAIL_REWIND;
95103
case CURLE_SSL_ENGINE_INITFAILED:
96104
return ErrorCode::SSL_ENGINE_INITFAILED;
105+
// Added in curl 7.13.1.
106+
#if LIBCURL_VERSION_NUM >= 0x070D01
97107
case CURLE_LOGIN_DENIED:
98108
return ErrorCode::LOGIN_DENIED;
109+
#endif
110+
// Added in curl 7.16.0.
111+
#if LIBCURL_VERSION_NUM >= 0x071000
99112
case CURLE_SSL_CACERT_BADFILE:
100113
return ErrorCode::SSL_CACERT_BADFILE;
114+
#endif
115+
// Added in curl 7.16.1.
116+
#if LIBCURL_VERSION_NUM >= 0x071001
101117
case CURLE_SSL_SHUTDOWN_FAILED:
102118
return ErrorCode::SSL_SHUTDOWN_FAILED;
119+
#endif
120+
// Added in curl 7.18.2.
121+
#if LIBCURL_VERSION_NUM >= 0x071202
103122
case CURLE_AGAIN:
104123
return ErrorCode::AGAIN;
124+
#endif
125+
// Added in curl 7.19.0.
126+
#if LIBCURL_VERSION_NUM >= 0x071300
105127
case CURLE_SSL_CRL_BADFILE:
106128
return ErrorCode::SSL_CRL_BADFILE;
107129
case CURLE_SSL_ISSUER_ERROR:
108130
return ErrorCode::SSL_ISSUER_ERROR;
131+
#endif
132+
// Added in curl 7.21.0.
133+
#if LIBCURL_VERSION_NUM >= 0x071500
109134
case CURLE_CHUNK_FAILED:
110135
return ErrorCode::CHUNK_FAILED;
136+
#endif
137+
// Added in curl 7.30.0.
138+
#if LIBCURL_VERSION_NUM >= 0x071E00
111139
case CURLE_NO_CONNECTION_AVAILABLE:
112140
return ErrorCode::NO_CONNECTION_AVAILABLE;
141+
#endif
142+
// Added in curl 7.39.0.
143+
#if LIBCURL_VERSION_NUM >= 0x072700
113144
case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
114145
return ErrorCode::SSL_PINNEDPUBKEYNOTMATCH;
146+
#endif
147+
// Added in curl 7.41.0.
148+
#if LIBCURL_VERSION_NUM >= 0x072900
115149
case CURLE_SSL_INVALIDCERTSTATUS:
116150
return ErrorCode::SSL_INVALIDCERTSTATUS;
151+
#endif
152+
// Added in curl 7.49.0.
153+
#if LIBCURL_VERSION_NUM >= 0x073100
117154
case CURLE_HTTP2_STREAM:
118155
return ErrorCode::HTTP2_STREAM;
156+
#endif
157+
// Added in curl 7.59.0.
158+
#if LIBCURL_VERSION_NUM >= 0x073B00
119159
case CURLE_RECURSIVE_API_CALL:
120160
return ErrorCode::RECURSIVE_API_CALL;
161+
#endif
162+
// Added in curl 7.66.0.
163+
#if LIBCURL_VERSION_NUM >= 0x074200
121164
case CURLE_AUTH_ERROR:
122165
return ErrorCode::AUTH_ERROR;
166+
#endif
167+
// Added in curl 7.68.0.
168+
#if LIBCURL_VERSION_NUM >= 0x074400
123169
case CURLE_HTTP3:
124170
return ErrorCode::HTTP3;
171+
#endif
172+
// Added in curl 7.69.0.
173+
#if LIBCURL_VERSION_NUM >= 0x074500
125174
case CURLE_QUIC_CONNECT_ERROR:
126175
return ErrorCode::QUIC_CONNECT_ERROR;
176+
#endif
177+
// Added in curl 7.73.0.
178+
#if LIBCURL_VERSION_NUM >= 0x074900
127179
case CURLE_PROXY:
128180
return ErrorCode::PROXY;
181+
#endif
182+
// Added in curl 7.77.0.
183+
#if LIBCURL_VERSION_NUM >= 0x074D00
129184
case CURLE_SSL_CLIENTCERT:
130185
return ErrorCode::SSL_CLIENTCERT;
186+
#endif
187+
// Added in curl 7.84.0.
188+
#if LIBCURL_VERSION_NUM >= 0x075400
131189
case CURLE_UNRECOVERABLE_POLL:
132190
return ErrorCode::UNRECOVERABLE_POLL;
191+
#endif
192+
// Added in curl 7.6.0.
193+
#if LIBCURL_VERSION_NUM >= 0x080600
133194
case CURLE_TOO_LARGE:
134195
return ErrorCode::TOO_LARGE;
196+
#endif
135197
default:
136198
return ErrorCode::UNKNOWN_ERROR;
137199
}

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ macro(add_cpr_test _TEST_NAME)
2626
test_server
2727
GTest::GTest
2828
cpr::cpr
29-
CURL::libcurl)
29+
${CURL_LIB})
3030
add_test(NAME cpr_${_TEST_NAME}_tests COMMAND ${_TEST_NAME}_tests)
3131
# Group under the "tests" project folder in IDEs such as Visual Studio.
3232
set_property(TARGET ${_TEST_NAME}_tests PROPERTY FOLDER "tests")

0 commit comments

Comments
 (0)