Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
76 changes: 76 additions & 0 deletions src/StreamingBodyParser/FormUrlencodedParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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
*
* @internal
*/
public $body;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope because of PHP 5.3 here https://github.com/WyriHaximus/http/blob/97ade3db7e4b1fe079cff9a6643e61975e179b59/src/StreamingBodyParser/FormUrlencodedParser.php#L39-L43 or I could make a public onEnd method, mark it private and make everything that shouldn't be public private.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're correct. My bad 🤗

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'll look into refactoring it a bit anyway 👍


/**
* @var string
*
* @internal
*/
public $buffer = '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?


/**
* @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'));
$that = $this;
$this->body->on('end', function () use ($that) {
$that->body->removeAllListeners();
$that->parse($that->buffer);
$that->emit('end');
});
}

/**
* @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 parse($buffer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?

{
foreach (explode('&', $buffer) as $chunk) {
$this->emit(
'post',
explode('=', rawurldecode($chunk))
Copy link
Member

@jsor jsor Jun 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but maybe ensure here that there will be always an array with to entries (key and value) emitted. so that key1&key2 emits array('key1', null) and array('key2', null)`?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 👍

);
}
}
}
50 changes: 50 additions & 0 deletions tests/StreamingBodyParser/FormUrlencodedParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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('user=single&user2=second&us'));
$this->assertEquals(
array(
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('user', 'single'),
array('user2', 'second'),
array('users[]', 'first in array'),
),
$post
);
$stream->end();
$this->assertEquals(
array(
array('user', 'single'),
array('user2', 'second'),
array('users[]', 'first in array'),
array('users[]', 'second in array'),
),
$post
);
}
}