No Description

EasyHandle.php 2.8KB

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