Custom headers support in WebSocketClient.

This commit also converts all PoolVector<String> parameters to
`const Vector<String>` in both WebSocketServer and WebSocketClient.
This commit is contained in:
Fabio Alessandrelli 2019-10-08 20:13:24 +02:00
parent c723a8b6aa
commit 67a4c3033b
10 changed files with 30 additions and 22 deletions

View file

@ -64,11 +64,14 @@ EMSCRIPTEN_KEEPALIVE void _esws_on_close(void *obj, int code, char *reason, int
} }
} }
Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocols) { Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const PoolVector<String> p_protocols, const Vector<String> p_custom_headers) {
String proto_string = p_protocols.join(","); String proto_string = p_protocols.join(",");
String str = "ws://"; String str = "ws://";
if (p_custom_headers.size()) {
WARN_PRINT_ONCE("Custom headers are not supported in in HTML5 platform.");
}
if (p_ssl) { if (p_ssl) {
str = "wss://"; str = "wss://";
if (ssl_cert.is_valid()) { if (ssl_cert.is_valid()) {

View file

@ -50,7 +50,7 @@ public:
bool _is_connecting; bool _is_connecting;
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);
Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocol = PoolVector<String>()); Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const PoolVector<String> p_protocol = PoolVector<String>(), const Dictionary p_custom_headers = Dictionary());
Ref<WebSocketPeer> get_peer(int p_peer_id) const; Ref<WebSocketPeer> get_peer(int p_peer_id) const;
void disconnect_from_host(int p_code = 1000, String p_reason = ""); void disconnect_from_host(int p_code = 1000, String p_reason = "");
IP_Address get_connected_host() const; IP_Address get_connected_host() const;

View file

@ -40,7 +40,7 @@ WebSocketClient::WebSocketClient() {
WebSocketClient::~WebSocketClient() { WebSocketClient::~WebSocketClient() {
} }
Error WebSocketClient::connect_to_url(String p_url, PoolVector<String> p_protocols, bool gd_mp_api) { Error WebSocketClient::connect_to_url(String p_url, const Vector<String> p_protocols, bool gd_mp_api, const Vector<String> p_custom_headers) {
_is_multiplayer = gd_mp_api; _is_multiplayer = gd_mp_api;
String host = p_url; String host = p_url;
@ -72,7 +72,7 @@ Error WebSocketClient::connect_to_url(String p_url, PoolVector<String> p_protoco
host = host.substr(0, p_len); host = host.substr(0, p_len);
} }
return connect_to_host(host, path, port, ssl, p_protocols); return connect_to_host(host, path, port, ssl, p_protocols, p_custom_headers);
} }
void WebSocketClient::set_verify_ssl_enabled(bool p_verify_ssl) { void WebSocketClient::set_verify_ssl_enabled(bool p_verify_ssl) {
@ -143,7 +143,7 @@ void WebSocketClient::_on_error() {
} }
void WebSocketClient::_bind_methods() { void WebSocketClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("connect_to_url", "url", "protocols", "gd_mp_api"), &WebSocketClient::connect_to_url, DEFVAL(PoolVector<String>()), DEFVAL(false)); ClassDB::bind_method(D_METHOD("connect_to_url", "url", "protocols", "gd_mp_api", "custom_headers"), &WebSocketClient::connect_to_url, DEFVAL(Vector<String>()), DEFVAL(false), DEFVAL(Vector<String>()));
ClassDB::bind_method(D_METHOD("disconnect_from_host", "code", "reason"), &WebSocketClient::disconnect_from_host, DEFVAL(1000), DEFVAL("")); ClassDB::bind_method(D_METHOD("disconnect_from_host", "code", "reason"), &WebSocketClient::disconnect_from_host, DEFVAL(1000), DEFVAL(""));
ClassDB::bind_method(D_METHOD("get_connected_host"), &WebSocketClient::get_connected_host); ClassDB::bind_method(D_METHOD("get_connected_host"), &WebSocketClient::get_connected_host);
ClassDB::bind_method(D_METHOD("get_connected_port"), &WebSocketClient::get_connected_port); ClassDB::bind_method(D_METHOD("get_connected_port"), &WebSocketClient::get_connected_port);

View file

@ -49,7 +49,7 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
Error connect_to_url(String p_url, PoolVector<String> p_protocols = PoolVector<String>(), bool gd_mp_api = false); Error connect_to_url(String p_url, const Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false, const Vector<String> p_custom_headers = Vector<String>());
void set_verify_ssl_enabled(bool p_verify_ssl); void set_verify_ssl_enabled(bool p_verify_ssl);
bool is_verify_ssl_enabled() const; bool is_verify_ssl_enabled() const;
@ -57,7 +57,7 @@ public:
void set_trusted_ssl_certificate(Ref<X509Certificate> p_cert); void set_trusted_ssl_certificate(Ref<X509Certificate> p_cert);
virtual void poll() = 0; virtual void poll() = 0;
virtual Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocol = PoolVector<String>()) = 0; virtual Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocol = Vector<String>(), const Vector<String> p_custom_headers = Vector<String>()) = 0;
virtual void disconnect_from_host(int p_code = 1000, String p_reason = "") = 0; virtual void disconnect_from_host(int p_code = 1000, String p_reason = "") = 0;
virtual IP_Address get_connected_host() const = 0; virtual IP_Address get_connected_host() const = 0;
virtual uint16_t get_connected_port() const = 0; virtual uint16_t get_connected_port() const = 0;

View file

@ -42,7 +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("listen", "port", "protocols", "gd_mp_api"), &WebSocketServer::listen, DEFVAL(PoolVector<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);
ClassDB::bind_method(D_METHOD("get_peer_address", "id"), &WebSocketServer::get_peer_address); ClassDB::bind_method(D_METHOD("get_peer_address", "id"), &WebSocketServer::get_peer_address);

View file

@ -50,7 +50,7 @@ protected:
public: public:
virtual void poll() = 0; virtual void poll() = 0;
virtual Error listen(int p_port, PoolVector<String> p_protocols = PoolVector<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;
virtual bool has_peer(int p_id) const = 0; virtual bool has_peer(int p_id) const = 0;

View file

@ -152,7 +152,7 @@ bool WSLClient::_verify_headers(String &r_protocol) {
return true; return true;
} }
Error WSLClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocols) { Error WSLClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocols, const Vector<String> p_custom_headers) {
ERR_FAIL_COND_V(_connection.is_valid(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(_connection.is_valid(), ERR_ALREADY_IN_USE);
@ -181,7 +181,8 @@ Error WSLClient::connect_to_host(String p_host, String p_path, uint16_t p_port,
_connection = _tcp; _connection = _tcp;
_use_ssl = p_ssl; _use_ssl = p_ssl;
_host = p_host; _host = p_host;
_protocols = p_protocols; _protocols.clear();
_protocols.append_array(p_protocols);
_key = WSLPeer::generate_key(); _key = WSLPeer::generate_key();
// TODO custom extra headers (allow overriding this too?) // TODO custom extra headers (allow overriding this too?)
@ -200,6 +201,9 @@ Error WSLClient::connect_to_host(String p_host, String p_path, uint16_t p_port,
} }
request += "\r\n"; request += "\r\n";
} }
for (int i = 0; i < p_custom_headers.size(); i++) {
request += p_custom_headers[i] + "\r\n";
}
request += "\r\n"; request += "\r\n";
_request = request.utf8(); _request = request.utf8();
@ -294,7 +298,7 @@ void WSLClient::disconnect_from_host(int p_code, String p_reason) {
_key = ""; _key = "";
_host = ""; _host = "";
_protocols.resize(0); _protocols.clear();
_use_ssl = false; _use_ssl = false;
_request = ""; _request = "";

View file

@ -64,7 +64,7 @@ private:
String _key; String _key;
String _host; String _host;
PoolVector<String> _protocols; Vector<String> _protocols;
bool _use_ssl; bool _use_ssl;
void _do_handshake(); void _do_handshake();
@ -72,7 +72,7 @@ private:
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);
Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocol = PoolVector<String>()); Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocol = Vector<String>(), const Vector<String> p_custom_headers = Vector<String>());
int get_max_packet_size() const; int get_max_packet_size() const;
Ref<WebSocketPeer> get_peer(int p_peer_id) const; Ref<WebSocketPeer> get_peer(int p_peer_id) const;
void disconnect_from_host(int p_code = 1000, String p_reason = ""); void disconnect_from_host(int p_code = 1000, String p_reason = "");

