| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Handler; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Response; |
| 6 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 7 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface; |
| 8 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 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 |
/** @var StreamInterface Where data is being written */ |
| 19 |
public $sink; |
| 20 |
/** @var array Received HTTP headers so far */ |
| 21 |
public $headers = []; |
| 22 |
/** @var ResponseInterface Received response (if any) */ |
| 23 |
public $response; |
| 24 |
/** @var RequestInterface Request being sent */ |
| 25 |
public $request; |
| 26 |
/** @var array Request options */ |
| 27 |
public $options = []; |
| 28 |
/** @var int cURL error number (if any) */ |
| 29 |
public $errno = 0; |
| 30 |
/** @var \Exception Exception during on_headers (if any) */ |
| 31 |
public $onHeadersException; |
| 32 |
/** |
| 33 |
* Attach a response to the easy handle based on the received headers. |
| 34 |
* |
| 35 |
* @throws \RuntimeException if no headers have been received. |
| 36 |
*/ |
| 37 |
public function createResponse() |
| 38 |
{ |
| 39 |
if (empty($this->headers)) { |
| 40 |
throw new \RuntimeException('No headers have been received'); |
| 41 |
} |
| 42 |
// HTTP-version SP status-code SP reason-phrase |
| 43 |
$startLine = \explode(' ', \array_shift($this->headers), 3); |
| 44 |
$headers = \Dudlewebs\WPMCS\s3\GuzzleHttp\headers_from_lines($this->headers); |
| 45 |
$normalizedKeys = \Dudlewebs\WPMCS\s3\GuzzleHttp\normalize_header_keys($headers); |
| 46 |
if (!empty($this->options['decode_content']) && isset($normalizedKeys['content-encoding'])) { |
| 47 |
$headers['x-encoded-content-encoding'] = $headers[$normalizedKeys['content-encoding']]; |
| 48 |
unset($headers[$normalizedKeys['content-encoding']]); |
| 49 |
if (isset($normalizedKeys['content-length'])) { |
| 50 |
$headers['x-encoded-content-length'] = $headers[$normalizedKeys['content-length']]; |
| 51 |
$bodyLength = (int) $this->sink->getSize(); |
| 52 |
if ($bodyLength) { |
| 53 |
$headers[$normalizedKeys['content-length']] = $bodyLength; |
| 54 |
} else { |
| 55 |
unset($headers[$normalizedKeys['content-length']]); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
// Attach a response to the easy handle with the parsed headers. |
| 60 |
$this->response = new Response($startLine[1], $headers, $this->sink, \substr($startLine[0], 5), isset($startLine[2]) ? (string) $startLine[2] : null); |
| 61 |
} |
| 62 |
public function __get($name) |
| 63 |
{ |
| 64 |
$msg = $name === 'handle' ? 'The EasyHandle has been released' : 'Invalid property: ' . $name; |
| 65 |
throw new \BadMethodCallException($msg); |
| 66 |
} |
| 67 |
} |
| 68 |
|