Skip to content
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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/StreamingBodyParser/FormUrlencodedParser.php
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();
Copy link
Member

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 of HttpBodyStream?

Copy link
Member

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 of HttpBodyStream.

Copy link
Member Author

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.

$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
);
}
}
}
53 changes: 53 additions & 0 deletions tests/StreamingBodyParser/FormUrlencodedParserTest.php
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
);
}
}