Added set_extra_headers() to WebSocketServer

(cherry picked from commit fd4341fba4)
This commit is contained in:
Oğuzhan Eroğlu 2022-04-12 17:50:53 +03:00 committed by Rémi Verschelde
parent b0b1e55305
commit ebaca9d432
7 changed files with 25 additions and 3 deletions

View file

@ -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]). 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]).
</description> </description>
</method> </method>
<method name="set_extra_headers">
<return type="void" />
<argument index="0" name="headers" type="PoolStringArray" default="PoolStringArray( )" />
<description>
Sets additional headers to be sent to clients during the HTTP handshake.
</description>
</method>
<method name="stop"> <method name="stop">
<return type="void" /> <return type="void" />
<description> <description>

View file

@ -33,6 +33,9 @@
#include "emws_server.h" #include "emws_server.h"
#include "core/os/os.h" #include "core/os/os.h"
void EMWSServer::set_extra_headers(const Vector<String> &p_headers) {
}
Error EMWSServer::listen(int p_port, Vector<String> p_protocols, bool gd_mp_api) { Error EMWSServer::listen(int p_port, Vector<String> p_protocols, bool gd_mp_api) {
return FAILED; return FAILED;
} }

View file

@ -42,6 +42,7 @@ class EMWSServer : public WebSocketServer {
public: public:
Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets); 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<String> &p_headers);
Error listen(int p_port, Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false); Error listen(int p_port, Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false);
void stop(); void stop();
bool is_listening() const; bool is_listening() const;

View file

@ -42,6 +42,7 @@ WebSocketServer::~WebSocketServer() {
void WebSocketServer::_bind_methods() { void WebSocketServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_listening"), &WebSocketServer::is_listening); 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<String>()));
ClassDB::bind_method(D_METHOD("listen", "port", "protocols", "gd_mp_api"), &WebSocketServer::listen, DEFVAL(Vector<String>()), DEFVAL(false)); ClassDB::bind_method(D_METHOD("listen", "port", "protocols", "gd_mp_api"), &WebSocketServer::listen, DEFVAL(Vector<String>()), DEFVAL(false));
ClassDB::bind_method(D_METHOD("stop"), &WebSocketServer::stop); ClassDB::bind_method(D_METHOD("stop"), &WebSocketServer::stop);
ClassDB::bind_method(D_METHOD("has_peer", "id"), &WebSocketServer::has_peer); ClassDB::bind_method(D_METHOD("has_peer", "id"), &WebSocketServer::has_peer);

View file

@ -52,6 +52,7 @@ protected:
public: public:
virtual void poll() = 0; virtual void poll() = 0;
virtual void set_extra_headers(const Vector<String> &p_headers) = 0;
virtual Error listen(int p_port, const Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false) = 0; virtual Error listen(int p_port, const Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false) = 0;
virtual void stop() = 0; virtual void stop() = 0;
virtual bool is_listening() const = 0; virtual bool is_listening() const = 0;

View file

@ -104,7 +104,7 @@ bool WSLServer::PendingPeer::_parse_request(const Vector<String> p_protocols) {
return true; return true;
} }
Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols, uint64_t p_timeout) { Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols, uint64_t p_timeout, const Vector<String> &p_extra_headers) {
if (OS::get_singleton()->get_ticks_msec() - time > p_timeout) { if (OS::get_singleton()->get_ticks_msec() - time > p_timeout) {
print_verbose(vformat("WebSocket handshake timed out after %.3f seconds.", p_timeout * 0.001)); print_verbose(vformat("WebSocket handshake timed out after %.3f seconds.", p_timeout * 0.001));
return ERR_TIMEOUT; return ERR_TIMEOUT;
@ -149,6 +149,9 @@ Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols, uin
if (protocol != "") { if (protocol != "") {
s += "Sec-WebSocket-Protocol: " + protocol + "\r\n"; 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"; s += "\r\n";
response = s.utf8(); response = s.utf8();
has_request = true; has_request = true;
@ -175,6 +178,10 @@ Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols, uin
return OK; return OK;
} }
void WSLServer::set_extra_headers(const Vector<String> &p_headers) {
_extra_headers = p_headers;
}
Error WSLServer::listen(int p_port, const Vector<String> p_protocols, bool gd_mp_api) { Error WSLServer::listen(int p_port, const Vector<String> p_protocols, bool gd_mp_api) {
ERR_FAIL_COND_V(is_listening(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(is_listening(), ERR_ALREADY_IN_USE);
@ -206,7 +213,7 @@ void WSLServer::poll() {
List<Ref<PendingPeer>> remove_peers; List<Ref<PendingPeer>> remove_peers;
for (List<Ref<PendingPeer>>::Element *E = _pending.front(); E; E = E->next()) { for (List<Ref<PendingPeer>>::Element *E = _pending.front(); E; E = E->next()) {
Ref<PendingPeer> ppeer = E->get(); Ref<PendingPeer> 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) { if (err == ERR_BUSY) {
continue; continue;
} else if (err != OK) { } else if (err != OK) {

View file

@ -64,7 +64,7 @@ private:
PendingPeer(); PendingPeer();
Error do_handshake(const Vector<String> p_protocols, uint64_t p_timeout); Error do_handshake(const Vector<String> p_protocols, uint64_t p_timeout, const Vector<String> &p_extra_headers);
}; };
int _in_buf_size; int _in_buf_size;
@ -75,9 +75,11 @@ private:
List<Ref<PendingPeer>> _pending; List<Ref<PendingPeer>> _pending;
Ref<TCP_Server> _server; Ref<TCP_Server> _server;
Vector<String> _protocols; Vector<String> _protocols;
Vector<String> _extra_headers;
public: public:
Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets); 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<String> &p_headers);
Error listen(int p_port, const Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false); Error listen(int p_port, const Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false);
void stop(); void stop();
bool is_listening() const; bool is_listening() const;