Skip to content

Commit

Permalink
Add support for transfer-encoding chunked (Stream Client)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric Guillot committed Nov 13, 2013
1 parent 4b77b0f commit 25875ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/PicoFeed/Clients/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 10 additions & 0 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 25875ed

Please sign in to comment.