aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/hub/httpproxyhandler.h
blob: 8667c0ca1c3c56f251742daa969287578623c068 (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/thread.h>
#include <zenhttp/httpserver.h>
#include <zenhttp/websocket.h>

#include <functional>
#include <memory>
#include <unordered_map>

namespace zen {

class HttpClient;

class HttpProxyHandler
{
public:
	using PortValidator = std::function<bool(uint16_t)>;

	HttpProxyHandler();
	explicit HttpProxyHandler(PortValidator ValidatePort);
	~HttpProxyHandler();

	void SetPortValidator(PortValidator ValidatePort);

	HttpProxyHandler(const HttpProxyHandler&) = delete;
	HttpProxyHandler& operator=(const HttpProxyHandler&) = delete;

	void HandleProxyRequest(HttpServerRequest& Request, std::string_view PortStr, std::string_view PathTail);
	void PrunePort(uint16_t Port);
	void Shutdown();

	void OnWebSocketOpen(Ref<WebSocketConnection> Connection, std::string_view RelativeUri);
	void OnWebSocketMessage(WebSocketConnection& Conn, const WebSocketMessage& Msg);
	void OnWebSocketClose(WebSocketConnection& Conn, uint16_t Code, std::string_view Reason);

private:
	PortValidator m_ValidatePort;

	HttpClient& GetOrCreateProxyClient(uint16_t Port);

	RwLock													  m_ProxyClientsLock;
	std::unordered_map<uint16_t, std::unique_ptr<HttpClient>> m_ProxyClients;

	struct WsBridge;
	RwLock													m_WsBridgesLock;
	std::unordered_map<WebSocketConnection*, Ref<WsBridge>> m_WsBridges;
};

}  // namespace zen