Skip to content

Commit

Permalink
Merge pull request #29 from City-of-Helsinki/UHF-4227
Browse files Browse the repository at this point in the history
UHF-4227: Skip proxy data processing when response content-type is ap…
  • Loading branch information
tuutti authored Feb 11, 2022
2 parents 5b8ffa9 + 929b529 commit 67baa1a
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/HttpMiddleware/AssetHttpMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ private function processJson(string $content, Request $request) : ? string {

$hasChanges = FALSE;

if (!$content) {
if (!$content || !is_array($content)) {
return NULL;
}

foreach ($content as $key => $value) {
if (!isset($value['data'])) {
if (!isset($value['data']) || !is_string($value['data'])) {
continue;
}
$hasChanges = TRUE;
Expand All @@ -121,6 +121,23 @@ private function processJson(string $content, Request $request) : ? string {
return $hasChanges ? json_encode($content) : NULL;
}

/**
* Checks if the given response is json.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* The response.
*
* @return bool
* TRUE if response is JSON.
*/
private function isJsonResponse(Response $response) : bool {
if ($response instanceof JsonResponse) {
return TRUE;
}

return $response->headers->get('content-type') === 'application/json';
}

/**
* {@inheritdoc}
*/
Expand All @@ -141,7 +158,7 @@ public function handle(
return $response;
}

if ($response instanceof JsonResponse) {
if ($this->isJsonResponse($response)) {
if ($json = $this->processJson($content, $request)) {
return $response->setContent($json);
}
Expand Down

0 comments on commit 67baa1a

Please sign in to comment.