diff --git a/lib/PicoFeed/Clients/Stream.php b/lib/PicoFeed/Clients/Stream.php index 1a6b241..eaa610f 100644 --- a/lib/PicoFeed/Clients/Stream.php +++ b/lib/PicoFeed/Clients/Stream.php @@ -57,10 +57,31 @@ public function doRequest() 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; + } } \ No newline at end of file diff --git a/tests/StreamTest.php b/tests/StreamTest.php index eb9810a..5461784 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -7,6 +7,16 @@ class StreamTest extends PHPUnit_Framework_TestCase { + // public function testChunkedResponse() + // { + // $client = new Stream; + // $client->url = 'http://www.reddit.com/r/dwarffortress/.rss'; + // $result = $client->doRequest(); + + // var_dump($result); + // } + + public function testDownload() { $client = new Stream;