Nyholm / psr7-server

Helper classes to use any PSR7 implementation as your main request and response
MIT License
90 stars 21 forks source link

Duplicated port in URI #28

Closed ondrejbouda closed 5 years ago

ondrejbouda commented 5 years ago

Hi!

I am getting wrong URI when running on localhost with custom port:

<?php declare(strict_types = 1);

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

use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;

$psr17Factory = new Psr17Factory();

$creator = new ServerRequestCreator(
    $psr17Factory,
    $psr17Factory,
    $psr17Factory,
    $psr17Factory
);

$request = $creator->fromGlobals();

echo $request->getUri()->__toString();

GET http://localhost:8000/

http://localhost:8000:8000/

The problem is probably due to mechanism used to create URI in ServerRequestCreator::createUriFromArray(). URI host gets populated with $_SERVER['HTTP_HOST'], but this contains also the port (see for example here: https://stackoverflow.com/a/12046836 ).

I propose two solutions:

  1. strip the port from $_SERVER['HTTP_HOST']
  2. use $_SERVER['SERVER_NAME'] instead - I am not sure about the consequences

I may create a pull request with fix if you want.

Zegnat commented 5 years ago

Could you create a test for this to show it failing? From a quick reread of the code, we should already be handling ports as part of $_SERVER['HTTP_HOST']. Specifically, if HTTP_HOST ends in a port, we split it away from the hostname and set that port as the actual port to use: https://github.com/Nyholm/psr7-server/blob/4d037683112c0915b87739f052b651e4d6edeb8b/src/ServerRequestCreator.php#L256-L257

ondrejbouda commented 5 years ago

Great, it seems to be resolved by pull request #26 . I was using version 0.3.0 without this fix. I will use master for now.

stroebjo commented 5 years ago

Can you release a new Version with this fix? Had this issue in a project using your library.