From 3d269081df9ff82ae37c87928814c8f8c2f5d0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sat, 7 Apr 2018 15:47:12 +0200 Subject: [PATCH] Support legacy HTTP servers that use only LF instead of CRLF --- src/Request.php | 3 ++- tests/FunctionalIntegrationTest.php | 23 +++++++++++++++++++++++ tests/TestCase.php | 11 +++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Request.php b/src/Request.php index ea4d50b..caa242b 100644 --- a/src/Request.php +++ b/src/Request.php @@ -134,7 +134,8 @@ public function handleData($data) { $this->buffer .= $data; - if (false !== strpos($this->buffer, "\r\n\r\n")) { + // buffer until double CRLF (or double LF for compatibility with legacy servers) + if (false !== strpos($this->buffer, "\r\n\r\n") || false !== strpos($this->buffer, "\n\n")) { try { list($response, $bodyChunk) = $this->parseResponse($this->buffer); } catch (\InvalidArgumentException $exception) { diff --git a/tests/FunctionalIntegrationTest.php b/tests/FunctionalIntegrationTest.php index 1ed2228..1deebc7 100644 --- a/tests/FunctionalIntegrationTest.php +++ b/tests/FunctionalIntegrationTest.php @@ -29,6 +29,29 @@ public function testRequestToLocalhostEmitsSingleRemoteConnection() $loop->run(); } + public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResponse() + { + $loop = Factory::create(); + + $server = new Server(0, $loop); + $server->on('connection', function (ConnectionInterface $conn) use ($server) { + $conn->end("HTTP/1.0 200 OK\n\nbody"); + $server->close(); + }); + + $client = new Client($loop); + $request = $client->request('GET', str_replace('tcp:', 'http:', $server->getAddress())); + + $once = $this->expectCallableOnceWith('body'); + $request->on('response', function (Response $response) use ($once) { + $response->on('data', $once); + }); + + $request->end(); + + $loop->run(); + } + /** @group internet */ public function testSuccessfulResponseEmitsEnd() { diff --git a/tests/TestCase.php b/tests/TestCase.php index 9e090bc..901f82f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -26,6 +26,17 @@ protected function expectCallableOnce() return $mock; } + protected function expectCallableOnceWith($value) + { + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($value); + + return $mock; + } + protected function expectCallableNever() { $mock = $this->createCallableMock();