Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve bailouts #120

Open
wants to merge 5 commits into
base: 2.0-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Tests/AbstractWebApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ public function testGetBody()
}

/**
* @testdox Tests that the application correcty detects the request URI based on the injected data
* @testdox Tests that the application correctly detects the request URI based on the injected data
*
* @param string|null $https Value for $_SERVER['HTTPS'] or null to not set it
* @param string $phpSelf Value for $_SERVER['PHP_SELF']
Expand Down Expand Up @@ -1453,6 +1453,34 @@ public function testDetectRequestUri(
);
}

/**
* @testdox Tests that the application throws an exception when there isn't a valid HTTP host.
*
* @param string|null $https Value for $_SERVER['HTTPS'] or null to not set it
* @param string $phpSelf Value for $_SERVER['PHP_SELF']
* @param string $requestUri Value for $_SERVER['REQUEST_URI']
* @param string $httpHost Value for $_SERVER['HTTP_HOST']
* @param string $scriptName Value for $_SERVER['SCRIPT_NAME']
* @param string $queryString Value for $_SERVER['QUERY_STRING']
* @param string $expects Expected full URI string
*
* @covers \Joomla\Application\AbstractWebApplication
*
* @backupGlobals enabled
*/
public function testDetectRequestUriException()
{
$this->expectException(\InvalidArgumentException::class);
$mockInput = new Input([]);

$_SERVER['PHP_SELF'] = 'somthing/framework.php';
$_SERVER['HTTP_HOST'] = '';
$_SERVER['SCRIPT_NAME'] = 'somthing/framework.php';

$object = $this->getMockForAbstractClass(AbstractWebApplication::class, [$mockInput]);

TestHelper::invoke($object, 'detectRequestUri');
}
/**
* @testdox Tests the system URIs are correctly loaded when a URI is set in the application configuration
*
Expand Down
13 changes: 12 additions & 1 deletion src/AbstractWebApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,20 @@ protected function checkHeadersSent()
* @return string The requested URI
*
* @since 1.0.0
* @throws \InvalidArgumentException
*/
protected function detectRequestUri()
{
/**
* If we've arrived here via a CLI application (which shouldn't happen in our web app) then we can use this as
* an opportunity to bail out
*/
$httpHost = $this->input->server->getString('HTTP_HOST');

if ($httpHost === null) {
throw new \InvalidArgumentException('Found an empty hostname when parsing the request');
}

// First we need to detect the URI scheme.
$scheme = $this->isSslConnection() ? 'https://' : 'http://';

Expand All @@ -808,7 +819,7 @@ protected function detectRequestUri()
$phpSelf = $this->input->server->getString('PHP_SELF', '');
$requestUri = $this->input->server->getString('REQUEST_URI', '');

$uri = $scheme . $this->input->server->getString('HTTP_HOST');
$uri = $scheme . $httpHost;

if (!empty($phpSelf) && !empty($requestUri)) {
// If PHP_SELF and REQUEST_URI are both populated then we will assume "Apache Mode".
Expand Down