jenssegers / php-proxy

A PHP proxy script with https and post support
https://jenssegers.com
933 stars 266 forks source link

Proxy does not respect port 80 or 443 on target #41

Closed flip111 closed 5 years ago

flip111 commented 8 years ago

This is needed for when you have a server not running on port 80 or 443 (such as the php built-in webserver php -S localhost:8080). And you want to forward the request to a server with port 80 or 443.

tostercx commented 6 years ago

+1 just hit the same bug

tostercx commented 6 years ago

The problem is $target->getPort() won't return ports 80/443 even if you specify it in the string, as it's supposed to:

https://github.com/php-fig/http-message/blob/a6764a43faa657cef3e367a66785f2fc980bdff5/src/UriInterface.php#L95

It only returns non-standard ports. The problem is the new URI is made from $this->request->getUri(), which includes the current (possibly non-standard) port, but then it only replaces it if $target->getPort() is not null:

https://github.com/jenssegers/php-proxy/blob/2f723629194a99c5609c1ecea540c1f465e143f8/src/Proxy.php#L72

However withPort does support null values and sets the port to the default 80/443!

https://github.com/php-fig/http-message/blob/a6764a43faa657cef3e367a66785f2fc980bdff5/src/UriInterface.php#L232

So a much simpler solution is to remove the if and always do $uri->withPort($target->getPort())

jenssegers commented 5 years ago

Applied @tostercx's suggestion on master 👌