From: gggeek Date: Wed, 1 Feb 2023 17:21:03 +0000 (+0000) Subject: fix Client::getUrl X-Git-Tag: 4.10.0~40 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=80a07068191fe18d74e91bffc28394d4abcbb55d;p=plcapi.git fix Client::getUrl --- diff --git a/src/Client.php b/src/Client.php index 800e928e..49822222 100644 --- a/src/Client.php +++ b/src/Client.php @@ -918,25 +918,29 @@ class Client */ public function getUrl($component = null) { - switch($component) { - case PHP_URL_SCHEME: - return $this->method; - case PHP_URL_HOST: - return $this->server; - case PHP_URL_PORT: - return $this->port; - case PHP_URL_PATH: - return $this->path; - case '': - $url = $this->method . '://' . $this->server; - if (($this->port == 80 && in_array($this->method, array('http', 'http10', 'http11', 'h2c'))) || - ($this->port == 443 && in_array($this->method, array('https', 'h2')))) { - return $url . $this->path; - } else { - return $url . ':' . $this->port . $this->path; - } - default: - throw new ValueErrorException("Unsupported component '$component'"); + if (is_int($component) || ctype_digit($component)) { + switch ($component) { + case PHP_URL_SCHEME: + return $this->method; + case PHP_URL_HOST: + return $this->server; + case PHP_URL_PORT: + return $this->port; + case PHP_URL_PATH: + return $this->path; + case '': + + default: + throw new ValueErrorException("Unsupported component '$component'"); + } + } + + $url = $this->method . '://' . $this->server; + if ($this->port == 0 || ($this->port == 80 && in_array($this->method, array('http', 'http10', 'http11', 'h2c'))) || + ($this->port == 443 && in_array($this->method, array('https', 'h2')))) { + return $url . $this->path; + } else { + return $url . ':' . $this->port . $this->path; } } diff --git a/tests/07ClientTest.php b/tests/07ClientTest.php index 07a6abdc..e84e920e 100644 --- a/tests/07ClientTest.php +++ b/tests/07ClientTest.php @@ -85,4 +85,16 @@ class ClientTest extends PhpXmlRpc_LoggerAwareTestCase $ro = $r->value(); is_object($ro) && $this->assertEquals('hello', $ro->scalarVal()); } + + public function testgetUrl() + { + $m = $this->client->getUrl(PHP_URL_SCHEME); + $this->assertEquals($m, $this->client->method); + $h = $this->client->getUrl(PHP_URL_HOST); + $this->assertEquals($h, $this->client->server); + $p = $this->client->getUrl(PHP_URL_PORT); + $this->assertEquals($p, $this->client->port); + $p = $this->client->getUrl(PHP_URL_PATH); + $this->assertEquals($p, $this->client->path); + } }