Skip to content

Commit 1a19d31

Browse files
authored
Merge pull request #3 from bohanyang/remote-addr
BC: Add server params (REMOTE_ADDR) to PSR request
2 parents ac0094a + 962f323 commit 1a19d31

8 files changed

+31
-9
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
build/
55
composer.lock
66
vendor/
7+
.phpunit.result.cache

Diff for: README.md

+1-1
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

Diff for: composer.json

+1-1
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": {

Diff for: src/OnMessage.php

+1-1
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
}

Diff for: src/PsrRequestFactory.php

+15-2
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
*

Diff for: src/PsrRequestFactoryInterface.php

+2-1
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
}

Diff for: tests/Unit/OnMessageTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testInvoke(): void
4242

4343
/** @var MockObject|PsrRequestFactoryInterface $psrRequestFactory */
4444
$psrRequestFactory = $this->getMockByCalls(PsrRequestFactoryInterface::class, [
45-
Call::create('create')->with($workermanRequest)->willReturn($request),
45+
Call::create('create')->with($workermanTcpConnection, $workermanRequest)->willReturn($request),
4646
]);
4747

4848
/** @var MockObject|WorkermanResponseEmitterInterface $workermanResponseEmitter */

Diff for: tests/Unit/PsrRequestFactoryTest.php

+9-2
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.'),
@@ -157,11 +164,11 @@ static function (array $uploadedFiles) use ($uploadedFile1, $uploadedFile2, $upl
157164
/** @var MockObject|ServerRequestFactoryInterface $serverRequestFactory */
158165
$serverRequestFactory = $this->getMockByCalls(ServerRequestFactoryInterface::class, [
159166
Call::create('createServerRequest')
160-
->with('POST', '/application', [])
167+
->with('POST', '/application', ['REMOTE_ADDR' => '172.16.89.64', 'REMOTE_PORT' => '10817'])
161168
->willReturn($request),
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)