Skip to content

Commit 32d0abd

Browse files
committed
extract host & port from endpoint
1 parent 93d52b6 commit 32d0abd

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

src/OSS/Core/OssUtil.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,43 @@ public static function isIPFormat($endpoint)
362362
}
363363
}
364364

365+
/**
366+
* Get the host:port from endpoint.
367+
*
368+
* @param string $endpoint the endpoint.
369+
* @return boolean
370+
*/
371+
public static function getHostPortFromEndpoint($endpoint)
372+
{
373+
$str = $endpoint;
374+
$pos = strpos($str, "://");
375+
if ($pos !== false) {
376+
$str = substr($str, $pos+3);
377+
}
378+
379+
$pos = strpos($str, '#');
380+
if ($pos !== false) {
381+
$str = substr($str, 0, $pos);
382+
}
383+
384+
$pos = strpos($str, '?');
385+
if ($pos !== false) {
386+
$str = substr($str, 0, $pos);
387+
}
388+
389+
$pos = strpos($str, '/');
390+
if ($pos !== false) {
391+
$str = substr($str, 0, $pos);
392+
}
393+
394+
$pos = strpos($str, '@');
395+
if ($pos !== false) {
396+
$str = substr($str, $pos+1);
397+
}
398+
399+
return $str;
400+
}
401+
365402
/**
366403
* Generate the xml message of DeleteMultiObjects.
367404
*

src/OSS/OssClient.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,6 +2541,8 @@ private function checkEndpoint($endpoint, $isCName)
25412541
$ret_endpoint = $endpoint;
25422542
}
25432543

2544+
$ret_endpoint = OssUtil::getHostPortFromEndpoint($ret_endpoint);
2545+
25442546
if ($isCName) {
25452547
$this->hostType = self::OSS_HOST_TYPE_CNAME;
25462548
} elseif (OssUtil::isIPFormat($ret_endpoint)) {

tests/OSS/Tests/OssClientObjectTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,16 @@ public function testCheckMD5()
580580
}
581581
}
582582

583+
public function testWithInvalidBucketName()
584+
{
585+
try {
586+
$this->ossClient->createBucket("abcefc/", "test-key");
587+
$this->assertFalse(true);
588+
} catch (OssException $e) {
589+
$this->assertEquals('"abcefc/"bucket name is invalid', $e->getMessage());
590+
}
591+
}
592+
583593
public function setUp()
584594
{
585595
parent::setUp();

tests/OSS/Tests/OssUtilTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,30 @@ private function cleanXml($xml)
222222
return str_replace("\n", "", str_replace("\r", "", $xml));
223223
}
224224

225+
public function testGetHostPortFromEndpoint()
226+
{
227+
$str = OssUtil::getHostPortFromEndpoint('http://username:password@hostname:80/path?arg=value#anchor');
228+
$this->assertEquals('hostname:80', $str);
229+
230+
$str = OssUtil::getHostPortFromEndpoint('hostname:80');
231+
$this->assertEquals('hostname:80', $str);
232+
233+
$str = OssUtil::getHostPortFromEndpoint('www.hostname.com');
234+
$this->assertEquals('www.hostname.com', $str);
235+
236+
$str = OssUtil::getHostPortFromEndpoint('http://www.hostname.com');
237+
$this->assertEquals('www.hostname.com', $str);
238+
239+
$str = OssUtil::getHostPortFromEndpoint('https://www.hostname.com');
240+
$this->assertEquals('www.hostname.com', $str);
241+
242+
$str = OssUtil::getHostPortFromEndpoint('192.168.1.10:8080');
243+
$this->assertEquals('192.168.1.10:8080', $str);
244+
245+
$str = OssUtil::getHostPortFromEndpoint('http:///path?arg=value#anchor');
246+
$this->assertEquals('', $str);
247+
248+
$str = OssUtil::getHostPortFromEndpoint('file://username:password@hostname:80/path?arg=value#anchor');
249+
$this->assertEquals('hostname:80', $str);
250+
}
225251
}

0 commit comments

Comments
 (0)