Skip to content

Commit fc3ebe0

Browse files
committed
added RequestFactory::setForceHttps()
1 parent bb54e9d commit fc3ebe0

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/Bridges/HttpDI/HttpExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function getConfigSchema(): Nette\Schema\Schema
2929
{
3030
return Expect::structure([
3131
'proxy' => Expect::anyOf(Expect::arrayOf('string'), Expect::string()->castTo('array'))->firstIsDefault()->dynamic(),
32+
'forceHttps' => Expect::bool(false)->dynamic(),
3233
'headers' => Expect::arrayOf('scalar|null')->default([
3334
'X-Powered-By' => 'Nette Framework 3',
3435
'Content-Type' => 'text/html; charset=utf-8',
@@ -50,10 +51,14 @@ public function loadConfiguration(): void
5051
$builder = $this->getContainerBuilder();
5152
$config = $this->config;
5253

53-
$builder->addDefinition($this->prefix('requestFactory'))
54+
$requestFactory = $builder->addDefinition($this->prefix('requestFactory'))
5455
->setFactory(Nette\Http\RequestFactory::class)
5556
->addSetup('setProxy', [$config->proxy]);
5657

58+
if ($config->forceHttps) {
59+
$requestFactory->addSetup('setForceHttps');
60+
}
61+
5762
$request = $builder->addDefinition($this->prefix('request'))
5863
->setFactory('@Nette\Http\RequestFactory::fromGlobals');
5964

src/Http/RequestFactory.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class RequestFactory
3535
/** @var list<string> */
3636
private array $proxies = [];
3737

38+
private bool $forceHttps = false;
39+
3840

3941
public function setBinary(bool $binary = true): static
4042
{
@@ -53,6 +55,13 @@ public function setProxy($proxy): static
5355
}
5456

5557

58+
public function setForceHttps(bool $forceHttps = true): static
59+
{
60+
$this->forceHttps = $forceHttps;
61+
return $this;
62+
}
63+
64+
5665
/**
5766
* Returns new Request instance, using values from superglobals.
5867
*/
@@ -64,6 +73,10 @@ public function fromGlobals(): Request
6473
[$post, $cookies] = $this->getGetPostCookie($url);
6574
[$remoteAddr, $remoteHost] = $this->getClient($url);
6675

76+
if ($this->forceHttps) {
77+
$url->setScheme('https');
78+
}
79+
6780
return new Request(
6881
new UrlScript($url, $this->getScriptPath($url)),
6982
$post,

tests/Http/RequestFactory.scheme.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ class RequestFactorySchemeTest extends Tester\TestCase
7575
['https', 80, ['SERVER_NAME' => 'localhost:80', 'HTTPS' => 'off', 'HTTP_X_FORWARDED_PROTO' => 'https', 'HTTP_X_FORWARDED_PORT' => '80']],
7676
];
7777
}
78+
79+
80+
public function testForceHttps()
81+
{
82+
$_SERVER = ['SERVER_NAME' => 'localhost:80'];
83+
84+
$factory = new Nette\Http\RequestFactory;
85+
$factory->setForceHttps();
86+
$url = $factory->fromGlobals()->getUrl();
87+
88+
Assert::same('https', $url->getScheme());
89+
}
7890
}
7991

8092

0 commit comments

Comments
 (0)