Skip to content

Commit

Permalink
Remove internal Response class, use PSR-7 response instead
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Aug 28, 2020
1 parent cb9ced0 commit fc531a2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 514 deletions.
66 changes: 4 additions & 62 deletions src/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public function handleData($data)
// 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);
$response = gPsr\parse_response($this->buffer);
$bodyChunk = (string) $response->getBody();
} catch (\InvalidArgumentException $exception) {
$this->emit('error', array($exception));
}
Expand All @@ -155,17 +156,9 @@ public function handleData($data)
return;
}

$response->on('close', array($this, 'close'));
$that = $this;
$response->on('error', function (\Exception $error) use ($that) {
$that->closeError(new \RuntimeException(
"An error occured in the response",
0,
$error
));
});
$this->stream->on('close', array($this, 'handleClose'));

$this->emit('response', array($response, $this));
$this->emit('response', array($response, $this->stream));

$this->stream->emit('data', array($bodyChunk));
}
Expand Down Expand Up @@ -222,30 +215,6 @@ public function close()
$this->removeAllListeners();
}

protected function parseResponse($data)
{
$psrResponse = gPsr\parse_response($data);
$headers = array_map(function($val) {
if (1 === count($val)) {
$val = $val[0];
}

return $val;
}, $psrResponse->getHeaders());

$factory = $this->getResponseFactory();

$response = $factory(
'HTTP',
$psrResponse->getProtocolVersion(),
$psrResponse->getStatusCode(),
$psrResponse->getReasonPhrase(),
$headers
);

return array($response, (string)($psrResponse->getBody()));
}

protected function connect()
{
$scheme = $this->requestData->getScheme();
Expand All @@ -265,31 +234,4 @@ protected function connect()
return $this->connector
->connect($host . ':' . $port);
}

public function setResponseFactory($factory)
{
$this->responseFactory = $factory;
}

public function getResponseFactory()
{
if (null === $factory = $this->responseFactory) {
$stream = $this->stream;

$factory = function ($protocol, $version, $code, $reasonPhrase, $headers) use ($stream) {
return new Response(
$stream,
$protocol,
$version,
$code,
$reasonPhrase,
$headers
);
};

$this->responseFactory = $factory;
}

return $factory;
}
}
173 changes: 0 additions & 173 deletions src/Client/Response.php

This file was deleted.

21 changes: 7 additions & 14 deletions src/Io/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Http\Io;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\LoopInterface;
use React\Http\Client\Client as HttpClient;
use React\Http\Client\Response as ResponseStream;
Expand Down Expand Up @@ -108,26 +109,18 @@ public function send(RequestInterface $request)
$deferred->reject($error);
});

$requestStream->on('response', function (ResponseStream $responseStream) use ($deferred, $request) {
$requestStream->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($deferred, $request) {
$length = null;
$body = $responseStream;
$code = $responseStream->getCode();
$code = $response->getStatusCode();
if ($request->getMethod() === 'HEAD' || ($code >= 100 && $code < 200) || $code == 204 || $code == 304) {
$length = 0;
} elseif (\strtolower($responseStream->getHeaderLine('Transfer-Encoding')) === 'chunked') {
} elseif (\strtolower($response->getHeaderLine('Transfer-Encoding')) === 'chunked') {
$body = new ChunkedDecoder($body);
} elseif ($responseStream->hasHeader('Content-Length')) {
$length = (int) $responseStream->getHeaderLine('Content-Length');
} elseif ($response->hasHeader('Content-Length')) {
$length = (int) $response->getHeaderLine('Content-Length');
}

// apply response header values from response stream
$deferred->resolve(new Response(
$responseStream->getCode(),
$responseStream->getHeaders(),
new ReadableBodyStream($body, $length),
$responseStream->getVersion(),
$responseStream->getReasonPhrase()
));
$deferred->resolve($response->withBody(new ReadableBodyStream($body, $length)));
});

if ($body instanceof ReadableStreamInterface) {
Expand Down
19 changes: 10 additions & 9 deletions tests/Client/FunctionalIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace React\Tests\Http\Client;

use Clue\React\Block;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory;
use React\Http\Client\Client;
use React\Http\Client\Response;
use React\Promise\Deferred;
use React\Promise\Stream;
use React\Socket\Server;
use React\Socket\ConnectionInterface;
use React\Stream\ReadableStreamInterface;
use React\Tests\Http\TestCase;

class FunctionalIntegrationTest extends TestCase
Expand Down Expand Up @@ -69,8 +70,8 @@ public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResp
$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->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($once) {
$body->on('data', $once);
});

$promise = Stream\first($request, 'close');
Expand All @@ -88,8 +89,8 @@ public function testSuccessfulResponseEmitsEnd()
$request = $client->request('GET', 'http://www.google.com/');

$once = $this->expectCallableOnce();
$request->on('response', function (Response $response) use ($once) {
$response->on('end', $once);
$request->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($once) {
$body->on('end', $once);
});

$promise = Stream\first($request, 'close');
Expand All @@ -112,8 +113,8 @@ public function testPostDataReturnsData()
$request = $client->request('POST', 'https://' . (mt_rand(0, 1) === 0 ? 'eu.' : '') . 'httpbin.org/post', array('Content-Length' => strlen($data)));

$deferred = new Deferred();
$request->on('response', function (Response $response) use ($deferred) {
$deferred->resolve(Stream\buffer($response));
$request->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($deferred) {
$deferred->resolve(Stream\buffer($body));
});

$request->on('error', 'printf');
Expand Down Expand Up @@ -145,8 +146,8 @@ public function testPostJsonReturnsData()
$request = $client->request('POST', 'https://httpbin.org/post', array('Content-Length' => strlen($data), 'Content-Type' => 'application/json'));

$deferred = new Deferred();
$request->on('response', function (Response $response) use ($deferred) {
$deferred->resolve(Stream\buffer($response));
$request->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($deferred) {
$deferred->resolve(Stream\buffer($body));
});

$request->on('error', 'printf');
Expand Down
Loading

0 comments on commit fc531a2

Please sign in to comment.