Skip to content

Commit d92cb97

Browse files
committed
added RequestFactory::setForceHttps()
1 parent ffa2680 commit d92cb97

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/Bridges/HttpDI/HttpExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*
1818
* @property object{
1919
* proxy: array<string>,
20+
* forceHttps: bool,
2021
* headers: array<string, ?scalar>,
2122
* frames: string|bool|null,
2223
* csp: array<string, array<mixed>|scalar|null>,
@@ -40,6 +41,7 @@ public function getConfigSchema(): Nette\Schema\Schema
4041
{
4142
return Expect::structure([
4243
'proxy' => Expect::anyOf(Expect::arrayOf('string'), Expect::string()->castTo('array'))->firstIsDefault()->dynamic(),
44+
'forceHttps' => Expect::bool(false)->dynamic(),
4345
'headers' => Expect::arrayOf('scalar|null')->default([
4446
'X-Powered-By' => 'Nette Framework 3',
4547
'Content-Type' => 'text/html; charset=utf-8',
@@ -61,10 +63,14 @@ public function loadConfiguration(): void
6163
$builder = $this->getContainerBuilder();
6264
$config = $this->config;
6365

64-
$builder->addDefinition($this->prefix('requestFactory'))
66+
$requestFactory = $builder->addDefinition($this->prefix('requestFactory'))
6567
->setFactory(Nette\Http\RequestFactory::class)
6668
->addSetup('setProxy', [$config->proxy]);
6769

70+
if ($config->forceHttps) {
71+
$requestFactory->addSetup('setForceHttps');
72+
}
73+
6874
$request = $builder->addDefinition($this->prefix('request'))
6975
->setFactory('@Nette\Http\RequestFactory::fromGlobals');
7076

src/Http/RequestFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class RequestFactory
3333
];
3434

3535
private bool $binary = false;
36+
private bool $forceHttps = false;
3637

3738
/** @var list<string> */
3839
private array $proxies = [];
@@ -59,6 +60,16 @@ public function setProxy($proxy): static
5960
}
6061

6162

63+
/**
64+
* Forces the request scheme to HTTPS regardless of the server environment.
65+
*/
66+
public function setForceHttps(bool $forceHttps = true): static
67+
{
68+
$this->forceHttps = $forceHttps;
69+
return $this;
70+
}
71+
72+
6273
/**
6374
* Returns new Request instance, using values from superglobals.
6475
*/
@@ -69,6 +80,9 @@ public function fromGlobals(): Request
6980
$this->getPathAndQuery($url);
7081
[$post, $cookies] = $this->getGetPostCookie($url);
7182
[$remoteAddr, $remoteHost] = $this->getClient($url);
83+
if ($this->forceHttps) {
84+
$url->setScheme('https');
85+
}
7286

7387
return new Request(
7488
new UrlScript($url, $this->getScriptPath($url)),

tests/Http/RequestFactory.scheme.phpt

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

7890

0 commit comments

Comments
 (0)