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