Skip to content

Commit 3142b54

Browse files
committed
Refactor and import code
1 parent 4ac5278 commit 3142b54

File tree

14 files changed

+1127
-4
lines changed

14 files changed

+1127
-4
lines changed

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,18 @@ endif ()
126126
#-------------------------------------------------
127127
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
128128

129-
file(GLOB_RECURSE BOOST_WS_PROTO_HEADERS CONFIGURE_DEPENDS include/boost/*.hpp include/boost/*.natvis)
129+
file(GLOB_RECURSE BOOST_WS_PROTO_HEADERS CONFIGURE_DEPENDS include/boost/ws_proto/*.hpp include/boost/ws_proto/*.natvis)
130130
file(GLOB_RECURSE BOOST_WS_PROTO_SOURCES CONFIGURE_DEPENDS src/*.cpp src/*.hpp)
131131

132-
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/include/boost PREFIX "" FILES ${BOOST_WS_PROTO_HEADERS})
133-
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX "ws_proto" FILES ${BOOST_WS_PROTO_SOURCES})
132+
source_group("" FILES "include/boost/ws_proto.hpp" "build/Jamfile")
133+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/include/boost/ws_proto PREFIX "include" FILES ${BOOST_WS_PROTO_HEADERS})
134+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX "src" FILES ${BOOST_WS_PROTO_SOURCES})
134135

135-
add_library(boost_ws_proto ${BOOST_WS_PROTO_HEADERS} ${BOOST_WS_PROTO_SOURCES})
136+
add_library(boost_ws_proto include/boost/ws_proto.hpp build/Jamfile ${BOOST_WS_PROTO_HEADERS} ${BOOST_WS_PROTO_SOURCES})
136137
add_library(Boost::ws_proto ALIAS boost_ws_proto)
137138
target_compile_features(boost_ws_proto PUBLIC cxx_constexpr)
138139
target_include_directories(boost_ws_proto PUBLIC "${PROJECT_SOURCE_DIR}/include")
140+
target_include_directories(boost_ws_proto PRIVATE "${PROJECT_SOURCE_DIR}")
139141
target_link_libraries(boost_ws_proto PUBLIC ${BOOST_WS_PROTO_DEPENDENCIES})
140142
target_compile_definitions(boost_ws_proto PUBLIC BOOST_WS_PROTO_NO_LIB)
141143
target_compile_definitions(boost_ws_proto PRIVATE BOOST_WS_PROTO_SOURCE)

build/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ lib boost_ws_proto
4444
: requirements
4545
<library>/boost//buffers
4646
<library>/boost//http_proto
47+
<include>../
4748
<define>BOOST_WS_PROTO_SOURCE
4849
: usage-requirements
4950
<library>/boost//buffers

include/boost/ws_proto/handshake.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ system::result<bool>
2626
is_upgrade(
2727
http_proto::request_view const& req) noexcept;
2828

29+
/** Return a Websocket Upgrade HTTP request
30+
*/
2931
BOOST_WS_PROTO_DECL
3032
http_proto::request
3133
make_upgrade(

src/handshake.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99

1010
#include <boost/ws_proto/handshake.hpp>
11+
#include "src/impl/hybi13.hpp"
1112

1213
namespace boost {
1314
namespace ws_proto {
@@ -19,5 +20,33 @@ is_upgrade(
1920
return false;
2021
}
2122

23+
http_proto::request
24+
make_upgrade(
25+
urls::url_view target)
26+
{
27+
http_proto::request req;
28+
req.set_start_line(
29+
http_proto::method::get,
30+
target.buffer(),
31+
http_proto::version::http_1_1);
32+
req.set(http_proto::field::host, "host");
33+
req.set(http_proto::field::connection, "Upgrade");
34+
req.set(http_proto::field::upgrade, "websocket");
35+
36+
detail::request_key key;
37+
detail::make_request_key(key);
38+
req.set(http_proto::field::sec_websocket_key,
39+
core::string_view(key.data, key.size));
40+
req.set(http_proto::field::sec_websocket_version, "13");
41+
42+
/*
43+
this->build_request_pmd(req);
44+
decorator_opt(req);
45+
decorator(req);
46+
*/
47+
48+
return req;
49+
}
50+
2251
} // ws_proto
2352
} // boost

src/impl/base64.cpp

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
//
2+
// Copyright (c) 2016-2019 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+
/*
11+
Portions from http://www.adp-gmbh.ch/cpp/common/base64.html
12+
Copyright notice:
13+
14+
base64.cpp and base64.h
15+
16+
Copyright (C) 2004-2008 Rene Nyffenegger
17+
18+
This source code is provided 'as-is', without any express or implied
19+
warranty. In no event will the author be held liable for any damages
20+
arising from the use of this software.
21+
22+
Permission is granted to anyone to use this software for any purpose,
23+
including commercial applications, and to alter it and redistribute it
24+
freely, subject to the following restrictions:
25+
26+
1. The origin of this source code must not be misrepresented; you must not
27+
claim that you wrote the original source code. If you use this source code
28+
in a product, an acknowledgment in the product documentation would be
29+
appreciated but is not required.
30+
31+
2. Altered source versions must be plainly marked as such, and must not be
32+
misrepresented as being the original source code.
33+
34+
3. This notice may not be removed or altered from any source distribution.
35+
36+
Rene Nyffenegger rene.nyffenegger@adp-gmbh.ch
37+
*/
38+
39+
#include "src/impl/base64.hpp"
40+
#include <cctype>
41+
#include <string>
42+
#include <utility>
43+
44+
namespace boost {
45+
namespace ws_proto {
46+
47+
char const*
48+
base64_alphabet() noexcept
49+
{
50+
static char constexpr tab[] = {
51+
"ABCDEFGHIJKLMNOP"
52+
"QRSTUVWXYZabcdef"
53+
"ghijklmnopqrstuv"
54+
"wxyz0123456789+/"
55+
};
56+
return &tab[0];
57+
}
58+
59+
signed char const*
60+
base64_inverse() noexcept
61+
{
62+
static signed char constexpr tab[] = {
63+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15
64+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31
65+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 32-47
66+
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 48-63
67+
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
68+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 80-95
69+
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
70+
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 112-127
71+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128-143
72+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144-159
73+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160-175
74+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176-191
75+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192-207
76+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208-223
77+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239
78+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 240-255
79+
};
80+
return &tab[0];
81+
}
82+
83+
/** Encode a series of octets as a padded, base64 string.
84+
85+
The resulting string will not be null terminated.
86+
87+
@par Requires
88+
89+
The memory pointed to by `out` points to valid memory
90+
of at least `encoded_size(len)` bytes.
91+
92+
@return The number of characters written to `out`. This
93+
will exclude any null termination.
94+
*/
95+
std::size_t
96+
base64_encode(void* dest, void const* src, std::size_t len)
97+
{
98+
char* out = static_cast<char*>(dest);
99+
char const* in = static_cast<char const*>(src);
100+
auto const tab = base64_alphabet();
101+
102+
for(auto n = len / 3; n--;)
103+
{
104+
*out++ = tab[ (in[0] & 0xfc) >> 2];
105+
*out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
106+
*out++ = tab[((in[2] & 0xc0) >> 6) + ((in[1] & 0x0f) << 2)];
107+
*out++ = tab[ in[2] & 0x3f];
108+
in += 3;
109+
}
110+
111+
switch(len % 3)
112+
{
113+
case 2:
114+
*out++ = tab[ (in[0] & 0xfc) >> 2];
115+
*out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
116+
*out++ = tab[ (in[1] & 0x0f) << 2];
117+
*out++ = '=';
118+
break;
119+
120+
case 1:
121+
*out++ = tab[ (in[0] & 0xfc) >> 2];
122+
*out++ = tab[((in[0] & 0x03) << 4)];
123+
*out++ = '=';
124+
*out++ = '=';
125+
break;
126+
127+
case 0:
128+
break;
129+
}
130+
131+
return out - static_cast<char*>(dest);
132+
}
133+
134+
std::pair<std::size_t, std::size_t>
135+
base64_decode(void* dest, char const* src, std::size_t len)
136+
{
137+
char* out = static_cast<char*>(dest);
138+
auto in = reinterpret_cast<unsigned char const*>(src);
139+
unsigned char c3[3], c4[4] = {0,0,0,0};
140+
int i = 0;
141+
int j = 0;
142+
143+
auto const inverse = base64_inverse();
144+
145+
while(len-- && *in != '=')
146+
{
147+
auto const v = inverse[*in];
148+
if(v == -1)
149+
break;
150+
++in;
151+
c4[i] = v;
152+
if(++i == 4)
153+
{
154+
c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4);
155+
c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
156+
c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
157+
158+
for(i = 0; i < 3; i++)
159+
*out++ = c3[i];
160+
i = 0;
161+
}
162+
}
163+
164+
if(i)
165+
{
166+
c3[0] = ( c4[0] << 2) + ((c4[1] & 0x30) >> 4);
167+
c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
168+
c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
169+
170+
for(j = 0; j < i - 1; j++)
171+
*out++ = c3[j];
172+
}
173+
174+
return {out - static_cast<char*>(dest),
175+
in - reinterpret_cast<unsigned char const*>(src)};
176+
}
177+
178+
} // ws_proto
179+
} // boost

src/impl/base64.hpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// Copyright (c) 2016-2025 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/cppalliance/ws_proto
8+
//
9+
10+
#ifndef BOOST_WS_PROTO_SRC_IMPL_BASE64_HPP
11+
#define BOOST_WS_PROTO_SRC_IMPL_BASE64_HPP
12+
13+
#include <cctype>
14+
#include <utility>
15+
16+
namespace boost {
17+
namespace ws_proto {
18+
19+
char const*
20+
base64_alphabet() noexcept;
21+
22+
signed char const*
23+
base64_inverse() noexcept;
24+
25+
/// Returns max chars needed to encode a base64 string
26+
std::size_t
27+
constexpr
28+
base64_encoded_size(std::size_t n)
29+
{
30+
return 4 * ((n + 2) / 3);
31+
}
32+
33+
/// Returns max bytes needed to decode a base64 string
34+
std::size_t
35+
constexpr
36+
base64_decoded_size(std::size_t n)
37+
{
38+
return n / 4 * 3; // requires n&3==0, smaller
39+
}
40+
41+
/** Encode a series of octets as a padded, base64 string.
42+
43+
The resulting string will not be null terminated.
44+
45+
@par Requires
46+
47+
The memory pointed to by `out` points to valid memory
48+
of at least `encoded_size(len)` bytes.
49+
50+
@return The number of characters written to `out`. This
51+
will exclude any null termination.
52+
*/
53+
std::size_t
54+
base64_encode(void* dest, void const* src, std::size_t len);
55+
56+
/** Decode a padded base64 string into a series of octets.
57+
58+
@par Requires
59+
60+
The memory pointed to by `out` points to valid memory
61+
of at least `decoded_size(len)` bytes.
62+
63+
@return The number of octets written to `out`, and
64+
the number of characters read from the input string,
65+
expressed as a pair.
66+
*/
67+
std::pair<std::size_t, std::size_t>
68+
base64_decode(void* dest, char const* src, std::size_t len);
69+
70+
} // ws_proto
71+
} // boost
72+
73+
#endif

0 commit comments

Comments
 (0)