-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
Streaming body parser: Form Urlencoded #202
Closed
WyriHaximus
wants to merge
4
commits into
reactphp:master
from
WyriHaximus:feature-streaming-body-parser-formurlencoded
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
97ade3d
Form urlencoded streaming body parser
WyriHaximus 09857c7
Refactored the form urlencoded parser a bit cut down publicly visible AP
WyriHaximus 2d18657
Ensuring a post event always emits two entries, via @jsor at https://…
WyriHaximus 920e6f7
Cleaner and simpler $kv[1] as suggested by @andig at https://github.c…
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 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,77 @@ | ||
<?php | ||
|
||
namespace React\Http\StreamingBodyParser; | ||
|
||
use Evenement\EventEmitter; | ||
use Psr\Http\Message\RequestInterface; | ||
use React\Http\HttpBodyStream; | ||
|
||
final class FormUrlencodedParser extends EventEmitter | ||
{ | ||
/** | ||
* @var RequestInterface | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var HttpBodyStream | ||
*/ | ||
private $body; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $buffer = ''; | ||
|
||
/** | ||
* @param RequestInterface $request | ||
*/ | ||
public function __construct(RequestInterface $request) | ||
{ | ||
$this->request = $request; | ||
$this->body = $this->request->getBody(); | ||
$this->body->on('data', array($this, 'onData')); | ||
$this->body->on('end', array($this, 'onEnd')); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function onData($data) | ||
{ | ||
$this->buffer .= $data; | ||
|
||
$pos = strrpos($this->buffer, '&'); | ||
if ($pos === false) { | ||
return; | ||
} | ||
|
||
$buffer = substr($this->buffer, 0, $pos); | ||
$this->buffer = substr($this->buffer, $pos + 1); | ||
|
||
$this->parse($buffer); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function onEnd() | ||
{ | ||
$this->body->removeAllListeners(); | ||
$this->parse($this->buffer); | ||
$this->emit('end'); | ||
} | ||
|
||
private function parse($buffer) | ||
{ | ||
foreach (explode('&', $buffer) as $chunk) { | ||
$kv = explode('=', $chunk); | ||
$kv[0] = rawurldecode($kv[0]); | ||
$kv[1] = isset($kv[1]) ? rawurldecode($kv[1]) : null; | ||
$this->emit( | ||
'post', | ||
$kv | ||
); | ||
} | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
namespace React\Tests\Http\StreamingBodyParser; | ||
|
||
use React\Http\HttpBodyStream; | ||
use React\Http\StreamingBodyParser\FormUrlencodedParser; | ||
use React\Stream\ThroughStream; | ||
use React\Tests\Http\TestCase; | ||
use RingCentral\Psr7\Request; | ||
|
||
class FormUrlencodedParserTest extends TestCase | ||
{ | ||
public function testParse() | ||
{ | ||
$post = array(); | ||
$stream = new ThroughStream(); | ||
$request = new Request('POST', 'http://example.com/', array(), new HttpBodyStream($stream, 0)); | ||
$parser = new FormUrlencodedParser($request); | ||
$parser->on('post', function ($key, $value) use (&$post) { | ||
$post[] = array($key, $value); | ||
}); | ||
$stream->emit('data', array('hiphip&user=single&user2=second&us')); | ||
$this->assertEquals( | ||
array( | ||
array('hiphip', null), | ||
array('user', 'single'), | ||
array('user2', 'second'), | ||
), | ||
$post | ||
); | ||
$stream->emit('data', array('ers%5B%5D=first%20in%20array&users%5B%5D=second%20in%20array')); | ||
$this->assertEquals( | ||
array( | ||
array('hiphip', null), | ||
array('user', 'single'), | ||
array('user2', 'second'), | ||
array('users[]', 'first in array'), | ||
), | ||
$post | ||
); | ||
$stream->end(); | ||
$this->assertEquals( | ||
array( | ||
array('hiphip', null), | ||
array('user', 'single'), | ||
array('user2', 'second'), | ||
array('users[]', 'first in array'), | ||
array('users[]', 'second in array'), | ||
), | ||
$post | ||
); | ||
} | ||
} |
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.
Maybe check here if
$this->body
is an instance ofHttpBodyStream
?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.
WDYT? Since the class isn't internal, it isn't guaranteed that
$this->body
is an instance ofHttpBodyStream
.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.
I'm thinking a check on if body is a
ReadableStreamInterface
and else throw an exception.