Skip to content

Commit bed4ac9

Browse files
committed
Amendment to previous merge (possibly fubared something)
1 parent 90c20bd commit bed4ac9

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

src/Igorw/CgiHttpKernel/CgiHttpKernel.php

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Symfony\Component\HttpFoundation\File\UploadedFile;
1111
use Symfony\Component\HttpKernel\HttpKernelInterface;
1212
use Symfony\Component\Process\ProcessBuilder;
13-
use \RuntimeException;
1413

1514
class CgiHttpKernel implements HttpKernelInterface
1615
{
@@ -72,11 +71,12 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
7271

7372
$processOutput = $process->getOutput();
7473

75-
list($headerList, $body) = explode("\r\n\r\n", $processOutput, 2) + array(1 => '');
76-
$headers = $this->getHeaderMap($headerList);
77-
unset($headers['Cookie']);
74+
list($headerList, $body) = explode("\r\n\r\n", $processOutput, 2);
75+
$headerMap = $this->getHeaderMap($headerList);
76+
$cookies = $this->getCookies($headerMap);
7877

79-
$cookies = $this->getCookies($headers);
78+
$headers = $this->flattenHeaderMap($headerMap);
79+
unset($headers['Cookie']);
8080
$status = $this->getStatusCode($headers);
8181

8282
$response = new Response($body, $status, $headers);
@@ -103,43 +103,48 @@ private function getStatusCode(array $headers)
103103

104104
private function getHeaderMap($headerListRaw)
105105
{
106-
$headerMap = array();
107-
if (!strlen($headerListRaw)) {
108-
return $headerMap;
106+
if (0 === strlen($headerListRaw)) {
107+
return array();
109108
}
109+
110+
$headerMap = array();
111+
110112
$headerList = preg_replace('~\xD\xA[\t ]~', ' ', $headerListRaw);
111113
$headerLines = explode("\r\n", $headerList);
112114
foreach ($headerLines as $headerLine) {
113-
list($name, $value) = explode(':', $headerLine, 2) + array(1 => NULL);
114-
if (NULL === $value) {
115-
throw new RuntimeException('Unable to parse header line, name missing');
116-
}
117-
$name = implode('-', array_map('ucwords', explode('-', $name)));
118-
$value = trim($value, "\t ");
119-
switch($name) {
120-
case 'Set-Cookie':
121-
$headerMap[$name][] = $value;
122-
break;
123-
124-
default:
125-
if (isset($headerMap[$name])) {
126-
$value = $headerMap[$name] . ',' . $value;
127-
}
128-
$headerMap[$name] = $value;
115+
if (false === strpos($headerLine, ':')) {
116+
throw new \RuntimeException('Unable to parse header line, name missing');
129117
}
118+
119+
list($name, $value) = explode(':', $headerLine, 2);
120+
121+
$name = implode('-', array_map('ucwords', explode('-', $name)));
122+
$value = trim($value, "\t ");
123+
124+
$headerMap[$name][] = $value;
130125
}
126+
131127
return $headerMap;
132128
}
133129

134-
private function getCookies(array $headerList)
130+
private function flattenHeaderMap(array $headerMap)
131+
{
132+
$flatHeaderMap = array();
133+
foreach ($headerMap as $name => $values) {
134+
$flatHeaderMap[$name] = implode(', ', $values);
135+
}
136+
return $flatHeaderMap;
137+
}
138+
139+
private function getCookies(array $headerMap)
135140
{
136-
if (!isset($headerList['Set-Cookie'])) {
141+
if (!isset($headerMap['Set-Cookie'])) {
137142
return array();
138143
}
139144

140145
return array_map(
141146
array($this, 'cookieFromResponseHeaderValue'),
142-
$headerList['Set-Cookie']
147+
$headerMap['Set-Cookie']
143148
);
144149
}
145150

tests/functional/CgiHttpKernelTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,14 @@ public function doubleCrlfResponseBodyShouldBeDecodedProperly()
260260
$expected = "foo\r\n\r\nbar";
261261
$this->assertSame($expected, $response->getContent());
262262
}
263+
264+
/** @test */
265+
public function emptyResponseShouldWorkFine()
266+
{
267+
$request = Request::create('/empty.php');
268+
$response = $this->kernel->handle($request);
269+
270+
$expected = '';
271+
$this->assertSame($expected, $response->getContent());
272+
}
263273
}

tests/functional/Fixtures/empty.php

Whitespace-only changes.

0 commit comments

Comments
 (0)