[Net] Implement String::parse_url for parsing URLs.

Splits the URL into (scheme, host, port, path).
Supports both literal IPv4 and IPv6.
Strip credentials when present (e.g. http://user:pass@example.com/).

Use that function in both HTTPRequest and WebSocketClient.

(cherry picked from commit 3bb40669d5)
This commit is contained in:
Fabio Alessandrelli 2021-04-26 09:33:28 +02:00 committed by Rémi Verschelde
parent 3b60911857
commit 77e3514315
No known key found for this signature in database
GPG key ID: C3336907360768E1
4 changed files with 84 additions and 51 deletions

View file

@ -45,33 +45,18 @@ Error WebSocketClient::connect_to_url(String p_url, const Vector<String> p_proto
String host = p_url;
String path = "/";
int p_len = -1;
String scheme = "";
int port = 80;
Error err = p_url.parse_url(scheme, host, port, path);
ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid URL: " + p_url);
bool ssl = false;
if (host.begins_with("wss://")) {
ssl = true; // we should implement this
host = host.substr(6, host.length() - 6);
port = 443;
} else {
ssl = false;
if (host.begins_with("ws://"))
host = host.substr(5, host.length() - 5);
if (scheme == "wss://") {
ssl = true;
}
// Path
p_len = host.find("/");
if (p_len != -1) {
path = host.substr(p_len, host.length() - p_len);
host = host.substr(0, p_len);
if (port == 0) {
port = ssl ? 443 : 80;
}
// Port
p_len = host.find_last(":");
if (p_len != -1 && p_len == host.find(":")) {
port = host.substr(p_len, host.length() - p_len).to_int();
host = host.substr(0, p_len);
}
return connect_to_host(host, path, port, ssl, p_protocols, p_custom_headers);
}