forked from rbeeli/websocketclient-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandshake.hpp
313 lines (269 loc) · 9.48 KB
/
Handshake.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#pragma once
#include <expected>
#include <sstream>
#include <string>
#include <format>
#include <map>
#include <optional>
#include "ws_client/errors.hpp"
#include "ws_client/utils/string.hpp"
#include "ws_client/utils/base64.hpp"
#include "ws_client/utils/random.hpp"
#include "ws_client/utils/SHA1.hpp"
#include "ws_client/HttpHeader.hpp"
#include "ws_client/HttpParser.hpp"
#include "ws_client/PermessageDeflate.hpp"
#include "ws_client/URL.hpp"
namespace ws_client
{
using std::string;
template <typename TLogger>
class Handshake
{
protected:
TLogger* logger_;
URL url_;
xoshiro128p rnd_;
string request_SecWebSocketKey_;
HttpRequestHeader request_header_;
HttpResponseHeader response_header_;
std::optional<PermessageDeflate<TLogger>> permessage_deflate_{std::nullopt};
bool permessage_deflate_negotiated_{false};
bool success_{false};
public:
explicit Handshake(TLogger* logger, URL url) noexcept //
: logger_(logger), url_(url), rnd_(xoshiro128p())
{
}
// disable copying
Handshake(const Handshake&) = delete;
Handshake& operator=(Handshake const&) = delete;
// move constructor/assignment
Handshake(Handshake&& other) noexcept
: logger_(other.logger_),
url_(std::move(other.url_)),
rnd_(std::move(other.rnd_)),
request_SecWebSocketKey_(std::move(other.request_SecWebSocketKey_)),
response_header_(std::move(other.response_header_)),
permessage_deflate_(std::move(other.permessage_deflate_)),
permessage_deflate_negotiated_(other.permessage_deflate_negotiated_),
success_(other.success_)
{
}
Handshake& operator=(Handshake&& other) noexcept
{
if (this != &other)
{
logger_ = other.logger_;
url_ = std::move(other.url_);
rnd_ = std::move(other.rnd_);
request_SecWebSocketKey_ = std::move(other.request_SecWebSocketKey_);
response_header_ = std::move(other.response_header_);
permessage_deflate_ = std::move(other.permessage_deflate_);
permessage_deflate_negotiated_ = other.permessage_deflate_negotiated_;
success_ = other.success_;
}
return *this;
}
inline bool is_compression_negotiated() const
{
return permessage_deflate_negotiated_;
}
inline bool is_compression_requested() const
{
return permessage_deflate_.has_value();
}
inline const URL& url() noexcept
{
return url_;
}
inline bool is_success() const noexcept
{
return success_;
}
/**
* Gets the request header object sent to the server.
* Modify this object to change the request headers,
* i.e. to add custom headers for authentication, logging etc.
*/
inline HttpRequestHeader& get_request_header() noexcept
{
return request_header_;
}
inline const HttpResponseHeader& get_response_header() const noexcept
{
return response_header_;
}
inline PermessageDeflate<TLogger>& get_permessage_deflate() noexcept
{
return permessage_deflate_.value();
}
inline void set_permessage_deflate(PermessageDeflate<TLogger>&& extension)
{
permessage_deflate_ = extension;
}
/**
* Generates the HTTP request message to be sent to the server
* for the WebSocket handshake and connection upgrade.
*
* To add user-defined request headers, like authentication headers,
* modify the request_header object before calling this method,
* see `get_request_header()`.
*/
string get_request_message()
{
auto& fields = request_header_.fields;
request_header_.request_line = {
.method = "GET", .request_target = url_.resource(), .http_version = "HTTP/1.1"
};
fields.add_if_missing("Host", std::format("{}:{}", url_.host(), url_.port()));
fields.add_if_missing("Upgrade", "websocket");
fields.add_if_missing("Connection", "Upgrade");
fields.add_if_missing("Sec-WebSocket-Version", "13");
request_SecWebSocketKey_ = generate_SecWebSocketKey();
fields.set("Sec-WebSocket-Key", request_SecWebSocketKey_);
// permessage-deflate extension for compression, optional
if (permessage_deflate_.has_value() && !fields.contains_key("Sec-WebSocket-Extensions"))
{
fields.set(
"Sec-WebSocket-Extensions", permessage_deflate_->get_SecWebSocketExtensions_value()
);
}
fields.add_if_missing("User-Agent", "websocketclient-cpp");
// write request headers to string
std::ostringstream stream;
stream << request_header_;
string request = stream.str();
#if WS_CLIENT_LOG_HANDSHAKE > 0
if (logger_->template is_enabled<LogLevel::I, LogTopic::Handshake>())
{
logger_->template log<LogLevel::I, LogTopic::Handshake>(
std::format("Handshake HTTP request headers:\033[1;34m\n{}\033[0m", request)
);
}
#endif
return request;
}
[[nodiscard]] expected<void, WSError> process_response(const string& header_str)
{
#if WS_CLIENT_LOG_HANDSHAKE > 0
if (logger_->template is_enabled<LogLevel::I, LogTopic::Handshake>())
{
logger_->template log<LogLevel::I, LogTopic::Handshake>(
std::format("Handshake HTTP response headers:\033[1;35m\n{}\033[0m", header_str)
);
}
#endif
std::istringstream stream(header_str);
// first line = status line
WS_TRY(status_line_res, HttpParser::parse_request_status_line(stream));
HttpStatusLine& status_line = *status_line_res;
// validate HTTP status code
if (status_line.status_code != 101)
{
return WS_ERROR(
protocol_error,
std::format(
"HTTP error during WebSocket handshake response processing: {} {}",
status_line.status_code,
status_line.reason
),
close_code::not_set
);
}
// parse response header fields
WS_TRY(fields_res, HttpParser::parse_header_fields(stream));
response_header_ = HttpResponseHeader(status_line, std::move(*fields_res));
// validate "Connection: Upgrade" header
WS_TRYV(validate_ConnectionUpgrade());
// validate "Sec-WebSocket-Accept" header
WS_TRYV(validate_SecWebSocketAccept());
// validate "Sec-WebSocket-Version" header
WS_TRYV(validate_SecWebSocketVersion());
// negotiate & validate permessage-deflate extension
if (permessage_deflate_.has_value())
{
WS_TRY(negotiate_res, permessage_deflate_->negotiate(response_header_));
permessage_deflate_negotiated_ = *negotiate_res;
}
success_ = true;
return {};
}
protected:
[[nodiscard]] expected<void, WSError> validate_ConnectionUpgrade()
{
auto h_con = response_header_.fields.get_first("Connection");
if (!h_con.has_value())
{
return WS_ERROR(
protocol_error, "HTTP response is missing 'Connection' header", close_code::not_set
);
}
if (!equals_ci(*h_con, "Upgrade"))
{
return WS_ERROR(
protocol_error,
std::format("Invalid 'Connection' header, expected: 'Upgrade', got: {}", *h_con),
close_code::not_set
);
}
return {};
}
[[nodiscard]] expected<void, WSError> validate_SecWebSocketVersion()
{
auto h_ext = response_header_.fields.get_first("Sec-WebSocket-Version");
if (!h_ext.has_value())
return {}; // not provided by server - assume it's compatible
if (*h_ext != "13")
{
return WS_ERROR(
protocol_error,
std::format(
"Invalid 'Sec-WebSocket-Version' header, expected: 13, got: {}", *h_ext
),
close_code::not_set
);
}
return {};
}
[[nodiscard]] expected<void, WSError> validate_SecWebSocketAccept()
{
auto h_ext = response_header_.fields.get_first("Sec-WebSocket-Accept");
if (!h_ext.has_value())
{
return WS_ERROR(
protocol_error,
"HTTP response is missing 'Sec-WebSocket-Accept' header",
close_code::not_set
);
}
// calculate expected accept checksum using SHA1 and base64
SHA1 checksum;
checksum.update(request_SecWebSocketKey_ + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
auto sha1_bytes = checksum.final_bytes();
string expected_accept = base64_encode(sha1_bytes.data(), sha1_bytes.size());
if (*h_ext != expected_accept)
{
return WS_ERROR(
protocol_error,
std::format(
"Invalid 'Sec-WebSocket-Accept' header, expected: {}, got: {}",
expected_accept,
*h_ext
),
close_code::not_set
);
}
return {};
}
inline string generate_SecWebSocketKey()
{
unsigned char key[16];
uint64_t part = rnd_.next();
std::memcpy(key, &part, 8);
part = rnd_.next();
std::memcpy(key + 8, &part, 8);
return base64_encode(key, sizeof(key));
}
};
} // namespace ws_client