Skip to content

Commit a8901b2

Browse files
authoredOct 24, 2023
Improve header parsing and add tests (#1600)
1 parent 088b95f commit a8901b2

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed
 

‎src/Util/Http.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,25 @@ public static function getRequestHeaders(Dsn $dsn, string $sdkIdentifier, string
4040

4141
/**
4242
* @param string[][] $headers
43+
*
44+
* @param-out string[][] $headers
4345
*/
44-
public static function parseResponseHeaders(string $headerLine, &$headers): int
46+
public static function parseResponseHeaders(string $headerLine, array &$headers): int
4547
{
4648
if (false === strpos($headerLine, ':')) {
4749
return \strlen($headerLine);
4850
}
4951

50-
[$key, $value] = explode(':', trim($headerLine), 2);
51-
$headers[trim($key)] = trim($value);
52+
[$name, $value] = explode(':', trim($headerLine), 2);
53+
54+
$name = trim($name);
55+
$value = trim($value);
56+
57+
if (isset($headers[$name])) {
58+
$headers[$name][] = $value;
59+
} else {
60+
$headers[$name] = (array) $value;
61+
}
5262

5363
return \strlen($headerLine);
5464
}

‎tests/Util/HttpTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,42 @@ public static function getRequestHeadersDataProvider(): \Generator
4040
],
4141
];
4242
}
43+
44+
/**
45+
* @dataProvider parseResponseHeadersDataProvider
46+
*/
47+
public function testParseResponseHeaders(string $headerline, $expectedResult): void
48+
{
49+
$responseHeaders = [];
50+
51+
Http::parseResponseHeaders($headerline, $responseHeaders);
52+
53+
$this->assertSame($expectedResult, $responseHeaders);
54+
}
55+
56+
public static function parseResponseHeadersDataProvider(): \Generator
57+
{
58+
yield [
59+
'Content-Type: application/json',
60+
[
61+
'Content-Type' => [
62+
'application/json',
63+
],
64+
],
65+
];
66+
67+
yield [
68+
'X-Sentry-Rate-Limits: 60:transaction:key,2700:default;error;security:organization',
69+
[
70+
'X-Sentry-Rate-Limits' => [
71+
'60:transaction:key,2700:default;error;security:organization',
72+
],
73+
],
74+
];
75+
76+
yield [
77+
'Invalid',
78+
[],
79+
];
80+
}
4381
}

0 commit comments

Comments
 (0)
Please sign in to comment.