Skip to content

Commit 90c20bd

Browse files
committed
Merge remote-tracking branch 'hakre/Patch-1'
* hakre/Patch-1: Parsing HTTP header lines LWS and friends.
2 parents 8fe8aa0 + cddf2e3 commit 90c20bd

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

src/Igorw/CgiHttpKernel/CgiHttpKernel.php

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

1415
class CgiHttpKernel implements HttpKernelInterface
1516
{
@@ -69,11 +70,13 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
6970
$process->start();
7071
$process->wait();
7172

72-
list($headerList, $body) = explode("\r\n\r\n", $process->getOutput(), 2);
73-
$headers = $this->getHeaderMap(explode("\r\n", $headerList));
73+
$processOutput = $process->getOutput();
74+
75+
list($headerList, $body) = explode("\r\n\r\n", $processOutput, 2) + array(1 => '');
76+
$headers = $this->getHeaderMap($headerList);
7477
unset($headers['Cookie']);
7578

76-
$cookies = $this->getCookies(explode("\r\n", $headerList));
79+
$cookies = $this->getCookies($headers);
7780
$status = $this->getStatusCode($headers);
7881

7982
$response = new Response($body, $status, $headers);
@@ -98,26 +101,46 @@ private function getStatusCode(array $headers)
98101
return 200;
99102
}
100103

101-
private function getHeaderMap(array $headerList)
104+
private function getHeaderMap($headerListRaw)
102105
{
103-
$headerMap = array();
104-
foreach ($headerList as $item) {
105-
list($name, $value) = explode(': ', $item);
106-
$headerMap[$name] = $value;
106+
$headerMap = array();
107+
if (!strlen($headerListRaw)) {
108+
return $headerMap;
109+
}
110+
$headerList = preg_replace('~\xD\xA[\t ]~', ' ', $headerListRaw);
111+
$headerLines = explode("\r\n", $headerList);
112+
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;
129+
}
107130
}
108131
return $headerMap;
109132
}
110133

111134
private function getCookies(array $headerList)
112135
{
113-
$cookies = array();
114-
foreach ($headerList as $item) {
115-
list($name, $value) = explode(': ', $item);
116-
if ('set-cookie' === strtolower($name)) {
117-
$cookies[] = $this->cookieFromResponseHeaderValue($value);
118-
}
136+
if (!isset($headerList['Set-Cookie'])) {
137+
return array();
119138
}
120-
return $cookies;
139+
140+
return array_map(
141+
array($this, 'cookieFromResponseHeaderValue'),
142+
$headerList['Set-Cookie']
143+
);
121144
}
122145

123146
private function cookieFromResponseHeaderValue($value)

0 commit comments

Comments
 (0)