24 require_once
'HTTP/Request2/Exception.php';
129 101 =>
'Switching Protocols',
136 203 =>
'Non-Authoritative Information',
138 205 =>
'Reset Content',
139 206 =>
'Partial Content',
143 300 =>
'Multiple Choices',
144 301 =>
'Moved Permanently',
147 304 =>
'Not Modified',
149 307 =>
'Temporary Redirect',
153 400 =>
'Bad Request',
154 401 =>
'Unauthorized',
155 402 =>
'Payment Required',
158 405 =>
'Method Not Allowed',
159 406 =>
'Not Acceptable',
160 407 =>
'Proxy Authentication Required',
161 408 =>
'Request Timeout',
164 411 =>
'Length Required',
165 412 =>
'Precondition Failed',
166 413 =>
'Request Entity Too Large',
167 414 =>
'Request-URI Too Long',
168 415 =>
'Unsupported Media Type',
169 416 =>
'Requested Range Not Satisfiable',
170 417 =>
'Expectation Failed',
174 500 =>
'Internal Server Error',
175 501 =>
'Not Implemented',
176 502 =>
'Bad Gateway',
177 503 =>
'Service Unavailable',
178 504 =>
'Gateway Timeout',
179 505 =>
'HTTP Version Not Supported',
180 509 =>
'Bandwidth Limit Exceeded',
196 if (null ===
$code) {
197 return self::$phrases;
199 return isset(self::$phrases[
$code]) ? self::$phrases[
$code] : null;
214 if (!preg_match(
'!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) {
216 "Malformed response: {$statusLine}",
220 $this->version = $m[1];
221 $this->code = intval($m[2]);
222 $this->reasonPhrase = !empty($m[3]) ? trim($m[3]) : self::getDefaultReasonPhrase($this->code);
239 $headerLine = trim($headerLine,
"\r\n");
241 if (
'' == $headerLine) {
243 if (!empty($this->headers[
'set-cookie'])) {
244 $cookies = is_array($this->headers[
'set-cookie'])?
245 $this->headers[
'set-cookie']:
246 array($this->headers[
'set-cookie']);
247 foreach (
$cookies as $cookieString) {
250 unset($this->headers[
'set-cookie']);
252 foreach (array_keys($this->headers) as $k) {
253 if (is_array($this->headers[$k])) {
254 $this->headers[$k] = implode(
', ', $this->headers[$k]);
258 } elseif (preg_match(
'!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) {
260 $name = strtolower($m[1]);
261 $value = trim($m[2]);
262 if (empty($this->headers[$name])) {
263 $this->headers[$name] = $value;
265 if (!is_array($this->headers[$name])) {
266 $this->headers[$name] = array($this->headers[$name]);
268 $this->headers[$name][] = $value;
270 $this->lastHeader = $name;
272 } elseif (preg_match(
'!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) {
274 if (!is_array($this->headers[$this->lastHeader])) {
277 $key = count($this->headers[$this->lastHeader]) - 1;
299 if (!strpos($cookieString,
';')) {
301 $pos = strpos($cookieString,
'=');
302 $cookie[
'name'] = trim(substr($cookieString, 0, $pos));
303 $cookie[
'value'] = trim(substr($cookieString, $pos + 1));
307 $elements = explode(
';', $cookieString);
308 $pos = strpos($elements[0],
'=');
309 $cookie[
'name'] = trim(substr($elements[0], 0, $pos));
310 $cookie[
'value'] = trim(substr($elements[0], $pos + 1));
312 for ($i = 1; $i < count($elements); $i++) {
313 if (
false === strpos($elements[$i],
'=')) {
314 $elName = trim($elements[$i]);
317 list ($elName, $elValue) = array_map(
'trim', explode(
'=', $elements[$i]));
319 $elName = strtolower($elName);
320 if (
'secure' == $elName) {
321 $cookie[
'secure'] =
true;
322 } elseif (
'expires' == $elName) {
323 $cookie[
'expires'] = str_replace(
'"',
'', $elValue);
324 } elseif (
'path' == $elName ||
'domain' == $elName) {
325 $cookie[$elName] = urldecode($elValue);
327 $cookie[$elName] = $elValue;
331 $this->cookies[] = $cookie;
341 $this->body .= $bodyChunk;
384 return in_array($this->code, array(300, 301, 302, 303, 307))
385 && isset($this->headers[
'location']);
399 if (null === $headerName) {
402 $headerName = strtolower($headerName);
403 return isset($this->headers[$headerName])? $this->headers[$headerName]: null;
425 if (0 == strlen($this->body) || !$this->bodyEncoded
426 || !in_array(strtolower($this->
getHeader(
'content-encoding')), array(
'gzip',
'deflate'))
431 if (extension_loaded(
'mbstring') && (2 & ini_get(
'mbstring.func_overload'))) {
432 $oldEncoding = mb_internal_encoding();
433 mb_internal_encoding(
'8bit');
437 switch (strtolower($this->
getHeader(
'content-encoding'))) {
439 $decoded = self::decodeGzip($this->body);
442 $decoded = self::decodeDeflate($this->body);
444 }
catch (Exception $e) {
447 if (!empty($oldEncoding)) {
448 mb_internal_encoding($oldEncoding);
483 $length = strlen($data);
485 if (18 > $length || strcmp(substr($data, 0, 2),
"\x1f\x8b")) {
488 if (!function_exists(
'gzinflate')) {
490 'Unable to decode body: gzip extension not available',
494 $method = ord(substr($data, 2, 1));
497 'Error parsing gzip header: unknown compression method',
501 $flags = ord(substr($data, 3, 1));
504 'Error parsing gzip header: reserved bits are set',
513 if ($length - $headerLength - 2 < 8) {
515 'Error parsing gzip header: data too short',
519 $extraLength = unpack(
'v', substr($data, 10, 2));
520 if ($length - $headerLength - 2 - $extraLength[1] < 8) {
522 'Error parsing gzip header: data too short',
526 $headerLength += $extraLength[1] + 2;
530 if ($length - $headerLength - 1 < 8) {
532 'Error parsing gzip header: data too short',
536 $filenameLength = strpos(substr($data, $headerLength), chr(0));
537 if (
false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) {
539 'Error parsing gzip header: data too short',
543 $headerLength += $filenameLength + 1;
547 if ($length - $headerLength - 1 < 8) {
549 'Error parsing gzip header: data too short',
553 $commentLength = strpos(substr($data, $headerLength), chr(0));
554 if (
false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) {
556 'Error parsing gzip header: data too short',
560 $headerLength += $commentLength + 1;
564 if ($length - $headerLength - 2 < 8) {
566 'Error parsing gzip header: data too short',
570 $crcReal = 0xffff & crc32(substr($data, 0, $headerLength));
571 $crcStored = unpack(
'v', substr($data, $headerLength, 2));
572 if ($crcReal != $crcStored[1]) {
574 'Header CRC check failed',
581 $tmp = unpack(
'V2', substr($data, -8));
587 $unpacked = gzinflate(substr($data, $headerLength, -8));
588 if (
false === $unpacked) {
590 'gzinflate() call failed',
593 } elseif ($dataSize != strlen($unpacked)) {
595 'Data size check failed',
598 } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) {
600 'Data CRC check failed',
617 if (!function_exists(
'gzuncompress')) {
619 'Unable to decode body: gzip extension not available',
627 $header = unpack(
'n', substr($data, 0, 2));
628 return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data);
static getDefaultReasonPhrase($code=null)
__construct($statusLine, $bodyEncoded=true, $effectiveUrl=null)
parseCookie($cookieString)
parseHeaderLine($headerLine)
getHeader($headerName=null)
static decodeDeflate($data)