2019-01-03 05:07:54 +01:00
|
|
|
/*
|
|
|
|
* IXWebSocketHandshake.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "IXCancellationRequest.h"
|
2019-05-30 17:46:50 +02:00
|
|
|
#include "IXSocket.h"
|
2019-01-03 05:07:54 +01:00
|
|
|
#include "IXWebSocketHttpHeaders.h"
|
|
|
|
#include "IXWebSocketPerMessageDeflate.h"
|
|
|
|
#include "IXWebSocketPerMessageDeflateOptions.h"
|
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
2019-05-30 17:46:50 +02:00
|
|
|
#include <string>
|
2019-01-03 22:41:06 +01:00
|
|
|
#include <tuple>
|
2019-01-03 05:07:54 +01:00
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
namespace ix
|
2019-01-03 05:07:54 +01:00
|
|
|
{
|
|
|
|
struct WebSocketInitResult
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
int http_status;
|
|
|
|
std::string errorStr;
|
|
|
|
WebSocketHttpHeaders headers;
|
2019-01-04 02:44:10 +01:00
|
|
|
std::string uri;
|
2019-01-03 05:07:54 +01:00
|
|
|
|
|
|
|
WebSocketInitResult(bool s = false,
|
|
|
|
int status = 0,
|
|
|
|
const std::string& e = std::string(),
|
2019-01-04 02:44:10 +01:00
|
|
|
WebSocketHttpHeaders h = WebSocketHttpHeaders(),
|
|
|
|
const std::string& u = std::string())
|
2019-01-03 05:07:54 +01:00
|
|
|
{
|
|
|
|
success = s;
|
|
|
|
http_status = status;
|
|
|
|
errorStr = e;
|
|
|
|
headers = h;
|
2019-01-04 02:44:10 +01:00
|
|
|
uri = u;
|
2019-01-03 05:07:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-30 17:46:50 +02:00
|
|
|
class WebSocketHandshake
|
|
|
|
{
|
2019-01-03 05:07:54 +01:00
|
|
|
public:
|
|
|
|
WebSocketHandshake(std::atomic<bool>& requestInitCancellation,
|
|
|
|
std::shared_ptr<Socket> _socket,
|
|
|
|
WebSocketPerMessageDeflate& perMessageDeflate,
|
|
|
|
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
|
|
|
std::atomic<bool>& enablePerMessageDeflate);
|
|
|
|
|
|
|
|
WebSocketInitResult clientHandshake(const std::string& url,
|
|
|
|
const std::string& host,
|
|
|
|
const std::string& path,
|
2019-01-04 03:33:08 +01:00
|
|
|
int port,
|
|
|
|
int timeoutSecs);
|
|
|
|
|
2019-05-30 17:46:50 +02:00
|
|
|
WebSocketInitResult serverHandshake(int fd, int timeoutSecs);
|
2019-01-03 05:07:54 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string genRandomString(const int len);
|
|
|
|
|
|
|
|
// Parse HTTP headers
|
|
|
|
WebSocketInitResult sendErrorResponse(int code, const std::string& reason);
|
|
|
|
|
2019-01-03 22:41:06 +01:00
|
|
|
std::tuple<std::string, std::string, std::string> parseRequestLine(const std::string& line);
|
2019-01-06 23:26:11 +01:00
|
|
|
std::string trim(const std::string& str);
|
2019-01-26 01:17:47 +01:00
|
|
|
bool insensitiveStringCompare(const std::string& a, const std::string& b);
|
2019-01-03 22:41:06 +01:00
|
|
|
|
2019-01-03 05:07:54 +01:00
|
|
|
std::atomic<bool>& _requestInitCancellation;
|
|
|
|
std::shared_ptr<Socket> _socket;
|
|
|
|
WebSocketPerMessageDeflate& _perMessageDeflate;
|
|
|
|
WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions;
|
|
|
|
std::atomic<bool>& _enablePerMessageDeflate;
|
|
|
|
};
|
2019-05-30 17:46:50 +02:00
|
|
|
} // namespace ix
|