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