Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 5c10156

Browse files
committed
Merge branch 'hotfix/370-marshal-headers-from-sapi' into develop
Forward port #370 Conflicts: CHANGELOG.md
2 parents 7a1f535 + a16378c commit 5c10156

File tree

3 files changed

+80
-7
lines changed

3 files changed

+80
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ All notable changes to this project will be documented in this file, in reverse
2424

2525
- Nothing.
2626

27-
## 2.1.4 - TBD
27+
## 2.1.4 - 2019-10-08
2828

2929
### Added
3030

@@ -44,7 +44,9 @@ All notable changes to this project will be documented in this file, in reverse
4444

4545
### Fixed
4646

47-
- Nothing.
47+
- [#370](https://github.com/zendframework/zend-diactoros/pull/370) updates `Zend\Diactoros\marshalHeadersFromSapi()` to ensure all underscores in header name keys are converted to dashes (fixing issues with header names such as `CONTENT_SECURITY_POLICY`, which would previously resolve improperly to `content-security_policy`).
48+
49+
- [#370](https://github.com/zendframework/zend-diactoros/pull/370) updates `Zend\Diactoros\marshalHeadersFromSapi()` to ignore header names from the `$server` array that resolve to integers; previously, it would raise a fatal error.
4850

4951
## 2.1.3 - 2019-07-10
5052

src/functions/marshal_headers_from_sapi.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Zend\Diactoros;
1111

1212
use function array_key_exists;
13+
use function is_string;
1314
use function strpos;
1415
use function strtolower;
1516
use function strtr;
@@ -23,6 +24,14 @@ function marshalHeadersFromSapi(array $server) : array
2324
{
2425
$headers = [];
2526
foreach ($server as $key => $value) {
27+
if (! is_string($key)) {
28+
continue;
29+
}
30+
31+
if ($value === '') {
32+
continue;
33+
}
34+
2635
// Apache prefixes environment variables with REDIRECT_
2736
// if they are added by rewrite rules
2837
if (strpos($key, 'REDIRECT_') === 0) {
@@ -35,18 +44,14 @@ function marshalHeadersFromSapi(array $server) : array
3544
}
3645
}
3746

38-
if ($value === '') {
39-
continue;
40-
}
41-
4247
if (strpos($key, 'HTTP_') === 0) {
4348
$name = strtr(strtolower(substr($key, 5)), '_', '-');
4449
$headers[$name] = $value;
4550
continue;
4651
}
4752

4853
if (strpos($key, 'CONTENT_') === 0) {
49-
$name = 'content-' . strtolower(substr($key, 8));
54+
$name = strtr(strtolower($key), '_', '-');
5055
$headers[$name] = $value;
5156
continue;
5257
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4+
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace ZendTest\Diactoros\functions;
11+
12+
use PHPUnit\Framework\TestCase;
13+
14+
use function Zend\Diactoros\marshalHeadersFromSapi;
15+
16+
class MarshalHeadersFromSapiTest extends TestCase
17+
{
18+
public function testReturnsHeaders() : void
19+
{
20+
$server = [
21+
'REDIRECT_CONTENT_FOO' => 'redirect-foo',
22+
'CONTENT_FOO' => null,
23+
'REDIRECT_CONTENT_BAR' => 'redirect-bar',
24+
'CONTENT_BAR' => '',
25+
'REDIRECT_CONTENT_BAZ' => 'redirect-baz',
26+
'CONTENT_BAZ' => 'baz',
27+
'REDIRECT_CONTENT_VAR' => 'redirect-var',
28+
29+
'REDIRECT_HTTP_ABC' => 'redirect-abc',
30+
'HTTP_ABC' => null,
31+
'REDIRECT_HTTP_DEF' => 'redirect-def',
32+
'HTTP_DEF' => '',
33+
'REDIRECT_HTTP_GHI' => 'redirect-ghi',
34+
'HTTP_GHI' => 'ghi',
35+
'REDIRECT_HTTP_JKL' => 'redirect-jkl',
36+
37+
'HTTP_TEST_MNO' => 'mno',
38+
'HTTP_TEST_PQR' => '',
39+
'HTTP_TEST_STU' => null,
40+
'CONTENT_TEST_VW' => 'vw',
41+
'CONTENT_TEST_XY' => '',
42+
'CONTENT_TEST_ZZ' => null,
43+
44+
123 => 'integer',
45+
];
46+
47+
$expectedHeaders = [
48+
'content-foo' => null,
49+
'content-baz' => 'baz',
50+
'content-var' => 'redirect-var',
51+
52+
'abc' => null,
53+
'ghi' => 'ghi',
54+
'jkl' => 'redirect-jkl',
55+
56+
'test-mno' => 'mno',
57+
'test-stu' => null,
58+
'content-test-vw' => 'vw',
59+
'content-test-zz' => null,
60+
];
61+
62+
$headers = marshalHeadersFromSapi($server);
63+
64+
self::assertSame($expectedHeaders, $headers);
65+
}
66+
}

0 commit comments

Comments
 (0)