From 1161291ba000c0657a8d61cbfd3525b64341c541 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 22 Aug 2015 21:01:05 +0200 Subject: [PATCH 1/5] Replace the abandoned guzzle/parser with guzzlehttp/psr7, inspired by the work in https://github.com/reactphp/http/pull/29 --- composer.json | 2 +- src/Request.php | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index e971e32..312a1e5 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "require": { "php": ">=5.4.0", - "guzzle/parser": "~3.0", + "guzzlehttp/psr7": "^1.0", "react/socket-client": "0.4.*", "react/dns": "0.4.*", "react/event-loop": "0.4.*", diff --git a/src/Request.php b/src/Request.php index 8d96f6b..8ba5461 100644 --- a/src/Request.php +++ b/src/Request.php @@ -3,7 +3,7 @@ namespace React\HttpClient; use Evenement\EventEmitterTrait; -use Guzzle\Parser\Message\MessageParser; +use GuzzleHttp\Psr7 as g7; use React\SocketClient\ConnectorInterface; use React\Stream\WritableStreamInterface; @@ -203,20 +203,25 @@ public function close(\Exception $error = null) protected function parseResponse($data) { - $parser = new MessageParser(); - $parsed = $parser->parseResponse($data); + $psrResponse = g7\parse_response($data); + $headers = $psrResponse->getHeaders(); + array_walk($headers, function(&$val) { + if (1 === count($val)) { + $val = $val[0]; + } + }); $factory = $this->getResponseFactory(); $response = $factory( - $parsed['protocol'], - $parsed['version'], - $parsed['code'], - $parsed['reason_phrase'], - $parsed['headers'] + 'HTTP', + $psrResponse->getProtocolVersion(), + $psrResponse->getStatusCode(), + $psrResponse->getReasonPhrase(), + $headers ); - return array($response, $parsed['body']); + return array($response, $psrResponse->getBody()); } protected function connect() From 4b761c0ad1eef53f5c35f086454dde59693e0c83 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 22 Aug 2015 21:44:27 +0200 Subject: [PATCH 2/5] Added multivalueHeader test to ensure we got the headers back in the expected format --- tests/RequestTest.php | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 1193c90..80c2105 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -462,5 +462,43 @@ private function rejectedConnectionMock() ->with('www.example.com', 80) ->will($this->returnValue(new RejectedPromise(new \RuntimeException()))); } -} + /** @test */ + public function multivalueHeader() + { + $requestData = new RequestData('GET', 'http://www.example.com'); + $request = new Request($this->connector, $requestData); + + $this->successfulConnectionMock(); + + $response = $this->response; + + $response->expects($this->at(0)) + ->method('on') + ->with('end', $this->anything()); + $response->expects($this->at(1)) + ->method('on') + ->with('error', $this->anything()) + ->will($this->returnCallback(function ($event, $cb) use (&$errorCallback) { + $errorCallback = $cb; + })); + + $factory = $this->createCallableMock(); + $factory->expects($this->once()) + ->method('__invoke') + ->with('HTTP', '1.0', '200', 'OK', array('Content-Type' => 'text/plain', 'X-Xss-Protection' => '1; mode=block', 'Cache-Control' => 'public, must-revalidate, max-age=0')) + ->will($this->returnValue($response)); + + $request->setResponseFactory($factory); + $request->end(); + + $request->handleData("HTTP/1.0 200 OK\r\n"); + $request->handleData("Content-Type: text/plain\r\n"); + $request->handleData("X-Xss-Protection:1; mode=block\r\n"); + $request->handleData("Cache-Control:public, must-revalidate, max-age=0\r\n"); + $request->handleData("\r\nbody"); + + $this->assertNotNull($errorCallback); + call_user_func($errorCallback, new \Exception('test')); + } +} From 6ba89d2e1102d80eb40d5dcf9083ef65acf162b2 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 24 Aug 2015 11:38:24 +0200 Subject: [PATCH 3/5] Changed g7 to gPsr --- src/Request.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Request.php b/src/Request.php index 8ba5461..48e3474 100644 --- a/src/Request.php +++ b/src/Request.php @@ -3,7 +3,7 @@ namespace React\HttpClient; use Evenement\EventEmitterTrait; -use GuzzleHttp\Psr7 as g7; +use GuzzleHttp\Psr7 as gPsr; use React\SocketClient\ConnectorInterface; use React\Stream\WritableStreamInterface; @@ -203,7 +203,7 @@ public function close(\Exception $error = null) protected function parseResponse($data) { - $psrResponse = g7\parse_response($data); + $psrResponse = gPsr\parse_response($data); $headers = $psrResponse->getHeaders(); array_walk($headers, function(&$val) { if (1 === count($val)) { From 16fc073abb730157cec236497f51331e43d42a69 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 24 Aug 2015 11:43:37 +0200 Subject: [PATCH 4/5] Immutable array manipulation --- src/Request.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Request.php b/src/Request.php index 48e3474..994fbd0 100644 --- a/src/Request.php +++ b/src/Request.php @@ -204,12 +204,13 @@ public function close(\Exception $error = null) protected function parseResponse($data) { $psrResponse = gPsr\parse_response($data); - $headers = $psrResponse->getHeaders(); - array_walk($headers, function(&$val) { + $headers = array_map(function(&$val) { if (1 === count($val)) { $val = $val[0]; } - }); + + return $val; + }, $psrResponse->getHeaders()); $factory = $this->getResponseFactory(); From 4c3b5db85f9e4617a7d615b476f48329b0c1253b Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 24 Aug 2015 20:10:51 +0200 Subject: [PATCH 5/5] Removed not used & --- src/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Request.php b/src/Request.php index 994fbd0..a2f7024 100644 --- a/src/Request.php +++ b/src/Request.php @@ -204,7 +204,7 @@ public function close(\Exception $error = null) protected function parseResponse($data) { $psrResponse = gPsr\parse_response($data); - $headers = array_map(function(&$val) { + $headers = array_map(function($val) { if (1 === count($val)) { $val = $val[0]; }