-
-
Notifications
You must be signed in to change notification settings - Fork 166
Streaming body parsers foundation #69
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
Changes from 1 commit
0e88cc5
758ad27
304611f
dee32e3
1ae613f
2dcee0f
68340bb
d089c56
75aac07
7a1414a
2c40605
a2083d2
0afcef1
8810190
fe58c44
5ae429e
b3436bb
f18d114
e6d5a16
2dacc3d
76fd4cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| namespace React\Http\StreamingBodyParser; | ||
|
|
||
| use React\Http\Request; | ||
|
|
||
| class Factory | ||
| { | ||
| /** | ||
| * @param Request $request | ||
| * @return ParserInterface | ||
| */ | ||
| public static function create(Request $request) | ||
| { | ||
| return new RawBodyParser($request); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| namespace React\Http\StreamingBodyParser; | ||
|
|
||
| use Evenement\EventEmitterInterface; | ||
| use React\Http\Request; | ||
|
|
||
| interface ParserInterface extends EventEmitterInterface | ||
| { | ||
| public function __construct(Request $request); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| namespace React\Http\StreamingBodyParser; | ||
|
|
||
| use Evenement\EventEmitterTrait; | ||
| use React\Http\Request; | ||
|
|
||
| class RawBodyParser implements ParserInterface | ||
| { | ||
| use EventEmitterTrait; | ||
|
|
||
| /** | ||
| * @param Request $request | ||
| */ | ||
| public function __construct(Request $request) | ||
| { | ||
| $headers = $request->getHeaders(); | ||
| $headers = array_change_key_case($headers, CASE_LOWER); | ||
|
|
||
| ContentLengthBufferedSink::createPromise( | ||
| $request, | ||
| $headers['content-length'] | ||
|
||
| )->then([$this, 'finish']); | ||
|
||
| } | ||
|
|
||
| /** | ||
| * @param string $buffer | ||
| */ | ||
| public function finish($buffer) | ||
| { | ||
| $this->emit('body', [$buffer]); | ||
| $this->emit('end'); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace React\Tests\Http\StreamingBodyParser; | ||
|
|
||
| use React\Http\StreamingBodyParser\Factory; | ||
| use React\Http\Request; | ||
| use React\Tests\Http\TestCase; | ||
|
|
||
| class FactoryTest extends TestCase | ||
| { | ||
| public function testNoContentType() | ||
| { | ||
| $request = new Request('POST', 'http://example.com/', [], 1.1, [ | ||
| 'content-length' => 123, | ||
| ]); | ||
| $parser = Factory::create($request); | ||
| $this->assertInstanceOf('React\Http\StreamingBodyParser\RawBodyParser', $parser); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace React\Tests\Http\StreamingBodyParser; | ||
|
|
||
| use React\Http\StreamingBodyParser\RawBodyParser; | ||
| use React\Http\Request; | ||
| use React\Tests\Http\TestCase; | ||
|
|
||
| class RawBodyParserTest extends TestCase | ||
| { | ||
| public function testNoContentLength() | ||
| { | ||
| $body = ''; | ||
| $request = new Request('POST', 'http://example.com/', [], 1.1, [ | ||
| 'content-length' => 3, | ||
| ]); | ||
| $parser = new RawBodyParser($request); | ||
| $parser->on('body', function ($rawBody) use (&$body) { | ||
| $body = $rawBody; | ||
| }); | ||
| $request->emit('data', ['abc']); | ||
| $this->assertSame('abc', $body); | ||
| } | ||
| } |
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.
You have Factory method, so this antipattern
__constructdon't need to be in interface.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.
Another way of doing this is having a public static factory method that takes a request. But that would just wrap passing the request into the constructor. Let me give it a try to see how it works out, I don't mind either way 😄 .
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.
Updated the PR, with use a factory method to create parsers
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.
Looks much better for me!
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.
Since the parser implementations are now marked as
@internal, i propose to removecreatefrom the interface since this is now just a additional redundant factory method.