EasyHandle.php 2.8 KB

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