2017 © Pedro Peláez
 

library remote-port-middleware

PSR-7 Middleware that determines the client remote port and stores it as an ServerRequest attribute

image

luisinder/remote-port-middleware

PSR-7 Middleware that determines the client remote port and stores it as an ServerRequest attribute

  • Tuesday, December 13, 2016
  • by luisinder
  • Repository
  • 1 Watchers
  • 1 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Remote Port Middleware

Latest Stable Version Total Downloads, (*1)

PSR-7 / PSR-15 middleware that determines the client's remote TCP port (from the REMOTE_PORT server param) and stores it as a ServerRequest attribute named remotePort., (*2)

Works with: - Slim 4+ (PSR-15 single-pass) - Slim 3 (legacy double-pass) – still supported for backwards compatibility - Any PSR-15 compatible framework (Mezzio, etc.), (*3)

Requirements

  • PHP >= 8.0
  • psr/http-message ^1.0 || ^2.0
  • psr/http-server-middleware ^1.0

Installation

composer require luisinder/remote-port-middleware

How it works

On each request the middleware inspects $request->getServerParams()['REMOTE_PORT'] (if present) and attaches it to the request as remotePort (integer or null if missing)., (*4)

Usage (Slim 4+)

use Slim\Factory\AppFactory;
use Luisinder\Middleware\RemotePort;

require __DIR__ . '/vendor/autoload.php';

$app = AppFactory::create();
$app->add(RemotePort::class); // or new RemotePort()

$app->get('/', function ($request, $response) {
    $remotePort = $request->getAttribute('remotePort');
    $response->getBody()->write('Remote port: ' . ($remotePort ?? 'unknown'));
    return $response;
});

$app->run();

Container registration (optional)

If you prefer adding via DI container (e.g. using PHP-DI):, (*5)

$container->set(RemotePort::class, function() { return new RemotePort(); });
$app->add(RemotePort::class);

Usage (Slim 3 legacy)

$app->add(new Luisinder\Middleware\RemotePort());

$app->get('/', function ($request, $response) {
    $remotePort = $request->getAttribute('remotePort');
    return $response->write('Remote port: ' . ($remotePort ?? 'unknown'));
});

Attribute name

The attribute key is remotePort. Example:, (*6)

$remotePort = $request->getAttribute('remotePort'); // int|null

Error handling & edge cases

If REMOTE_PORT is missing the attribute value will be null., (*7)

Testing

You can simulate a request by constructing a PSR-7 ServerRequest with a custom server params array:, (*8)

$request = $request->withServerParams(['REMOTE_PORT' => 54321]);

Contributing

PRs and issues are welcome. Please include tests where possible., (*9)

License

BSD-3-Clause. See the LICENSE file for details., (*10)

The Versions

13/12 2016

dev-master

9999999-dev https://github.com/luisinder/remote-port-middleware

PSR-7 Middleware that determines the client remote port and stores it as an ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

middleware psr7 port

28/09 2016

1.0.1

1.0.1.0 https://github.com/luisinder/remote-port-middleware

PSR-7 Middleware that determines the client remote port and stores it as an ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

middleware psr7 port

28/09 2016

1.0

1.0.0.0 https://github.com/luisinder/remote-port-middleware

PSR-7 Middleware that determines the client remote port and stores it as an ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

middleware psr7 port