Skip to content

Commit 8e0967d

Browse files
committed
Added easy way to strip ingress prefixes from request path
1 parent 26e3864 commit 8e0967d

5 files changed

Lines changed: 50 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
### 11.4.2, <small>(2025-09-16)</small>
2+
3+
#### New
4+
5+
* The request class can now strip a configurable prefix from the request path.
6+
7+
--------------------------------------------------------
8+
19
### 11.3.1, 11.4.1 <small>(2025-09-01)</small>
210

311
#### Bugfixes

src/mako/Mako.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class Mako
1515
/**
1616
* Mako version.
1717
*/
18-
public const string VERSION = '11.4.1';
18+
public const string VERSION = '11.4.2';
1919

2020
/**
2121
* Mako major version.
@@ -30,5 +30,5 @@ final class Mako
3030
/**
3131
* Mako patch version.
3232
*/
33-
public const int VERSION_PATCH = 1;
33+
public const int VERSION_PATCH = 2;
3434
}

src/mako/application/services/HTTPService.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ public function register(): void
4444
// Request
4545

4646
$this->container->registerSingleton([Request::class, 'request'], static function ($container) use ($config) {
47-
$request = new Request(['languages' => $config['languages']], $container->get(Signer::class), $config['script_name'] ?? null);
47+
$request = new Request(
48+
['languages' => $config['languages']],
49+
$container->get(Signer::class),
50+
$config['script_name'] ?? null,
51+
$config['ingress_prefix'] ?? null,
52+
);
4853

4954
if (!empty($config['trusted_proxies'])) {
5055
$request->setTrustedProxies($config['trusted_proxies']);

src/mako/http/Request.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use function rawurldecode;
3636
use function rtrim;
3737
use function str_replace;
38+
use function str_starts_with;
3839
use function stripos;
3940
use function strlen;
4041
use function strpos;
@@ -188,8 +189,12 @@ class Request
188189
/**
189190
* Constructor.
190191
*/
191-
public function __construct(array $request = [], ?Signer $signer = null, ?string $scriptName = null)
192-
{
192+
public function __construct(
193+
array $request = [],
194+
?Signer $signer = null,
195+
?string $scriptName = null,
196+
protected ?string $ingressPrefix = null
197+
) {
193198
// Collect request data
194199

195200
$this->query = new Parameters($request['get'] ?? $_GET);
@@ -217,6 +222,20 @@ public function __construct(array $request = [], ?Signer $signer = null, ?string
217222
$this->method = $request['method'] ?? $this->determineMethod();
218223
}
219224

225+
/**
226+
* Strips the ingress prefix from the path.
227+
*/
228+
protected function stripIngressPrefix(string $path): string
229+
{
230+
if ($this->ingressPrefix) {
231+
if (str_starts_with($path, $this->ingressPrefix)) {
232+
return mb_substr($path, mb_strlen($this->ingressPrefix));
233+
}
234+
}
235+
236+
return $path;
237+
}
238+
220239
/**
221240
* Strips the locale segment from the path.
222241
*/
@@ -271,7 +290,7 @@ protected function determinePath(array $languages): string
271290
}
272291
}
273292

274-
return $this->stripLocaleSegment($languages, $path);
293+
return $this->stripLocaleSegment($languages, $this->stripIngressPrefix($path));
275294
}
276295

277296
/**

tests/unit/http/RequestTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,18 @@ public function testPath(): void
473473
$this->assertEquals('/foo/bar', $request->getPath());
474474
}
475475

476+
/**
477+
*
478+
*/
479+
public function testPathWithPrefix(): void
480+
{
481+
$server = ['PATH_INFO' => '/api/openapi/docs'];
482+
483+
$request = new Request(['server' => $server], ingressPrefix: '/api');
484+
485+
$this->assertEquals('/openapi/docs', $request->getPath());
486+
}
487+
476488
/**
477489
*
478490
*/

0 commit comments

Comments
 (0)