Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
drupal_http_request() overrides the Host header from the parsed URL host: $options['headers']['Host'] = $uri['host']
However, if the Host header is explicitly set by the caller, drupal_http_request() should honor it.
Comment | File | Size | Author |
---|---|---|---|
#2 | drupal-http-request-allow-host-header-2555649-2.patch | 1.83 KB | mvc |
Comments
Comment #2
mvcTrivial patch attached.
Note that with this patch, when setting the Host header explicitly, the caller will be responsible for also appending non-default port numbers as per the HTTP/1.1 spec.
Comment #3
Anonymous (not verified) CreditAttribution: Anonymous commentedRTBC. As mentioned, the patch is trivial, won't break anything (it's just some if checks), & it does what it says.
Comment #4
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedComment #5
Dinesh18 CreditAttribution: Dinesh18 as a volunteer commentedVerified the patch manually and looks good to me.
+1 to RTBC
Comment #6
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedIt seems like it would be of limited usefulness given that you already specify the URL... but if people want it, hey, why not allow it :)
Committed to 7.x - thanks!
Comment #8
mvcThanks! Sometimes the Host header and the domain name in the URI are in fact different (such as testing a new vhost). You could do this in D8 with Guzzle like this: https://github.com/guzzle/guzzle/issues/1678
Comment #10
emilymoi CreditAttribution: emilymoi commentedThis change broke my site after I upgraded to Drupal 7.61.
The issue is that if a redirect occurs to a different host the 'Host' header field will still contain the original host from the first request ($options is passed back into drupal_http_request when following the redirect). So with this change it sees that the 'Host' header field is populated (with the old host information from the first request), and it fails to set the 'Host' header field to the new host we redirected to. This results in an error. If I revert the change from this patch everything works fine.
I can suggest some solutions but wanted to point out this was a problem and needs to be fixed. I have reverted this patch on my site to restore proper functionality.
Comment #11
emilymoi CreditAttribution: emilymoi commentedI have created a new issue #3018637: [regression] Unset the 'host' header in drupal_http_request() during redirect. and provided a patch for review.
Comment #12
Rhane CreditAttribution: Rhane commentedThis needs reopened or at least the bug that it introduced fixed. I can confirm the behavior emily mentioned as well affecting an integration.
Comment #13
Rhane CreditAttribution: Rhane commented