Skip to content

Commit 90472f4

Browse files
authored
Merge pull request #12 from akrabat/psr-15
Support PSR-15 Middlware Interface
2 parents a671a10 + 71f0310 commit 90472f4

File tree

3 files changed

+134
-9
lines changed

3 files changed

+134
-9
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "akrabat/proxy-detection-middleware",
3-
"description": "PSR-7 Middleware that determines the scheme, host and port from the 'X-Forwarded-Proto', 'X-Forwarded-Host' and 'X-Forwarded-Port' headers and updates the Request's Uri object.",
3+
"description": "PSR-7/PSR-15 Middleware that determines the scheme, host and port from the 'X-Forwarded-Proto', 'X-Forwarded-Host' and 'X-Forwarded-Port' headers and updates the Request's Uri object.",
44
"keywords": [
55
"psr7", "middleware", "ip"
66
],
@@ -15,7 +15,8 @@
1515
}
1616
],
1717
"require": {
18-
"psr/http-message": "^1.0"
18+
"psr/http-message": "^1.0",
19+
"psr/http-server-middleware": "^1.0"
1920
},
2021
"require-dev": {
2122
"php": "^8.0",

composer.lock

Lines changed: 115 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ProxyDetection.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
use Psr\Http\Message\ResponseInterface;
55
use Psr\Http\Message\ServerRequestInterface;
66
use Psr\Http\Message\UriInterface;
7+
use Psr\Http\Server\MiddlewareInterface;
8+
use Psr\Http\Server\RequestHandlerInterface;
79

8-
class ProxyDetection
10+
class ProxyDetection implements MiddlewareInterface
911
{
1012
/**
1113
* List of trusted proxy IP addresses
@@ -27,6 +29,11 @@ public function __construct($trustedProxies = [])
2729
$this->trustedProxies = $trustedProxies;
2830
}
2931

32+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
33+
{
34+
return $handler->handle($this->updateRequest($request));
35+
}
36+
3037
/**
3138
* Override the request URI's scheme, host and port as determined from the proxy headers
3239
*
@@ -41,7 +48,12 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
4148
if (!$next) {
4249
return $response;
4350
}
44-
51+
52+
return $next($this->updateRequest($request), $response);
53+
}
54+
55+
protected function updateRequest(ServerRequestInterface $request): ServerRequestInterface
56+
{
4557
if (!empty($this->trustedProxies)) {
4658
// get IP address from REMOTE_ADDR
4759
$ipAddress = null;
@@ -51,7 +63,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
5163
}
5264

5365
if (!in_array($ipAddress, $this->trustedProxies)) {
54-
return $response = $next($request, $response);
66+
return $request;
5567
}
5668
}
5769

@@ -61,9 +73,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
6173
$uri = $this->processPortHeader($request, $uri);
6274
$uri = $this->processHostHeader($request, $uri);
6375

64-
$request = $request->withUri($uri);
65-
66-
return $response = $next($request, $response);
76+
return $request->withUri($uri);
6777
}
6878

6979
/**

0 commit comments

Comments
 (0)