Skip to content

Commit b47cfbd

Browse files
committed
added RequestFactory::setForceHttps()
1 parent 3e48185 commit b47cfbd

3 files changed

Lines changed: 35 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|null>,
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class RequestFactory
3737
/** @var list<string> */
3838
private array $proxies = [];
3939

40+
private bool $forceHttps = false;
41+
4042

4143
/**
4244
* Disables sanitization of request data (GET, POST, cookies, file names) for binary-safe handling.
@@ -59,6 +61,16 @@ public function setProxy($proxy): static
5961
}
6062

6163

64+
/**
65+
* Forces the request scheme to HTTPS regardless of the server environment.
66+
*/
67+
public function setForceHttps(bool $forceHttps = true): static
68+
{
69+
$this->forceHttps = $forceHttps;
70+
return $this;
71+
}
72+
73+
6274
/**
6375
* Returns new Request instance, using values from superglobals.
6476
*/
@@ -70,6 +82,10 @@ public function fromGlobals(): Request
7082
[$post, $cookies] = $this->getGetPostCookie($url);
7183
[$remoteAddr, $remoteHost] = $this->getClient($url);
7284

85+
if ($this->forceHttps) {
86+
$url->setScheme('https');
87+
}
88+
7389
return new Request(
7490
new UrlScript($url, $this->getScriptPath($url)),
7591
$post,

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)