1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Response;
use Zend\Http\Exception;
use Zend\Http\Response;
use Zend\Stdlib\ErrorHandler;
/**
* Represents an HTTP response message as PHP stream resource
*/
class Stream extends Response
{
/**
* The Content-Length value, if set
*
* @var int
*/
protected $contentLength;
/**
* The portion of the body that has already been streamed
*
* @var int
*/
protected $contentStreamed = 0;
/**
* Response as stream
*
* @var resource
*/
protected $stream;
/**
* The name of the file containing the stream
*
* Will be empty if stream is not file-based.
*
* @var string
*/
protected $streamName;
/**
* Should we clean up the stream file when this response is closed?
*
* @var bool
*/
protected $cleanup;
/**
* Set content length
*
* @param int $contentLength
*/
public function setContentLength($contentLength = null)
{
$this->contentLength = $contentLength;
}
/**
* Get content length
*
* @return int|null
*/
public function getContentLength()
{
return $this->contentLength;
}
/**
* Get the response as stream
*
* @return resource
*/
public function getStream()
{
return $this->stream;
}
/**
* Set the response stream
*
* @param resource $stream
* @return Stream
*/
public function setStream($stream)
{
$this->stream = $stream;
return $this;
}
/**
* Get the cleanup trigger
*
* @return bool
*/
public function getCleanup()
{
return $this->cleanup;
}
/**
* Set the cleanup trigger
*
* @param bool $cleanup
*/
public function setCleanup($cleanup = true)
{
$this->cleanup = $cleanup;
}
/**
* Get file name associated with the stream
*
* @return string
*/
public function getStreamName()
{
return $this->streamName;
}
/**
* Set file name associated with the stream
*
* @param string $streamName Name to set
* @return Stream
*/
public function setStreamName($streamName)
{
$this->streamName = $streamName;
return $this;
}
/**
* Create a new Zend\Http\Response\Stream object from a stream
*
* @param string $responseString
* @param resource $stream
* @return Stream
* @throws Exception\InvalidArgumentException
* @throws Exception\OutOfRangeException
*/
public static function fromStream($responseString, $stream)
{
if (! is_resource($stream) || get_resource_type($stream) !== 'stream') {
throw new Exception\InvalidArgumentException('A valid stream is required');
}
$headerComplete = false;
$headersString = '';
$responseArray = [];
if ($responseString) {
$responseArray = explode("\n", $responseString);
}
while (! empty($responseArray)) {
$nextLine = array_shift($responseArray);
$headersString .= $nextLine . "\n";
$nextLineTrimmed = trim($nextLine);
if ($nextLineTrimmed == '') {
$headerComplete = true;
break;
}
}
if (! $headerComplete) {
while (false !== ($nextLine = fgets($stream))) {
$headersString .= trim($nextLine) . "\r\n";
if ($nextLine == "\r\n" || $nextLine == "\n") {
$headerComplete = true;
break;
}
}
}
if (! $headerComplete) {
throw new Exception\OutOfRangeException('End of header not found');
}
/** @var Stream $response */
$response = static::fromString($headersString);
if (is_resource($stream)) {
$response->setStream($stream);
}
if (! empty($responseArray)) {
$response->content = implode("\n", $responseArray);
}
$headers = $response->getHeaders();
foreach ($headers as $header) {
if ($header instanceof \Zend\Http\Header\ContentLength) {
$response->setContentLength((int) $header->getFieldValue());
$contentLength = $response->getContentLength();
if (strlen($response->content) > $contentLength) {
throw new Exception\OutOfRangeException(sprintf(
'Too much content was extracted from the stream (%d instead of %d bytes)',
strlen($response->content),
$contentLength
));
}
break;
}
}
return $response;
}
/**
* Get the response body as string
*
* This method returns the body of the HTTP response (the content), as it
* should be in it's readable version - that is, after decoding it (if it
* was decoded), deflating it (if it was gzip compressed), etc.
*
* If you want to get the raw body (as transferred on wire) use
* $this->getRawBody() instead.
*
* @return string
*/
public function getBody()
{
if ($this->stream !== null) {
$this->readStream();
}
return parent::getBody();
}
/**
* Get the raw response body (as transferred "on wire") as string
*
* If the body is encoded (with Transfer-Encoding, not content-encoding -
* IE "chunked" body), gzip compressed, etc. it will not be decoded.
*
* @return string
*/
public function getRawBody()
{
if ($this->stream) {
$this->readStream();
}
return $this->content;
}
/**
* Read stream content and return it as string
*
* Function reads the remainder of the body from the stream and closes the stream.
*
* @return string
*/
protected function readStream()
{
$contentLength = $this->getContentLength();
if (null !== $contentLength) {
$bytes = $contentLength - $this->contentStreamed;
} else {
$bytes = -1; // Read the whole buffer
}
if (! is_resource($this->stream) || $bytes == 0) {
return '';
}
$this->content .= stream_get_contents($this->stream, $bytes);
$this->contentStreamed += strlen($this->content);
if ($this->getContentLength() == $this->contentStreamed) {
$this->stream = null;
}
}
/**
* Destructor
*/
public function __destruct()
{
if (is_resource($this->stream)) {
$this->stream = null; //Could be listened by others
}
if ($this->cleanup) {
ErrorHandler::start(E_WARNING);
unlink($this->streamName);
ErrorHandler::stop();
}
}
}