From f4f6b024038a6b777507c1d6b848ae5bab2c7422 Mon Sep 17 00:00:00 2001 From: yorickdewid Date: Mon, 3 Oct 2016 13:47:22 +0200 Subject: [PATCH 1/2] Set cast type without dereferencing pointer --- CMakeLists.txt | 46 +++++++++++++++++++-------------------- examples/CMakeLists.txt | 44 ++++++++++++++++++++----------------- examples/example_chat.cpp | 2 +- include/crow/websocket.h | 2 +- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e2d3bc7dc..8ef83ca64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,54 +1,54 @@ cmake_minimum_required(VERSION 2.8) project (crow_all) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") + find_package(Tcmalloc) find_package(Threads) find_package(OpenSSL) + if(OPENSSL_FOUND) - include_directories(${OPENSSL_INCLUDE_DIR}) + include_directories(${OPENSSL_INCLUDE_DIR}) endif() if (NOT CMAKE_BUILD_TYPE) - message(STATUS "No build type selected, default to Release") - set(CMAKE_BUILD_TYPE "Release") + message(STATUS "No build type selected, default to Release") + set(CMAKE_BUILD_TYPE "Release") endif() - if (MSVC) -set(Boost_USE_STATIC_LIBS "On") -find_package( Boost 1.52 COMPONENTS system thread regex REQUIRED ) + set(Boost_USE_STATIC_LIBS "On") + find_package( Boost 1.52 COMPONENTS system thread regex REQUIRED ) else() -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++1y -pedantic -Wextra") -find_package( Boost 1.52 COMPONENTS system thread REQUIRED ) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++1y -pedantic -Wextra") + find_package( Boost 1.52 COMPONENTS system thread REQUIRED ) endif() include_directories( ${Boost_INCLUDE_DIR} ) -set(PROJECT_INCLUDE_DIR -${PROJECT_SOURCE_DIR}/include -) +set(PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include) include_directories("${PROJECT_INCLUDE_DIR}") include_directories("${PROJECT_SOURCE_DIR}") #add_subdirectory(src) add_subdirectory(examples) + if (MSVC) else() -add_subdirectory(tests) + add_subdirectory(tests) -enable_testing() -add_test(NAME crow_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/unittest) -add_test(NAME template_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/template/test.py WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests/template) + enable_testing() + add_test(NAME crow_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/unittest) + add_test(NAME template_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/template/test.py WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests/template) -file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/amalgamate) + file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/amalgamate) -add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/amalgamate/crow_all.h - COMMAND python ${PROJECT_SOURCE_DIR}/amalgamate/merge_all.py ${PROJECT_SOURCE_DIR}/include - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/amalgamate/crow_all.h ${PROJECT_SOURCE_DIR}/amalgamate - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/amalgamate - DEPENDS ${PROJECT_SOURCE_DIR}/include/*.h - ) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/amalgamate/crow_all.h + COMMAND python ${PROJECT_SOURCE_DIR}/amalgamate/merge_all.py ${PROJECT_SOURCE_DIR}/include + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/amalgamate/crow_all.h ${PROJECT_SOURCE_DIR}/amalgamate + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/amalgamate + DEPENDS ${PROJECT_SOURCE_DIR}/include/*.h + ) -add_custom_target(amalgamation ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/amalgamate/crow_all.h) + add_custom_target(amalgamation ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/amalgamate/crow_all.h) endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index ef65f026c..ee9bd0c30 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -2,24 +2,29 @@ cmake_minimum_required(VERSION 2.8) project (crow_examples) if (MSVC) -add_executable(example_vs example_vs.cpp) -target_link_libraries(example_vs ${Boost_LIBRARIES}) -target_link_libraries(example_vs ${CMAKE_THREAD_LIBS_INIT}) + add_executable(example_vs example_vs.cpp) + target_link_libraries(example_vs ${Boost_LIBRARIES}) + target_link_libraries(example_vs ${CMAKE_THREAD_LIBS_INIT}) else () - -add_executable(helloworld helloworld.cpp) -target_link_libraries(helloworld ${Boost_LIBRARIES}) -target_link_libraries(helloworld ${CMAKE_THREAD_LIBS_INIT}) + add_executable(helloworld helloworld.cpp) + target_link_libraries(helloworld ${Boost_LIBRARIES}) + target_link_libraries(helloworld ${CMAKE_THREAD_LIBS_INIT}) if (OPENSSL_FOUND) -add_executable(example_ssl ssl/example_ssl.cpp) -target_link_libraries(example_ssl ${Boost_LIBRARIES}) -target_link_libraries(example_ssl ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES}) + add_executable(example_ssl ssl/example_ssl.cpp) + target_link_libraries(example_ssl ${Boost_LIBRARIES}) + target_link_libraries(example_ssl ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES}) endif() add_executable(example_websocket websocket/example_ws.cpp) target_link_libraries(example_websocket ${Boost_LIBRARIES}) target_link_libraries(example_websocket ${CMAKE_THREAD_LIBS_INIT} ssl crypto) +add_custom_command(OUTPUT ws.html + COMMAND ${CMAKE_COMMAND} -E + copy ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html ${CMAKE_CURRENT_BINARY_DIR}/ws.html + DEPENDS ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html +) +add_custom_target(example_ws_copy ALL DEPENDS ws.html) add_executable(example example.cpp) #target_link_libraries(example crow) @@ -27,7 +32,7 @@ target_link_libraries(example ${Boost_LIBRARIES}) target_link_libraries(example ${CMAKE_THREAD_LIBS_INIT}) if (Tcmalloc_FOUND) -target_link_libraries(example ${Tcmalloc_LIBRARIES}) + target_link_libraries(example ${Tcmalloc_LIBRARIES}) endif(Tcmalloc_FOUND) add_executable(example_with_all example_with_all.cpp) @@ -36,21 +41,20 @@ target_link_libraries(example_with_all ${Boost_LIBRARIES}) target_link_libraries(example_with_all ${CMAKE_THREAD_LIBS_INIT}) add_custom_command(OUTPUT example_test.py - COMMAND ${CMAKE_COMMAND} -E - copy ${PROJECT_SOURCE_DIR}/example_test.py ${CMAKE_CURRENT_BINARY_DIR}/example_test.py - DEPENDS ${PROJECT_SOURCE_DIR}/example_test.py - ) + COMMAND ${CMAKE_COMMAND} -E + copy ${PROJECT_SOURCE_DIR}/example_test.py ${CMAKE_CURRENT_BINARY_DIR}/example_test.py + DEPENDS ${PROJECT_SOURCE_DIR}/example_test.py +) add_custom_target(example_copy ALL DEPENDS example_test.py) add_executable(example_chat example_chat.cpp) -#target_link_libraries(example_chat crow) target_link_libraries(example_chat ${Boost_LIBRARIES}) target_link_libraries(example_chat ${CMAKE_THREAD_LIBS_INIT}) add_custom_command(OUTPUT example_chat.html - COMMAND ${CMAKE_COMMAND} -E - copy ${PROJECT_SOURCE_DIR}/example_chat.html ${CMAKE_CURRENT_BINARY_DIR}/example_chat.html - DEPENDS ${PROJECT_SOURCE_DIR}/example_chat.html - ) + COMMAND ${CMAKE_COMMAND} -E + copy ${PROJECT_SOURCE_DIR}/example_chat.html ${CMAKE_CURRENT_BINARY_DIR}/example_chat.html + DEPENDS ${PROJECT_SOURCE_DIR}/example_chat.html +) add_custom_target(example_chat_copy ALL DEPENDS example_chat.html) #SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pg" ) diff --git a/examples/example_chat.cpp b/examples/example_chat.cpp index d187f5194..c510bb07c 100644 --- a/examples/example_chat.cpp +++ b/examples/example_chat.cpp @@ -15,7 +15,7 @@ void broadcast(const string& msg) x["msgs"][0] = msgs.back(); x["last"] = msgs.size(); string body = crow::json::dump(x); - for(auto p:ress) + for(auto p : ress) { auto* res = p.first; CROW_LOG_DEBUG << res << " replied: " << body; diff --git a/include/crow/websocket.h b/include/crow/websocket.h index a1e8e8f75..c3ec33162 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -216,7 +216,7 @@ namespace crow [this](const boost::system::error_code& ec, std::size_t bytes_transferred) { is_reading = false; - remaining_length_ = ntohs(*(uint16_t*)&remaining_length_); + remaining_length_ = ntohs((uint16_t)remaining_length_); #ifdef CROW_ENABLE_DEBUG if (!ec && bytes_transferred != 2) { From e40605da59e368be524dee0e9e9d41ef05149473 Mon Sep 17 00:00:00 2001 From: yorickdewid Date: Mon, 3 Oct 2016 15:32:16 +0200 Subject: [PATCH 2/2] HTTP response redirect - Log address --- include/crow/http_response.h | 6 ++++++ include/crow/http_server.h | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/crow/http_response.h b/include/crow/http_response.h index f5e03b46a..794017479 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -86,6 +86,12 @@ namespace crow completed_ = false; } + void redirect(const std::string& location) + { + code = 301; + set_header("Location", location); + } + void write(const std::string& body_part) { body += body_part; diff --git a/include/crow/http_server.h b/include/crow/http_server.h index d5abb1189..ab2631b61 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -141,7 +141,7 @@ namespace crow }); } - CROW_LOG_INFO << server_name_ << " server is running, local port " << port_; + CROW_LOG_INFO << server_name_ << " server is running at " << bindaddr_ <<":" << port_; signals_.async_wait( [&](const boost::system::error_code& /*error*/, int /*signal_number*/){