Group common example headers

This commit is contained in:
Vinnie Falco
2017-06-24 22:24:39 -07:00
parent 84d7601bdf
commit bb1dd72638
27 changed files with 143 additions and 88 deletions

View File

@@ -5,6 +5,8 @@ Version 67:
* Merge stream_base to stream and tidy * Merge stream_base to stream and tidy
* Use boost::string_view * Use boost::string_view
* Rename to http-server-fast * Rename to http-server-fast
* Appveyor use Boost 1.64.0
* Group common example headers
API Changes: API Changes:

View File

@@ -184,8 +184,8 @@ file(GLOB_RECURSE BEAST_INCLUDES
${PROJECT_SOURCE_DIR}/include/beast/*.ipp ${PROJECT_SOURCE_DIR}/include/beast/*.ipp
) )
file(GLOB_RECURSE EXAMPLE_INCLUDES file(GLOB_RECURSE COMMON_INCLUDES
${PROJECT_SOURCE_DIR}/example/*.hpp ${PROJECT_SOURCE_DIR}/example/common/*.hpp
) )
file(GLOB_RECURSE EXTRAS_INCLUDES file(GLOB_RECURSE EXTRAS_INCLUDES
@@ -193,10 +193,6 @@ file(GLOB_RECURSE EXTRAS_INCLUDES
${PROJECT_SOURCE_DIR}/extras/beast/*.ipp ${PROJECT_SOURCE_DIR}/extras/beast/*.ipp
) )
file(GLOB_RECURSE SERVER_INCLUDES
${PROJECT_SOURCE_DIR}/example/server-framework/*.hpp
)
add_subdirectory (test) add_subdirectory (test)
if ((NOT "${VARIANT}" STREQUAL "coverage") AND if ((NOT "${VARIANT}" STREQUAL "coverage") AND

View File

@@ -79,9 +79,9 @@
[import ../example/doc/http_examples.hpp] [import ../example/doc/http_examples.hpp]
[import ../example/echo-op/echo_op.cpp] [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/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 ../example/websocket-client/websocket_client.cpp]
[import ../test/exemplars.cpp] [import ../test/exemplars.cpp]

View File

@@ -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] [section Server Framework]
This is a complete program and framework of classes implementing This is a complete program and framework of classes implementing
a general purpose server that users may copy to use as the basis a general purpose server that users may copy to use as the basis
for writing their own servers. It serves both HTTP and WebSocket. 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/file_service.hpp]
* [repo_file example/server-framework/framework.hpp] * [repo_file example/server-framework/framework.hpp]
* [repo_file example/server-framework/http_async_port.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/https_ports.hpp]
* [repo_file example/server-framework/main.cpp] * [repo_file example/server-framework/main.cpp]
* [repo_file example/server-framework/multi_port.hpp] * [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/server.hpp]
* [repo_file example/server-framework/service_list.hpp] * [repo_file example/server-framework/service_list.hpp]
* [repo_file example/server-framework/ssl_certificate.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_async_port.hpp]
* [repo_file example/server-framework/ws_sync_port.hpp] * [repo_file example/server-framework/ws_sync_port.hpp]
* [repo_file example/server-framework/ws_upgrade_service.hpp] * [repo_file example/server-framework/ws_upgrade_service.hpp]

View File

@@ -5,8 +5,8 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// //
#ifndef BEAST_EXAMPLE_SERVER_DETECT_SSL_HPP #ifndef BEAST_EXAMPLE_COMMON_DETECT_SSL_HPP
#define BEAST_EXAMPLE_SERVER_DETECT_SSL_HPP #define BEAST_EXAMPLE_COMMON_DETECT_SSL_HPP
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>

View File

@@ -5,8 +5,8 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// //
#ifndef BEAST_EXAMPLE_HTTP_SERVER_FILE_BODY_HPP #ifndef BEAST_EXAMPLE_COMMON_FILE_BODY_HPP
#define BEAST_EXAMPLE_HTTP_SERVER_FILE_BODY_HPP #define BEAST_EXAMPLE_COMMON_FILE_BODY_HPP
#include <beast/core/error.hpp> #include <beast/core/error.hpp>
#include <beast/http/message.hpp> #include <beast/http/message.hpp>

View File

@@ -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 <beast/core/string.hpp>
#include <boost/filesystem/path.hpp>
// Return a reasonable mime type based on the extension of a file.
//
template<class = void>
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

View File

@@ -5,13 +5,12 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// //
#ifndef BEAST_EXAMPLE_SERVER_RFC7231_HPP #ifndef BEAST_EXAMPLE_COMMON_RFC7231_HPP
#define BEAST_EXAMPLE_SERVER_RFC7231_HPP #define BEAST_EXAMPLE_COMMON_RFC7231_HPP
#include <beast/core/string.hpp> #include <beast/core/string.hpp>
#include <beast/http/message.hpp> #include <beast/http/message.hpp>
namespace framework {
namespace rfc7231 { namespace rfc7231 {
// This aggregates a collection of algorithms // This aggregates a collection of algorithms
@@ -36,6 +35,5 @@ is_expect_100_continue(beast::http::request<
} }
} // rfc7231 } // rfc7231
} // framework
#endif #endif

View File

@@ -5,8 +5,8 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// //
#ifndef BEAST_EXAMPLE_SERVER_SSL_STREAM_HPP #ifndef BEAST_EXAMPLE_COMMON_SSL_STREAM_HPP
#define BEAST_EXAMPLE_SERVER_SSL_STREAM_HPP #define BEAST_EXAMPLE_COMMON_SSL_STREAM_HPP
// This include is necessary to work with `ssl::stream` and `beast::websocket::stream` // This include is necessary to work with `ssl::stream` and `beast::websocket::stream`
#include <beast/websocket/ssl.hpp> #include <beast/websocket/ssl.hpp>

View File

@@ -5,10 +5,8 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// //
#ifndef BEAST_EXAMPLE_SERVER_WRITE_MSG_HPP #ifndef BEAST_EXAMPLE_COMMON_WRITE_MSG_HPP
#define BEAST_EXAMPLE_SERVER_WRITE_MSG_HPP #define BEAST_EXAMPLE_COMMON_WRITE_MSG_HPP
#include "server.hpp"
#include <beast/core/async_result.hpp> #include <beast/core/async_result.hpp>
#include <beast/core/handler_ptr.hpp> #include <beast/core/handler_ptr.hpp>
@@ -20,8 +18,6 @@
#include <boost/asio/handler_continuation_hook.hpp> #include <boost/asio/handler_continuation_hook.hpp>
#include <boost/asio/handler_invoke_hook.hpp> #include <boost/asio/handler_invoke_hook.hpp>
namespace framework {
namespace detail { namespace detail {
/** Composed operation to send an HTTP message /** Composed operation to send an HTTP message
@@ -108,7 +104,7 @@ public:
// This gets called when beast::http::async_write completes // This gets called when beast::http::async_write completes
// //
void void
operator()(error_code ec) operator()(beast::error_code ec)
{ {
d_.invoke(ec); d_.invoke(ec);
} }
@@ -196,7 +192,7 @@ template<
class AsyncWriteStream, class AsyncWriteStream,
bool isRequest, class Body, class Fields, bool isRequest, class Body, class Fields,
class WriteHandler> class WriteHandler>
beast::async_return_type<WriteHandler, void(error_code)> beast::async_return_type<WriteHandler, void(beast::error_code)>
async_write_msg( async_write_msg(
AsyncWriteStream& stream, AsyncWriteStream& stream,
beast::http::message<isRequest, Body, Fields>&& msg, beast::http::message<isRequest, Body, Fields>&& msg,
@@ -212,11 +208,11 @@ async_write_msg(
static_assert(beast::http::is_body_reader<Body>::value, static_assert(beast::http::is_body_reader<Body>::value,
"BodyReader requirements not met"); "BodyReader requirements not met");
beast::async_completion<WriteHandler, void(error_code)> init{handler}; beast::async_completion<WriteHandler, void(beast::error_code)> init{handler};
detail::write_msg_op< ::detail::write_msg_op<
AsyncWriteStream, AsyncWriteStream,
beast::handler_type<WriteHandler, void(error_code)>, beast::handler_type<WriteHandler, void(beast::error_code)>,
isRequest, Body, Fields>{ isRequest, Body, Fields>{
init.completion_handler, init.completion_handler,
stream, stream,
@@ -225,6 +221,4 @@ async_write_msg(
return init.result.get(); return init.result.get();
} }
} // framework
#endif #endif

View File

@@ -3,9 +3,15 @@
GroupSources(include/beast beast) GroupSources(include/beast beast)
GroupSources(example/server-framework "/") GroupSources(example/server-framework "/")
GroupSources(example/common "common")
file(GLOB_RECURSE SERVER_INCLUDES
${PROJECT_SOURCE_DIR}/example/server-framework/*.hpp
)
add_executable (server-framework add_executable (server-framework
${BEAST_INCLUDES} ${BEAST_INCLUDES}
${COMMON_INCLUDES}
${SERVER_INCLUDES} ${SERVER_INCLUDES}
main.cpp main.cpp
) )

View File

@@ -8,8 +8,9 @@
#ifndef BEAST_EXAMPLE_SERVER_FILE_SERVICE_HPP #ifndef BEAST_EXAMPLE_SERVER_FILE_SERVICE_HPP
#define BEAST_EXAMPLE_SERVER_FILE_SERVICE_HPP #define BEAST_EXAMPLE_SERVER_FILE_SERVICE_HPP
#include "file_body.hpp"
#include "framework.hpp" #include "framework.hpp"
#include "../common/file_body.hpp"
#include "../common/mime_types.hpp"
#include <beast/core/string.hpp> #include <beast/core/string.hpp>
#include <beast/http/empty_body.hpp> #include <beast/http/empty_body.hpp>
@@ -182,37 +183,6 @@ public:
} }
private: 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 // Return an HTTP Not Found response
// //
template<class Body, class Fields> template<class Body, class Fields>

View File

@@ -11,9 +11,10 @@
#include "server.hpp" #include "server.hpp"
#include "http_base.hpp" #include "http_base.hpp"
#include "rfc7231.hpp"
#include "service_list.hpp" #include "service_list.hpp"
#include "write_msg.hpp"
#include "../common/rfc7231.hpp"
#include "../common/write_msg.hpp"
#include <beast/core/flat_buffer.hpp> #include <beast/core/flat_buffer.hpp>
#include <beast/http/dynamic_body.hpp> #include <beast/http/dynamic_body.hpp>

View File

@@ -11,9 +11,10 @@
#include "server.hpp" #include "server.hpp"
#include "http_base.hpp" #include "http_base.hpp"
#include "rfc7231.hpp"
#include "service_list.hpp" #include "service_list.hpp"
#include "write_msg.hpp"
#include "../common/rfc7231.hpp"
#include "../common/write_msg.hpp"
#include <beast/core/flat_buffer.hpp> #include <beast/core/flat_buffer.hpp>
#include <beast/core/handler_ptr.hpp> #include <beast/core/handler_ptr.hpp>

View File

@@ -10,7 +10,8 @@
#include "http_sync_port.hpp" #include "http_sync_port.hpp"
#include "http_async_port.hpp" #include "http_async_port.hpp"
#include "ssl_stream.hpp"
#include "../common/ssl_stream.hpp"
#include <boost/asio/ssl.hpp> #include <boost/asio/ssl.hpp>

View File

@@ -8,12 +8,13 @@
#ifndef BEAST_EXAMPLE_SERVER_MULTI_PORT_HPP #ifndef BEAST_EXAMPLE_SERVER_MULTI_PORT_HPP
#define BEAST_EXAMPLE_SERVER_MULTI_PORT_HPP #define BEAST_EXAMPLE_SERVER_MULTI_PORT_HPP
#include "detect_ssl.hpp"
#include "ws_async_port.hpp" #include "ws_async_port.hpp"
#include "http_async_port.hpp" #include "http_async_port.hpp"
#include "https_ports.hpp" #include "https_ports.hpp"
#include "wss_ports.hpp" #include "wss_ports.hpp"
#include "../common/detect_ssl.hpp"
#include <beast/core.hpp> #include <beast/core.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>

View File

@@ -10,7 +10,8 @@
#include "ws_sync_port.hpp" #include "ws_sync_port.hpp"
#include "ws_async_port.hpp" #include "ws_async_port.hpp"
#include "ssl_stream.hpp"
#include "../common/ssl_stream.hpp"
#include <boost/asio/ssl.hpp> #include <boost/asio/ssl.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>

View File

@@ -5,7 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // 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 <beast/core/flat_buffer.hpp> #include <beast/core/flat_buffer.hpp>
#include <beast/core/ostream.hpp> #include <beast/core/ostream.hpp>

View File

@@ -6,7 +6,7 @@
// //
#include "example/doc/http_examples.hpp" #include "example/doc/http_examples.hpp"
#include "example/server-framework/file_body.hpp" #include "example/common/file_body.hpp"
#include <beast/core/read_size.hpp> #include <beast/core/read_size.hpp>
#include <beast/core/detail/clamp.hpp> #include <beast/core/detail/clamp.hpp>

View File

@@ -10,7 +10,6 @@ add_executable (server-test
${BEAST_INCLUDES} ${BEAST_INCLUDES}
${SERVER_INCLUDES} ${SERVER_INCLUDES}
../../extras/beast/unit_test/main.cpp ../../extras/beast/unit_test/main.cpp
file_body.cpp
file_service.cpp file_service.cpp
framework.cpp framework.cpp
http_async_port.cpp http_async_port.cpp
@@ -18,17 +17,21 @@ add_executable (server-test
http_sync_port.cpp http_sync_port.cpp
https_ports.cpp https_ports.cpp
multi_port.cpp multi_port.cpp
rfc7231.cpp
server.cpp server.cpp
service_list.cpp service_list.cpp
ssl_certificate ssl_certificate
ssl_stream.cpp
tests.cpp tests.cpp
write_msg.cpp
ws_async_port.cpp ws_async_port.cpp
ws_sync_port.cpp ws_sync_port.cpp
ws_upgrade_service.cpp ws_upgrade_service.cpp
wss_ports.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 target_link_libraries(server-test

View File

@@ -7,7 +7,6 @@
unit-test server-test : unit-test server-test :
../../extras/beast/unit_test/main.cpp ../../extras/beast/unit_test/main.cpp
file_body.cpp
file_service.cpp file_service.cpp
framework.cpp framework.cpp
http_async_port.cpp http_async_port.cpp
@@ -15,16 +14,21 @@ unit-test server-test :
http_sync_port.cpp http_sync_port.cpp
https_ports.cpp https_ports.cpp
multi_port.cpp multi_port.cpp
rfc7231.cpp
server.cpp server.cpp
service_list.cpp service_list.cpp
ssl_certificate.cpp ssl_certificate.cpp
ssl_stream.cpp
tests.cpp tests.cpp
write_msg.cpp write_msg.cpp
ws_async_port.cpp ws_async_port.cpp
ws_sync_port.cpp ws_sync_port.cpp
ws_upgrade_service.cpp ws_upgrade_service.cpp
detect_ssl.cpp
file_body.cpp
mime_types.cpp
rfc7231.cpp
ssl_stream.cpp
write_msg.cpp
: :
<variant>coverage:<build>no <variant>coverage:<build>no
<variant>ubasan:<build>no <variant>ubasan:<build>no

View File

@@ -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"

View File

@@ -6,5 +6,5 @@
// //
// Test that header file is self-contained. // Test that header file is self-contained.
#include "../../example/server-framework/file_body.hpp" #include "../../example/common/file_body.hpp"

View File

@@ -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"

View File

@@ -6,5 +6,5 @@
// //
// Test that header file is self-contained. // Test that header file is self-contained.
#include "../../example/server-framework/rfc7231.hpp" #include "../../example/common/rfc7231.hpp"

View File

@@ -8,6 +8,6 @@
#if BEAST_USE_OPENSSL #if BEAST_USE_OPENSSL
// Test that header file is self-contained. // Test that header file is self-contained.
#include "../../example/server-framework/ssl_stream.hpp" #include "../../example/common/ssl_stream.hpp"
#endif #endif

View File

@@ -6,5 +6,5 @@
// //
// Test that header file is self-contained. // Test that header file is self-contained.
#include "../../example/server-framework/write_msg.hpp" #include "../../example/common/write_msg.hpp"