Debian Codegraph @

vendor/react/http-client/src/Response.php

raw
1 <?php
2
3 namespace React\HttpClient;
4
5 use Evenement\EventEmitter;
6 use React\Stream\ReadableStreamInterface;
7 use React\Stream\Util;
8 use React\Stream\WritableStreamInterface;
9
10 /**
11 * @event data ($bodyChunk)
12 * @event error
13 * @event end
14 */
15 class Response extends EventEmitter implements ReadableStreamInterface
16 {
17 private $stream;
18 private $protocol;
19 private $version;
20 private $code;
21 private $reasonPhrase;
22 private $headers;
23 private $readable = true;
24
25 public function __construct(ReadableStreamInterface $stream, $protocol, $version, $code, $reasonPhrase, $headers)
26 {
27 $this->stream = $stream;
28 $this->protocol = $protocol;
29 $this->version = $version;
30 $this->code = $code;
31 $this->reasonPhrase = $reasonPhrase;
32 $this->headers = $headers;
33
34 if (strtolower($this->getHeaderLine('Transfer-Encoding')) === 'chunked') {
35 $this->stream = new ChunkedStreamDecoder($stream);
36 $this->removeHeader('Transfer-Encoding');
37 }
38
39 $this->stream->on('data', array($this, 'handleData'));
40 $this->stream->on('error', array($this, 'handleError'));
41 $this->stream->on('end', array($this, 'handleEnd'));
42 $this->stream->on('close', array($this, 'handleClose'));
43 }
44
45 public function getProtocol()
46 {
47 return $this->protocol;
48 }
49
50 public function getVersion()
51 {
52 return $this->version;
53 }
54
55 public function getCode()
56 {
57 return $this->code;
58 }
59
60 public function getReasonPhrase()
61 {
62 return $this->reasonPhrase;
63 }
64
65 public function getHeaders()
66 {
67 return $this->headers;
68 }
69
70 private function removeHeader($name)
71 {
72 foreach ($this->headers as $key => $value) {
73 if (strcasecmp($name, $key) === 0) {
74 unset($this->headers[$key]);
75 break;
76 }
77 }
78 }
79
80 private function getHeader($name)
81 {
82 $name = strtolower($name);
83 $normalized = array_change_key_case($this->headers, CASE_LOWER);
84
85 return isset($normalized[$name]) ? (array)$normalized[$name] : array();
86 }
87
88 private function getHeaderLine($name)
89 {
90 return implode(', ' , $this->getHeader($name));
91 }
92
93 /** @internal */
94 public function handleData($data)
95 {
96 if ($this->readable) {
97 $this->emit('data', array($data));
98 }
99 }
100
101 /** @internal */
102 public function handleEnd()
103 {
104 if (!$this->readable) {
105 return;
106 }
107 $this->emit('end');
108 $this->close();
109 }
110
111 /** @internal */
112 public function handleError(\Exception $error)
113 {
114 if (!$this->readable) {
115 return;
116 }
117 $this->emit('error', array(new \RuntimeException(
118 "An error occurred in the underlying stream",
119 0,
120 $error
121 )));
122
123 $this->close();
124 }
125
126 /** @internal */
127 public function handleClose()
128 {
129 $this->close();
130 }
131
132 public function close()
133 {
134 if (!$this->readable) {
135 return;
136 }
137
138 $this->readable = false;
139 $this->stream->close();
140
141 $this->emit('close');
142 $this->removeAllListeners();
143 }
144
145 public function isReadable()
146 {
147 return $this->readable;
148 }
149
150 public function pause()
151 {
152 if (!$this->readable) {
153 return;
154 }
155
156 $this->stream->pause();
157 }
158
159 public function resume()
160 {
161 if (!$this->readable) {
162 return;
163 }
164
165 $this->stream->resume();
166 }
167
168 public function pipe(WritableStreamInterface $dest, array $options = array())
169 {
170 Util::pipe($this, $dest, $options);
171
172 return $dest;
173 }
174 }