Skip to content

Commit

Permalink
fix: sketchy content-type in proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
  • Loading branch information
julien-nc committed Sep 24, 2024
1 parent e05baf1 commit 955efa1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions lib/Controller/MapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@ public function getMapTilerTiles(string $version, ?string $key = null): JSONResp
}
}

/**
* @param array $headers
* @param string $defaultType
* @return string
*/
private function getContentTypeFromHeaders(array $headers, string $defaultType): string {
if (isset($headers['Content-Type'])) {
if (is_string($headers['Content-Type'])) {
return $headers['Content-Type'];
} elseif (is_array($headers['Content-Type'])
&& count($headers['Content-Type']) > 0
&& is_string($headers['Content-Type'][0])
) {
return $headers['Content-Type'][0];
}
}
return $defaultType;
}

/**
* @param string $version
* @param int $z
Expand All @@ -132,7 +151,7 @@ public function getMapTilerTile(string $version, int $z, int $x, int $y, string
$response = new DataDisplayResponse(
$tileResponse['body'],
Http::STATUS_OK,
['Content-Type' => $tileResponse['headers']['Content-Type'] ?? 'image/jpeg']
['Content-Type' => $this->getContentTypeFromHeaders($tileResponse['headers'], 'image/jpeg')],
);
$response->cacheFor(60 * 60 * 24);
return $response;
Expand All @@ -159,7 +178,7 @@ public function getMapTilerSprite(string $version, string $ext): Response {
$response = new DataDisplayResponse(
$sprite['body'],
Http::STATUS_OK,
['Content-Type' => $sprite['headers']['Content-Type'] ?? 'image/png']
['Content-Type' => $this->getContentTypeFromHeaders($sprite['headers'], 'image/png')],
);
}
$response->cacheFor(60 * 60 * 24);
Expand All @@ -182,7 +201,7 @@ public function getMapTilerResource(string $name): Response {
$response = new DataDisplayResponse(
$resourceResponse['body'],
Http::STATUS_OK,
['Content-Type' => $resourceResponse['headers']['Content-Type'] ?? 'image/png']
['Content-Type' => $this->getContentTypeFromHeaders($resourceResponse['headers'], 'image/png')],
);
$response->cacheFor(60 * 60 * 24);
return $response;
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/MapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public function searchLocation(string $userId, string $query, string $format = '
* Make an HTTP request to the Osm API
*
* @param string|null $userId
* @param string $endPoint The path to reach in api.github.com
* @param string $endPoint The path to reach in https://nominatim.openstreetmap.org
* @param array $params Query parameters (key/val pairs)
* @param string $method HTTP query method
* @param bool $rawResponse
Expand Down

0 comments on commit 955efa1

Please # to comment.