Fix handling of default headers in HttpFactory::makeClient()
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 15 Oct 2020 08:56:44 +0000 (10:56 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 15 Oct 2020 08:56:44 +0000 (10:56 +0200)
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.

wcfsetup/install/files/lib/system/io/HttpFactory.class.php

index 4657461a9edf392d7148d4d53834aa9e3137a8b1..7ee07e2fe4cf2f8a7b822c197dc3ce6556c202c0 100644 (file)
@@ -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);
        }
 }