1 | <?php
|
---|
2 | /**
|
---|
3 | * Class representing a HTTP response
|
---|
4 | *
|
---|
5 | * PHP version 5
|
---|
6 | *
|
---|
7 | * LICENSE:
|
---|
8 | *
|
---|
9 | * Copyright (c) 2008-2012, Alexey Borzov <avb@php.net>
|
---|
10 | * All rights reserved.
|
---|
11 | *
|
---|
12 | * Redistribution and use in source and binary forms, with or without
|
---|
13 | * modification, are permitted provided that the following conditions
|
---|
14 | * are met:
|
---|
15 | *
|
---|
16 | * * Redistributions of source code must retain the above copyright
|
---|
17 | * notice, this list of conditions and the following disclaimer.
|
---|
18 | * * Redistributions in binary form must reproduce the above copyright
|
---|
19 | * notice, this list of conditions and the following disclaimer in the
|
---|
20 | * documentation and/or other materials provided with the distribution.
|
---|
21 | * * The names of the authors may not be used to endorse or promote products
|
---|
22 | * derived from this software without specific prior written permission.
|
---|
23 | *
|
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
---|
25 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
26 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
27 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
---|
28 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
30 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
31 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
32 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
35 | *
|
---|
36 | * @category HTTP
|
---|
37 | * @package HTTP_Request2
|
---|
38 | * @author Alexey Borzov <avb@php.net>
|
---|
39 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
---|
40 | * @version SVN: $Id: Response.php 324936 2012-04-07 07:49:03Z avb $
|
---|
41 | * @link http://pear.php.net/package/HTTP_Request2
|
---|
42 | */
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Exception class for HTTP_Request2 package
|
---|
46 | */
|
---|
47 | require_once 'HTTP/Request2/Exception.php';
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Class representing a HTTP response
|
---|
51 | *
|
---|
52 | * The class is designed to be used in "streaming" scenario, building the
|
---|
53 | * response as it is being received:
|
---|
54 | * <code>
|
---|
55 | * $statusLine = read_status_line();
|
---|
56 | * $response = new HTTP_Request2_Response($statusLine);
|
---|
57 | * do {
|
---|
58 | * $headerLine = read_header_line();
|
---|
59 | * $response->parseHeaderLine($headerLine);
|
---|
60 | * } while ($headerLine != '');
|
---|
61 | *
|
---|
62 | * while ($chunk = read_body()) {
|
---|
63 | * $response->appendBody($chunk);
|
---|
64 | * }
|
---|
65 | *
|
---|
66 | * var_dump($response->getHeader(), $response->getCookies(), $response->getBody());
|
---|
67 | * </code>
|
---|
68 | *
|
---|
69 | * @category HTTP
|
---|
70 | * @package HTTP_Request2
|
---|
71 | * @author Alexey Borzov <avb@php.net>
|
---|
72 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
---|
73 | * @version Release: 2.1.1
|
---|
74 | * @link http://pear.php.net/package/HTTP_Request2
|
---|
75 | * @link http://tools.ietf.org/html/rfc2616#section-6
|
---|
76 | */
|
---|
77 | class HTTP_Request2_Response
|
---|
78 | {
|
---|
79 | /**
|
---|
80 | * HTTP protocol version (e.g. 1.0, 1.1)
|
---|
81 | * @var string
|
---|
82 | */
|
---|
83 | protected $version;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Status code
|
---|
87 | * @var integer
|
---|
88 | * @link http://tools.ietf.org/html/rfc2616#section-6.1.1
|
---|
89 | */
|
---|
90 | protected $code;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Reason phrase
|
---|
94 | * @var string
|
---|
95 | * @link http://tools.ietf.org/html/rfc2616#section-6.1.1
|
---|
96 | */
|
---|
97 | protected $reasonPhrase;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Effective URL (may be different from original request URL in case of redirects)
|
---|
101 | * @var string
|
---|
102 | */
|
---|
103 | protected $effectiveUrl;
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Associative array of response headers
|
---|
107 | * @var array
|
---|
108 | */
|
---|
109 | protected $headers = array();
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Cookies set in the response
|
---|
113 | * @var array
|
---|
114 | */
|
---|
115 | protected $cookies = array();
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Name of last header processed by parseHederLine()
|
---|
119 | *
|
---|
120 | * Used to handle the headers that span multiple lines
|
---|
121 | *
|
---|
122 | * @var string
|
---|
123 | */
|
---|
124 | protected $lastHeader = null;
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Response body
|
---|
128 | * @var string
|
---|
129 | */
|
---|
130 | protected $body = '';
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Whether the body is still encoded by Content-Encoding
|
---|
134 | *
|
---|
135 | * cURL provides the decoded body to the callback; if we are reading from
|
---|
136 | * socket the body is still gzipped / deflated
|
---|
137 | *
|
---|
138 | * @var bool
|
---|
139 | */
|
---|
140 | protected $bodyEncoded;
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Associative array of HTTP status code / reason phrase.
|
---|
144 | *
|
---|
145 | * @var array
|
---|
146 | * @link http://tools.ietf.org/html/rfc2616#section-10
|
---|
147 | */
|
---|
148 | protected static $phrases = array(
|
---|
149 |
|
---|
150 | // 1xx: Informational - Request received, continuing process
|
---|
151 | 100 => 'Continue',
|
---|
152 | 101 => 'Switching Protocols',
|
---|
153 |
|
---|
154 | // 2xx: Success - The action was successfully received, understood and
|
---|
155 | // accepted
|
---|
156 | 200 => 'OK',
|
---|
157 | 201 => 'Created',
|
---|
158 | 202 => 'Accepted',
|
---|
159 | 203 => 'Non-Authoritative Information',
|
---|
160 | 204 => 'No Content',
|
---|
161 | 205 => 'Reset Content',
|
---|
162 | 206 => 'Partial Content',
|
---|
163 |
|
---|
164 | // 3xx: Redirection - Further action must be taken in order to complete
|
---|
165 | // the request
|
---|
166 | 300 => 'Multiple Choices',
|
---|
167 | 301 => 'Moved Permanently',
|
---|
168 | 302 => 'Found', // 1.1
|
---|
169 | 303 => 'See Other',
|
---|
170 | 304 => 'Not Modified',
|
---|
171 | 305 => 'Use Proxy',
|
---|
172 | 307 => 'Temporary Redirect',
|
---|
173 |
|
---|
174 | // 4xx: Client Error - The request contains bad syntax or cannot be
|
---|
175 | // fulfilled
|
---|
176 | 400 => 'Bad Request',
|
---|
177 | 401 => 'Unauthorized',
|
---|
178 | 402 => 'Payment Required',
|
---|
179 | 403 => 'Forbidden',
|
---|
180 | 404 => 'Not Found',
|
---|
181 | 405 => 'Method Not Allowed',
|
---|
182 | 406 => 'Not Acceptable',
|
---|
183 | 407 => 'Proxy Authentication Required',
|
---|
184 | 408 => 'Request Timeout',
|
---|
185 | 409 => 'Conflict',
|
---|
186 | 410 => 'Gone',
|
---|
187 | 411 => 'Length Required',
|
---|
188 | 412 => 'Precondition Failed',
|
---|
189 | 413 => 'Request Entity Too Large',
|
---|
190 | 414 => 'Request-URI Too Long',
|
---|
191 | 415 => 'Unsupported Media Type',
|
---|
192 | 416 => 'Requested Range Not Satisfiable',
|
---|
193 | 417 => 'Expectation Failed',
|
---|
194 |
|
---|
195 | // 5xx: Server Error - The server failed to fulfill an apparently
|
---|
196 | // valid request
|
---|
197 | 500 => 'Internal Server Error',
|
---|
198 | 501 => 'Not Implemented',
|
---|
199 | 502 => 'Bad Gateway',
|
---|
200 | 503 => 'Service Unavailable',
|
---|
201 | 504 => 'Gateway Timeout',
|
---|
202 | 505 => 'HTTP Version Not Supported',
|
---|
203 | 509 => 'Bandwidth Limit Exceeded',
|
---|
204 |
|
---|
205 | );
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Returns the default reason phrase for the given code or all reason phrases
|
---|
209 | *
|
---|
210 | * @param int $code Response code
|
---|
211 | *
|
---|
212 | * @return string|array|null Default reason phrase for $code if $code is given
|
---|
213 | * (null if no phrase is available), array of all
|
---|
214 | * reason phrases if $code is null
|
---|
215 | * @link http://pear.php.net/bugs/18716
|
---|
216 | */
|
---|
217 | public static function getDefaultReasonPhrase($code = null)
|
---|
218 | {
|
---|
219 | if (null === $code) {
|
---|
220 | return self::$phrases;
|
---|
221 | } else {
|
---|
222 | return isset(self::$phrases[$code]) ? self::$phrases[$code] : null;
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Constructor, parses the response status line
|
---|
228 | *
|
---|
229 | * @param string $statusLine Response status line (e.g. "HTTP/1.1 200 OK")
|
---|
230 | * @param bool $bodyEncoded Whether body is still encoded by Content-Encoding
|
---|
231 | * @param string $effectiveUrl Effective URL of the response
|
---|
232 | *
|
---|
233 | * @throws HTTP_Request2_MessageException if status line is invalid according to spec
|
---|
234 | */
|
---|
235 | public function __construct($statusLine, $bodyEncoded = true, $effectiveUrl = null)
|
---|
236 | {
|
---|
237 | if (!preg_match('!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) {
|
---|
238 | throw new HTTP_Request2_MessageException(
|
---|
239 | "Malformed response: {$statusLine}",
|
---|
240 | HTTP_Request2_Exception::MALFORMED_RESPONSE
|
---|
241 | );
|
---|
242 | }
|
---|
243 | $this->version = $m[1];
|
---|
244 | $this->code = intval($m[2]);
|
---|
245 | $this->reasonPhrase = !empty($m[3]) ? trim($m[3]) : self::getDefaultReasonPhrase($this->code);
|
---|
246 | $this->bodyEncoded = (bool)$bodyEncoded;
|
---|
247 | $this->effectiveUrl = (string)$effectiveUrl;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Parses the line from HTTP response filling $headers array
|
---|
252 | *
|
---|
253 | * The method should be called after reading the line from socket or receiving
|
---|
254 | * it into cURL callback. Passing an empty string here indicates the end of
|
---|
255 | * response headers and triggers additional processing, so be sure to pass an
|
---|
256 | * empty string in the end.
|
---|
257 | *
|
---|
258 | * @param string $headerLine Line from HTTP response
|
---|
259 | */
|
---|
260 | public function parseHeaderLine($headerLine)
|
---|
261 | {
|
---|
262 | $headerLine = trim($headerLine, "\r\n");
|
---|
263 |
|
---|
264 | if ('' == $headerLine) {
|
---|
265 | // empty string signals the end of headers, process the received ones
|
---|
266 | if (!empty($this->headers['set-cookie'])) {
|
---|
267 | $cookies = is_array($this->headers['set-cookie'])?
|
---|
268 | $this->headers['set-cookie']:
|
---|
269 | array($this->headers['set-cookie']);
|
---|
270 | foreach ($cookies as $cookieString) {
|
---|
271 | $this->parseCookie($cookieString);
|
---|
272 | }
|
---|
273 | unset($this->headers['set-cookie']);
|
---|
274 | }
|
---|
275 | foreach (array_keys($this->headers) as $k) {
|
---|
276 | if (is_array($this->headers[$k])) {
|
---|
277 | $this->headers[$k] = implode(', ', $this->headers[$k]);
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | } elseif (preg_match('!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) {
|
---|
282 | // string of the form header-name: header value
|
---|
283 | $name = strtolower($m[1]);
|
---|
284 | $value = trim($m[2]);
|
---|
285 | if (empty($this->headers[$name])) {
|
---|
286 | $this->headers[$name] = $value;
|
---|
287 | } else {
|
---|
288 | if (!is_array($this->headers[$name])) {
|
---|
289 | $this->headers[$name] = array($this->headers[$name]);
|
---|
290 | }
|
---|
291 | $this->headers[$name][] = $value;
|
---|
292 | }
|
---|
293 | $this->lastHeader = $name;
|
---|
294 |
|
---|
295 | } elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) {
|
---|
296 | // continuation of a previous header
|
---|
297 | if (!is_array($this->headers[$this->lastHeader])) {
|
---|
298 | $this->headers[$this->lastHeader] .= ' ' . trim($m[1]);
|
---|
299 | } else {
|
---|
300 | $key = count($this->headers[$this->lastHeader]) - 1;
|
---|
301 | $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Parses a Set-Cookie header to fill $cookies array
|
---|
308 | *
|
---|
309 | * @param string $cookieString value of Set-Cookie header
|
---|
310 | *
|
---|
311 | * @link http://web.archive.org/web/20080331104521/http://cgi.netscape.com/newsref/std/cookie_spec.html
|
---|
312 | */
|
---|
313 | protected function parseCookie($cookieString)
|
---|
314 | {
|
---|
315 | $cookie = array(
|
---|
316 | 'expires' => null,
|
---|
317 | 'domain' => null,
|
---|
318 | 'path' => null,
|
---|
319 | 'secure' => false
|
---|
320 | );
|
---|
321 |
|
---|
322 | if (!strpos($cookieString, ';')) {
|
---|
323 | // Only a name=value pair
|
---|
324 | $pos = strpos($cookieString, '=');
|
---|
325 | $cookie['name'] = trim(substr($cookieString, 0, $pos));
|
---|
326 | $cookie['value'] = trim(substr($cookieString, $pos + 1));
|
---|
327 |
|
---|
328 | } else {
|
---|
329 | // Some optional parameters are supplied
|
---|
330 | $elements = explode(';', $cookieString);
|
---|
331 | $pos = strpos($elements[0], '=');
|
---|
332 | $cookie['name'] = trim(substr($elements[0], 0, $pos));
|
---|
333 | $cookie['value'] = trim(substr($elements[0], $pos + 1));
|
---|
334 |
|
---|
335 | for ($i = 1; $i < count($elements); $i++) {
|
---|
336 | if (false === strpos($elements[$i], '=')) {
|
---|
337 | $elName = trim($elements[$i]);
|
---|
338 | $elValue = null;
|
---|
339 | } else {
|
---|
340 | list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i]));
|
---|
341 | }
|
---|
342 | $elName = strtolower($elName);
|
---|
343 | if ('secure' == $elName) {
|
---|
344 | $cookie['secure'] = true;
|
---|
345 | } elseif ('expires' == $elName) {
|
---|
346 | $cookie['expires'] = str_replace('"', '', $elValue);
|
---|
347 | } elseif ('path' == $elName || 'domain' == $elName) {
|
---|
348 | $cookie[$elName] = urldecode($elValue);
|
---|
349 | } else {
|
---|
350 | $cookie[$elName] = $elValue;
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 | $this->cookies[] = $cookie;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Appends a string to the response body
|
---|
359 | *
|
---|
360 | * @param string $bodyChunk part of response body
|
---|
361 | */
|
---|
362 | public function appendBody($bodyChunk)
|
---|
363 | {
|
---|
364 | $this->body .= $bodyChunk;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Returns the effective URL of the response
|
---|
369 | *
|
---|
370 | * This may be different from the request URL if redirects were followed.
|
---|
371 | *
|
---|
372 | * @return string
|
---|
373 | * @link http://pear.php.net/bugs/bug.php?id=18412
|
---|
374 | */
|
---|
375 | public function getEffectiveUrl()
|
---|
376 | {
|
---|
377 | return $this->effectiveUrl;
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Returns the status code
|
---|
382 | *
|
---|
383 | * @return integer
|
---|
384 | */
|
---|
385 | public function getStatus()
|
---|
386 | {
|
---|
387 | return $this->code;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Returns the reason phrase
|
---|
392 | *
|
---|
393 | * @return string
|
---|
394 | */
|
---|
395 | public function getReasonPhrase()
|
---|
396 | {
|
---|
397 | return $this->reasonPhrase;
|
---|
398 | }
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Whether response is a redirect that can be automatically handled by HTTP_Request2
|
---|
402 | *
|
---|
403 | * @return bool
|
---|
404 | */
|
---|
405 | public function isRedirect()
|
---|
406 | {
|
---|
407 | return in_array($this->code, array(300, 301, 302, 303, 307))
|
---|
408 | && isset($this->headers['location']);
|
---|
409 | }
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Returns either the named header or all response headers
|
---|
413 | *
|
---|
414 | * @param string $headerName Name of header to return
|
---|
415 | *
|
---|
416 | * @return string|array Value of $headerName header (null if header is
|
---|
417 | * not present), array of all response headers if
|
---|
418 | * $headerName is null
|
---|
419 | */
|
---|
420 | public function getHeader($headerName = null)
|
---|
421 | {
|
---|
422 | if (null === $headerName) {
|
---|
423 | return $this->headers;
|
---|
424 | } else {
|
---|
425 | $headerName = strtolower($headerName);
|
---|
426 | return isset($this->headers[$headerName])? $this->headers[$headerName]: null;
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Returns cookies set in response
|
---|
432 | *
|
---|
433 | * @return array
|
---|
434 | */
|
---|
435 | public function getCookies()
|
---|
436 | {
|
---|
437 | return $this->cookies;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Returns the body of the response
|
---|
442 | *
|
---|
443 | * @return string
|
---|
444 | * @throws HTTP_Request2_Exception if body cannot be decoded
|
---|
445 | */
|
---|
446 | public function getBody()
|
---|
447 | {
|
---|
448 | if (0 == strlen($this->body) || !$this->bodyEncoded
|
---|
449 | || !in_array(strtolower($this->getHeader('content-encoding')), array('gzip', 'deflate'))
|
---|
450 | ) {
|
---|
451 | return $this->body;
|
---|
452 |
|
---|
453 | } else {
|
---|
454 | if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) {
|
---|
455 | $oldEncoding = mb_internal_encoding();
|
---|
456 | mb_internal_encoding('8bit');
|
---|
457 | }
|
---|
458 |
|
---|
459 | try {
|
---|
460 | switch (strtolower($this->getHeader('content-encoding'))) {
|
---|
461 | case 'gzip':
|
---|
462 | $decoded = self::decodeGzip($this->body);
|
---|
463 | break;
|
---|
464 | case 'deflate':
|
---|
465 | $decoded = self::decodeDeflate($this->body);
|
---|
466 | }
|
---|
467 | } catch (Exception $e) {
|
---|
468 | }
|
---|
469 |
|
---|
470 | if (!empty($oldEncoding)) {
|
---|
471 | mb_internal_encoding($oldEncoding);
|
---|
472 | }
|
---|
473 | if (!empty($e)) {
|
---|
474 | throw $e;
|
---|
475 | }
|
---|
476 | return $decoded;
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | /**
|
---|
481 | * Get the HTTP version of the response
|
---|
482 | *
|
---|
483 | * @return string
|
---|
484 | */
|
---|
485 | public function getVersion()
|
---|
486 | {
|
---|
487 | return $this->version;
|
---|
488 | }
|
---|
489 |
|
---|
490 | /**
|
---|
491 | * Decodes the message-body encoded by gzip
|
---|
492 | *
|
---|
493 | * The real decoding work is done by gzinflate() built-in function, this
|
---|
494 | * method only parses the header and checks data for compliance with
|
---|
495 | * RFC 1952
|
---|
496 | *
|
---|
497 | * @param string $data gzip-encoded data
|
---|
498 | *
|
---|
499 | * @return string decoded data
|
---|
500 | * @throws HTTP_Request2_LogicException
|
---|
501 | * @throws HTTP_Request2_MessageException
|
---|
502 | * @link http://tools.ietf.org/html/rfc1952
|
---|
503 | */
|
---|
504 | public static function decodeGzip($data)
|
---|
505 | {
|
---|
506 | $length = strlen($data);
|
---|
507 | // If it doesn't look like gzip-encoded data, don't bother
|
---|
508 | if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) {
|
---|
509 | return $data;
|
---|
510 | }
|
---|
511 | if (!function_exists('gzinflate')) {
|
---|
512 | throw new HTTP_Request2_LogicException(
|
---|
513 | 'Unable to decode body: gzip extension not available',
|
---|
514 | HTTP_Request2_Exception::MISCONFIGURATION
|
---|
515 | );
|
---|
516 | }
|
---|
517 | $method = ord(substr($data, 2, 1));
|
---|
518 | if (8 != $method) {
|
---|
519 | throw new HTTP_Request2_MessageException(
|
---|
520 | 'Error parsing gzip header: unknown compression method',
|
---|
521 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
522 | );
|
---|
523 | }
|
---|
524 | $flags = ord(substr($data, 3, 1));
|
---|
525 | if ($flags & 224) {
|
---|
526 | throw new HTTP_Request2_MessageException(
|
---|
527 | 'Error parsing gzip header: reserved bits are set',
|
---|
528 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
529 | );
|
---|
530 | }
|
---|
531 |
|
---|
532 | // header is 10 bytes minimum. may be longer, though.
|
---|
533 | $headerLength = 10;
|
---|
534 | // extra fields, need to skip 'em
|
---|
535 | if ($flags & 4) {
|
---|
536 | if ($length - $headerLength - 2 < 8) {
|
---|
537 | throw new HTTP_Request2_MessageException(
|
---|
538 | 'Error parsing gzip header: data too short',
|
---|
539 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
540 | );
|
---|
541 | }
|
---|
542 | $extraLength = unpack('v', substr($data, 10, 2));
|
---|
543 | if ($length - $headerLength - 2 - $extraLength[1] < 8) {
|
---|
544 | throw new HTTP_Request2_MessageException(
|
---|
545 | 'Error parsing gzip header: data too short',
|
---|
546 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
547 | );
|
---|
548 | }
|
---|
549 | $headerLength += $extraLength[1] + 2;
|
---|
550 | }
|
---|
551 | // file name, need to skip that
|
---|
552 | if ($flags & 8) {
|
---|
553 | if ($length - $headerLength - 1 < 8) {
|
---|
554 | throw new HTTP_Request2_MessageException(
|
---|
555 | 'Error parsing gzip header: data too short',
|
---|
556 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
557 | );
|
---|
558 | }
|
---|
559 | $filenameLength = strpos(substr($data, $headerLength), chr(0));
|
---|
560 | if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) {
|
---|
561 | throw new HTTP_Request2_MessageException(
|
---|
562 | 'Error parsing gzip header: data too short',
|
---|
563 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
564 | );
|
---|
565 | }
|
---|
566 | $headerLength += $filenameLength + 1;
|
---|
567 | }
|
---|
568 | // comment, need to skip that also
|
---|
569 | if ($flags & 16) {
|
---|
570 | if ($length - $headerLength - 1 < 8) {
|
---|
571 | throw new HTTP_Request2_MessageException(
|
---|
572 | 'Error parsing gzip header: data too short',
|
---|
573 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
574 | );
|
---|
575 | }
|
---|
576 | $commentLength = strpos(substr($data, $headerLength), chr(0));
|
---|
577 | if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) {
|
---|
578 | throw new HTTP_Request2_MessageException(
|
---|
579 | 'Error parsing gzip header: data too short',
|
---|
580 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
581 | );
|
---|
582 | }
|
---|
583 | $headerLength += $commentLength + 1;
|
---|
584 | }
|
---|
585 | // have a CRC for header. let's check
|
---|
586 | if ($flags & 2) {
|
---|
587 | if ($length - $headerLength - 2 < 8) {
|
---|
588 | throw new HTTP_Request2_MessageException(
|
---|
589 | 'Error parsing gzip header: data too short',
|
---|
590 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
591 | );
|
---|
592 | }
|
---|
593 | $crcReal = 0xffff & crc32(substr($data, 0, $headerLength));
|
---|
594 | $crcStored = unpack('v', substr($data, $headerLength, 2));
|
---|
595 | if ($crcReal != $crcStored[1]) {
|
---|
596 | throw new HTTP_Request2_MessageException(
|
---|
597 | 'Header CRC check failed',
|
---|
598 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
599 | );
|
---|
600 | }
|
---|
601 | $headerLength += 2;
|
---|
602 | }
|
---|
603 | // unpacked data CRC and size at the end of encoded data
|
---|
604 | $tmp = unpack('V2', substr($data, -8));
|
---|
605 | $dataCrc = $tmp[1];
|
---|
606 | $dataSize = $tmp[2];
|
---|
607 |
|
---|
608 | // finally, call the gzinflate() function
|
---|
609 | // don't pass $dataSize to gzinflate, see bugs #13135, #14370
|
---|
610 | $unpacked = gzinflate(substr($data, $headerLength, -8));
|
---|
611 | if (false === $unpacked) {
|
---|
612 | throw new HTTP_Request2_MessageException(
|
---|
613 | 'gzinflate() call failed',
|
---|
614 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
615 | );
|
---|
616 | } elseif ($dataSize != strlen($unpacked)) {
|
---|
617 | throw new HTTP_Request2_MessageException(
|
---|
618 | 'Data size check failed',
|
---|
619 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
620 | );
|
---|
621 | } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) {
|
---|
622 | throw new HTTP_Request2_Exception(
|
---|
623 | 'Data CRC check failed',
|
---|
624 | HTTP_Request2_Exception::DECODE_ERROR
|
---|
625 | );
|
---|
626 | }
|
---|
627 | return $unpacked;
|
---|
628 | }
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * Decodes the message-body encoded by deflate
|
---|
632 | *
|
---|
633 | * @param string $data deflate-encoded data
|
---|
634 | *
|
---|
635 | * @return string decoded data
|
---|
636 | * @throws HTTP_Request2_LogicException
|
---|
637 | */
|
---|
638 | public static function decodeDeflate($data)
|
---|
639 | {
|
---|
640 | if (!function_exists('gzuncompress')) {
|
---|
641 | throw new HTTP_Request2_LogicException(
|
---|
642 | 'Unable to decode body: gzip extension not available',
|
---|
643 | HTTP_Request2_Exception::MISCONFIGURATION
|
---|
644 | );
|
---|
645 | }
|
---|
646 | // RFC 2616 defines 'deflate' encoding as zlib format from RFC 1950,
|
---|
647 | // while many applications send raw deflate stream from RFC 1951.
|
---|
648 | // We should check for presence of zlib header and use gzuncompress() or
|
---|
649 | // gzinflate() as needed. See bug #15305
|
---|
650 | $header = unpack('n', substr($data, 0, 2));
|
---|
651 | return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data);
|
---|
652 | }
|
---|
653 | }
|
---|
654 | ?> |
---|