Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Writable updated #2

Open
wants to merge 17 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Io/ClientRequestStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ public function handleData($data)
if ($successfulEndReceived && $connection->isReadable() && $that->hasMessageKeepAliveEnabled($response) && $that->hasMessageKeepAliveEnabled($request)) {
$connectionManager->keepAlive($request->getUri(), $connection);
} else {
$connection->close();
if (! $that->responseIsAnUpgradeResponse($response)) {
$connection->close();
}
}

$that->close();
Expand All @@ -199,7 +201,12 @@ public function handleData($data)
} elseif ($response->hasHeader('Content-Length')) {
$length = (int) $response->getHeaderLine('Content-Length');
}
$response = $response->withBody($body = new ReadableBodyStream($body, $length));

$body = $this->responseIsAnUpgradeResponse($response)
? new DuplexBodyStream($connection)
: new ReadableBodyStream($body, $length);

$response = $response->withBody($body);
$body->on('end', function () use (&$successfulEndReceived) {
$successfulEndReceived = true;
});
Expand All @@ -216,6 +223,14 @@ public function handleData($data)
}
}

protected function responseIsAnUpgradeResponse($response)
{
return
$response->hasHeader('Connection') &&
(in_array('upgrade', array_map('strtolower', $response->getHeader('Connection')))) &&
(int) $response->getStatusCode() === 101;
}

/** @internal */
public function handleEnd()
{
Expand Down
34 changes: 34 additions & 0 deletions src/Io/DuplexBodyStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace React\Http\Io;

use React\Socket\ConnectionInterface;
use React\Stream\DuplexStreamInterface;

/**
* @internal
*/
class DuplexBodyStream extends ReadableBodyStream implements DuplexStreamInterface
{
private $connection;
public function __construct(ConnectionInterface $connection)
{
$this->connection = $connection;
parent::__construct($connection);
}

public function isWritable()
{
return $this->connection->isWritable();
}

public function write($data)
{
return $this->connection->write($data);
}

public function end($data = null)
{
return $this->connection->end($data);
}
}
15 changes: 12 additions & 3 deletions src/Io/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,24 @@ public function onResponse(ResponseInterface $response, RequestInterface $reques
return $this->onResponseRedirect($response, $request, $deferred, $state);
}

// only status codes 200-399 are considered to be valid, reject otherwise
if ($this->obeySuccessCode && ($response->getStatusCode() < 200 || $response->getStatusCode() >= 400)) {
throw new ResponseException($response);
// only status codes 100-399 are considered to be valid, reject otherwise
if ($this->obeySuccessCode && $this->failed($response)) {
$requestString = \RingCentral\Psr7\str($request);
$message = 'HTTP status code ' . $response->getStatusCode() . ' (' . $response->getReasonPhrase() . ')';
$message .= ": {$request->getUri()}\n{$requestString}";

throw new ResponseException($response, $message);
}

// resolve our initial promise
return $response;
}

private function failed(ResponseInterface $response)
{
return $response->getStatusCode() >= 500 || ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500);
}

/**
* @param ResponseInterface $response
* @param RequestInterface $request
Expand Down