From 204790f1fe5c870e44ab3a555b30b30abac5159c Mon Sep 17 00:00:00 2001 From: Andy Vaughn Date: Tue, 19 Mar 2019 12:50:59 -0700 Subject: [PATCH] plug is not allowed uri with empty host --- src/ApiPlug.php | 2 + .../HttpPlugUriHostRequiredException.php | 45 +++++++++++++++++++ src/HttpPlug.php | 7 ++- tests/HttpPlug/__construct_Test.php | 36 +++++++++++++++ tests/XUri/newFromString_Test.php | 1 - 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/Exception/HttpPlugUriHostRequiredException.php create mode 100644 tests/HttpPlug/__construct_Test.php diff --git a/src/ApiPlug.php b/src/ApiPlug.php index a938c5a..5630160 100644 --- a/src/ApiPlug.php +++ b/src/ApiPlug.php @@ -22,6 +22,7 @@ use Exception; use MindTouch\Http\Content\IContent; use MindTouch\Http\Exception\ApiResultException; +use MindTouch\Http\Exception\HttpPlugUriHostRequiredException; use MindTouch\Http\Exception\HttpResultParserContentExceedsMaxContentLengthException; use MindTouch\Http\Parser\SerializedPhpArrayParser; @@ -85,6 +86,7 @@ public static function urlEncode(string $string, bool $doubleEncode = false) : s /** * @param XUri $uri - target uri * @param string $format + * @throws HttpPlugUriHostRequiredException */ public function __construct(XUri $uri, string $format = self::DREAM_FORMAT_PHP) { parent::__construct($uri); diff --git a/src/Exception/HttpPlugUriHostRequiredException.php b/src/Exception/HttpPlugUriHostRequiredException.php new file mode 100644 index 0000000..9a1f6cc --- /dev/null +++ b/src/Exception/HttpPlugUriHostRequiredException.php @@ -0,0 +1,45 @@ +uri = $uri; + parent::__construct('Uri does not contain a valid hostname: ' . $uri->toString()); + } + + /** + * @return XUri + */ + private function getUri() : XUri { + return $this->uri; + } +} diff --git a/src/HttpPlug.php b/src/HttpPlug.php index 8e6426d..4012b1e 100644 --- a/src/HttpPlug.php +++ b/src/HttpPlug.php @@ -22,6 +22,7 @@ use InvalidArgumentException; use MindTouch\Http\Content\FileContent; use MindTouch\Http\Content\IContent; +use MindTouch\Http\Exception\HttpPlugUriHostRequiredException; use MindTouch\Http\Exception\HttpResultParserContentExceedsMaxContentLengthException; use MindTouch\Http\Exception\MalformedPathQueryFragmentException; use MindTouch\Http\Exception\NotImplementedException; @@ -89,9 +90,13 @@ class HttpPlug { /** * @param XUri $uri - target uri + * @throws HttpPlugUriHostRequiredException */ public function __construct(XUri $uri) { $this->headers = new Headers(); + if(StringUtil::isNullOrEmpty($uri->getHost())) { + throw new HttpPlugUriHostRequiredException($uri); + } $this->uri = $uri; } @@ -212,7 +217,7 @@ public function withoutHeader(string $name) : object { */ public function withUri(XUri $uri, bool $preserveHost = false) : object { $plug = clone $this; - $host = $plug->uri->getHost(); + $host = StringUtil::stringify($plug->uri->getHost()); $plug->uri = $uri; if($preserveHost) { $plug->uri = $plug->uri->withHost($host); diff --git a/tests/HttpPlug/__construct_Test.php b/tests/HttpPlug/__construct_Test.php new file mode 100644 index 0000000..1d2d180 --- /dev/null +++ b/tests/HttpPlug/__construct_Test.php @@ -0,0 +1,36 @@ +