symfony/symfony — pull request #8882 by dhotson (2013-08-29 16:15:18+00:00)
Title: Request->getPort() should prefer HTTP_HOST over SERVER_PORT

| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes (although there is an existing test failure on latest master)
| Fixed tickets | https://github.com/symfony/symfony/issues/3420
| License | MIT
| Doc PR | none

What is this?
----

_Note to reviewer: This follows on from a previous pull request: https://github.com/symfony/symfony/pull/8184
Since the introduction of `Request->setTrustedHosts()` — the implementation of this fix has been simplified._

This fixes the semantics of the `Request#getPort()` method to prefer the HTTP_HOST header over SERVER_PORT.

This is relevant in situations where the web server is running on a different port to the public facing website. e.g. load balancers, proxies, port forwarding.

Consistency
----

This change makes Symfony more consistent with other popular web frameworks.

Preferring HTTP_HOST over SERVER_NAME and SERVER_PORT is the strategy used by Ruby's Rack and Python's Django.

Python has a PEP that describes how to reconstruct URLs:
http://www.python.org/dev/peps/pep-0333/#url-reconstruction

https://github.com/rack/rack/blob/b6290a184cead2ffd731647d0836c5ebd3c21e7a/lib/rack/request.rb#L92