From 0596e8179affb4457aef29316a40d67f4957473a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dirk=20R=C3=BCdiger?= Date: Sun, 16 May 2021 22:18:38 +0200 Subject: [PATCH] Uncompress the tile if the upstream source delivered gzipped http responses See https://stackoverflow.com/a/50297173/981739 --- index.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/index.php b/index.php index d07a65a..bde2c80 100644 --- a/index.php +++ b/index.php @@ -115,9 +115,30 @@ function getTile ($layers, $layer, $location, $apiKeyParameters) error_log ("Remote tile failed {$url}"); return false; } + + if (isGzipHeaderSet(transformIntoHeaderMap($http_response_header))) { + $binary = gzdecode($binary); + } return $binary; } +function transformIntoHeaderMap(array $headers) +{ + $headersWithValues = array_filter($headers, function ($header) { return strpos($header, ':') !== false; }); + $headerMap = []; + foreach ($headersWithValues as $header) { + list($key, $value) = explode(':', $header); + $headerMap[$key] = trim($value); + } + return $headerMap; +} + +function isGzipHeaderSet(array $headerMap) +{ + return isset($headerMap['Content-Encoding']) && $headerMap['Content-Encoding'] == 'gzip'; +} + + # Define a function for multiple tries of getting a tile function getTileWithRetries ($layers, &$layer, $location, $apiKeyParameters, $fallback) {