Properly merge server origins with ports

dev
gravel 7 months ago
parent f56a328e35
commit e073e69e18
Signed by: gravel
GPG Key ID: C0538F3C906B308F

@ -789,7 +789,7 @@
} }
// Prefer domain names over IPs (connections to SOGS survive relocation). // 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(); $this->base_url = $this->get_scheme() . "://" . $server->get_hostname();
} }
@ -1048,11 +1048,13 @@
/** /**
* Returns the hostname for this server. * 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. * @return string URL with hostname and port, if applicable.
* Scheme not included. * Scheme not included.
*/ */
function get_hostname() { function get_hostname(bool $include_port = true) {
return url_get_base($this->base_url, include_scheme: false); return url_get_base($this->base_url, include_scheme: false, include_port: $include_port);
} }
/** /**

@ -84,15 +84,17 @@
* @param string $url The URL to slice the path from. * @param string $url The URL to slice the path from.
* @param bool $include_scheme [optional] * @param bool $include_scheme [optional]
* Includes the scheme. `true` by default. * 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), * @return string A URL composed of the original scheme (unless specified),
* hostname, and port (if present). * 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); $url_components = parse_url($url);
$scheme = $url_components['scheme']; $scheme = $url_components['scheme'];
$host = $url_components['host']; $host = $url_components['host'];
if (isset($url_components['port'])) { if (isset($url_components['port']) && $include_port) {
$port = $url_components['port']; $port = $url_components['port'];
$host .= ":$port"; $host .= ":$port";
} }

Loading…
Cancel
Save