]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/websocket/detail/hybi13.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / websocket / detail / hybi13.hpp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
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)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_HPP
11 #define BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_HPP
12
13 #include <boost/beast/core/static_string.hpp>
14 #include <boost/beast/core/string.hpp>
15 #include <boost/beast/core/detail/base64.hpp>
16 #include <boost/beast/core/detail/sha1.hpp>
17 #include <boost/assert.hpp>
18 #include <array>
19 #include <cstdint>
20 #include <string>
21 #include <type_traits>
22
23 namespace boost {
24 namespace beast {
25 namespace websocket {
26 namespace detail {
27
28 using sec_ws_key_type = static_string<
29 beast::detail::base64::encoded_size(16)>;
30
31 using sec_ws_accept_type = static_string<
32 beast::detail::base64::encoded_size(20)>;
33
34 template<class Gen>
35 void
36 make_sec_ws_key(sec_ws_key_type& key, Gen& g)
37 {
38 char a[16];
39 for(int i = 0; i < 16; i += 4)
40 {
41 auto const v = g();
42 a[i ] = v & 0xff;
43 a[i+1] = (v >> 8) & 0xff;
44 a[i+2] = (v >> 16) & 0xff;
45 a[i+3] = (v >> 24) & 0xff;
46 }
47 key.resize(key.max_size());
48 key.resize(beast::detail::base64::encode(
49 key.data(), &a[0], 16));
50 }
51
52 template<class = void>
53 void
54 make_sec_ws_accept(sec_ws_accept_type& accept,
55 string_view key)
56 {
57 BOOST_ASSERT(key.size() <= sec_ws_key_type::max_size_n);
58 static_string<sec_ws_key_type::max_size_n + 36> m(key);
59 m.append("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
60 beast::detail::sha1_context ctx;
61 beast::detail::init(ctx);
62 beast::detail::update(ctx, m.data(), m.size());
63 char digest[beast::detail::sha1_context::digest_size];
64 beast::detail::finish(ctx, &digest[0]);
65 accept.resize(accept.max_size());
66 accept.resize(beast::detail::base64::encode(
67 accept.data(), &digest[0], sizeof(digest)));
68 }
69
70 } // detail
71 } // websocket
72 } // beast
73 } // boost
74
75 #endif