View file

@ -43,7 +43,7 @@ WSLServer::PendingPeer::PendingPeer() {
memset(req_buf, 0, sizeof(req_buf)); memset(req_buf, 0, sizeof(req_buf));
} }
bool WSLServer::PendingPeer::_parse_request(const PoolStringArray p_protocols) { bool WSLServer::PendingPeer::_parse_request(const Vector<String> p_protocols) {
Vector<String> psa = String((char *)req_buf).split("\r\n"); Vector<String> psa = String((char *)req_buf).split("\r\n");
int len = psa.size(); int len = psa.size();
ERR_FAIL_COND_V_MSG(len < 4, false, "Not enough response headers, got: " + itos(len) + ", expected >= 4."); ERR_FAIL_COND_V_MSG(len < 4, false, "Not enough response headers, got: " + itos(len) + ", expected >= 4.");
@ -98,7 +98,7 @@ bool WSLServer::PendingPeer::_parse_request(const PoolStringArray p_protocols) {
return true; return true;
} }
Error WSLServer::PendingPeer::do_handshake(PoolStringArray p_protocols) { Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols) {
if (OS::get_singleton()->get_ticks_msec() - time > WSL_SERVER_TIMEOUT) if (OS::get_singleton()->get_ticks_msec() - time > WSL_SERVER_TIMEOUT)
return ERR_TIMEOUT; return ERR_TIMEOUT;
if (use_ssl) { if (use_ssl) {
@ -154,11 +154,11 @@ Error WSLServer::PendingPeer::do_handshake(PoolStringArray p_protocols) {
return OK; return OK;
} }
Error WSLServer::listen(int p_port, PoolVector<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);
_is_multiplayer = gd_mp_api; _is_multiplayer = gd_mp_api;
_protocols = p_protocols; _protocols.append_array(p_protocols);
_server->listen(p_port); _server->listen(p_port);
return OK; return OK;
@ -252,6 +252,7 @@ void WSLServer::stop() {
} }
_pending.clear(); _pending.clear();
_peer_map.clear(); _peer_map.clear();
_protocols.clear();
} }
bool WSLServer::has_peer(int p_id) const { bool WSLServer::has_peer(int p_id) const {

View file

@ -50,7 +50,7 @@ private:
class PendingPeer : public Reference { class PendingPeer : public Reference {
private: private:
bool _parse_request(const PoolStringArray p_protocols); bool _parse_request(const Vector<String> p_protocols);
public: public:
Ref<StreamPeerTCP> tcp; Ref<StreamPeerTCP> tcp;
@ -68,7 +68,7 @@ private:
PendingPeer(); PendingPeer();
Error do_handshake(const PoolStringArray p_protocols); Error do_handshake(const Vector<String> p_protocols);
}; };
int _in_buf_size; int _in_buf_size;
@ -78,11 +78,11 @@ private:
List<Ref<PendingPeer> > _pending; List<Ref<PendingPeer> > _pending;
Ref<TCP_Server> _server; Ref<TCP_Server> _server;
PoolStringArray _protocols; Vector<String> _protocols;
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);
Error listen(int p_port, PoolVector<String> p_protocols = PoolVector<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;
int get_max_packet_size() const; int get_max_packet_size() const;