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

Remove guzzle parser #26

Closed
wants to merge 13 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Expand Up @@ -5,7 +5,6 @@
"license": "MIT",
"require": {
"php": ">=5.4.0",
"guzzle/parser": "~3.0",
"react/socket-client": "0.4.*",
"react/dns": "0.4.*",
"react/event-loop": "0.4.*",
Expand Down
9 changes: 4 additions & 5 deletions src/Request.php
Expand Up @@ -3,7 +3,6 @@
namespace React\HttpClient;

use Evenement\EventEmitterTrait;
use Guzzle\Parser\Message\MessageParser;
use React\SocketClient\ConnectorInterface;
use React\Stream\WritableStreamInterface;

Expand Down Expand Up @@ -193,20 +192,20 @@ public function close(\Exception $error = null)

protected function parseResponse($data)
{
$parser = new MessageParser();
$parsed = $parser->parseResponse($data);
$parser = new ResponseParser();
$parsed = $parser->parse($data);

$factory = $this->getResponseFactory();

$response = $factory(
$parsed['protocol'],
$parsed['version'],
$parsed['code'],
$parsed['reason_phrase'],
$parsed['reason'],
$parsed['headers']
);

return array($response, $parsed['body']);
return [ $response, $parsed['body']];
}

protected function connect()
Expand Down
74 changes: 74 additions & 0 deletions src/ResponseParser.php
@@ -0,0 +1,74 @@
<?php

namespace React\HttpClient;

class ResponseParser
{
public function parse($raw)
{
if (empty($raw) or false === strpos($raw, "\r\n\r\n")) {
return false;
}

list($head, $body) = explode("\r\n\r\n", $raw, 2);

$lines = array_map('trim', explode("\n", $head));
$first_line = array_shift($lines);

if (!strpos($first_line, ' ')) {
return false;
}

list($http, $code, $reason) = explode(' ', $first_line.' ', 3);

if (!strpos($http, '/') or (int)$code < 100 or (int)$code >= 1000) {
return false;
}

list($protocol, $version) = explode('/', $http, 2);

if (empty($protocol) or $protocol !== 'HTTP') {
return false;
}

if (empty($version) or !is_numeric($version)) {
return false;
}

$headers = [];
foreach ($lines as $line) {
if (!strpos($line, ':')) {
continue;
}

list($name, $value) = array_map('trim', explode(':', $line, 2));

$name = strtolower($name);

if (empty($name)) {
continue;
}

if (strpos($value, ';')) {
$value = array_map('trim', explode(';', $value));
} else {
$value = [ $value ];
}

if (!isset($headers[$name])) {
$headers[$name] = [];
}

$headers[$name] = array_merge($headers[$name], [ $value ]);
}

return [
'protocol' => $protocol,
'version' => $version,
'code' => (int)$code,
'reason' => trim($reason),
'headers' => $headers,
'body' => $body
];
}
}
4 changes: 2 additions & 2 deletions tests/RequestTest.php
Expand Up @@ -83,7 +83,7 @@ public function requestShouldBindToStreamEventsAndUseconnector()
$factory = $this->createCallableMock();
$factory->expects($this->once())
->method('__invoke')
->with('HTTP', '1.0', '200', 'OK', array('Content-Type' => 'text/plain'))
->with('HTTP', '1.0', '200', 'OK', [ 'content-type' => [[ 'text/plain' ]] ])
->will($this->returnValue($response));

$request->setResponseFactory($factory);
Expand Down Expand Up @@ -371,7 +371,7 @@ public function requestShouldRelayErrorEventsFromResponse()
$factory = $this->createCallableMock();
$factory->expects($this->once())
->method('__invoke')
->with('HTTP', '1.0', '200', 'OK', array('Content-Type' => 'text/plain'))
->with('HTTP', '1.0', '200', 'OK', [ 'content-type' => [[ 'text/plain' ]]])
->will($this->returnValue($response));

$request->setResponseFactory($factory);
Expand Down
100 changes: 100 additions & 0 deletions tests/ResponseParserTest.php
@@ -0,0 +1,100 @@
<?php

namespace React\Tests\HttpClient;

use React\HttpClient\ResponseParser;

class ResponseParserTest extends TestCase
{
private $parser;

public function setUp()
{
$this->parser = new ResponseParser;
}

/**
* @dataProvider dataProvider
*/
public function testParse($raw, $expected, $msg = '')
{
$this->assertEquals($expected, $this->parser->parse($raw), $msg);
}

public function dataProvider()
{
return [
[ "", false, 'empty' ],
[ "MWA-HA-HA\r\n\r\nNOT A VALID\r\nRESPONSE", false, 'invalid' ],
[ "HTTP/1.0 200 OK\r\nContent-Length: 4\r\nContent-Type: text/plain;charset=utf-8\r\n\r\nTest", [
'protocol' => 'HTTP',
'version' => '1.0',
'code' => 200,
'reason' => 'OK',
'headers' => [
'content-length' => [
[
4
]
],
'content-type' => [
[
'text/plain',
'charset=utf-8'
]
]
],
'body' => 'Test'
], 'valid' ],
[ "HTTP/1.0 404\r\n\r\n", [
'protocol' => 'HTTP',
'version' => '1.0',
'code' => 404,
'reason' => '',
'headers' => [],
'body' => ''
], 'missing reason' ],
[ "/1.0 404 Not Found\r\n\r\n", false, 'missing protocol 1' ],
[ "1.0 404 Not Found\r\n\r\n", false, 'missing protocol 2' ],
[ "HTTP/ 404 Not Found\r\n\r\n", false, 'missing version 1' ],
[ "HTTP 404 Not Found\r\n\r\n", false, 'missing version 2' ],
[ "HTTP/1.0 Not Found\r\n\r\n", false, 'missing code' ],
[ "FTP/1.0 404 Not Found\r\n\r\n", false, 'invalid protocol' ],
[ "HTTP/1.0 42 Answer to the Ultimate Question\r\n\r\n", false, 'invalid code' ],
[ "HTTP/1.0 200 OK\r\nContent-Length 4\r\n\r\nTest", [
'protocol' => 'HTTP',
'version' => '1.0',
'code' => 200,
'reason' => 'OK',
'headers' => [],
'body' => 'Test'
], 'missing colon' ],
[ "HTTP/1.0 200 OK\r\n: 42\r\n\r\nTest", [
'protocol' => 'HTTP',
'version' => '1.0',
'code' => 200,
'reason' => 'OK',
'headers' => [],
'body' => 'Test'
], 'missing header name' ],
[ "HTTP/1.0 200 OK\r\nX-Header-Name: foo\r\nX-Header-Name: bar;baz=1\r\n\r\nTest", [
'protocol' => 'HTTP',
'version' => '1.0',
'code' => 200,
'reason' => 'OK',
'headers' => [
'x-header-name' => [
[
'foo'
],
[
'bar',
'baz=1'
]
]
],
'body' => 'Test'
], 'multiple headers with the same name' ],
];
}
}