2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BEAST_WEBSOCKET_DETAIL_DECORATOR_HPP
9 #define BEAST_WEBSOCKET_DETAIL_DECORATOR_HPP
11 #include <beast/http/message.hpp>
12 #include <beast/http/string_body.hpp>
13 #include <beast/version.hpp>
14 #include <type_traits>
21 using request_type = http::request_header;
23 using response_type = http::response_header;
25 struct abstract_decorator
28 ~abstract_decorator() = default;
32 operator()(request_type& req) const = 0;
36 operator()(response_type& res) const = 0;
40 class decorator : public abstract_decorator
44 class call_req_possible
46 template<class U, class R = decltype(
47 std::declval<U const>().operator()(
48 std::declval<request_type&>()),
52 static std::false_type check(...);
54 using type = decltype(check<F>(0));
57 class call_res_possible
59 template<class U, class R = decltype(
60 std::declval<U const>().operator()(
61 std::declval<response_type&>()),
65 static std::false_type check(...);
67 using type = decltype(check<F>(0));
82 operator()(request_type& req) const override
84 (*this)(req, typename call_req_possible::type{});
88 operator()(response_type& res) const override
90 (*this)(res, typename call_res_possible::type{});
95 operator()(request_type& req, std::true_type) const
101 operator()(request_type& req, std::false_type) const
103 req.fields.replace("User-Agent",
104 std::string{"Beast/"} + BEAST_VERSION_STRING);
108 operator()(response_type& res, std::true_type) const
114 operator()(response_type& res, std::false_type) const
116 res.fields.replace("Server",
117 std::string{"Beast/"} + BEAST_VERSION_STRING);
123 std::shared_ptr<abstract_decorator> p_;
126 decorator_type() = delete;
127 decorator_type(decorator_type&&) = default;
128 decorator_type(decorator_type const&) = default;
129 decorator_type& operator=(decorator_type&&) = default;
130 decorator_type& operator=(decorator_type const&) = default;
132 template<class F, class =
133 typename std::enable_if<! std::is_same<
134 typename std::decay<F>::type,
135 decorator_type>::value>>
136 decorator_type(F&& f)
137 : p_(std::make_shared<decorator<F>>(
144 operator()(request_type& req)
151 operator()(response_type& res)
158 struct default_decorator