From 5e1b14e74d822d2f13636888d0110e9fc93a7150 Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Mon, 15 Apr 2019 10:07:04 +0800 Subject: [PATCH] Add a config option allowing to disable SSL. --- CMakeLists.txt | 18 +++++++++++------- examples/CMakeLists.txt | 40 +++++++++++++++++++++++----------------- webcc/config.h.example | 5 ++++- webcc/config.h.in | 7 +++++++ webcc/http_client.cc | 5 +++++ webcc/http_socket.cc | 8 ++++++-- webcc/http_socket.h | 10 +++++++++- 7 files changed, 65 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5caad12..8b2869e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,9 +20,11 @@ if(WIN32) endif() endif() -set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (0:OFF, 1:ON)") +set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (1:Yes, 0:No)") set(WEBCC_LOG_LEVEL 2 CACHE STRING "Log level (0:VERB, 1:INFO, 2:WARN, 3:ERRO or 4:FATA)") +set(WEBCC_ENABLE_SSL 0 CACHE STRING "Enable SSL/HTTPS (need OpenSSL)? (1:Yes, 0:No)") + if(WEBCC_ENABLE_UNITTEST) enable_testing() endif() @@ -84,12 +86,14 @@ if(Boost_FOUND) message(STATUS ${Boost_LIBRARIES}) endif() -set(OPENSSL_USE_STATIC_LIBS ON) -set(OPENSSL_MSVC_STATIC_RT ON) -find_package(OpenSSL) -if(OPENSSL_FOUND) - include_directories(${OPENSSL_INCLUDE_DIR}) - message(STATUS "OpenSSL libs: " ${OPENSSL_LIBRARIES}) +if(WEBCC_ENABLE_SSL) + set(OPENSSL_USE_STATIC_LIBS ON) + set(OPENSSL_MSVC_STATIC_RT ON) + find_package(OpenSSL) + if(OPENSSL_FOUND) + include_directories(${OPENSSL_INCLUDE_DIR}) + message(STATUS "OpenSSL libs: " ${OPENSSL_LIBRARIES}) + endif() endif() include_directories( diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2a7500f..a260e69 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,17 +1,21 @@ # Examples # Common libraries to link for examples. -set(EXAMPLE_COMMON_LIBS webcc ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} - "${CMAKE_THREAD_LIBS_INIT}") +set(EXAMPLE_LIBS webcc ${Boost_LIBRARIES} "${CMAKE_THREAD_LIBS_INIT}") + +if(WEBCC_ENABLE_SSL) + set(EXAMPLE_LIBS ${EXAMPLE_LIBS} ${OPENSSL_LIBRARIES}) +endif() + if(WIN32) - set(EXAMPLE_COMMON_LIBS ${EXAMPLE_COMMON_LIBS} zlibstatic crypt32) + set(EXAMPLE_LIBS ${EXAMPLE_LIBS} zlibstatic crypt32) else() - set(EXAMPLE_COMMON_LIBS ${EXAMPLE_COMMON_LIBS} ${ZLIB_LIBRARIES}) + set(EXAMPLE_LIBS ${EXAMPLE_LIBS} ${ZLIB_LIBRARIES}) endif() if(UNIX) # Add `-ldl` for Linux to avoid "undefined reference to `dlopen'". - set(EXAMPLE_COMMON_LIBS ${EXAMPLE_COMMON_LIBS} ${CMAKE_DL_LIBS}) + set(EXAMPLE_LIBS ${EXAMPLE_LIBS} ${CMAKE_DL_LIBS}) endif() set(REST_BOOK_SRCS @@ -22,31 +26,33 @@ set(REST_BOOK_SRCS ) add_executable(http_client http_client.cc) -target_link_libraries(http_client ${EXAMPLE_COMMON_LIBS}) +target_link_libraries(http_client ${EXAMPLE_LIBS}) -add_executable(github_client github_client.cc) -target_link_libraries(github_client ${EXAMPLE_COMMON_LIBS} jsoncpp) +if(WEBCC_ENABLE_SSL) + add_executable(github_client github_client.cc) + target_link_libraries(github_client ${EXAMPLE_LIBS} jsoncpp) +endif() add_executable(file_upload_client file_upload_client.cc) -target_link_libraries(file_upload_client ${EXAMPLE_COMMON_LIBS}) +target_link_libraries(file_upload_client ${EXAMPLE_LIBS}) add_executable(file_upload_server file_upload_server.cc) -target_link_libraries(file_upload_server ${EXAMPLE_COMMON_LIBS}) +target_link_libraries(file_upload_server ${EXAMPLE_LIBS}) add_executable(rest_book_server rest_book_server.cc ${REST_BOOK_SRCS}) -target_link_libraries(rest_book_server ${EXAMPLE_COMMON_LIBS} jsoncpp) +target_link_libraries(rest_book_server ${EXAMPLE_LIBS} jsoncpp) add_executable(rest_book_client rest_book_client.cc ${REST_BOOK_SRCS}) -target_link_libraries(rest_book_client ${EXAMPLE_COMMON_LIBS} jsoncpp) +target_link_libraries(rest_book_client ${EXAMPLE_LIBS} jsoncpp) if(WEBCC_ENABLE_SOAP) add_executable(soap_calc_server soap_calc_server.cc) add_executable(soap_calc_client soap_calc_client.cc) add_executable(soap_calc_client_parasoft soap_calc_client_parasoft.cc) - target_link_libraries(soap_calc_server ${EXAMPLE_COMMON_LIBS} pugixml) - target_link_libraries(soap_calc_client ${EXAMPLE_COMMON_LIBS} pugixml) - target_link_libraries(soap_calc_client_parasoft ${EXAMPLE_COMMON_LIBS} pugixml) + target_link_libraries(soap_calc_server ${EXAMPLE_LIBS} pugixml) + target_link_libraries(soap_calc_client ${EXAMPLE_LIBS} pugixml) + target_link_libraries(soap_calc_client_parasoft ${EXAMPLE_LIBS} pugixml) set(SOAP_BOOK_SRCS common/book.cc @@ -58,6 +64,6 @@ if(WEBCC_ENABLE_SOAP) add_executable(soap_book_server soap_book_server.cc ${SOAP_BOOK_SRCS}) add_executable(soap_book_client soap_book_client.cc ${SOAP_BOOK_SRCS}) - target_link_libraries(soap_book_server ${EXAMPLE_COMMON_LIBS} pugixml) - target_link_libraries(soap_book_client ${EXAMPLE_COMMON_LIBS} pugixml) + target_link_libraries(soap_book_server ${EXAMPLE_LIBS} pugixml) + target_link_libraries(soap_book_client ${EXAMPLE_LIBS} pugixml) endif() diff --git a/webcc/config.h.example b/webcc/config.h.example index fbb384d..b1a234e 100644 --- a/webcc/config.h.example +++ b/webcc/config.h.example @@ -3,7 +3,7 @@ // Compile configurations. -// Set 0 to disable logging. +// Set 1/0 to enable/disable logging. #define WEBCC_ENABLE_LOG 1 #if WEBCC_ENABLE_LOG @@ -11,4 +11,7 @@ #define WEBCC_LOG_LEVEL 2 #endif +// Set 1/0 to enable/disable SSL/HTTPS. +#define WEBCC_ENABLE_SSL 1 + #endif // WEBCC_CONFIG_H_ diff --git a/webcc/config.h.in b/webcc/config.h.in index 53d1c3e..1a41d62 100644 --- a/webcc/config.h.in +++ b/webcc/config.h.in @@ -2,11 +2,18 @@ #define WEBCC_CONFIG_H_ // Compile configurations. +// This file is auto-generated by CMake during configuration +// from config.h.in. +// Set 1/0 to enable/disable logging. #define WEBCC_ENABLE_LOG @WEBCC_ENABLE_LOG@ #if WEBCC_ENABLE_LOG +// 0:VERB, 1:INFO, 2:WARN, 3:ERRO or 4:FATA #define WEBCC_LOG_LEVEL @WEBCC_LOG_LEVEL@ #endif +// Set 1/0 to enable/disable SSL/HTTPS. +#define WEBCC_ENABLE_SSL @WEBCC_ENABLE_SSL@ + #endif // WEBCC_CONFIG_H_ diff --git a/webcc/http_client.cc b/webcc/http_client.cc index f945c68..b377ad2 100644 --- a/webcc/http_client.cc +++ b/webcc/http_client.cc @@ -73,8 +73,13 @@ void HttpClient::Close() { Error HttpClient::Connect(HttpRequestPtr request) { if (request->url().scheme() == "https") { +#if WEBCC_ENABLE_SSL socket_.reset(new HttpSslSocket{ io_context_, ssl_verify_ }); return DoConnect(request, kPort443); +#else + LOG_ERRO("SSL/HTTPS support is not enabled."); + return kSchemaError; +#endif // WEBCC_ENABLE_SSL } else { socket_.reset(new HttpSocket{ io_context_ }); return DoConnect(request, kPort80); diff --git a/webcc/http_socket.cc b/webcc/http_socket.cc index 206359c..db417ff 100644 --- a/webcc/http_socket.cc +++ b/webcc/http_socket.cc @@ -6,8 +6,6 @@ #include "webcc/logger.h" -namespace ssl = boost::asio::ssl; - namespace webcc { // ----------------------------------------------------------------------------- @@ -37,6 +35,10 @@ void HttpSocket::Close(boost::system::error_code* ec) { // ----------------------------------------------------------------------------- +#if WEBCC_ENABLE_SSL + +namespace ssl = boost::asio::ssl; + HttpSslSocket::HttpSslSocket(boost::asio::io_context& io_context, bool ssl_verify) : ssl_context_(ssl::context::sslv23), @@ -89,4 +91,6 @@ void HttpSslSocket::Handshake(const std::string& host, } } +#endif // WEBCC_ENABLE_SSL + } // namespace webcc diff --git a/webcc/http_socket.h b/webcc/http_socket.h index 8a73d1b..ef9ce05 100644 --- a/webcc/http_socket.h +++ b/webcc/http_socket.h @@ -4,10 +4,14 @@ #include #include "boost/asio/ip/tcp.hpp" -#include "boost/asio/ssl.hpp" +#include "webcc/config.h" #include "webcc/http_request.h" +#if WEBCC_ENABLE_SSL +#include "boost/asio/ssl.hpp" +#endif // WEBCC_ENABLE_SSL + namespace webcc { // ----------------------------------------------------------------------------- @@ -58,6 +62,8 @@ private: // ----------------------------------------------------------------------------- +#if WEBCC_ENABLE_SSL + class HttpSslSocket : public HttpSocketBase { public: explicit HttpSslSocket(boost::asio::io_context& io_context, @@ -85,6 +91,8 @@ private: bool ssl_verify_; }; +#endif // WEBCC_ENABLE_SSL + } // namespace webcc #endif // WEBCC_HTTP_SOCKET_H_