| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\HttpClient; |
| 4 |
|
| 5 |
use Evenement\EventEmitter; |
| 6 |
use React\Promise; |
| 7 |
use React\Socket\ConnectionInterface; |
| 8 |
use React\Socket\ConnectorInterface; |
| 9 |
use React\Stream\WritableStreamInterface; |
| 10 |
use RingCentral\Psr7 as gPsr; |
| 11 |
|
| 12 |
/** |
| 13 |
* @event response |
| 14 |
* @event drain |
| 15 |
* @event error |
| 16 |
* @event end |
| 17 |
*/ |
| 18 |
class Request extends EventEmitter implements WritableStreamInterface |
| 19 |
{ |
| 20 |
const STATE_INIT = 0; |
| 21 |
const STATE_WRITING_HEAD = 1; |
| 22 |
const STATE_HEAD_WRITTEN = 2; |
| 23 |
const STATE_END = 3; |
| 24 |
|
| 25 |
private $connector; |
| 26 |
private $requestData; |
| 27 |
|
| 28 |
private $stream; |
| 29 |
private $buffer; |
| 30 |
private $responseFactory; |
| 31 |
private $state = self::STATE_INIT; |
| 32 |
private $ended = false; |
| 33 |
|
| 34 |
private $pendingWrites = ''; |
| 35 |
|
| 36 |
public function __construct(ConnectorInterface $connector, RequestData $requestData) |
| 37 |
{ |
| 38 |
$this->connector = $connector; |
| 39 |
$this->requestData = $requestData; |
| 40 |
} |
| 41 |
|
| 42 |
public function isWritable() |
| 43 |
{ |
| 44 |
return self::STATE_END > $this->state && !$this->ended; |
| 45 |
} |
| 46 |
|
| 47 |
private function writeHead() |
| 48 |
{ |
| 49 |
$this->state = self::STATE_WRITING_HEAD; |
| 50 |
|
| 51 |
$requestData = $this->requestData; |
| 52 |
$streamRef = &$this->stream; |
| 53 |
$stateRef = &$this->state; |
| 54 |
$pendingWrites = &$this->pendingWrites; |
| 55 |
$that = $this; |
| 56 |
|
| 57 |
$promise = $this->connect(); |
| 58 |
$promise->then( |
| 59 |
function (ConnectionInterface $stream) use ($requestData, &$streamRef, &$stateRef, &$pendingWrites, $that) { |
| 60 |
$streamRef = $stream; |
| 61 |
|
| 62 |
$stream->on('drain', array($that, 'handleDrain')); |
| 63 |
$stream->on('data', array($that, 'handleData')); |
| 64 |
$stream->on('end', array($that, 'handleEnd')); |
| 65 |
$stream->on('error', array($that, 'handleError')); |
| 66 |
$stream->on('close', array($that, 'handleClose')); |
| 67 |
|
| 68 |
$headers = (string) $requestData; |
| 69 |
|
| 70 |
$more = $stream->write($headers . $pendingWrites); |
| 71 |
|
| 72 |
$stateRef = Request::STATE_HEAD_WRITTEN; |
| 73 |
|
| 74 |
// clear pending writes if non-empty |
| 75 |
if ($pendingWrites !== '') { |
| 76 |
$pendingWrites = ''; |
| 77 |
|
| 78 |
if ($more) { |
| 79 |
$that->emit('drain'); |
| 80 |
} |
| 81 |
} |
| 82 |
}, |
| 83 |
array($this, 'closeError') |
| 84 |
); |
| 85 |
|
| 86 |
$this->on('close', function() use ($promise) { |
| 87 |
$promise->cancel(); |
| 88 |
}); |
| 89 |
} |
| 90 |
|
| 91 |
public function write($data) |
| 92 |
{ |
| 93 |
if (!$this->isWritable()) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
// write directly to connection stream if already available |
| 98 |
if (self::STATE_HEAD_WRITTEN <= $this->state) { |
| 99 |
return $this->stream->write($data); |
| 100 |
} |
| 101 |
|
| 102 |
// otherwise buffer and try to establish connection |
| 103 |
$this->pendingWrites .= $data; |
| 104 |
if (self::STATE_WRITING_HEAD > $this->state) { |
| 105 |
$this->writeHead(); |
| 106 |
} |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
public function end($data = null) |
| 112 |
{ |
| 113 |
if (!$this->isWritable()) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
if (null !== $data) { |
| 118 |
$this->write($data); |
| 119 |
} else if (self::STATE_WRITING_HEAD > $this->state) { |
| 120 |
$this->writeHead(); |
| 121 |
} |
| 122 |
|
| 123 |
$this->ended = true; |
| 124 |
} |
| 125 |
|
| 126 |
/** @internal */ |
| 127 |
public function handleDrain() |
| 128 |
{ |
| 129 |
$this->emit('drain'); |
| 130 |
} |
| 131 |
|
| 132 |
/** @internal */ |
| 133 |
public function handleData($data) |
| 134 |
{ |
| 135 |
$this->buffer .= $data; |
| 136 |
|
| 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")) { |
| 139 |
try { |
| 140 |
list($response, $bodyChunk) = $this->parseResponse($this->buffer); |
| 141 |
} catch (\InvalidArgumentException $exception) { |
| 142 |
$this->emit('error', array($exception)); |
| 143 |
} |
| 144 |
|
| 145 |
$this->buffer = null; |
| 146 |
|
| 147 |
$this->stream->removeListener('drain', array($this, 'handleDrain')); |
| 148 |
$this->stream->removeListener('data', array($this, 'handleData')); |
| 149 |
$this->stream->removeListener('end', array($this, 'handleEnd')); |
| 150 |
$this->stream->removeListener('error', array($this, 'handleError')); |
| 151 |
$this->stream->removeListener('close', array($this, 'handleClose')); |
| 152 |
|
| 153 |
if (!isset($response)) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
$response->on('close', array($this, 'close')); |
| 158 |
$that = $this; |
| 159 |
$response->on('error', function (\Exception $error) use ($that) { |
| 160 |
$that->closeError(new \RuntimeException( |
| 161 |
"An error occured in the response", |
| 162 |
0, |
| 163 |
$error |
| 164 |
)); |
| 165 |
}); |
| 166 |
|
| 167 |
$this->emit('response', array($response, $this)); |
| 168 |
|
| 169 |
$this->stream->emit('data', array($bodyChunk)); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** @internal */ |
| 174 |
public function handleEnd() |
| 175 |
{ |
| 176 |
$this->closeError(new \RuntimeException( |
| 177 |
"Connection ended before receiving response" |
| 178 |
)); |
| 179 |
} |
| 180 |
|
| 181 |
/** @internal */ |
| 182 |
public function handleError(\Exception $error) |
| 183 |
{ |
| 184 |
$this->closeError(new \RuntimeException( |
| 185 |
"An error occurred in the underlying stream", |
| 186 |
0, |
| 187 |
$error |
| 188 |
)); |
| 189 |
} |
| 190 |
|
| 191 |
/** @internal */ |
| 192 |
public function handleClose() |
| 193 |
{ |
| 194 |
$this->close(); |
| 195 |
} |
| 196 |
|
| 197 |
/** @internal */ |
| 198 |
public function closeError(\Exception $error) |
| 199 |
{ |
| 200 |
if (self::STATE_END <= $this->state) { |
| 201 |
return; |
| 202 |
} |
| 203 |
$this->emit('error', array($error)); |
| 204 |
$this->close(); |
| 205 |
} |
| 206 |
|
| 207 |
public function close() |
| 208 |
{ |
| 209 |
if (self::STATE_END <= $this->state) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
$this->state = self::STATE_END; |
| 214 |
$this->pendingWrites = ''; |
| 215 |
|
| 216 |
if ($this->stream) { |
| 217 |
$this->stream->close(); |
| 218 |
} |
| 219 |
|
| 220 |
$this->emit('close'); |
| 221 |
$this->removeAllListeners(); |
| 222 |
} |
| 223 |
|
| 224 |
protected function parseResponse($data) |
| 225 |
{ |
| 226 |
$psrResponse = gPsr\parse_response($data); |
| 227 |
$headers = array_map(function($val) { |
| 228 |
if (1 === count($val)) { |
| 229 |
$val = $val[0]; |
| 230 |
} |
| 231 |
|
| 232 |
return $val; |
| 233 |
}, $psrResponse->getHeaders()); |
| 234 |
|
| 235 |
$factory = $this->getResponseFactory(); |
| 236 |
|
| 237 |
$response = $factory( |
| 238 |
'HTTP', |
| 239 |
$psrResponse->getProtocolVersion(), |
| 240 |
$psrResponse->getStatusCode(), |
| 241 |
$psrResponse->getReasonPhrase(), |
| 242 |
$headers |
| 243 |
); |
| 244 |
|
| 245 |
return array($response, (string)($psrResponse->getBody())); |
| 246 |
} |
| 247 |
|
| 248 |
protected function connect() |
| 249 |
{ |
| 250 |
$scheme = $this->requestData->getScheme(); |
| 251 |
if ($scheme !== 'https' && $scheme !== 'http') { |
| 252 |
return Promise\reject( |
| 253 |
new \InvalidArgumentException('Invalid request URL given') |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
$host = $this->requestData->getHost(); |
| 258 |
$port = $this->requestData->getPort(); |
| 259 |
|
| 260 |
if ($scheme === 'https') { |
| 261 |
$host = 'tls://' . $host; |
| 262 |
} |
| 263 |
|
| 264 |
return $this->connector |
| 265 |
->connect($host . ':' . $port); |
| 266 |
} |
| 267 |
|
| 268 |
public function setResponseFactory($factory) |
| 269 |
{ |
| 270 |
$this->responseFactory = $factory; |
| 271 |
} |
| 272 |
|
| 273 |
public function getResponseFactory() |
| 274 |
{ |
| 275 |
if (null === $factory = $this->responseFactory) { |
| 276 |
$stream = $this->stream; |
| 277 |
|
| 278 |
$factory = function ($protocol, $version, $code, $reasonPhrase, $headers) use ($stream) { |
| 279 |
return new Response( |
| 280 |
$stream, |
| 281 |
$protocol, |
| 282 |
$version, |
| 283 |
$code, |
| 284 |
$reasonPhrase, |
| 285 |
$headers |
| 286 |
); |
| 287 |
}; |
| 288 |
|
| 289 |
$this->responseFactory = $factory; |
| 290 |
} |
| 291 |
|
| 292 |
return $factory; |
| 293 |
} |
| 294 |
} |