From e073e69e18204d0d90c90f7187b2b0136d894e60 Mon Sep 17 00:00:00 2001 From: gravel Date: Thu, 14 Dec 2023 14:47:19 +0000 Subject: [PATCH] Properly merge server origins with ports --- php/servers/servers-rooms.php | 8 +++++--- php/utils/utils.php | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/php/servers/servers-rooms.php b/php/servers/servers-rooms.php index 65b148c..65f0eb0 100644 --- a/php/servers/servers-rooms.php +++ b/php/servers/servers-rooms.php @@ -789,7 +789,7 @@ } // Prefer domain names over IPs (connections to SOGS survive relocation). - if (filter_var($this->get_hostname(), FILTER_VALIDATE_IP)) { + if (filter_var($this->get_hostname(include_port: false), FILTER_VALIDATE_IP)) { $this->base_url = $this->get_scheme() . "://" . $server->get_hostname(); } @@ -1048,11 +1048,13 @@ /** * Returns the hostname for this server. + * @param bool $include_scheme [optional] + * Includes the port. `true` by default. * @return string URL with hostname and port, if applicable. * Scheme not included. */ - function get_hostname() { - return url_get_base($this->base_url, include_scheme: false); + function get_hostname(bool $include_port = true) { + return url_get_base($this->base_url, include_scheme: false, include_port: $include_port); } /** diff --git a/php/utils/utils.php b/php/utils/utils.php index 4c6850d..6921609 100644 --- a/php/utils/utils.php +++ b/php/utils/utils.php @@ -84,15 +84,17 @@ * @param string $url The URL to slice the path from. * @param bool $include_scheme [optional] * Includes the scheme. `true` by default. + * @param bool $include_scheme [optional] + * Includes the port. `true` by default. * @return string A URL composed of the original scheme (unless specified), * hostname, and port (if present). */ - function url_get_base(string $url, bool $include_scheme = true) { + function url_get_base(string $url, bool $include_scheme = true, bool $include_port = true) { $url_components = parse_url($url); $scheme = $url_components['scheme']; $host = $url_components['host']; - if (isset($url_components['port'])) { + if (isset($url_components['port']) && $include_port) { $port = $url_components['port']; $host .= ":$port"; }