Skip to content

Commit 4d6c900

Browse files
author
Hannes Rantzsch
committed
WIP: alternative asio client based on botan
1 parent c9707ef commit 4d6c900

File tree

7 files changed

+199
-29
lines changed

7 files changed

+199
-29
lines changed

Release/CMakeLists.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ set(CPPREST_VERSION_REVISION 12)
1515

1616
enable_testing()
1717

18+
set(CPPREST_NO_OPENSSL OFF CACHE BOOL "Exclude OpenSSL. Implies CPPREST_EXCLUDE_WEBSOCKETS and CPPREST_EXCLUDE_LISTENER.")
19+
set(CPPREST_EXCLUDE_LISTENER OFF CACHE BOOL "Exclude listerner functionality.")
20+
21+
if(CPPREST_NO_OPENSSL)
22+
message(STATUS "OpenSSL disabled; disabling websockets and http listener.")
23+
set(CPPREST_HTTP_LISTENER_IMPL none CACHE STRING "Internal use.")
24+
set(CPPREST_EXCLUDE_WEBSOCKETS ON CACHE BOOL "Exclude websockets functionality.")
25+
set(BUILD_SAMPLES OFF CACHE BOOL "Build sample applications.")
26+
endif()
27+
1828
set(WERROR ON CACHE BOOL "Treat Warnings as Errors.")
1929
set(CPPREST_EXCLUDE_WEBSOCKETS OFF CACHE BOOL "Exclude websockets functionality.")
2030
set(CPPREST_EXCLUDE_COMPRESSION OFF CACHE BOOL "Exclude compression functionality.")
@@ -61,8 +71,12 @@ endif()
6171

