Skip to content

Commit e62a4b0

Browse files
yersegSergey Kazmin
and
Sergey Kazmin
authored
Co-authored-by: Sergey Kazmin <[email protected]>
1 parent ff34749 commit e62a4b0

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

Diff for: CMakeLists.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* HTTPLIB_REQUIRE_OPENSSL (default off)
77
* HTTPLIB_REQUIRE_ZLIB (default off)
88
* HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on)
9+
* HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on)
910
* HTTPLIB_REQUIRE_BROTLI (default off)
1011
* HTTPLIB_COMPILE (default off)
1112
* HTTPLIB_TEST (default off)
@@ -43,6 +44,7 @@
4344
* HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled.
4445
* HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled.
4546
* HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled.
47+
* HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN - a bool for if support of loading system certs from the Apple Keychain is enabled.
4648
* HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only.
4749
* HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include).
4850
* HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so).
@@ -92,6 +94,7 @@ endif()
9294
option(HTTPLIB_TEST "Enables testing and builds tests" OFF)
9395
option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF)
9496
option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON)
97+
option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system certs from the Apple Keychain." ON)
9598
# Defaults to static library
9699
option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF)
97100
if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
@@ -137,6 +140,10 @@ if(Brotli_FOUND)
137140
set(HTTPLIB_IS_USING_BROTLI TRUE)
138141
endif()
139142

143+
if(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
144+
set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN TRUE)
145+
endif()
146+
140147
# Used for default, common dirs that the end-user can change (if needed)
141148
# like CMAKE_INSTALL_INCLUDEDIR or CMAKE_INSTALL_DATADIR
142149
include(GNUInstallDirs)
@@ -207,7 +214,7 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
207214
$<$<PLATFORM_ID:Windows>:crypt32>
208215
$<$<PLATFORM_ID:Windows>:cryptui>
209216
# Needed for API from MacOS Security framework
210-
"$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>>:-framework CoreFoundation -framework Security>"
217+
"$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>, $<BOOL:${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN}>>:-framework CoreFoundation -framework Security>"
211218
# Can't put multiple targets in a single generator expression or it bugs out.
212219
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::common>
213220
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::encoder>
@@ -222,6 +229,7 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
222229
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:CPPHTTPLIB_BROTLI_SUPPORT>
223230
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:CPPHTTPLIB_ZLIB_SUPPORT>
224231
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:CPPHTTPLIB_OPENSSL_SUPPORT>
232+
$<$<AND:$<PLATFORM_ID:Darwin>,$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>, $<BOOL:${HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN}>>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN>
225233
)
226234

227235
# CMake configuration files installation directory

Diff for: httplib.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ using socket_t = int;
239239
#pragma comment(lib, "crypt32.lib")
240240
#pragma comment(lib, "cryptui.lib")
241241
#endif
242-
#elif defined(__APPLE__)
242+
#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__)
243243
#include <TargetConditionals.h>
244244
#if TARGET_OS_OSX
245245
#include <CoreFoundation/CoreFoundation.h>
@@ -2668,7 +2668,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
26682668

26692669
auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
26702670
if (sock != INVALID_SOCKET) {
2671-
sockaddr_un addr {};
2671+
sockaddr_un addr{};
26722672
addr.sun_family = AF_UNIX;
26732673
std::copy(host.begin(), host.end(), addr.sun_path);
26742674

@@ -4513,7 +4513,7 @@ inline bool load_system_certs_on_windows(X509_STORE *store) {
45134513

45144514
return result;
45154515
}
4516-
#elif defined(__APPLE__)
4516+
#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__)
45174517
#if TARGET_OS_OSX
45184518
template <typename T>
45194519
using CFObjectPtr =
@@ -8064,9 +8064,9 @@ inline bool SSLClient::load_certs() {
80648064
#ifdef _WIN32
80658065
loaded =
80668066
detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_));
8067-
#elif defined(__APPLE__)
8067+
#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__)
80688068
#if TARGET_OS_OSX
8069-
loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_));
8069+
loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_));
80708070
#endif // TARGET_OS_OSX
80718071
#endif // _WIN32
80728072
if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); }

Diff for: meson.build

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ if openssl_dep.found()
3535
deps += openssl_dep
3636
args += '-DCPPHTTPLIB_OPENSSL_SUPPORT'
3737
if host_machine.system() == 'darwin'
38-
deps += dependency('appleframeworks', modules: ['CoreFoundation', 'Security'])
38+
macosx_keychain_dep = dependency('appleframeworks', modules: ['CoreFoundation', 'Security'], required: get_option('cpp-httplib_macosx_keychain'))
39+
if macosx_keychain_dep.found()
40+
deps += macosx_keychain_dep
41+
args += '-DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN'
42+
endif
3943
endif
4044
endif
4145

Diff for: meson_options.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
option('cpp-httplib_openssl', type: 'feature', value: 'auto', description: 'Enable OpenSSL support')
66
option('cpp-httplib_zlib', type: 'feature', value: 'auto', description: 'Enable zlib support')
77
option('cpp-httplib_brotli', type: 'feature', value: 'auto', description: 'Enable Brotli support')
8+
option('cpp-httplib_macosx_keychain', type: 'feature', value: 'auto', description: 'Enable loading certs from the Keychain on Apple devices')
89
option('cpp-httplib_compile', type: 'boolean', value: false, description: 'Split the header into a compilable header & source file (requires python3)')
910
option('cpp-httplib_test', type: 'boolean', value: false, description: 'Build tests')

Diff for: test/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPEN
1111
ifneq ($(OS), Windows_NT)
1212
UNAME_S := $(shell uname -s)
1313
ifeq ($(UNAME_S), Darwin)
14-
OPENSSL_SUPPORT += -framework CoreFoundation -framework Security
14+
OPENSSL_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework CoreFoundation -framework Security
1515
endif
1616
endif
1717

0 commit comments

Comments
 (0)