Skip to content

Commit 22e87bc

Browse files
authored
Merge pull request #130 from clue-labs/legacy-line-feed
Support legacy HTTP servers that use only LF instead of CRLF
2 parents 7dd4909 + 3d26908 commit 22e87bc

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/Request.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ public function handleData($data)
134134
{
135135
$this->buffer .= $data;
136136

137-
if (false !== strpos($this->buffer, "\r\n\r\n")) {
137+
// buffer until double CRLF (or double LF for compatibility with legacy servers)
138+
if (false !== strpos($this->buffer, "\r\n\r\n") || false !== strpos($this->buffer, "\n\n")) {
138139
try {
139140
list($response, $bodyChunk) = $this->parseResponse($this->buffer);
140141
} catch (\InvalidArgumentException $exception) {

tests/FunctionalIntegrationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ public function testRequestToLocalhostEmitsSingleRemoteConnection()
2929
$loop->run();
3030
}
3131

32+
public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResponse()
33+
{
34+
$loop = Factory::create();
35+
36+
$server = new Server(0, $loop);
37+
$server->on('connection', function (ConnectionInterface $conn) use ($server) {
38+
$conn->end("HTTP/1.0 200 OK\n\nbody");
39+
$server->close();
40+
});
41+
42+
$client = new Client($loop);
43+
$request = $client->request('GET', str_replace('tcp:', 'http:', $server->getAddress()));
44+
45+
$once = $this->expectCallableOnceWith('body');
46+
$request->on('response', function (Response $response) use ($once) {
47+
$response->on('data', $once);
48+
});
49+
50+
$request->end();
51+
52+
$loop->run();
53+
}
54+
3255
/** @group internet */
3356
public function testSuccessfulResponseEmitsEnd()
3457
{

tests/TestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ protected function expectCallableOnce()
2626
return $mock;
2727
}
2828

29+
protected function expectCallableOnceWith($value)
30+
{
31+
$mock = $this->createCallableMock();
32+
$mock
33+
->expects($this->once())
34+
->method('__invoke')
35+
->with($value);
36+
37+
return $mock;
38+
}
39+
2940
protected function expectCallableNever()
3041
{
3142
$mock = $this->createCallableMock();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy