Add support for transfer-encoding chunked (Stream Client)

This commit is contained in:
Frédéric Guillot 2013-11-12 22:31:04 -05:00
parent 4fdd2913ae
commit d6984c426c

View File

@ -57,10 +57,31 @@ class Stream extends \PicoFeed\Client
fclose($stream);
if (isset($headers['Transfer-Encoding']) && $headers['Transfer-Encoding'] === 'chunked') {
$body = $this->decodeChunked($body);
}
return array(
'status' => $status,
'body' => $body,
'headers' => $headers
);
}
public function decodeChunked($str)
{
for ($result = ''; ! empty($str); $str = trim($str)) {
// Get the chunk length
$pos = strpos($str, "\r\n");
$len = hexdec(substr($str, 0, $pos));
// Append the chunk to the result
$result .= substr($str, $pos + 2, $len);
$str = substr($str, $pos + 2 + $len);
}
return $result;
}
}