Debian Codegraph @

vendor/guzzlehttp/guzzle/src/Handler/EasyHandle.php

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