diff --git a/modules/websocket/doc_classes/WebSocketServer.xml b/modules/websocket/doc_classes/WebSocketServer.xml index 4cb2dfb88..ec3781853 100644 --- a/modules/websocket/doc_classes/WebSocketServer.xml +++ b/modules/websocket/doc_classes/WebSocketServer.xml @@ -59,6 +59,13 @@ If [code]false[/code] is passed instead (default), you must call [PacketPeer] functions ([code]put_packet[/code], [code]get_packet[/code], etc.), on the [WebSocketPeer] returned via [code]get_peer(id)[/code] to communicate with the peer with given [code]id[/code] (e.g. [code]get_peer(id).get_available_packet_count[/code]). + + + + + Sets additional headers to be sent to clients during the HTTP handshake. + + diff --git a/modules/websocket/emws_server.cpp b/modules/websocket/emws_server.cpp index 96473520d..9422bf24c 100644 --- a/modules/websocket/emws_server.cpp +++ b/modules/websocket/emws_server.cpp @@ -33,6 +33,9 @@ #include "emws_server.h" #include "core/os/os.h" +void EMWSServer::set_extra_headers(const Vector &p_headers) { +} + Error EMWSServer::listen(int p_port, Vector p_protocols, bool gd_mp_api) { return FAILED; } diff --git a/modules/websocket/emws_server.h b/modules/websocket/emws_server.h index 432a19c1e..9f7ad6305 100644 --- a/modules/websocket/emws_server.h +++ b/modules/websocket/emws_server.h @@ -41,6 +41,7 @@ class EMWSServer : public WebSocketServer { public: Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets); + void set_extra_headers(const Vector &p_headers); Error listen(int p_port, Vector p_protocols = Vector(), bool gd_mp_api = false); void stop(); bool is_listening() const; diff --git a/modules/websocket/websocket_server.cpp b/modules/websocket/websocket_server.cpp index 4544043f6..b0418484e 100644 --- a/modules/websocket/websocket_server.cpp +++ b/modules/websocket/websocket_server.cpp @@ -42,6 +42,7 @@ WebSocketServer::~WebSocketServer() { void WebSocketServer::_bind_methods() { ClassDB::bind_method(D_METHOD("is_listening"), &WebSocketServer::is_listening); + ClassDB::bind_method(D_METHOD("set_extra_headers", "headers"), &WebSocketServer::set_extra_headers, DEFVAL(Vector())); ClassDB::bind_method(D_METHOD("listen", "port", "protocols", "gd_mp_api"), &WebSocketServer::listen, DEFVAL(Vector()), DEFVAL(false)); ClassDB::bind_method(D_METHOD("stop"), &WebSocketServer::stop); ClassDB::bind_method(D_METHOD("has_peer", "id"), &WebSocketServer::has_peer); diff --git a/modules/websocket/websocket_server.h b/modules/websocket/websocket_server.h index a98b1afeb..cb8989492 100644 --- a/modules/websocket/websocket_server.h +++ b/modules/websocket/websocket_server.h @@ -51,6 +51,7 @@ protected: public: virtual void poll() = 0; + virtual void set_extra_headers(const Vector &p_headers) = 0; virtual Error listen(int p_port, const Vector p_protocols = Vector(), bool gd_mp_api = false) = 0; virtual void stop() = 0; virtual bool is_listening() const = 0; diff --git a/modules/websocket/wsl_server.cpp b/modules/websocket/wsl_server.cpp index 0c500499a..c24077fbe 100644 --- a/modules/websocket/wsl_server.cpp +++ b/modules/websocket/wsl_server.cpp @@ -104,7 +104,7 @@ bool WSLServer::PendingPeer::_parse_request(const Vector p_protocols) { return true; } -Error WSLServer::PendingPeer::do_handshake(const Vector p_protocols, uint64_t p_timeout) { +Error WSLServer::PendingPeer::do_handshake(const Vector p_protocols, uint64_t p_timeout, const Vector &p_extra_headers) { if (OS::get_singleton()->get_ticks_msec() - time > p_timeout) { print_verbose(vformat("WebSocket handshake timed out after %.3f seconds.", p_timeout * 0.001)); return ERR_TIMEOUT; @@ -149,6 +149,9 @@ Error WSLServer::PendingPeer::do_handshake(const Vector p_protocols, uin if (protocol != "") { s += "Sec-WebSocket-Protocol: " + protocol + "\r\n"; } + for (int i = 0; i < p_extra_headers.size(); i++) { + s += p_extra_headers[i] + "\r\n"; + } s += "\r\n"; response = s.utf8(); has_request = true; @@ -175,6 +178,10 @@ Error WSLServer::PendingPeer::do_handshake(const Vector p_protocols, uin return OK; } +void WSLServer::set_extra_headers(const Vector &p_headers) { + _extra_headers = p_headers; +} + Error WSLServer::listen(int p_port, const Vector p_protocols, bool gd_mp_api) { ERR_FAIL_COND_V(is_listening(), ERR_ALREADY_IN_USE); @@ -206,7 +213,7 @@ void WSLServer::poll() { List> remove_peers; for (List>::Element *E = _pending.front(); E; E = E->next()) { Ref ppeer = E->get(); - Error err = ppeer->do_handshake(_protocols, handshake_timeout); + Error err = ppeer->do_handshake(_protocols, handshake_timeout, _extra_headers); if (err == ERR_BUSY) { continue; } else if (err != OK) { diff --git a/modules/websocket/wsl_server.h b/modules/websocket/wsl_server.h index ad3e7e7a0..2a64dc69b 100644 --- a/modules/websocket/wsl_server.h +++ b/modules/websocket/wsl_server.h @@ -63,7 +63,7 @@ private: PendingPeer(); - Error do_handshake(const Vector p_protocols, uint64_t p_timeout); + Error do_handshake(const Vector p_protocols, uint64_t p_timeout, const Vector &p_extra_headers); }; int _in_buf_size; @@ -74,9 +74,11 @@ private: List> _pending; Ref _server; Vector _protocols; + Vector _extra_headers; public: Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets); + void set_extra_headers(const Vector &p_headers); Error listen(int p_port, const Vector p_protocols = Vector(), bool gd_mp_api = false); void stop(); bool is_listening() const;