-
-
Notifications
You must be signed in to change notification settings - Fork 167
[WIP] Streaming parser bufferedsink #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
WyriHaximus
wants to merge
17
commits into
reactphp:0.5
from
WyriHaximus:feature-streaming-parser-bufferedsink
Closed
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
bf97310
Streaming parser bufferedsink
WyriHaximus 9207588
BodyBufferedSink: https://github.com/reactphp/http/pull/73#issuecomme…
WyriHaximus b046c84
Removed old vars I forgot to removed :X
WyriHaximus 0f54149
Correct PostBufferedSinkTest class name
WyriHaximus b775b96
$result now contains the body not an array with different types of da…
WyriHaximus ee9039f
PostBufferedSink: https://github.com/reactphp/http/pull/73#issuecomme…
WyriHaximus 11fb097
Removed unused uses
WyriHaximus 0619c14
Then => done: https://github.com/reactphp/http/pull/73#discussion_r84…
WyriHaximus 64ce2d6
Corrected return comment for BodyBufferedSink::createPromise
WyriHaximus b1d8e0e
Corrected return comment for PostBufferedSink::createPromise
WyriHaximus 75facbb
Call the correct sink for body buffered sink test
WyriHaximus 14c2111
Call the correct sink for post buffered sink test
WyriHaximus 21e943a
FileBufferedSink: https://github.com/reactphp/http/pull/73#issuecomme…
WyriHaximus 2437eef
Append additional body calls
WyriHaximus 1d9a18d
NoBodyParser is no more
WyriHaximus e8c452b
Resource BufferedSink as a handy wrapper around the other parser sink…
WyriHaximus c2603b8
Type in PostBufferedSinkTest https://github.com/reactphp/http/pull/73…
WyriHaximus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,90 @@ | ||
| <?php | ||
|
|
||
| namespace React\Http\StreamingBodyParser; | ||
|
|
||
| use React\Http\File; | ||
| use React\Promise\Deferred; | ||
| use React\Promise\PromiseInterface; | ||
| use React\Stream\BufferedSink as StreamBufferedSink; | ||
|
|
||
| class BufferedSink | ||
| { | ||
| /** | ||
| * @param ParserInterface $parser | ||
| * @return PromiseInterface | ||
| */ | ||
| public static function createPromise(ParserInterface $parser) | ||
| { | ||
| if ($parser instanceof NoBodyParser) { | ||
| return \React\Promise\resolve([ | ||
| 'post' => [], | ||
| 'files' => [], | ||
| 'body' => '', | ||
| ]); | ||
| } | ||
|
|
||
| $deferred = new Deferred(); | ||
| $postFields = []; | ||
| $files = []; | ||
| $body = ''; | ||
| $parser->on('post', function ($key, $value) use (&$postFields) { | ||
| self::extractPost($postFields, $key, $value); | ||
| }); | ||
| $parser->on('file', function ($name, File $file) use (&$files) { | ||
| StreamBufferedSink::createPromise($file->getStream())->then(function ($buffer) use ($name, $file, &$files) { | ||
| $files[] = [ | ||
| 'name' => $name, | ||
| 'file' => $file, | ||
| 'buffer' => $buffer, | ||
| ]; | ||
| }); | ||
| }); | ||
| $parser->on('body', function ($rawBody) use (&$body) { | ||
| $body = $rawBody; | ||
| }); | ||
| $parser->on('end', function () use ($deferred, &$postFields, &$files, &$body) { | ||
| $deferred->resolve([ | ||
| 'post' => $postFields, | ||
| 'files' => $files, | ||
| 'body' => $body, | ||
| ]); | ||
| }); | ||
|
|
||
| return $deferred->promise(); | ||
| } | ||
|
|
||
| public static function extractPost(&$postFields, $key, $value) | ||
| { | ||
| $chunks = explode('[', $key); | ||
| if (count($chunks) == 1) { | ||
| $postFields[$key] = $value; | ||
| return; | ||
| } | ||
|
|
||
| $chunkKey = $chunks[0]; | ||
| if (!isset($postFields[$chunkKey])) { | ||
| $postFields[$chunkKey] = []; | ||
| } | ||
|
|
||
| $parent = &$postFields; | ||
| for ($i = 1; $i < count($chunks); $i++) { | ||
| $previousChunkKey = $chunkKey; | ||
| if (!isset($parent[$previousChunkKey])) { | ||
| $parent[$previousChunkKey] = []; | ||
| } | ||
| $parent = &$parent[$previousChunkKey]; | ||
| $chunkKey = $chunks[$i]; | ||
|
|
||
| if ($chunkKey == ']') { | ||
| $parent[] = $value; | ||
| return; | ||
| } | ||
|
|
||
| $chunkKey = rtrim($chunkKey, ']'); | ||
| if ($i == count($chunks) - 1) { | ||
| $parent[$chunkKey] = $value; | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,100 @@ | ||
| <?php | ||
|
|
||
| namespace React\Tests\Http\StreamingBodyParser; | ||
|
|
||
| use Clue\React\Block; | ||
| use React\EventLoop\Factory; | ||
| use React\Http\File; | ||
| use React\Http\StreamingBodyParser\BufferedSink; | ||
| use React\Http\StreamingBodyParser\NoBodyParser; | ||
| use React\Http\Request; | ||
| use React\Stream\ThroughStream; | ||
| use React\Tests\Http\TestCase; | ||
|
|
||
| class DeferredStreamTest extends TestCase | ||
| { | ||
| public function testDoneParser() | ||
| { | ||
| $parser = new NoBodyParser(new Request('get', 'http://example.com')); | ||
| $deferredStream = BufferedSink::createPromise($parser); | ||
| $result = Block\await($deferredStream, Factory::create(), 10); | ||
| $this->assertSame([ | ||
| 'post' => [], | ||
| 'files' => [], | ||
| 'body' => '', | ||
| ], $result); | ||
| } | ||
|
|
||
| public function testDeferredStream() | ||
| { | ||
| $parser = new DummyParser(new Request('get', 'http://example.com')); | ||
| $deferredStream = BufferedSink::createPromise($parser); | ||
| $parser->emit('post', ['foo', 'bar']); | ||
| $parser->emit('post', ['array[]', 'foo']); | ||
| $parser->emit('post', ['array[]', 'bar']); | ||
| $parser->emit('post', ['dem[two]', 'bar']); | ||
| $parser->emit('post', ['dom[two][]', 'bar']); | ||
|
|
||
| $stream = new ThroughStream(); | ||
| $file = new File('bar.ext', 'text', $stream); | ||
| $parser->emit('file', ['foo', $file]); | ||
| $stream->end('foo.bar'); | ||
|
|
||
| $parser->emit('body', ['abc']); | ||
|
|
||
| $parser->emit('end'); | ||
|
|
||
| $result = Block\await($deferredStream, Factory::create(), 10); | ||
| $this->assertSame([ | ||
| 'foo' => 'bar', | ||
| 'array' => [ | ||
| 'foo', | ||
| 'bar', | ||
| ], | ||
| 'dem' => [ | ||
| 'two' => 'bar', | ||
| ], | ||
| 'dom' => [ | ||
| 'two' => [ | ||
| 'bar', | ||
| ], | ||
| ], | ||
| ], $result['post']); | ||
|
|
||
| $this->assertSame('foo', $result['files'][0]['name']); | ||
| $this->assertSame('bar.ext', $result['files'][0]['file']->getFilename()); | ||
| $this->assertSame('text', $result['files'][0]['file']->getContentType()); | ||
| $this->assertSame('foo.bar', $result['files'][0]['buffer']); | ||
|
|
||
| $this->assertSame('abc', $result['body']); | ||
| } | ||
|
|
||
| public function testExtractPost() | ||
| { | ||
| $postFields = []; | ||
| BufferedSink::extractPost($postFields, 'dem', 'value'); | ||
| BufferedSink::extractPost($postFields, 'dom[one][two][]', 'value_a'); | ||
| BufferedSink::extractPost($postFields, 'dom[one][two][]', 'value_b'); | ||
| BufferedSink::extractPost($postFields, 'dam[]', 'value_a'); | ||
| BufferedSink::extractPost($postFields, 'dam[]', 'value_b'); | ||
| BufferedSink::extractPost($postFields, 'dum[sum]', 'value'); | ||
| $this->assertSame([ | ||
| 'dem' => 'value', | ||
| 'dom' => [ | ||
| 'one' => [ | ||
| 'two' => [ | ||
| 'value_a', | ||
| 'value_b', | ||
| ], | ||
| ], | ||
| ], | ||
| 'dam' => [ | ||
| 'value_a', | ||
| 'value_b', | ||
| ], | ||
| 'dum' => [ | ||
| 'sum' => 'value', | ||
| ], | ||
| ], $postFields); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
doneinstead ofthen.