FIX Director::hostName() returning null for bare hostnames#11979
Open
erikfrerejean wants to merge 1 commit into
Open
FIX Director::hostName() returning null for bare hostnames#11979erikfrerejean wants to merge 1 commit into
erikfrerejean wants to merge 1 commit into
Conversation
When host() returns a hostname without a port and without a protocol (e.g. the RFC 7230 compliant Host header value "example.com"), parse_url() interprets it as a path and returns null for PHP_URL_HOST. Delegate to protocolAndHost() so parse_url() always receives a scheme-prefixed string, and explicitly narrow the result so the documented string|null return type holds. Fixes silverstripe#11081
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Director::hostName()returnsnullwhen the resolved host is a bare hostname (no port, no protocol) — for example, the RFC 7230-compliantHostheader valueexample.com. PHP'sparse_url()interprets such a string as a path and returnsnullforPHP_URL_HOST, so the wrapping?: nullpropagates that null upwards and consumers receivenullinstead of the expected hostname.The fix delegates to
protocolAndHost()soparse_url()always receives a scheme-prefixed URL, which it can parse reliably regardless of whether the host carries a port or not.The bug was reported (#11081) and reproduces verbatim against the current
6.2branch.Manual testing steps
Director::host()resolves via$_SERVER['HTTP_HOST'](i.e. unsetalternate_base_url/ no trusted-proxy override).Hostheader is a bare hostname without a port, e.g.Host: example.com.Director::hostName()returnsnull. After this PR: it returns"example.com".The included unit test (
DirectorTest::testHostNameWithoutPort) reproduces this without needing a live server.Issues
$_SERVER['HTTP_HOST']#11081Pull request checklist