2019-01-02 20:07:54 -08:00
|
|
|
/*
|
|
|
|
* IXWebSocketHandshake.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "IXCancellationRequest.h"
|
2019-05-30 08:46:50 -07:00
|
|
|
#include "IXSocket.h"
|
2019-01-02 20:07:54 -08:00
|
|
|
#include "IXWebSocketHttpHeaders.h"
|
2019-10-13 13:37:34 -07:00
|
|
|
#include "IXWebSocketInitResult.h"
|
2019-01-02 20:07:54 -08:00
|
|
|
#include "IXWebSocketPerMessageDeflate.h"
|
|
|
|
#include "IXWebSocketPerMessageDeflateOptions.h"
|
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
2019-05-30 08:46:50 -07:00
|
|
|
#include <string>
|
2019-01-02 20:07:54 -08:00
|
|
|
|
2019-02-20 18:59:07 -08:00
|
|
|
namespace ix
|
2019-01-02 20:07:54 -08:00
|
|
|
{
|
2019-05-30 08:46:50 -07:00
|
|
|
class WebSocketHandshake
|
|
|
|
{
|
2019-01-02 20:07:54 -08:00
|
|
|
public:
|
|
|
|
WebSocketHandshake(std::atomic<bool>& requestInitCancellation,
|
2020-03-24 12:40:58 -07:00
|
|
|
std::unique_ptr<Socket>& _socket,
|
2020-03-23 18:46:30 -07:00
|
|
|
WebSocketPerMessageDeflatePtr& perMessageDeflate,
|
2019-01-02 20:07:54 -08:00
|
|
|
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
|
|
|
std::atomic<bool>& enablePerMessageDeflate);
|
|
|
|
|
2019-09-23 10:25:23 -07:00
|
|
|
WebSocketInitResult clientHandshake(const std::string& url,
|
|
|
|
const WebSocketHttpHeaders& extraHeaders,
|
|
|
|
const std::string& host,
|
|
|
|
const std::string& path,
|
|
|
|
int port,
|
|
|
|
int timeoutSecs);
|
2019-01-03 18:33:08 -08:00
|
|
|
|
2021-03-16 09:56:08 -07:00
|
|
|
WebSocketInitResult serverHandshake(int timeoutSecs, bool enablePerMessageDeflate);
|
2019-01-02 20:07:54 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string genRandomString(const int len);
|
|
|
|
|
|
|
|
// Parse HTTP headers
|
|
|
|
WebSocketInitResult sendErrorResponse(int code, const std::string& reason);
|
|
|
|
|
2019-01-25 16:17:47 -08:00
|
|
|
bool insensitiveStringCompare(const std::string& a, const std::string& b);
|
2019-01-03 13:41:06 -08:00
|
|
|
|
2019-01-02 20:07:54 -08:00
|
|
|
std::atomic<bool>& _requestInitCancellation;
|
2020-03-24 12:40:58 -07:00
|
|
|
std::unique_ptr<Socket>& _socket;
|
2020-03-23 18:46:30 -07:00
|
|
|
WebSocketPerMessageDeflatePtr& _perMessageDeflate;
|
2019-01-02 20:07:54 -08:00
|
|
|
WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions;
|
|
|
|
std::atomic<bool>& _enablePerMessageDeflate;
|
|
|
|
};
|
2019-05-30 08:46:50 -07:00
|
|
|
} // namespace ix
|