1/*
2 * IXWebSocketHandshake.h
3 * Author: Benjamin Sergeant
4 * Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
5 */
6
7#pragma once
8
9#include "IXCancellationRequest.h"
10#include "IXSocket.h"
11#include "IXWebSocketHttpHeaders.h"
12#include "IXWebSocketInitResult.h"
13#include "IXWebSocketPerMessageDeflate.h"
14#include "IXWebSocketPerMessageDeflateOptions.h"
15#include <atomic>
16#include <chrono>
17#include <memory>
18#include <string>
19
20namespace ix
21{
22 class WebSocketHandshake
23 {
24 public:
25 WebSocketHandshake(std::atomic<bool>& requestInitCancellation,
26 std::unique_ptr<Socket>& _socket,
27 WebSocketPerMessageDeflatePtr& perMessageDeflate,
28 WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
29 std::atomic<bool>& enablePerMessageDeflate);
30
31 WebSocketInitResult clientHandshake(const std::string& url,
32 const WebSocketHttpHeaders& extraHeaders,
33 const std::string& host,
34 const std::string& path,
35 int port,
36 int timeoutSecs);
37
38 WebSocketInitResult serverHandshake(int timeoutSecs, bool enablePerMessageDeflate);
39
40 private:
41 std::string genRandomString(const int len);
42
43 // Parse HTTP headers
44 WebSocketInitResult sendErrorResponse(int code, const std::string& reason);
45
46 bool insensitiveStringCompare(const std::string& a, const std::string& b);
47
48 std::atomic<bool>& _requestInitCancellation;
49 std::unique_ptr<Socket>& _socket;
50 WebSocketPerMessageDeflatePtr& _perMessageDeflate;
51 WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions;
52 std::atomic<bool>& _enablePerMessageDeflate;
53 };
54} // namespace ix
55