refine the usage of build options

master
Chunting Gu 5 years ago
parent 233aa5910c
commit 1b8b0b24ac

@ -20,10 +20,10 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR}/bin/release)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR}/bin/release) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR}/bin/release)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR}/bin/release) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR}/bin/release)
option(WEBCC_ENABLE_AUTOTEST "Build automation test?" OFF) option(BUILD_AUTOTEST "Build automation test?" OFF)
option(WEBCC_ENABLE_UNITTEST "Build unit test?" OFF) option(BUILD_UNITTEST "Build unit test?" OFF)
option(WEBCC_ENABLE_EXAMPLES "Build examples?" ON) option(BUILD_EXAMPLES "Build examples?" ON)
option(WEBCC_ENABLE_QT_EXAMPLES "Build Qt application examples?" OFF) option(BUILD_QT_EXAMPLES "Build Qt application examples?" OFF)
set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (1:Yes, 0:No)") set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (1:Yes, 0:No)")
set(WEBCC_ENABLE_SSL 0 CACHE STRING "Enable SSL/HTTPS (need OpenSSL)? (1:Yes, 0:No)") set(WEBCC_ENABLE_SSL 0 CACHE STRING "Enable SSL/HTTPS (need OpenSSL)? (1:Yes, 0:No)")
@ -31,14 +31,14 @@ set(WEBCC_ENABLE_GZIP 0 CACHE STRING "Enable gzip compression (need Zlib)? (1:Y
set(WEBCC_LOG_LEVEL 2 CACHE STRING "Log level (0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO)") set(WEBCC_LOG_LEVEL 2 CACHE STRING "Log level (0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO)")
if(WEBCC_ENABLE_UNITTEST) if(BUILD_UNITTEST)
enable_testing() enable_testing()
endif() endif()
if(WIN32) if(WIN32)
# Asio needs this! # Asio needs this!
# 0x0601 means Win7. So our application targets Win7 and above. # 0x0601 means Win7. So our application targets Win7 and above.
add_definitions(-D_WIN32_WINNT=0x0601) add_compile_definitions(_WIN32_WINNT=0x0601)
endif() endif()
# C++ standard requirements. # C++ standard requirements.
@ -89,7 +89,7 @@ endif()
add_subdirectory(webcc) add_subdirectory(webcc)
if(WEBCC_ENABLE_AUTOTEST OR WEBCC_ENABLE_EXAMPLES) if(BUILD_AUTOTEST OR BUILD_EXAMPLES)
# For including jsoncpp as "json/json.h". # For including jsoncpp as "json/json.h".
include_directories(${THIRD_PARTY_DIR}/src/jsoncpp) include_directories(${THIRD_PARTY_DIR}/src/jsoncpp)
@ -97,7 +97,7 @@ if(WEBCC_ENABLE_AUTOTEST OR WEBCC_ENABLE_EXAMPLES)
endif() endif()
# GTest # GTest
if(WEBCC_ENABLE_AUTOTEST OR WEBCC_ENABLE_UNITTEST) if(BUILD_AUTOTEST OR BUILD_UNITTEST)
find_package(GTest REQUIRED) find_package(GTest REQUIRED)
if(GTEST_FOUND) if(GTEST_FOUND)
add_definitions(-DGTEST_LANG_CXX11=1) add_definitions(-DGTEST_LANG_CXX11=1)
@ -105,14 +105,14 @@ if(WEBCC_ENABLE_AUTOTEST OR WEBCC_ENABLE_UNITTEST)
endif() endif()
endif() endif()
if(WEBCC_ENABLE_AUTOTEST) if(BUILD_AUTOTEST)
add_subdirectory(autotest) add_subdirectory(autotest)
endif() endif()
if(WEBCC_ENABLE_UNITTEST) if(BUILD_UNITTEST)
add_subdirectory(unittest) add_subdirectory(unittest)
endif() endif()
if(WEBCC_ENABLE_EXAMPLES) if(BUILD_EXAMPLES)
add_subdirectory(examples) add_subdirectory(examples)
endif() endif()

@ -141,7 +141,9 @@ $ ls -l /usr/local/lib/libboost*
### Googletest ### Googletest
TODO ```
$ sudo apt install libgtest-dev
```
### Webcc ### Webcc
@ -156,13 +158,14 @@ $ cd build
``` ```
$ cmake -G"Unix Makefiles" \ $ cmake -G"Unix Makefiles" \
-DBUILD_AUTOTEST=OFF \
-DBUILD_UNITTEST=OFF \
-DBUILD_EXAMPLES=ON \
-DBUILD_QT_EXAMPLES=OFF \
-DWEBCC_ENABLE_LOG=1 \ -DWEBCC_ENABLE_LOG=1 \
-DWEBCC_LOG_LEVEL=0 \ -DWEBCC_LOG_LEVEL=0 \
-DWEBCC_ENABLE_SSL=1 \ -DWEBCC_ENABLE_SSL=1 \
-DWEBCC_ENABLE_GZIP=1 \ -DWEBCC_ENABLE_GZIP=1 \
-DWEBCC_ENABLE_AUTOTEST=OFF \
-DWEBCC_ENABLE_UNITTEST=OFF \
-DWEBCC_ENABLE_EXAMPLES=ON \
.. ..
``` ```

@ -48,6 +48,6 @@ endif()
add_subdirectory(book_server) add_subdirectory(book_server)
add_subdirectory(book_client) add_subdirectory(book_client)
if(WEBCC_ENABLE_QT_EXAMPLES) if(BUILD_QT_EXAMPLES)
add_subdirectory(qt_app_server) add_subdirectory(qt_app_server)
endif() endif()

@ -5,7 +5,7 @@
#include "webcc/logger.h" #include "webcc/logger.h"
#include "webcc/utility.h" #include "webcc/utility.h"
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
#include "webcc/gzip.h" #include "webcc/gzip.h"
#endif #endif
@ -15,7 +15,7 @@ namespace webcc {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
bool StringBody::Compress() { bool StringBody::Compress() {
if (compressed_) { if (compressed_) {
@ -110,7 +110,7 @@ void FormBody::InitPayload() {
Payload FormBody::NextPayload(bool free_previous) { Payload FormBody::NextPayload(bool free_previous) {
Payload payload; Payload payload;
// Free previous payload. // Free previous payload.
if (free_previous) { if (free_previous) {
if (index_ > 0) { if (index_ > 0) {

@ -29,7 +29,7 @@ public:
return GetSize() == 0; return GetSize() == 0;
} }
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
// Compress the data with Gzip. // Compress the data with Gzip.
// If data size <= threshold (1400 bytes), no compression will be taken // If data size <= threshold (1400 bytes), no compression will be taken
@ -90,7 +90,7 @@ public:
return compressed_; return compressed_;
} }
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
bool Compress() override; bool Compress() override;

@ -78,7 +78,7 @@ void Client::Close() {
void Client::Connect(RequestPtr request) { void Client::Connect(RequestPtr request) {
if (request->url().scheme() == "https") { if (request->url().scheme() == "https") {
#if WEBCC_ENABLE_SSL #ifdef WEBCC_ENABLE_SSL
socket_.reset(new SslSocket{ io_context_, ssl_verify_ }); socket_.reset(new SslSocket{ io_context_, ssl_verify_ });
DoConnect(request, "443"); DoConnect(request, "443");
#else #else

@ -19,7 +19,7 @@ void ClientSession::Accept(const std::string& content_types) {
} }
} }
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
// Content-Encoding Tokens: // Content-Encoding Tokens:
// (https://en.wikipedia.org/wiki/HTTP_compression) // (https://en.wikipedia.org/wiki/HTTP_compression)
@ -117,7 +117,7 @@ ResponsePtr ClientSession::DoSend(RequestPtr request, bool stream) {
client->set_ssl_verify(ssl_verify_); client->set_ssl_verify(ssl_verify_);
client->set_buffer_size(buffer_size_); client->set_buffer_size(buffer_size_);
client->set_timeout(timeout_); client->set_timeout(timeout_);
Error error = client->Request(request, !reuse, stream); Error error = client->Request(request, !reuse, stream);
if (error) { if (error) {

@ -51,7 +51,7 @@ public:
// Set content types to accept. // Set content types to accept.
void Accept(const std::string& content_types); void Accept(const std::string& content_types);
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
// Accept Gzip compressed response data or not. // Accept Gzip compressed response data or not.
void AcceptGzip(bool gzip = true); void AcceptGzip(bool gzip = true);

@ -6,7 +6,7 @@
// Set 1/0 to enable/disable logging. // Set 1/0 to enable/disable logging.
#define WEBCC_ENABLE_LOG 1 #define WEBCC_ENABLE_LOG 1
#if WEBCC_ENABLE_LOG #ifdef WEBCC_ENABLE_LOG
// 0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO // 0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO
#define WEBCC_LOG_LEVEL 2 #define WEBCC_LOG_LEVEL 2
#endif #endif

@ -8,7 +8,7 @@
// Set 1/0 to enable/disable logging. // Set 1/0 to enable/disable logging.
#define WEBCC_ENABLE_LOG @WEBCC_ENABLE_LOG@ #define WEBCC_ENABLE_LOG @WEBCC_ENABLE_LOG@
#if WEBCC_ENABLE_LOG #ifdef WEBCC_ENABLE_LOG
// 0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO // 0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO
#define WEBCC_LOG_LEVEL @WEBCC_LOG_LEVEL@ #define WEBCC_LOG_LEVEL @WEBCC_LOG_LEVEL@
#endif // WEBCC_ENABLE_LOG #endif // WEBCC_ENABLE_LOG

@ -1,6 +1,6 @@
#include "webcc/logger.h" #include "webcc/logger.h"
#if WEBCC_ENABLE_LOG #ifdef WEBCC_ENABLE_LOG
#include <cassert> #include <cassert>
#include <chrono> #include <chrono>

@ -4,7 +4,7 @@
// This file was generated from "config.h.in" by CMake. // This file was generated from "config.h.in" by CMake.
#include "webcc/config.h" #include "webcc/config.h"
#if WEBCC_ENABLE_LOG #ifdef WEBCC_ENABLE_LOG
#include <cstring> // for strrchr() #include <cstring> // for strrchr()
#include <string> #include <string>

@ -7,7 +7,7 @@
#include "webcc/string.h" #include "webcc/string.h"
#include "webcc/utility.h" #include "webcc/utility.h"
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
#include "webcc/gzip.h" #include "webcc/gzip.h"
#endif #endif
@ -48,7 +48,7 @@ bool StringBodyHandler::Finish() {
auto body = std::make_shared<StringBody>(std::move(content_), IsCompressed()); auto body = std::make_shared<StringBody>(std::move(content_), IsCompressed());
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
LOG_INFO("Decompress the HTTP content..."); LOG_INFO("Decompress the HTTP content...");
if (!body->Decompress()) { if (!body->Decompress()) {
LOG_ERRO("Cannot decompress the HTTP content!"); LOG_ERRO("Cannot decompress the HTTP content!");

@ -5,7 +5,7 @@
#include "webcc/string.h" #include "webcc/string.h"
#include "webcc/utility.h" #include "webcc/utility.h"
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
#include "webcc/gzip.h" #include "webcc/gzip.h"
#endif #endif
@ -32,7 +32,7 @@ RequestPtr RequestBuilder::operator()() {
if (body_) { if (body_) {
request->SetContentType(media_type_, charset_); request->SetContentType(media_type_, charset_);
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
if (gzip_ && body_->Compress()) { if (gzip_ && body_->Compress()) {
request->SetHeader(headers::kContentEncoding, "gzip"); request->SetHeader(headers::kContentEncoding, "gzip");
} }
@ -54,7 +54,7 @@ RequestPtr RequestBuilder::operator()() {
return request; return request;
} }
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
// Accept Gzip compressed response data or not. // Accept Gzip compressed response data or not.
RequestBuilder& RequestBuilder::AcceptGzip(bool gzip) { RequestBuilder& RequestBuilder::AcceptGzip(bool gzip) {

@ -124,7 +124,7 @@ public:
return Header(headers::kAccept, content_types); return Header(headers::kAccept, content_types);
} }
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
// Accept Gzip compressed response data or not. // Accept Gzip compressed response data or not.
RequestBuilder& AcceptGzip(bool gzip = true); RequestBuilder& AcceptGzip(bool gzip = true);
@ -179,7 +179,7 @@ public:
// Add the `Date` header to the request. // Add the `Date` header to the request.
RequestBuilder& Date(); RequestBuilder& Date();
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
// Compress the body data (only for string body). // Compress the body data (only for string body).
// NOTE: // NOTE:
@ -220,7 +220,7 @@ private:
// Persistent connection. // Persistent connection.
bool keep_alive_ = true; bool keep_alive_ = true;
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
bool gzip_ = false; bool gzip_ = false;
#endif // WEBCC_ENABLE_GZIP #endif // WEBCC_ENABLE_GZIP
}; };

@ -4,7 +4,7 @@
#include "webcc/logger.h" #include "webcc/logger.h"
#include "webcc/utility.h" #include "webcc/utility.h"
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
#include "webcc/gzip.h" #include "webcc/gzip.h"
#endif #endif
@ -24,7 +24,7 @@ ResponsePtr ResponseBuilder::operator()() {
if (body_) { if (body_) {
response->SetContentType(media_type_, charset_); response->SetContentType(media_type_, charset_);
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
if (gzip_) { if (gzip_) {
// Don't try to compress the response if the request doesn't accept gzip. // Don't try to compress the response if the request doesn't accept gzip.
if (request_ && request_->AcceptEncodingGzip()) { if (request_ && request_->AcceptEncodingGzip()) {

@ -109,7 +109,7 @@ public:
// Add the `Date` header to the response. // Add the `Date` header to the response.
ResponseBuilder& Date(); ResponseBuilder& Date();
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
ResponseBuilder& Gzip(bool gzip = true) { ResponseBuilder& Gzip(bool gzip = true) {
gzip_ = gzip; gzip_ = gzip;
return *this; return *this;
@ -131,7 +131,7 @@ private:
// Character set of the body (e.g., "utf-8"). // Character set of the body (e.g., "utf-8").
std::string charset_; std::string charset_;
#if WEBCC_ENABLE_GZIP #ifdef WEBCC_ENABLE_GZIP
// Compress the body data (only for string body). // Compress the body data (only for string body).
bool gzip_ = false; bool gzip_ = false;
#endif // WEBCC_ENABLE_GZIP #endif // WEBCC_ENABLE_GZIP

@ -1,6 +1,6 @@
#include "webcc/socket.h" #include "webcc/socket.h"
#if WEBCC_ENABLE_SSL #ifdef WEBCC_ENABLE_SSL
#if (defined(_WIN32) || defined(_WIN64)) #if (defined(_WIN32) || defined(_WIN64))
#include <windows.h> #include <windows.h>
@ -75,7 +75,7 @@ bool Socket::Close() {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if WEBCC_ENABLE_SSL #ifdef WEBCC_ENABLE_SSL
#if (defined(_WIN32) || defined(_WIN64)) #if (defined(_WIN32) || defined(_WIN64))

@ -8,7 +8,7 @@
#include "webcc/config.h" #include "webcc/config.h"
#include "webcc/request.h" #include "webcc/request.h"
#if WEBCC_ENABLE_SSL #ifdef WEBCC_ENABLE_SSL
#include "boost/asio/ssl.hpp" #include "boost/asio/ssl.hpp"
#endif // WEBCC_ENABLE_SSL #endif // WEBCC_ENABLE_SSL
@ -62,7 +62,7 @@ private:
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if WEBCC_ENABLE_SSL #ifdef WEBCC_ENABLE_SSL
class SslSocket : public SocketBase { class SslSocket : public SocketBase {
public: public:

Loading…
Cancel
Save