FileClient: Merge HTTP headers from cURL options#912
FileClient: Merge HTTP headers from cURL options#912jtojnar merged 1 commit intosimplepie:masterfrom
Conversation
The `$curl_options` parameter could override the `CURLOPT_HTTPHEADER` option, so make sure to merge the HTTP headers properly
fix FreshRSS#6712 (comment) We would sometimes wrongly override the default HTTP headers of SimplePie FreshRSS/simplepie#33 simplepie/simplepie#912
* Fix regression cURL HTTP headers fix #6712 (comment) We would sometimes wrongly override the default HTTP headers of SimplePie FreshRSS/simplepie#33 simplepie/simplepie#912 * Sync SimplePie FreshRSS/simplepie#33
jtojnar
left a comment
There was a problem hiding this comment.
Thanks.
This makes sense to me: AFAICT, set_curl_options is the only way besides subclassing to pass extra headers with requests, if a bit indiscriminate, so we should support it. And we probably do not want to discard headers set by SimplePie, as it should, in theory, know what it is doing.
Generally, it might be desirable to provide a mechanism for overriding the headers set by SimplePie (the way that can be used for removing headers set by cURL – setting a header to an empty value – does not work, duplicate headers from $headers and $curl_options will be sent), but since the only SimplePie only ever sets one of four headers (Accept, if-modified-since, if-none-match, X-FORWARDED-FOR), implementing it would probably not be worth it. Especially when the FileClient is meant to away eventually.
Comparison of the behavior
Test script
<?php
require __DIR__ . '/vendor/autoload.php';
$file = new SimplePie\File(
'https://httpbin.org/headers',
headers: [
'X-Both-Args' => 'Headers',
'X-Headers-Arg' => '1',
'X-Headers-Arg-Hopefully-Shadowed' => 'Headers',
],
curl_options: [
\CURLOPT_HTTPHEADER => [
'X-Both-Args: Options',
'X-Curl-Options-Arg: 1',
'X-Headers-Arg-Hopefully-Shadowed:',
'X-Headers-Arg-Hopefully-Shadowed: Options',
]
]
);
echo $file->get_body_content();master
{
"headers": {
"Accept": "*/*",
"Accept-Encoding": "deflate, gzip, br, zstd",
"Host": "httpbin.org",
"Referer": "https://httpbin.org/headers",
"X-Amzn-Trace-Id": "Root=1-67d54198-20e1c4c154bbe72c0db9357d",
"X-Both-Args": "Options",
"X-Curl-Options-Arg": "1",
"X-Headers-Arg-Hopefully-Shadowed": "Options"
}
}This PR
{
"headers": {
"Accept": "*/*",
"Accept-Encoding": "deflate, gzip, br, zstd",
"Host": "httpbin.org",
"Referer": "https://httpbin.org/headers",
"X-Amzn-Trace-Id": "Root=1-67d54136-2cf0ca7c20c8e2cd2ae221cc",
"X-Both-Args": "Headers,Options",
"X-Curl-Options-Arg": "1",
"X-Headers-Arg": "1",
"X-Headers-Arg-Hopefully-Shadowed": "Headers,Options"
}
}HTTP headers were not merged properly, and were also not accounting for possible casing differences. Follow-up of simplepie#912 It was for instance not possible to override the `Accept` header from the cURL options. The existing code could not work properly because we were merging arrays of strings and not arrays of key-values, leading to strange duplicates. Downstream: FreshRSS/FreshRSS#8246
| } | ||
| if (isset($curl_options[CURLOPT_HTTPHEADER])) { | ||
| if (is_array($curl_options[CURLOPT_HTTPHEADER])) { | ||
| $headers2 = array_merge($headers2, $curl_options[CURLOPT_HTTPHEADER]); |
HTTP headers were not merged properly, and were also not accounting for possible casing differences. Follow-up of simplepie#912 It was for instance not possible to override the `Accept` header from the cURL options. The existing code could not work properly because we were merging arrays of strings and not arrays of key-values, leading to strange duplicates. Downstream: FreshRSS/FreshRSS#8246 Upstream: simplepie#956
HTTP headers were not merged properly, and were also not accounting for possible casing differences. Follow-up of simplepie#912 It was for instance not possible to override the `Accept` header from the cURL options. The existing code could not work properly because we were merging arrays of strings and not arrays of key-values, leading to strange duplicates. Downstream: * FreshRSS/FreshRSS#8246 Upstream: * simplepie#956
The
$curl_optionsparameter could override theCURLOPT_HTTPHEADERoption, so make sure to merge the HTTP headers properly.Before this patch, providing an additional HTTP header via
$curl_options[CURLOPT_HTTPHEADER]would remove all essential headers such as Accept, if-modified-since, etc.