Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
Closed
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
23 changes: 23 additions & 0 deletions src/HttpsUri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Http;

use Zend\Uri\Http as Http;

class HttpsUri extends Http
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason to introduce this class.
$uri->setScheme('https'); provides exactly the same behavior

{
/**
* Get the scheme part of the URI
*
* @return string|null
*/
public function getScheme()
{
return 'https';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks Uri contract by ignoring scheme changes
Besides, internal logic will be broken in unpredictable ways where scheme property is used directly

}
}
6 changes: 6 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Zend\Stdlib\RequestInterface;
use Zend\Uri\Exception as UriException;
use Zend\Uri\Http as HttpUri;
use Zend\Http\HttpsUri;

/**
* HTTP Request
Expand Down Expand Up @@ -228,6 +229,11 @@ public function getUri()
if ($this->uri === null || is_string($this->uri)) {
$this->uri = new HttpUri($this->uri);
}

$headers = $this->getHeaders()->toArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need all the headers. Use has() and get()

if (array_key_exists('X-Forwarded-Proto', $headers) && $headers['X-Forwarded-Proto'] === 'https') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will end up overriding scheme each time Uri is fetched from Request. Getters must not have side effects

$this->uri = new HttpsUri($this->uri);
}
return $this->uri;
}

Expand Down
20 changes: 20 additions & 0 deletions test/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,26 @@ public function testFromStringFactoryCreatesSingleObjectWithHeaderFolding()
$this->assertEquals('foo-bar', $header->getFieldValue());
}

public function testRequestUriRespectsXForwardedProtoHeader()
{
$request = Request::fromString("GET /foo HTTP/1.1");
$request->setUri("http://someurl.test");

$headers = new Headers();
$h = new GenericHeader('X-Forwarded-Proto', 'https');
$headers->addHeader($h);
$request->setHeaders($headers);
$this->assertEquals('https', $request->getUri()->getScheme());
echo "End of my test";
}

public function testRequestUriSchemeWithoutXForwardedProtoHeader()
{
$request = new Request();
$request->setUri("http://someurl.test");
$this->assertEquals('http', $request->getUri()->getScheme());
}

/**
* @see http://en.wikipedia.org/wiki/HTTP_response_splitting
* @group ZF2015-04
Expand Down