Skip to content

Commit

Permalink
feat(Common): sanitize headers before adding them to span attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
neiluJ committed Jan 30, 2024
1 parent 50c380a commit b87e5b4
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Http/TracedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,37 @@ public function getStatusCode(): int

public function getHeaders(bool $throw = true): array
{
return $this->response->getHeaders($throw);
$headers = [];
$raw = $this->response->getHeaders($throw);
foreach ($raw as $header => $value) {
$headers[$header] = $this->toReadableHeaderValue($value);
}

/* @phpstan-ignore-next-line */
return $headers;
}

private function toReadableHeaderValue(mixed $value): string
{
if (null === $value) {
return 'null';
} elseif (\is_array($value)) {
return implode(', ', array_map([$this, __FUNCTION__], $value));
} elseif (\is_scalar($value)) {
if (\is_bool($value)) {
return true === $value ? 'true' : 'false';
}

return (string) $value;
} elseif (\is_object($value)) {
if (method_exists($value, '__toString')) {
return (string) $value;
}

return '(object)#'.$value::class;
}

return \gettype($value);
}

public function getContent(bool $throw = true): string
Expand Down

0 comments on commit b87e5b4

Please sign in to comment.