Skip to content

Commit

Permalink
Merge pull request #130 from clue-labs/legacy-line-feed
Browse files Browse the repository at this point in the history
Support legacy HTTP servers that use only LF instead of CRLF
  • Loading branch information
WyriHaximus committed Apr 9, 2018
2 parents 7dd4909 + 3d26908 commit 22e87bc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Request.php
Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions tests/FunctionalIntegrationTest.php
Expand Up @@ -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()
{
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase.php
Expand Up @@ -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();
Expand Down

0 comments on commit 22e87bc

Please sign in to comment.