Skip to content

Commit 6a10a4e

Browse files
committed
BC: Add server params (REMOTE_ADDR) to PSR request
1 parent ac0094a commit 6a10a4e

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ A request handler adapter for workerman, using PSR-7, PSR-15 and PSR-17.
3737
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-workerman-request-handler][1].
3838

3939
```sh
40-
composer require chubbyphp/chubbyphp-workerman-request-handler "^1.2"
40+
composer require chubbyphp/chubbyphp-workerman-request-handler "^2.0"
4141
```
4242

4343
## Usage

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"extra": {
5252
"branch-alias": {
53-
"dev-master": "1.2-dev"
53+
"dev-master": "2.0-dev"
5454
}
5555
},
5656
"scripts": {

src/OnMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(
2020
public function __invoke(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): void
2121
{
2222
$this->workermanResponseEmitter->emit(
23-
$this->requestHander->handle($this->psrRequestFactory->create($workermanRequest)),
23+
$this->requestHander->handle($this->psrRequestFactory->create($workermanTcpConnection, $workermanRequest)),
2424
$workermanTcpConnection
2525
);
2626
}

src/PsrRequestFactory.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Psr\Http\Message\StreamFactoryInterface;
1010
use Psr\Http\Message\UploadedFileFactoryInterface;
1111
use Psr\Http\Message\UploadedFileInterface;
12+
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
1213
use Workerman\Protocols\Http\Request as WorkermanRequest;
1314

1415
final class PsrRequestFactory implements PsrRequestFactoryInterface
@@ -20,11 +21,12 @@ public function __construct(
2021
) {
2122
}
2223

23-
public function create(WorkermanRequest $workermanRequest): ServerRequestInterface
24+
public function create(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): ServerRequestInterface
2425
{
2526
$request = $this->serverRequestFactory->createServerRequest(
2627
$workermanRequest->method(),
27-
$workermanRequest->uri()
28+
$workermanRequest->uri(),
29+
$this->createServerParams($workermanTcpConnection),
2830
);
2931

3032
/** @var array<string, string> $headers */
@@ -47,6 +49,17 @@ public function create(WorkermanRequest $workermanRequest): ServerRequestInterfa
4749
return $request;
4850
}
4951

52+
/**
53+
* @return array<string, string>
54+
*/
55+
private function createServerParams(WorkermanTcpConnection $workermanTcpConnection): array
56+
{
57+
return [
58+
'REMOTE_ADDR' => $workermanTcpConnection->getRemoteIp(),
59+
'REMOTE_PORT' => (string) $workermanTcpConnection->getRemotePort(),
60+
];
61+
}
62+
5063
/**
5164
* @param array<string, array<string, int|string>> $files
5265
*

src/PsrRequestFactoryInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace Chubbyphp\WorkermanRequestHandler;
66

77
use Psr\Http\Message\ServerRequestInterface;
8+
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
89
use Workerman\Protocols\Http\Request as WorkermanRequest;
910

1011
interface PsrRequestFactoryInterface
1112
{
12-
public function create(WorkermanRequest $workermanRequest): ServerRequestInterface;
13+
public function create(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): ServerRequestInterface;
1314
}

tests/Unit/PsrRequestFactoryTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Psr\Http\Message\StreamInterface;
1717
use Psr\Http\Message\UploadedFileFactoryInterface;
1818
use Psr\Http\Message\UploadedFileInterface;
19+
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
1920
use Workerman\Protocols\Http\Request as WorkermanRequest;
2021

2122
/**
@@ -70,6 +71,12 @@ public function testInvoke(): void
7071
Call::create('rawBody')->with()->willReturn('This is the body.'),
7172
]);
7273

74+
/** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */
75+
$workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class, [
76+
Call::create('getRemoteIp')->with()->willReturn('172.16.89.64'),
77+
Call::create('getRemotePort')->with()->willReturn(10817),
78+
]);
79+
7380
/** @var MockObject|StreamInterface $requestBody */
7481
$requestBody = $this->getMockByCalls(StreamInterface::class, [
7582
Call::create('write')->with('This is the body.'),
@@ -162,6 +169,6 @@ static function (array $uploadedFiles) use ($uploadedFile1, $uploadedFile2, $upl
162169
]);
163170

164171
$psrRequestFactory = new PsrRequestFactory($serverRequestFactory, $streamFactory, $uploadedFileFactory);
165-
$psrRequestFactory->create($workermanRequest);
172+
$psrRequestFactory->create($workermanTcpConnection, $workermanRequest);
166173
}
167174
}

0 commit comments

Comments
 (0)