Skip to content

Commit

Permalink
plug is not allowed uri with empty host
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Vaughn committed Mar 19, 2019
1 parent e972a8b commit 204790f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/ApiPlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
45 changes: 45 additions & 0 deletions src/Exception/HttpPlugUriHostRequiredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);
/**
* MindTouch HTTP
* Copyright (C) 2006-2018 MindTouch, Inc.
* www.mindtouch.com [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace MindTouch\Http\Exception;

use Exception;
use MindTouch\Http\XUri;

class HttpPlugUriHostRequiredException extends Exception {

/**
* @var XUri
*/
private $uri;

/**
* @param XUri $uri
*/
public function __construct(XUri $uri) {
$this->uri = $uri;
parent::__construct('Uri does not contain a valid hostname: ' . $uri->toString());
}

/**
* @return XUri
*/
private function getUri() : XUri {
return $this->uri;
}
}
7 changes: 6 additions & 1 deletion src/HttpPlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions tests/HttpPlug/__construct_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);
/**
* MindTouch HTTP
* Copyright (C) 2006-2018 MindTouch, Inc.
* www.mindtouch.com [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace MindTouch\Http\tests\HttpPlug;

use MindTouch\Http\HttpPlug;
use MindTouch\Http\tests\MindTouchHttpUnitTestCase;
use MindTouch\Http\XUri;

class __construct_Test extends MindTouchHttpUnitTestCase {

/**
* @test
* @expectedException \MindTouch\Http\Exception\HttpPlugUriHostRequiredException
*/
public function Cannot_construct_plug_with_empty_host() {

// act
new HttpPlug(XUri::tryParse('file:///etc/passwd'));
}
}
1 change: 0 additions & 1 deletion tests/XUri/newFromString_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
namespace MindTouch\Http\tests\XUri;

use MindTouch\Http\Exception\MalformedUriException;
use MindTouch\Http\tests\MindTouchHttpUnitTestCase;
use MindTouch\Http\XUri;

Expand Down

0 comments on commit 204790f

Please sign in to comment.