From bb1dd7263878ab29adac47b05b74a7b08d3a0143 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 24 Jun 2017 22:24:39 -0700 Subject: [PATCH] Group common example headers --- CHANGELOG.md | 2 + CMakeLists.txt | 8 +--- doc/0_main.qbk | 4 +- doc/2_examples.qbk | 21 +++++++-- .../detect_ssl.hpp | 4 +- .../file_body.hpp | 4 +- example/common/mime_types.hpp | 46 +++++++++++++++++++ .../{server-framework => common}/rfc7231.hpp | 6 +-- .../ssl_stream.hpp | 4 +- .../write_msg.hpp | 20 +++----- example/server-framework/CMakeLists.txt | 6 +++ example/server-framework/file_service.hpp | 34 +------------- example/server-framework/http_async_port.hpp | 5 +- example/server-framework/http_sync_port.hpp | 5 +- example/server-framework/https_ports.hpp | 3 +- example/server-framework/multi_port.hpp | 3 +- example/server-framework/wss_ports.hpp | 3 +- test/core/doc_examples.cpp | 2 +- test/http/doc_examples.cpp | 2 +- test/server/CMakeLists.txt | 11 +++-- test/server/Jamfile | 10 ++-- test/server/detect_ssl.cpp | 10 ++++ test/server/file_body.cpp | 2 +- test/server/mime_types.cpp | 10 ++++ test/server/rfc7231.cpp | 2 +- test/server/ssl_stream.cpp | 2 +- test/server/write_msg.cpp | 2 +- 27 files changed, 143 insertions(+), 88 deletions(-) rename example/{server-framework => common}/detect_ssl.hpp (99%) rename example/{server-framework => common}/file_body.hpp (99%) create mode 100644 example/common/mime_types.hpp rename example/{server-framework => common}/rfc7231.hpp (88%) rename example/{server-framework => common}/ssl_stream.hpp (99%) rename example/{server-framework => common}/write_msg.hpp (94%) create mode 100644 test/server/detect_ssl.cpp create mode 100644 test/server/mime_types.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index b8ddc8c6..716415f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ Version 67: * Merge stream_base to stream and tidy * Use boost::string_view * Rename to http-server-fast +* Appveyor use Boost 1.64.0 +* Group common example headers API Changes: diff --git a/CMakeLists.txt b/CMakeLists.txt index 2cbcc773..1be70e14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,8 +184,8 @@ file(GLOB_RECURSE BEAST_INCLUDES ${PROJECT_SOURCE_DIR}/include/beast/*.ipp ) -file(GLOB_RECURSE EXAMPLE_INCLUDES - ${PROJECT_SOURCE_DIR}/example/*.hpp +file(GLOB_RECURSE COMMON_INCLUDES + ${PROJECT_SOURCE_DIR}/example/common/*.hpp ) file(GLOB_RECURSE EXTRAS_INCLUDES @@ -193,10 +193,6 @@ file(GLOB_RECURSE EXTRAS_INCLUDES ${PROJECT_SOURCE_DIR}/extras/beast/*.ipp ) -file(GLOB_RECURSE SERVER_INCLUDES - ${PROJECT_SOURCE_DIR}/example/server-framework/*.hpp - ) - add_subdirectory (test) if ((NOT "${VARIANT}" STREQUAL "coverage") AND diff --git a/doc/0_main.qbk b/doc/0_main.qbk index 7a3f687a..7e01cac4 100644 --- a/doc/0_main.qbk +++ b/doc/0_main.qbk @@ -79,9 +79,9 @@ [import ../example/doc/http_examples.hpp] [import ../example/echo-op/echo_op.cpp] +[import ../example/common/detect_ssl.hpp] +[import ../example/common/file_body.hpp] [import ../example/http-client/http_client.cpp] -[import ../example/server-framework/detect_ssl.hpp] -[import ../example/server-framework/file_body.hpp] [import ../example/websocket-client/websocket_client.cpp] [import ../test/exemplars.cpp] diff --git a/doc/2_examples.qbk b/doc/2_examples.qbk index a4b1dea0..e6c6d7d8 100644 --- a/doc/2_examples.qbk +++ b/doc/2_examples.qbk @@ -125,14 +125,28 @@ the example described in the Core Foundations document section. +[section Common Code] + +This code is reused between some of the examples. The header files +stand alone can be directly included in your projects. + +* [repo_file example/common/detect_ssl.hpp] +* [repo_file example/common/file_body.hpp] +* [repo_file example/common/mime_types.hpp] +* [repo_file example/common/rfc7231.hpp] +* [repo_file example/common/ssl_stream.hpp] +* [repo_file example/common/write_msg.hpp] + +[endsect] + + + [section Server Framework] This is a complete program and framework of classes implementing a general purpose server that users may copy to use as the basis for writing their own servers. It serves both HTTP and WebSocket. -* [repo_file example/server-framework/detect_ssl.hpp] -* [repo_file example/server-framework/file_body.hpp] * [repo_file example/server-framework/file_service.hpp] * [repo_file example/server-framework/framework.hpp] * [repo_file example/server-framework/http_async_port.hpp] @@ -141,12 +155,9 @@ for writing their own servers. It serves both HTTP and WebSocket. * [repo_file example/server-framework/https_ports.hpp] * [repo_file example/server-framework/main.cpp] * [repo_file example/server-framework/multi_port.hpp] -* [repo_file example/server-framework/rfc7231.hpp] * [repo_file example/server-framework/server.hpp] * [repo_file example/server-framework/service_list.hpp] * [repo_file example/server-framework/ssl_certificate.hpp] -* [repo_file example/server-framework/ssl_stream.hpp] -* [repo_file example/server-framework/write_msg.hpp] * [repo_file example/server-framework/ws_async_port.hpp] * [repo_file example/server-framework/ws_sync_port.hpp] * [repo_file example/server-framework/ws_upgrade_service.hpp] diff --git a/example/server-framework/detect_ssl.hpp b/example/common/detect_ssl.hpp similarity index 99% rename from example/server-framework/detect_ssl.hpp rename to example/common/detect_ssl.hpp index 89876872..298a644b 100644 --- a/example/server-framework/detect_ssl.hpp +++ b/example/common/detect_ssl.hpp @@ -5,8 +5,8 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#ifndef BEAST_EXAMPLE_SERVER_DETECT_SSL_HPP -#define BEAST_EXAMPLE_SERVER_DETECT_SSL_HPP +#ifndef BEAST_EXAMPLE_COMMON_DETECT_SSL_HPP +#define BEAST_EXAMPLE_COMMON_DETECT_SSL_HPP #include #include diff --git a/example/server-framework/file_body.hpp b/example/common/file_body.hpp similarity index 99% rename from example/server-framework/file_body.hpp rename to example/common/file_body.hpp index 892c40aa..a75386ff 100644 --- a/example/server-framework/file_body.hpp +++ b/example/common/file_body.hpp @@ -5,8 +5,8 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#ifndef BEAST_EXAMPLE_HTTP_SERVER_FILE_BODY_HPP -#define BEAST_EXAMPLE_HTTP_SERVER_FILE_BODY_HPP +#ifndef BEAST_EXAMPLE_COMMON_FILE_BODY_HPP +#define BEAST_EXAMPLE_COMMON_FILE_BODY_HPP #include #include diff --git a/example/common/mime_types.hpp b/example/common/mime_types.hpp new file mode 100644 index 00000000..65f646f9 --- /dev/null +++ b/example/common/mime_types.hpp @@ -0,0 +1,46 @@ +// +// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef BEAST_EXAMPLE_COMMON_MIME_TYPES_HPP +#define BEAST_EXAMPLE_COMMON_MIME_TYPES_HPP + +#include +#include + +// Return a reasonable mime type based on the extension of a file. +// +template +beast::string_view +mime_type(boost::filesystem::path const& path) +{ + using beast::iequals; + auto const ext = path.extension().string(); + if(iequals(ext, ".txt")) return "text/plain"; + if(iequals(ext, ".htm")) return "text/html"; + if(iequals(ext, ".html")) return "text/html"; + if(iequals(ext, ".php")) return "text/html"; + if(iequals(ext, ".css")) return "text/css"; + if(iequals(ext, ".js")) return "application/javascript"; + if(iequals(ext, ".json")) return "application/json"; + if(iequals(ext, ".xml")) return "application/xml"; + if(iequals(ext, ".swf")) return "application/x-shockwave-flash"; + if(iequals(ext, ".flv")) return "video/x-flv"; + if(iequals(ext, ".png")) return "image/png"; + if(iequals(ext, ".jpe")) return "image/jpeg"; + if(iequals(ext, ".jpeg")) return "image/jpeg"; + if(iequals(ext, ".jpg")) return "image/jpeg"; + if(iequals(ext, ".gif")) return "image/gif"; + if(iequals(ext, ".bmp")) return "image/bmp"; + if(iequals(ext, ".ico")) return "image/vnd.microsoft.icon"; + if(iequals(ext, ".tiff")) return "image/tiff"; + if(iequals(ext, ".tif")) return "image/tiff"; + if(iequals(ext, ".svg")) return "image/svg+xml"; + if(iequals(ext, ".svgz")) return "image/svg+xml"; + return "application/text"; +} + +#endif diff --git a/example/server-framework/rfc7231.hpp b/example/common/rfc7231.hpp similarity index 88% rename from example/server-framework/rfc7231.hpp rename to example/common/rfc7231.hpp index 506f4476..1ee20441 100644 --- a/example/server-framework/rfc7231.hpp +++ b/example/common/rfc7231.hpp @@ -5,13 +5,12 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#ifndef BEAST_EXAMPLE_SERVER_RFC7231_HPP -#define BEAST_EXAMPLE_SERVER_RFC7231_HPP +#ifndef BEAST_EXAMPLE_COMMON_RFC7231_HPP +#define BEAST_EXAMPLE_COMMON_RFC7231_HPP #include #include -namespace framework { namespace rfc7231 { // This aggregates a collection of algorithms @@ -36,6 +35,5 @@ is_expect_100_continue(beast::http::request< } } // rfc7231 -} // framework #endif diff --git a/example/server-framework/ssl_stream.hpp b/example/common/ssl_stream.hpp similarity index 99% rename from example/server-framework/ssl_stream.hpp rename to example/common/ssl_stream.hpp index 98052031..4238ecde 100644 --- a/example/server-framework/ssl_stream.hpp +++ b/example/common/ssl_stream.hpp @@ -5,8 +5,8 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#ifndef BEAST_EXAMPLE_SERVER_SSL_STREAM_HPP -#define BEAST_EXAMPLE_SERVER_SSL_STREAM_HPP +#ifndef BEAST_EXAMPLE_COMMON_SSL_STREAM_HPP +#define BEAST_EXAMPLE_COMMON_SSL_STREAM_HPP // This include is necessary to work with `ssl::stream` and `beast::websocket::stream` #include diff --git a/example/server-framework/write_msg.hpp b/example/common/write_msg.hpp similarity index 94% rename from example/server-framework/write_msg.hpp rename to example/common/write_msg.hpp index 8bbb9b2d..ebec85fe 100644 --- a/example/server-framework/write_msg.hpp +++ b/example/common/write_msg.hpp @@ -5,10 +5,8 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#ifndef BEAST_EXAMPLE_SERVER_WRITE_MSG_HPP -#define BEAST_EXAMPLE_SERVER_WRITE_MSG_HPP - -#include "server.hpp" +#ifndef BEAST_EXAMPLE_COMMON_WRITE_MSG_HPP +#define BEAST_EXAMPLE_COMMON_WRITE_MSG_HPP #include #include @@ -20,8 +18,6 @@ #include #include -namespace framework { - namespace detail { /** Composed operation to send an HTTP message @@ -108,7 +104,7 @@ public: // This gets called when beast::http::async_write completes // void - operator()(error_code ec) + operator()(beast::error_code ec) { d_.invoke(ec); } @@ -196,7 +192,7 @@ template< class AsyncWriteStream, bool isRequest, class Body, class Fields, class WriteHandler> -beast::async_return_type +beast::async_return_type async_write_msg( AsyncWriteStream& stream, beast::http::message&& msg, @@ -212,11 +208,11 @@ async_write_msg( static_assert(beast::http::is_body_reader::value, "BodyReader requirements not met"); - beast::async_completion init{handler}; + beast::async_completion init{handler}; - detail::write_msg_op< + ::detail::write_msg_op< AsyncWriteStream, - beast::handler_type, + beast::handler_type, isRequest, Body, Fields>{ init.completion_handler, stream, @@ -225,6 +221,4 @@ async_write_msg( return init.result.get(); } -} // framework - #endif diff --git a/example/server-framework/CMakeLists.txt b/example/server-framework/CMakeLists.txt index 8fd8b72c..c111c352 100644 --- a/example/server-framework/CMakeLists.txt +++ b/example/server-framework/CMakeLists.txt @@ -3,9 +3,15 @@ GroupSources(include/beast beast) GroupSources(example/server-framework "/") +GroupSources(example/common "common") + +file(GLOB_RECURSE SERVER_INCLUDES + ${PROJECT_SOURCE_DIR}/example/server-framework/*.hpp + ) add_executable (server-framework ${BEAST_INCLUDES} + ${COMMON_INCLUDES} ${SERVER_INCLUDES} main.cpp ) diff --git a/example/server-framework/file_service.hpp b/example/server-framework/file_service.hpp index 4101f680..d21cf136 100644 --- a/example/server-framework/file_service.hpp +++ b/example/server-framework/file_service.hpp @@ -8,8 +8,9 @@ #ifndef BEAST_EXAMPLE_SERVER_FILE_SERVICE_HPP #define BEAST_EXAMPLE_SERVER_FILE_SERVICE_HPP -#include "file_body.hpp" #include "framework.hpp" +#include "../common/file_body.hpp" +#include "../common/mime_types.hpp" #include #include @@ -182,37 +183,6 @@ public: } private: - // Return a reasonable mime type based on the extension of a file. - // - beast::string_view - mime_type(boost::filesystem::path const& path) const - { - using beast::iequals; - auto const ext = path.extension().string(); - if(iequals(ext, ".txt")) return "text/plain"; - if(iequals(ext, ".htm")) return "text/html"; - if(iequals(ext, ".html")) return "text/html"; - if(iequals(ext, ".php")) return "text/html"; - if(iequals(ext, ".css")) return "text/css"; - if(iequals(ext, ".js")) return "application/javascript"; - if(iequals(ext, ".json")) return "application/json"; - if(iequals(ext, ".xml")) return "application/xml"; - if(iequals(ext, ".swf")) return "application/x-shockwave-flash"; - if(iequals(ext, ".flv")) return "video/x-flv"; - if(iequals(ext, ".png")) return "image/png"; - if(iequals(ext, ".jpe")) return "image/jpeg"; - if(iequals(ext, ".jpeg")) return "image/jpeg"; - if(iequals(ext, ".jpg")) return "image/jpeg"; - if(iequals(ext, ".gif")) return "image/gif"; - if(iequals(ext, ".bmp")) return "image/bmp"; - if(iequals(ext, ".ico")) return "image/vnd.microsoft.icon"; - if(iequals(ext, ".tiff")) return "image/tiff"; - if(iequals(ext, ".tif")) return "image/tiff"; - if(iequals(ext, ".svg")) return "image/svg+xml"; - if(iequals(ext, ".svgz")) return "image/svg+xml"; - return "application/text"; - } - // Return an HTTP Not Found response // template diff --git a/example/server-framework/http_async_port.hpp b/example/server-framework/http_async_port.hpp index 74d32706..54db9266 100644 --- a/example/server-framework/http_async_port.hpp +++ b/example/server-framework/http_async_port.hpp @@ -11,9 +11,10 @@ #include "server.hpp" #include "http_base.hpp" -#include "rfc7231.hpp" #include "service_list.hpp" -#include "write_msg.hpp" + +#include "../common/rfc7231.hpp" +#include "../common/write_msg.hpp" #include #include diff --git a/example/server-framework/http_sync_port.hpp b/example/server-framework/http_sync_port.hpp index 973cb03d..62ab75c9 100644 --- a/example/server-framework/http_sync_port.hpp +++ b/example/server-framework/http_sync_port.hpp @@ -11,9 +11,10 @@ #include "server.hpp" #include "http_base.hpp" -#include "rfc7231.hpp" #include "service_list.hpp" -#include "write_msg.hpp" + +#include "../common/rfc7231.hpp" +#include "../common/write_msg.hpp" #include #include diff --git a/example/server-framework/https_ports.hpp b/example/server-framework/https_ports.hpp index 2d260d4d..8a4a5c24 100644 --- a/example/server-framework/https_ports.hpp +++ b/example/server-framework/https_ports.hpp @@ -10,7 +10,8 @@ #include "http_sync_port.hpp" #include "http_async_port.hpp" -#include "ssl_stream.hpp" + +#include "../common/ssl_stream.hpp" #include diff --git a/example/server-framework/multi_port.hpp b/example/server-framework/multi_port.hpp index dca77ec4..37741332 100644 --- a/example/server-framework/multi_port.hpp +++ b/example/server-framework/multi_port.hpp @@ -8,12 +8,13 @@ #ifndef BEAST_EXAMPLE_SERVER_MULTI_PORT_HPP #define BEAST_EXAMPLE_SERVER_MULTI_PORT_HPP -#include "detect_ssl.hpp" #include "ws_async_port.hpp" #include "http_async_port.hpp" #include "https_ports.hpp" #include "wss_ports.hpp" +#include "../common/detect_ssl.hpp" + #include #include diff --git a/example/server-framework/wss_ports.hpp b/example/server-framework/wss_ports.hpp index 38cc2bab..ff7f87e1 100644 --- a/example/server-framework/wss_ports.hpp +++ b/example/server-framework/wss_ports.hpp @@ -10,7 +10,8 @@ #include "ws_sync_port.hpp" #include "ws_async_port.hpp" -#include "ssl_stream.hpp" + +#include "../common/ssl_stream.hpp" #include #include diff --git a/test/core/doc_examples.cpp b/test/core/doc_examples.cpp index 33caa7f9..ff4d2e4b 100644 --- a/test/core/doc_examples.cpp +++ b/test/core/doc_examples.cpp @@ -5,7 +5,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#include "example/server-framework/detect_ssl.hpp" +#include "example/common/detect_ssl.hpp" #include #include diff --git a/test/http/doc_examples.cpp b/test/http/doc_examples.cpp index 7b80a6f0..3b073715 100644 --- a/test/http/doc_examples.cpp +++ b/test/http/doc_examples.cpp @@ -6,7 +6,7 @@ // #include "example/doc/http_examples.hpp" -#include "example/server-framework/file_body.hpp" +#include "example/common/file_body.hpp" #include #include diff --git a/test/server/CMakeLists.txt b/test/server/CMakeLists.txt index 0f3f9f82..a8b98969 100644 --- a/test/server/CMakeLists.txt +++ b/test/server/CMakeLists.txt @@ -10,7 +10,6 @@ add_executable (server-test ${BEAST_INCLUDES} ${SERVER_INCLUDES} ../../extras/beast/unit_test/main.cpp - file_body.cpp file_service.cpp framework.cpp http_async_port.cpp @@ -18,17 +17,21 @@ add_executable (server-test http_sync_port.cpp https_ports.cpp multi_port.cpp - rfc7231.cpp server.cpp service_list.cpp ssl_certificate - ssl_stream.cpp tests.cpp - write_msg.cpp ws_async_port.cpp ws_sync_port.cpp ws_upgrade_service.cpp wss_ports.cpp + + detect_ssl.cpp + file_body.cpp + mime_types.cpp + rfc7231.cpp + ssl_stream.cpp + write_msg.cpp ) target_link_libraries(server-test diff --git a/test/server/Jamfile b/test/server/Jamfile index 755ee0be..ecced9b3 100644 --- a/test/server/Jamfile +++ b/test/server/Jamfile @@ -7,7 +7,6 @@ unit-test server-test : ../../extras/beast/unit_test/main.cpp - file_body.cpp file_service.cpp framework.cpp http_async_port.cpp @@ -15,16 +14,21 @@ unit-test server-test : http_sync_port.cpp https_ports.cpp multi_port.cpp - rfc7231.cpp server.cpp service_list.cpp ssl_certificate.cpp - ssl_stream.cpp tests.cpp write_msg.cpp ws_async_port.cpp ws_sync_port.cpp ws_upgrade_service.cpp + + detect_ssl.cpp + file_body.cpp + mime_types.cpp + rfc7231.cpp + ssl_stream.cpp + write_msg.cpp : coverage:no ubasan:no diff --git a/test/server/detect_ssl.cpp b/test/server/detect_ssl.cpp new file mode 100644 index 00000000..00fced9f --- /dev/null +++ b/test/server/detect_ssl.cpp @@ -0,0 +1,10 @@ +// +// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +// Test that header file is self-contained. +#include "../../example/common/detect_ssl.hpp" + diff --git a/test/server/file_body.cpp b/test/server/file_body.cpp index 3a30b7ba..ab4e80cc 100644 --- a/test/server/file_body.cpp +++ b/test/server/file_body.cpp @@ -6,5 +6,5 @@ // // Test that header file is self-contained. -#include "../../example/server-framework/file_body.hpp" +#include "../../example/common/file_body.hpp" diff --git a/test/server/mime_types.cpp b/test/server/mime_types.cpp new file mode 100644 index 00000000..a681a902 --- /dev/null +++ b/test/server/mime_types.cpp @@ -0,0 +1,10 @@ +// +// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +// Test that header file is self-contained. +#include "../../example/common/mime_types.hpp" + diff --git a/test/server/rfc7231.cpp b/test/server/rfc7231.cpp index 1ec53550..8c5c017c 100644 --- a/test/server/rfc7231.cpp +++ b/test/server/rfc7231.cpp @@ -6,5 +6,5 @@ // // Test that header file is self-contained. -#include "../../example/server-framework/rfc7231.hpp" +#include "../../example/common/rfc7231.hpp" diff --git a/test/server/ssl_stream.cpp b/test/server/ssl_stream.cpp index 871c32bd..6a54bdbd 100644 --- a/test/server/ssl_stream.cpp +++ b/test/server/ssl_stream.cpp @@ -8,6 +8,6 @@ #if BEAST_USE_OPENSSL // Test that header file is self-contained. -#include "../../example/server-framework/ssl_stream.hpp" +#include "../../example/common/ssl_stream.hpp" #endif diff --git a/test/server/write_msg.cpp b/test/server/write_msg.cpp index bea6c8da..80d62d68 100644 --- a/test/server/write_msg.cpp +++ b/test/server/write_msg.cpp @@ -6,5 +6,5 @@ // // Test that header file is self-contained. -#include "../../example/server-framework/write_msg.hpp" +#include "../../example/common/write_msg.hpp"