6272
include(cmake/cpprest_find_boost.cmake)
6373
include(cmake/cpprest_find_zlib.cmake)
64-
include(cmake/cpprest_find_openssl.cmake)
65-
include(cmake/cpprest_find_websocketpp.cmake)
74+
if(CPPREST_NO_OPENSSL)
75+
include(cmake/cpprest_find_botan.cmake)
76+
else()
77+
include(cmake/cpprest_find_openssl.cmake)
78+
include(cmake/cpprest_find_websocketpp.cmake)
79+
endif()
6680
include(cmake/cpprest_find_brotli.cmake)
6781
include(CheckIncludeFiles)
6882
include(GNUInstallDirs)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
function(cpprest_find_botan)
2+
if(TARGET cpprestsdk_botan_internal)
3+
return()
4+
endif()
5+
6+
INCLUDE (FindPackageHandleStandardArgs)
7+
8+
SET (_BOTAN_POSSIBLE_DIRS ${BOTAN_ROOT_DIR})
9+
10+
IF (WIN32)
11+
SET (_BOTAN_POSSIBLE_DIRS ${_BOTAN_POSSIBLE_DIRS} "C:\\usr\\local" "C:\\usr")
12+
ENDIF (WIN32)
13+
14+
# Note: after 1.11.34, botan library moved to semantic versioning (a.k.a. 2.0)
15+
SET (_BOTAN_POSSIBLE_INCLUDE_SUFFIXES include include/botan-1.10 include/botan-1.11 include/botan-2.0 include/botan-2)
16+
SET (_BOTAN_POSSIBLE_LIB_SUFFIXES lib)
17+
18+
FIND_PATH (BOTAN_ROOT_DIR
19+
NAMES botan/botan.h
20+
PATHS ${_BOTAN_POSSIBLE_DIRS}
21+
PATH_SUFFIXES ${_BOTAN_POSSIBLE_INCLUDE_SUFFIXES}
22+
DOC "Botan root directory")
23+
24+
FIND_PATH (BOTAN_INCLUDE_DIR
25+
NAMES botan/botan.h
26+
PATHS ${BOTAN_ROOT_DIR}
27+
PATH_SUFFIXES ${_BOTAN_POSSIBLE_INCLUDE_SUFFIXES}
28+
DOC "Botan include directory")
29+
30+
FIND_LIBRARY (BOTAN_LIBRARY_DEBUG
31+
NAMES botand botand-1.10 botand-1.11 botand-2
32+
PATHS ${_BOTAN_POSSIBLE_DIRS}
33+
PATH_SUFFIXES ${_BOTAN_POSSIBLE_LIB_SUFFIXES}
34+
DOC "Botan debug library")
35+
36+
FIND_LIBRARY (BOTAN_LIBRARY_RELEASE
37+
NAMES botan botan-1.10 botan-1.11 botan-2
38+
PATHS ${_BOTAN_POSSIBLE_DIRS}
39+
PATH_SUFFIXES ${_BOTAN_POSSIBLE_LIB_SUFFIXES}
40+
DOC "Botan release library")
41+
42+
IF (NOT DEFINED BOTAN_LIBRARIES)
43+
IF (BOTAN_LIBRARY_DEBUG AND BOTAN_LIBRARY_RELEASE)
44+
SET (BOTAN_LIBRARIES
45+
optimized ${BOTAN_LIBRARY_RELEASE}
46+
debug ${BOTAN_LIBRARY_DEBUG})
47+
ELSEIF (BOTAN_LIBRARY_RELEASE)
48+
SET (BOTAN_LIBRARIES ${BOTAN_LIBRARY_RELEASE})
49+
ENDIF (BOTAN_LIBRARY_DEBUG AND BOTAN_LIBRARY_RELEASE)
50+
ENDIF (NOT DEFINED BOTAN_LIBRARIES)
51+
52+
IF (BOTAN_INCLUDE_DIR)
53+
SET (BOTAN_INCLUDE_DIRS ${BOTAN_INCLUDE_DIR})
54+
55+
SET (_BOTAN_VERSION_HEADER ${BOTAN_INCLUDE_DIR}/botan/build.h)
56+
57+
IF (EXISTS ${_BOTAN_VERSION_HEADER})
58+
FILE (STRINGS ${_BOTAN_VERSION_HEADER} _BOTAN_VERSION_TMP REGEX
59+
"#define BOTAN_VERSION_(MAJOR|MINOR|PATCH)[ \t]+[0-9]+")
60+
61+
STRING (REGEX REPLACE
62+
".*#define BOTAN_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" BOTAN_VERSION_MAJOR
63+
${_BOTAN_VERSION_TMP})
64+
STRING (REGEX REPLACE
65+
".*#define BOTAN_VERSION_MINOR[ \t]+([0-9]+).*" "\\1" BOTAN_VERSION_MINOR
66+
${_BOTAN_VERSION_TMP})
67+
STRING (REGEX REPLACE
68+
".*#define BOTAN_VERSION_PATCH[ \t]+([0-9]+).*" "\\1" BOTAN_VERSION_PATCH
69+
${_BOTAN_VERSION_TMP})
70+
71+
SET (BOTAN_VERSION_COUNT 3)
72+
SET (BOTAN_VERSION
73+
${BOTAN_VERSION_MAJOR}.${BOTAN_VERSION_MINOR}.${BOTAN_VERSION_PATCH})
74+
ENDIF (EXISTS ${_BOTAN_VERSION_HEADER})
75+
ENDIF (BOTAN_INCLUDE_DIR)
76+
77+
MARK_AS_ADVANCED (BOTAN_ROOT_DIR BOTAN_INCLUDE_DIR BOTAN_LIBRARY_DEBUG
78+
BOTAN_LIBRARY_RELEASE)
79+
80+
FIND_PACKAGE_HANDLE_STANDARD_ARGS (Botan REQUIRED_VARS BOTAN_INCLUDE_DIRS
81+
BOTAN_LIBRARIES VERSION_VAR BOTAN_VERSION)
82+
83+
add_library(Botan INTERFACE)
84+
target_include_directories(Botan INTERFACE ${BOTAN_INCLUDE_DIR})
85+
target_link_libraries(Botan INTERFACE ${BOTAN_LIBRARIES})
86+
87+
add_library(cpprestsdk_botan_internal INTERFACE)
88+
target_link_libraries(cpprestsdk_botan_internal INTERFACE "$<BUILD_INTERFACE:${BOTAN_LIBRARY}>")
89+
target_include_directories(cpprestsdk_botan_internal INTERFACE "$<BUILD_INTERFACE:${BOTAN_INCLUDE_DIR}>")
90+
endfunction()

Release/include/cpprest/http_client.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ typedef void* native_handle;
6161
#include "cpprest/oauth2.h"
6262

6363
#if !defined(_WIN32) && !defined(__cplusplus_winrt) || defined(CPPREST_FORCE_HTTP_CLIENT_ASIO)
64+
65+
#if defined(CPPREST_BOTAN_SSL)
66+
#include <botan/asio_context.h>
67+
#else // CPPREST_BOTAN_SSL
68+
6469
#if defined(__clang__)
6570
#pragma clang diagnostic push
6671
#pragma clang diagnostic ignored "-Wconversion"
@@ -69,6 +74,9 @@ typedef void* native_handle;
6974
#if defined(__clang__)
7075
#pragma clang diagnostic pop
7176
#endif
77+
78+
#endif // CPPREST_BOTAN_SSL
79+
7280
#endif
7381

7482
/// The web namespace contains functionality common to multiple protocols like HTTP and WebSockets.
@@ -86,6 +94,12 @@ namespace client
8694
using web::credentials;
8795
using web::web_proxy;
8896

