2 namespace GuzzleHttp\Handler;
4 use GuzzleHttp\Psr7\Response;
5 use Psr\Http\Message\RequestInterface;
6 use Psr\Http\Message\ResponseInterface;
7 use Psr\Http\Message\StreamInterface;
10 * Represents a cURL easy handle and the data it populates.
14 final class EasyHandle
16 /** @var resource cURL resource */
19 /** @var StreamInterface Where data is being written */
22 /** @var array Received HTTP headers so far */
25 /** @var ResponseInterface Received response (if any) */
28 /** @var RequestInterface Request being sent */
31 /** @var array Request options */
34 /** @var int cURL error number (if any) */
37 /** @var \Exception Exception during on_headers (if any) */
38 public $onHeadersException;
41 * Attach a response to the easy handle based on the received headers.
43 * @throws \RuntimeException if no headers have been received.
45 public function createResponse()
47 if (empty($this->headers)) {
48 throw new \RuntimeException('No headers have been received');
51 // HTTP-version SP status-code SP reason-phrase
52 $startLine = explode(' ', array_shift($this->headers), 3);
53 $headers = \GuzzleHttp\headers_from_lines($this->headers);
54 $normalizedKeys = \GuzzleHttp\normalize_header_keys($headers);
56 if (!empty($this->options['decode_content'])
57 && isset($normalizedKeys['content-encoding'])
59 $headers['x-encoded-content-encoding']
60 = $headers[$normalizedKeys['content-encoding']];
61 unset($headers[$normalizedKeys['content-encoding']]);
62 if (isset($normalizedKeys['content-length'])) {
63 $headers['x-encoded-content-length']
64 = $headers[$normalizedKeys['content-length']];
66 $bodyLength = (int) $this->sink->getSize();
68 $headers[$normalizedKeys['content-length']] = $bodyLength;
70 unset($headers[$normalizedKeys['content-length']]);
75 // Attach a response to the easy handle with the parsed headers.
76 $this->response = new Response(
80 substr($startLine[0], 5),
81 isset($startLine[2]) ? (string) $startLine[2] : null
85 public function __get($name)
87 $msg = $name === 'handle'
88 ? 'The EasyHandle has been released'
89 : 'Invalid property: ' . $name;
90 throw new \BadMethodCallException($msg);