-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19dd5ac
commit 7662852
Showing
5 changed files
with
412 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
<?php | ||
|
||
namespace React\HttpClient; | ||
|
||
use Evenement\EventEmitterTrait; | ||
use Exception; | ||
use React\Stream\ReadableStreamInterface; | ||
use React\Stream\Util; | ||
use React\Stream\WritableStreamInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class ChunkedStreamDecoder implements ReadableStreamInterface | ||
{ | ||
const CRLF = "\r\n"; | ||
|
||
use EventEmitterTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $buffer = ''; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $remainingLength = 0; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $nextChunkIsLength = true; | ||
|
||
/** | ||
* @var ReadableStreamInterface | ||
*/ | ||
protected $stream; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $closed = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $reachedEnd = false; | ||
|
||
/** | ||
* @param ReadableStreamInterface $stream | ||
*/ | ||
public function __construct(ReadableStreamInterface $stream) | ||
{ | ||
$this->stream = $stream; | ||
$this->stream->on('data', array($this, 'handleData')); | ||
$this->stream->on('end', array($this, 'handleEnd')); | ||
Util::forwardEvents($this->stream, $this, [ | ||
'error', | ||
]); | ||
} | ||
|
||
/** @internal */ | ||
public function handleData($data) | ||
{ | ||
$this->buffer .= $data; | ||
|
||
do { | ||
$bufferLength = strlen($this->buffer); | ||
$continue = $this->iterateBuffer(); | ||
$iteratedBufferLength = strlen($this->buffer); | ||
} while ( | ||
$continue && | ||
$bufferLength !== $iteratedBufferLength && | ||
$iteratedBufferLength > 0 | ||
); | ||
|
||
if ($this->buffer === false) { | ||
$this->buffer = ''; | ||
} | ||
} | ||
|
||
protected function iterateBuffer() | ||
{ | ||
if (strlen($this->buffer) <= 1) { | ||
return false; | ||
} | ||
|
||
if ($this->nextChunkIsLength) { | ||
$crlfPosition = strpos($this->buffer, static::CRLF); | ||
if ($crlfPosition === false && strlen($this->buffer) > 1024) { | ||
$this->emit('error', [ | ||
new Exception('Chunk length header longer then 1024 bytes'), | ||
]); | ||
$this->close(); | ||
return false; | ||
} | ||
if ($crlfPosition === false) { | ||
return false; // Chunk header hasn't completely come in yet | ||
} | ||
$this->nextChunkIsLength = false; | ||
$lengthChunk = substr($this->buffer, 0, $crlfPosition); | ||
if (strpos($lengthChunk, ';') !== false) { | ||
list($lengthChunk) = explode(';', $lengthChunk, 2); | ||
} | ||
if (dechex(hexdec($lengthChunk)) !== $lengthChunk) { | ||
$this->emit('error', [ | ||
new Exception('Unable to validate "' . $lengthChunk . '" as chunk length header'), | ||
]); | ||
$this->close(); | ||
return false; | ||
} | ||
$this->remainingLength = hexdec($lengthChunk); | ||
$this->buffer = substr($this->buffer, $crlfPosition + 2); | ||
return true; | ||
} | ||
|
||
if ($this->remainingLength > 0) { | ||
$chunkLength = $this->getChunkLength(); | ||
if ($chunkLength === 0) { | ||
return true; | ||
} | ||
$this->emit('data', array( | ||
substr($this->buffer, 0, $chunkLength), | ||
$this | ||
)); | ||
$this->remainingLength -= $chunkLength; | ||
$this->buffer = substr($this->buffer, $chunkLength); | ||
return true; | ||
} | ||
|
||
$this->nextChunkIsLength = true; | ||
$this->buffer = substr($this->buffer, 2); | ||
|
||
if (substr($this->buffer, 0, 3) === "0\r\n") { | ||
$this->reachedEnd = true; | ||
$this->emit('end'); | ||
$this->close(); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
protected function getChunkLength() | ||
{ | ||
$bufferLength = strlen($this->buffer); | ||
|
||
if ($bufferLength >= $this->remainingLength) { | ||
return $this->remainingLength; | ||
} | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
return $bufferLength; | ||
} | ||
|
||
public function pause() | ||
{ | ||
$this->stream->pause(); | ||
} | ||
|
||
public function resume() | ||
{ | ||
$this->stream->resume(); | ||
} | ||
|
||
public function isReadable() | ||
{ | ||
return $this->stream->isReadable(); | ||
} | ||
|
||
public function pipe(WritableStreamInterface $dest, array $options = array()) | ||
{ | ||
Util::pipe($this, $dest, $options); | ||
|
||
return $dest; | ||
} | ||
|
||
public function close() | ||
{ | ||
$this->closed = true; | ||
return $this->stream->close(); | ||
} | ||
|
||
/** @internal */ | ||
public function handleEnd() | ||
{ | ||
if ($this->closed) { | ||
return; | ||
} | ||
|
||
if ($this->buffer === '' && $this->reachedEnd) { | ||
$this->emit('end'); | ||
$this->close(); | ||
return; | ||
} | ||
|
||
$this->emit( | ||
'error', | ||
[ | ||
new Exception('Stream ended with incomplete control code') | ||
] | ||
); | ||
$this->close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
namespace React\Tests\HttpClient; | ||
|
||
use Exception; | ||
use React\HttpClient\ChunkedStreamDecoder; | ||
use React\Stream\ThroughStream; | ||
|
||
class DecodeChunkedStreamTest extends TestCase | ||
{ | ||
public function provideChunkedEncoding() | ||
{ | ||
return [ | ||
'data-set-1' => [ | ||
["4\r\nWiki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"], | ||
], | ||
'data-set-2' => [ | ||
["4\r\nWiki\r\n", "5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"], | ||
], | ||
'data-set-3' => [ | ||
["4\r\nWiki\r\n", "5\r\n", "pedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"], | ||
], | ||
'data-set-4' => [ | ||
["4\r\nWiki\r\n", "5\r\n", "pedia\r\ne\r\n in\r\n", "\r\nchunks.\r\n0\r\n\r\n"], | ||
], | ||
'data-set-5' => [ | ||
["4\r\n", "Wiki\r\n", "5\r\n", "pedia\r\ne\r\n in\r\n", "\r\nchunks.\r\n0\r\n\r\n"], | ||
], | ||
'data-set-6' => [ | ||
["4\r\n", "Wiki\r\n", "5\r\n", "pedia\r\ne; foo=[bar,beer,pool,cue,win,won]\r\n", " in\r\n", "\r\nchunks.\r\n0\r\n\r\n"], | ||
], | ||
'header-fields' => [ | ||
["4; foo=bar\r\n", "Wiki\r\n", "5\r\n", "pedia\r\ne\r\n", " in\r\n", "\r\nchunks.\r\n", "0\r\n\r\n"], | ||
], | ||
'character-for-charactrr' => [ | ||
str_split("4\r\nWiki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"), | ||
], | ||
'extra-newline-in-wiki-character-for-chatacter' => [ | ||
str_split("6\r\nWi\r\nki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"), | ||
"Wi\r\nkipedia in\r\n\r\nchunks." | ||
], | ||
'extra-newline-in-wiki' => [ | ||
["6\r\nWi\r\n", "ki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"], | ||
"Wi\r\nkipedia in\r\n\r\nchunks." | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider provideChunkedEncoding | ||
*/ | ||
public function testChunkedEncoding(array $strings, $expected = "Wikipedia in\r\n\r\nchunks.") | ||
{ | ||
$stream = new ThroughStream(); | ||
$response = new ChunkedStreamDecoder($stream); | ||
$buffer = ''; | ||
$response->on('data', function ($data) use (&$buffer) { | ||
$buffer .= $data; | ||
}); | ||
$response->on('error', function (Exception $exception) { | ||
throw $exception; | ||
}); | ||
foreach ($strings as $string) { | ||
$stream->write($string); | ||
} | ||
$this->assertSame($expected, $buffer); | ||
} | ||
|
||
public function provideInvalidChunkedEncoding() | ||
{ | ||
return [ | ||
'chunk-body-longer-than-header-suggests' => [ | ||
["4\r\nWiwot40n98w3498tw3049nyn039409t34\r\n", "ki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n"], | ||
], | ||
'invalid-header-charactrrs' => [ | ||
str_split("xyz\r\nWi\r\nki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n") | ||
], | ||
'header-chunk-to-long' => [ | ||
str_split(str_repeat('a', 2015) . "\r\nWi\r\nki\r\n5\r\npedia\r\ne\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n") | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider provideInvalidChunkedEncoding | ||
* @expectedException Exception | ||
*/ | ||
public function testInvalidChunkedEncoding(array $strings) | ||
{ | ||
$stream = new ThroughStream(); | ||
$response = new ChunkedStreamDecoder($stream); | ||
$response->on('error', function (Exception $exception) { | ||
throw $exception; | ||
}); | ||
foreach ($strings as $string) { | ||
$stream->write($string); | ||
} | ||
} | ||
|
||
public function testHandleEnd() | ||
{ | ||
$ended = false; | ||
$stream = new ThroughStream(); | ||
$response = new ChunkedStreamDecoder($stream); | ||
$response->on('end', function () use (&$ended) { | ||
$ended = true; | ||
}); | ||
|
||
$stream->write("4\r\nWiki\r\n0\r\n\r\n"); | ||
|
||
$this->assertTrue($ended); | ||
} | ||
|
||
public function testHandleEndIncomplete() | ||
{ | ||
$exception = null; | ||
$stream = new ThroughStream(); | ||
$response = new ChunkedStreamDecoder($stream); | ||
$response->on('error', function ($e) use (&$exception) { | ||
$exception = $e; | ||
}); | ||
|
||
$stream->end("4\r\nWiki"); | ||
|
||
$this->assertInstanceOf('Exception', $exception); | ||
} | ||
|
||
public function testHandleEndTrailers() | ||
{ | ||
$ended = false; | ||
$stream = new ThroughStream(); | ||
$response = new ChunkedStreamDecoder($stream); | ||
$response->on('end', function () use (&$ended) { | ||
$ended = true; | ||
}); | ||
|
||
$stream->write("4\r\nWiki\r\n0\r\nabc: def\r\nghi: klm\r\n\r\n"); | ||
|
||
$this->assertTrue($ended); | ||
} | ||
} |
Oops, something went wrong.
return min($bufferLength, $this->remainingLength);
is much more readable.