Bez popisu

EasyHandle.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. unset($headers[$normalizedKeys['content-encoding']]);
  48. if (isset($normalizedKeys['content-length'])) {
  49. $bodyLength = (int) $this->sink->getSize();
  50. if ($bodyLength) {
  51. $headers[$normalizedKeys['content-length']] = $bodyLength;
  52. } else {
  53. unset($headers[$normalizedKeys['content-length']]);
  54. }
  55. }
  56. }
  57. // Attach a response to the easy handle with the parsed headers.
  58. $this->response = new Response(
  59. $startLine[1],
  60. $headers,
  61. $this->sink,
  62. substr($startLine[0], 5),
  63. isset($startLine[2]) ? (string) $startLine[2] : null
  64. );
  65. }
  66. public function __get($name)
  67. {
  68. $msg = $name === 'handle'
  69. ? 'The EasyHandle has been released'
  70. : 'Invalid property: ' . $name;
  71. throw new \BadMethodCallException($msg);
  72. }
  73. }