97+
#if defined(CPPREST_BOTAN_SSL)
98+
using ssl_context_t = Botan::TLS::Context;
99+
#else
100+
using ssl_context_t = boost::asio::ssl::context;
101+
#endif
102+
89103
/// <summary>
90104
/// HTTP client configuration class, used to set the possible configuration options
91105
/// used to create an http_client instance.
@@ -334,15 +348,15 @@ class http_client_config
334348
/// </summary>
335349
/// <param name="callback">A user callback allowing for customization of the ssl context at construction
336350
/// time.</param>
337-
void set_ssl_context_callback(const std::function<void(boost::asio::ssl::context&)>& callback)
351+
void set_ssl_context_callback(const std::function<void(ssl_context_t&)>& callback)
338352
{
339353
m_ssl_context_callback = callback;
340354
}
341355

342356
/// <summary>
343357
/// Gets the user's callback to allow for customization of the ssl context.
344358
/// </summary>
345-
const std::function<void(boost::asio::ssl::context&)>& get_ssl_context_callback() const
359+
const std::function<void(ssl_context_t&)>& get_ssl_context_callback() const
346360
{
347361
return m_ssl_context_callback;
348362
}
@@ -386,7 +400,7 @@ class http_client_config
386400
std::function<void(native_handle)> m_set_user_nativesessionhandle_options;
387401

388402
#if !defined(_WIN32) && !defined(__cplusplus_winrt) || defined(CPPREST_FORCE_HTTP_CLIENT_ASIO)
389-
std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
403+
std::function<void(ssl_context_t&)> m_ssl_context_callback;
390404
bool m_tlsext_sni_enabled;
391405
#endif
392406
#if defined(_WIN32) && !defined(__cplusplus_winrt)
@@ -402,6 +416,8 @@ class http_pipeline;
402416
class http_client
403417
{
404418
public:
419+
using ssl_context = ssl_context_t;
420+
405421
/// <summary>
406422
/// Creates a new http_client connected to specified uri.
407423
/// </summary>

Release/src/CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,18 @@ endif()
127127
# Http client component
128128
if(CPPREST_HTTP_CLIENT_IMPL STREQUAL "asio")
129129
cpprest_find_boost()
130-
cpprest_find_openssl()
131-
target_compile_definitions(cpprest PUBLIC -DCPPREST_FORCE_HTTP_CLIENT_ASIO)
130+
if(CPPREST_NO_OPENSSL)
131+
cpprest_find_botan()
132+
set(CPPREST_BOTAN_SSL_FLAG "-DCPPREST_BOTAN_SSL")
133+
set(CPPREST_SSL_LINK_LIB cpprestsdk_botan_internal)
134+
else()
135+
cpprest_find_openssl()
136+
set(CPPREST_BOTAN_SSL_FLAG "")
137+
set(CPPREST_SSL_LINK_LIB cpprestsdk_openssl_internal)
138+
endif()
139+
target_compile_definitions(cpprest PUBLIC ${CPPREST_BOTAN_SSL_FLAG} -DCPPREST_FORCE_HTTP_CLIENT_ASIO)
132140
target_sources(cpprest PRIVATE http/client/http_client_asio.cpp http/client/x509_cert_utilities.cpp)
133-
target_link_libraries(cpprest PUBLIC cpprestsdk_boost_internal cpprestsdk_openssl_internal)
141+
target_link_libraries(cpprest PUBLIC cpprestsdk_boost_internal ${CPPREST_SSL_LINK_LIB})
134142
elseif(CPPREST_HTTP_CLIENT_IMPL STREQUAL "winhttp")
135143
target_link_libraries(cpprest PRIVATE
136144
httpapi.lib
@@ -248,6 +256,7 @@ if(CPPREST_INSTALL)
248256
set(CPPREST_USES_ZLIB OFF)
249257
set(CPPREST_USES_BROTLI OFF)
250258
set(CPPREST_USES_OPENSSL OFF)
259+
set(CPPREST_USES_BOTAN OFF)
251260

252261
set(CPPREST_TARGETS cpprest)
253262
if(TARGET cpprestsdk_boost_internal)
@@ -266,6 +275,10 @@ if(CPPREST_INSTALL)
266275
list(APPEND CPPREST_TARGETS cpprestsdk_openssl_internal)
267276
set(CPPREST_USES_OPENSSL ON)
268277
endif()
278+
if(TARGET cpprestsdk_botan_internal)
279+
list(APPEND CPPREST_TARGETS cpprestsdk_botan_internal)
280+
set(CPPREST_USES_BOTAN ON)
281+
endif()
269282
if(TARGET cpprestsdk_websocketpp_internal)
270283
list(APPEND CPPREST_TARGETS cpprestsdk_websocketpp_internal)
271284
endif()

0 commit comments

Comments
 (0)