Page MenuHomePhabricator

D11338.diff
No OneTemporary

D11338.diff

diff --git a/src/parser/PhutilURI.php b/src/parser/PhutilURI.php
--- a/src/parser/PhutilURI.php
+++ b/src/parser/PhutilURI.php
@@ -147,6 +147,19 @@
return $this->port;
}
+ public function getPortWithProtocolDefault() {
+ static $default_ports = array(
+ 'http' => '80',
+ 'https' => '443',
+ 'ssh' => '22',
+ );
+
+ return nonempty(
+ $this->getPort(),
+ idx($default_ports, $this->getProtocol()),
+ '');
+ }
+
public function setPath($path) {
if ($this->domain && strlen($path) && $path[0] !== '/') {
$path = '/'.$path;
diff --git a/src/parser/__tests__/PhutilURITestCase.php b/src/parser/__tests__/PhutilURITestCase.php
--- a/src/parser/__tests__/PhutilURITestCase.php
+++ b/src/parser/__tests__/PhutilURITestCase.php
@@ -116,4 +116,18 @@
$uri->getQueryParams());
}
+ public function testDefaultPorts() {
+ $uri = new PhutilURI('http://www.example.com');
+ $this->assertEqual('80', $uri->getPortWithProtocolDefault());
+
+ $uri = new PhutilURI('https://www.example.com');
+ $this->assertEqual('443', $uri->getPortWithProtocolDefault());
+
+ $uri = new PhutilURI('ssh://git@example.com/example/example.git');
+ $this->assertEqual('22', $uri->getPortWithProtocolDefault());
+
+ $uri = new PhutilURI('unknown://www.example.com');
+ $this->assertEqual('', $uri->getPortWithProtocolDefault());
+ }
+
}

File Metadata

Mime Type
text/plain
Expires
Fri, Mar 14, 1:32 PM (1 w, 1 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7658372
Default Alt Text
D11338.diff (1 KB)

Event Timeline