Skip to content

Commit 235f374

Browse files
committed
Extend BehindProxy to also accept separate arguments
1 parent 281f90b commit 235f374

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Web change log
55

66
## 5.0.0 / ????-??-??
77

8+
* Extended `web.filters.BehindProxy` to also accept separate arguments
9+
for remote URL and local path
10+
(@thekid)
811
* Merged PR #126: Verify websocket origin, which is an effective measure
912
against Cross Site WebSocket Hijacking
1013
(@thekid)

src/main/php/web/filters/BehindProxy.class.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,37 @@ class BehindProxy implements Filter {
1313
private $remote, $replace;
1414

1515
/**
16-
* Creates a new instance given a map of the front-facing URL to the local path
16+
* Creates a new instance given a map of the front-facing URL to the local path:
1717
*
18-
* @param [:string] $mapping e.g. `["http://remote.url/" => "/local.path"]`
18+
* ```php
19+
* // Using a map
20+
* new BehindProxy(['http://remote.url/' => '/local.path']);
21+
*
22+
* // Specifying remote URL and local path as separate arguments
23+
* new BehindProxy('http://remote.url/', '/local.path');
24+
* ```
25+
*
26+
* @param [:string]|string $mapping
27+
* @param ?string $target
1928
* @throws lang.IllegalArgumentException
2029
*/
21-
public function __construct($mapping) {
22-
if (1 !== sizeof($mapping)) {
23-
throw new IllegalArgumentException('Expected a map in the form ["http://remote.url/" => "/local.path"]');
30+
public function __construct($mapping, $target= null) {
31+
if (is_array($mapping)) {
32+
if (1 !== sizeof($mapping)) {
33+
throw new IllegalArgumentException('Expected a map in the form ["http://remote.url/" => "/local.path"]');
34+
}
35+
36+
$this->remote= new URI(key($mapping));
37+
$target= current($mapping);
38+
} else {
39+
$this->remote= new URI($mapping);
2440
}
2541

26-
$this->remote= new URI(key($mapping));
2742
if ($this->remote->isRelative()) {
2843
throw new IllegalArgumentException('Remote URL must be absolute');
2944
}
3045

31-
$this->replace= '#^'.rtrim(preg_quote(current($mapping), '#'), '/').'/#';
46+
$this->replace= '#^'.rtrim(preg_quote($target, '#'), '/').'/#';
3247
}
3348

3449
/**

src/test/php/web/unittest/filters/BehindProxyTest.class.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,23 @@
99
class BehindProxyTest {
1010

1111
#[Test]
12-
public function can_create() {
12+
public function can_create_with_mapping() {
1313
new BehindProxy(['http://remote' => '/']);
1414
}
1515

16+
#[Test]
17+
public function can_create_with_remote_and_local() {
18+
new BehindProxy('http://remote', '/');
19+
}
20+
21+
#[Test]
22+
public function mapping_and_remote_and_local_equal() {
23+
Assert::equals(
24+
new BehindProxy(['http://remote' => '/']),
25+
new BehindProxy('http://remote', '/')
26+
);
27+
}
28+
1629
#[Test, Expect(IllegalArgumentException::class), Values([[[]], [[1]], [[1, 2, 3]]])]
1730
public function raises_exception_for_invalid($arg) {
1831
new BehindProxy($arg);

0 commit comments

Comments
 (0)