From: Tim Düsterhus Date: Thu, 15 Oct 2020 08:56:44 +0000 (+0200) Subject: Fix handling of default headers in HttpFactory::makeClient() X-Git-Tag: 5.3.0_RC_2~7 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=b2d3455d291305bab4b913492c2c33b0245d7b67;p=GitHub%2FWoltLab%2FWCF.git Fix handling of default headers in HttpFactory::makeClient() If a `headers` array is given the default user agent would not be applied, even if the `user-agent` key is not part of the `headers` that are given. This caused Guzzle to use its default user agent. --- diff --git a/wcfsetup/install/files/lib/system/io/HttpFactory.class.php b/wcfsetup/install/files/lib/system/io/HttpFactory.class.php index 4657461a9e..7ee07e2fe4 100644 --- a/wcfsetup/install/files/lib/system/io/HttpFactory.class.php +++ b/wcfsetup/install/files/lib/system/io/HttpFactory.class.php @@ -54,11 +54,28 @@ final class HttpFactory { * @see Client */ public static function makeClient(array $options = []) { - return new Client(array_merge([ + $defaults = [ 'proxy' => PROXY_SERVER_HTTP, - 'headers' => [ - 'user-agent' => self::getDefaultUserAgent(), - ], - ], $options)); + 'headers' => [], + ]; + + foreach ($defaults as $key => $value) { + if (!array_key_exists($key, $options)) { + $options[$key] = $value; + } + } + + $foundUserAgent = false; + foreach ($options['headers'] as $headerName => $value) { + if (strtolower($headerName) === 'user-agent') { + $foundUserAgent = true; + break; + } + } + if (!$foundUserAgent) { + $options['headers']['user-agent'] = self::getDefaultUserAgent(); + } + + return new Client($options); } }