Skip to content

Commit

Permalink
Merge pull request #3 from bohanyang/remote-addr
Browse files Browse the repository at this point in the history
BC: Add server params (REMOTE_ADDR) to PSR request
  • Loading branch information
dominikzogg committed Jul 4, 2022
2 parents ac0094a + 962f323 commit 1a19d31
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
build/
composer.lock
vendor/
.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ A request handler adapter for workerman, using PSR-7, PSR-15 and PSR-17.
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-workerman-request-handler][1].

```sh
composer require chubbyphp/chubbyphp-workerman-request-handler "^1.2"
composer require chubbyphp/chubbyphp-workerman-request-handler "^2.0"
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.2-dev"
"dev-master": "2.0-dev"
}
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/OnMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
public function __invoke(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): void
{
$this->workermanResponseEmitter->emit(
$this->requestHander->handle($this->psrRequestFactory->create($workermanRequest)),
$this->requestHander->handle($this->psrRequestFactory->create($workermanTcpConnection, $workermanRequest)),
$workermanTcpConnection
);
}
Expand Down
17 changes: 15 additions & 2 deletions src/PsrRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
use Workerman\Protocols\Http\Request as WorkermanRequest;

final class PsrRequestFactory implements PsrRequestFactoryInterface
Expand All @@ -20,11 +21,12 @@ public function __construct(
) {
}

public function create(WorkermanRequest $workermanRequest): ServerRequestInterface
public function create(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): ServerRequestInterface
{
$request = $this->serverRequestFactory->createServerRequest(
$workermanRequest->method(),
$workermanRequest->uri()
$workermanRequest->uri(),
$this->createServerParams($workermanTcpConnection),
);

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

/**
* @return array<string, string>
*/
private function createServerParams(WorkermanTcpConnection $workermanTcpConnection): array
{
return [
'REMOTE_ADDR' => $workermanTcpConnection->getRemoteIp(),
'REMOTE_PORT' => (string) $workermanTcpConnection->getRemotePort(),
];
}

/**
* @param array<string, array<string, int|string>> $files
*
Expand Down
3 changes: 2 additions & 1 deletion src/PsrRequestFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace Chubbyphp\WorkermanRequestHandler;

use Psr\Http\Message\ServerRequestInterface;
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
use Workerman\Protocols\Http\Request as WorkermanRequest;

interface PsrRequestFactoryInterface
{
public function create(WorkermanRequest $workermanRequest): ServerRequestInterface;
public function create(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): ServerRequestInterface;
}
2 changes: 1 addition & 1 deletion tests/Unit/OnMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testInvoke(): void

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

/** @var MockObject|WorkermanResponseEmitterInterface $workermanResponseEmitter */
Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/PsrRequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
use Workerman\Protocols\Http\Request as WorkermanRequest;

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

/** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class, [
Call::create('getRemoteIp')->with()->willReturn('172.16.89.64'),
Call::create('getRemotePort')->with()->willReturn(10817),
]);

/** @var MockObject|StreamInterface $requestBody */
$requestBody = $this->getMockByCalls(StreamInterface::class, [
Call::create('write')->with('This is the body.'),
Expand Down Expand Up @@ -157,11 +164,11 @@ static function (array $uploadedFiles) use ($uploadedFile1, $uploadedFile2, $upl
/** @var MockObject|ServerRequestFactoryInterface $serverRequestFactory */
$serverRequestFactory = $this->getMockByCalls(ServerRequestFactoryInterface::class, [
Call::create('createServerRequest')
->with('POST', '/application', [])
->with('POST', '/application', ['REMOTE_ADDR' => '172.16.89.64', 'REMOTE_PORT' => '10817'])
->willReturn($request),
]);

$psrRequestFactory = new PsrRequestFactory($serverRequestFactory, $streamFactory, $uploadedFileFactory);
$psrRequestFactory->create($workermanRequest);
$psrRequestFactory->create($workermanTcpConnection, $workermanRequest);
}
}

0 comments on commit 1a19d31

Please sign in to comment.