| [01a620f] | 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Handler;
|
|---|
| 3 |
|
|---|
| 4 | use GuzzleHttp\Psr7\Response;
|
|---|
| 5 | use Psr\Http\Message\RequestInterface;
|
|---|
| 6 | use Psr\Http\Message\ResponseInterface;
|
|---|
| 7 | use Psr\Http\Message\StreamInterface;
|
|---|
| 8 |
|
|---|
| 9 | /**
|
|---|
| 10 | * Represents a cURL easy handle and the data it populates.
|
|---|
| 11 | *
|
|---|
| 12 | * @internal
|
|---|
| 13 | */
|
|---|
| 14 | final class EasyHandle
|
|---|
| 15 | {
|
|---|
| 16 | /** @var resource cURL resource */
|
|---|
| 17 | public $handle;
|
|---|
| 18 |
|
|---|
| 19 | /** @var StreamInterface Where data is being written */
|
|---|
| 20 | public $sink;
|
|---|
| 21 |
|
|---|
| 22 | /** @var array Received HTTP headers so far */
|
|---|
| 23 | public $headers = [];
|
|---|
| 24 |
|
|---|
| 25 | /** @var ResponseInterface Received response (if any) */
|
|---|
| 26 | public $response;
|
|---|
| 27 |
|
|---|
| 28 | /** @var RequestInterface Request being sent */
|
|---|
| 29 | public $request;
|
|---|
| 30 |
|
|---|
| 31 | /** @var array Request options */
|
|---|
| 32 | public $options = [];
|
|---|
| 33 |
|
|---|
| 34 | /** @var int cURL error number (if any) */
|
|---|
| 35 | public $errno = 0;
|
|---|
| 36 |
|
|---|
| 37 | /** @var \Exception Exception during on_headers (if any) */
|
|---|
| 38 | public $onHeadersException;
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Attach a response to the easy handle based on the received headers.
|
|---|
| 42 | *
|
|---|
| 43 | * @throws \RuntimeException if no headers have been received.
|
|---|
| 44 | */
|
|---|
| 45 | public function createResponse()
|
|---|
| 46 | {
|
|---|
| 47 | if (empty($this->headers)) {
|
|---|
| 48 | throw new \RuntimeException('No headers have been received');
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | // HTTP-version SP status-code SP reason-phrase
|
|---|
| 52 | $startLine = explode(' ', array_shift($this->headers), 3);
|
|---|
| 53 | $headers = \GuzzleHttp\headers_from_lines($this->headers);
|
|---|
| 54 | $normalizedKeys = \GuzzleHttp\normalize_header_keys($headers);
|
|---|
| 55 |
|
|---|
| 56 | if (!empty($this->options['decode_content'])
|
|---|
| 57 | && isset($normalizedKeys['content-encoding'])
|
|---|
| 58 | ) {
|
|---|
| 59 | unset($headers[$normalizedKeys['content-encoding']]);
|
|---|
| 60 | if (isset($normalizedKeys['content-length'])) {
|
|---|
| 61 | $bodyLength = (int) $this->sink->getSize();
|
|---|
| 62 | if ($bodyLength) {
|
|---|
| 63 | $headers[$normalizedKeys['content-length']] = $bodyLength;
|
|---|
| 64 | } else {
|
|---|
| 65 | unset($headers[$normalizedKeys['content-length']]);
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | // Attach a response to the easy handle with the parsed headers.
|
|---|
| 71 | $this->response = new Response(
|
|---|
| 72 | $startLine[1],
|
|---|
| 73 | $headers,
|
|---|
| 74 | $this->sink,
|
|---|
| 75 | substr($startLine[0], 5),
|
|---|
| 76 | isset($startLine[2]) ? (int) $startLine[2] : null
|
|---|
| 77 | );
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | public function __get($name)
|
|---|
| 81 | {
|
|---|
| 82 | $msg = $name === 'handle'
|
|---|
| 83 | ? 'The EasyHandle has been released'
|
|---|
| 84 | : 'Invalid property: ' . $name;
|
|---|
| 85 | throw new \BadMethodCallException($msg);
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|