vendor/guzzlehttp/guzzle/src/Handler/EasyHandle.php
138 lines · 3636 bytes
1<?php
2
3namespace GuzzleHttp\Handler;
4
5use GuzzleHttp\Psr7\Response;
6use GuzzleHttp\Utils;
7use Psr\Http\Message\RequestInterface;
8use Psr\Http\Message\ResponseInterface;
9use Psr\Http\Message\StreamInterface;
10
11/**
12 * Represents a cURL easy handle and the data it populates.
13 *
14 * @internal
15 */
16final class EasyHandle
17{
18 /**
19 * @var resource|\CurlHandle cURL resource
20 */
21 public $handle;
22
23 /**
24 * @var StreamInterface Where data is being written
25 */
26 public $sink;
27
28 /**
29 * @var array Received HTTP headers so far
30 */
31 public $headers = [];
32
33 /**
34 * @var array Valid trailer lines, retained only when an on_trailers
35 * callback is configured
36 */
37 public $trailers = [];
38
39 /**
40 * @var bool Whether this handle was configured with CURLOPT_PIPEWAIT
41 */
42 public $usesPipewait = false;
43
44 /**
45 * @var ResponseInterface|null Received response (if any)
46 */
47 public $response;
48
49 /**
50 * @var RequestInterface Request being sent
51 */
52 public $request;
53
54 /**
55 * @var array Request options
56 */
57 public $options = [];
58
59 /**
60 * @var int cURL error number (if any)
61 */
62 public $errno = 0;
63
64 /**
65 * @var string|null Effective CURLOPT_PROXY value the handle was created with (if any)
66 */
67 public $effectiveProxy;
68
69 /**
70 * Proxy tunnel or SOCKS proxy section signature for connection-reuse
71 * isolation, or null when the request does not require sectioning.
72 *
73 * @var string|null
74 */
75 public $proxyTunnelSignature;
76
77 /**
78 * @var \Throwable|null Exception during on_headers (if any)
79 */
80 public $onHeadersException;
81
82 /**
83 * @var \Throwable|null Exception during createResponse (if any)
84 */
85 public $createResponseException;
86
87 /**
88 * Attach a response to the easy handle based on the received headers.
89 *
90 * @throws \RuntimeException if no headers have been received or the first
91 * header line is invalid.
92 */
93 public function createResponse(): void
94 {
95 $this->response = null;
96
97 [$ver, $status, $reason, $headers] = HeaderProcessor::parseHeaders($this->headers);
98
99 $normalizedKeys = Utils::normalizeHeaderKeys($headers);
100
101 if (isset($this->options['decode_content']) && $this->options['decode_content'] !== false && isset($normalizedKeys['content-encoding'])) {
102 $headers['x-encoded-content-encoding'] = $headers[$normalizedKeys['content-encoding']];
103 unset($headers[$normalizedKeys['content-encoding']]);
104 if (isset($normalizedKeys['content-length'])) {
105 $headers['x-encoded-content-length'] = $headers[$normalizedKeys['content-length']];
106
107 $bodyLength = (int) $this->sink->getSize();
108 if ($bodyLength) {
109 $headers[$normalizedKeys['content-length']] = [(string) $bodyLength];
110 } else {
111 unset($headers[$normalizedKeys['content-length']]);
112 }
113 }
114 }
115
116 // Attach a response to the easy handle with the parsed headers.
117 $this->response = new Response(
118 $status,
119 $headers,
120 $this->sink,
121 $ver,
122 $reason
123 );
124 }
125
126 /**
127 * @param string $name
128 *
129 * @return void
130 *
131 * @throws \BadMethodCallException
132 */
133 public function __get($name)
134 {
135 $msg = $name === 'handle' ? 'The EasyHandle has been released' : 'Invalid property: '.$name;
136 throw new \BadMethodCallException($msg);
137 }
138}