Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Attributes | Static Protected Attributes
Zend_Http_Response Class Reference
Inheritance diagram for Zend_Http_Response:
Zend_Http_Response_Stream

Public Member Functions

 __construct ($code, array $headers, $body=null, $version='1.1', $message=null)
 
 isError ()
 
 isSuccessful ()
 
 isRedirect ()
 
 getBody ()
 
 getRawBody ()
 
 getVersion ()
 
 getStatus ()
 
 getMessage ()
 
 getHeaders ()
 
 getHeader ($header)
 
 getHeadersAsString ($status_line=true, $br="\n")
 
 asString ($br="\r\n")
 
 __toString ()
 

Static Public Member Functions

static responseCodeAsText ($code=null, $http11=true)
 
static extractCode ($response_str)
 
static extractMessage ($response_str)
 
static extractVersion ($response_str)
 
static extractHeaders ($response_str)
 
static extractBody ($response_str)
 
static decodeChunkedBody ($body)
 
static decodeGzip ($body)
 
static decodeDeflate ($body)
 
static fromString ($response_str)
 

Protected Attributes

 $version
 
 $code
 
 $message
 
 $headers = array()
 
 $body
 

Static Protected Attributes

static $messages
 

Detailed Description

Definition at line 39 of file Response.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $code,
array  $headers,
  $body = null,
  $version = '1.1',
  $message = null 
)

HTTP response constructor

In most cases, you would use Zend_Http_Response::fromString to parse an HTTP response string and create a new Zend_Http_Response object.

NOTE: The constructor no longer accepts nulls or empty values for the code and headers and will throw an exception if the passed values do not form a valid HTTP responses.

If no message is passed, the message will be guessed according to the response code.

Parameters
int$codeResponse code (200, 404, ...)
array$headersHeaders array
string$bodyResponse body
string$versionHTTP version
string$messageResponse code as text
Exceptions
Zend_Http_Exception

Definition at line 156 of file Response.php.

157  {
158  // Make sure the response code is valid and set it
159  if (self::responseCodeAsText($code) === null) {
160  #require_once 'Zend/Http/Exception.php';
161  throw new Zend_Http_Exception("{$code} is not a valid HTTP response code");
162  }
163 
164  $this->code = $code;
165 
166  foreach ($headers as $name => $value) {
167  if (is_int($name)) {
168  $header = explode(":", $value, 2);
169  if (count($header) != 2) {
170  #require_once 'Zend/Http/Exception.php';
171  throw new Zend_Http_Exception("'{$value}' is not a valid HTTP header");
172  }
173 
174  $name = trim($header[0]);
175  $value = trim($header[1]);
176  }
177 
178  $this->headers[ucwords(strtolower($name))] = $value;
179  }
180 
181  // Set the body
182  $this->body = $body;
183 
184  // Set the HTTP version
185  if (! preg_match('|^\d\.\d$|', $version)) {
186  #require_once 'Zend/Http/Exception.php';
187  throw new Zend_Http_Exception("Invalid HTTP response version: $version");
188  }
189 
190  $this->version = $version;
191 
192  // If we got the response message, set it. Else, set it according to
193  // the response code
194  if (is_string($message)) {
195  $this->message = $message;
196  } else {
197  $this->message = self::responseCodeAsText($code);
198  }
199  }
$value
Definition: gender.phtml:16
static responseCodeAsText($code=null, $http11=true)
Definition: Response.php:429
if(!isset($_GET['name'])) $name
Definition: log.php:14

Member Function Documentation

◆ __toString()

__toString ( )

Implements magic __toString()

Returns
string

Definition at line 412 of file Response.php.

413  {
414  return $this->asString();
415  }
asString($br="\r\n")
Definition: Response.php:402

◆ asString()

asString (   $br = "\r\n")

Get the entire response as string

Parameters
string$brLine breaks (eg. "\n", "\r\n", "<br />")
Returns
string

Definition at line 402 of file Response.php.

403  {
404  return $this->getHeadersAsString(true, $br) . $br . $this->getRawBody();
405  }
getHeadersAsString($status_line=true, $br="\n")
Definition: Response.php:372

◆ decodeChunkedBody()

static decodeChunkedBody (   $body)
static

Decode a "chunked" transfer-encoded body and return the decoded text

Parameters
string$body
Returns
string

Definition at line 599 of file Response.php.

600  {
601  $decBody = '';
602 
603  // If mbstring overloads substr and strlen functions, we have to
604  // override it's internal encoding
605  if (function_exists('mb_internal_encoding') &&
606  ((int) ini_get('mbstring.func_overload')) & 2) {
607 
608  $mbIntEnc = mb_internal_encoding();
609  mb_internal_encoding('ASCII');
610  }
611 
612  while (trim($body)) {
613  if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m)) {
614  #require_once 'Zend/Http/Exception.php';
615  throw new Zend_Http_Exception("Error parsing body - doesn't seem to be a chunked message");
616  }
617 
618  $length = hexdec(trim($m[1]));
619  $cut = strlen($m[0]);
620  $decBody .= substr($body, $cut, $length);
621  $body = substr($body, $cut + $length + 2);
622  }
623 
624  if (isset($mbIntEnc)) {
625  mb_internal_encoding($mbIntEnc);
626  }
627 
628  return $decBody;
629  }

◆ decodeDeflate()

static decodeDeflate (   $body)
static

Decode a zlib deflated message (when Content-encoding = deflate)

Currently requires PHP with zlib support

Parameters
string$body
Returns
string

Some servers (IIS ?) send a broken deflate response, without the RFC-required zlib header.

We try to detect the zlib header, and if it does not exsit we teat the body is plain DEFLATE content.

This method was adapted from PEAR HTTP_Request2 by (c) Alexey Borzov

http://framework.zend.com/issues/browse/ZF-6040

Definition at line 659 of file Response.php.

660  {
661  if (! function_exists('gzuncompress')) {
662  #require_once 'Zend/Http/Exception.php';
663  throw new Zend_Http_Exception(
664  'zlib extension is required in order to decode "deflate" encoding'
665  );
666  }
667 
679  $zlibHeader = unpack('n', substr($body, 0, 2));
680  if ($zlibHeader[1] % 31 == 0 && ord($body[0]) == 0x78 && in_array(ord($body[1]), array(0x01, 0x5e, 0x9c, 0xda))) {
681  return gzuncompress($body);
682  } else {
683  return gzinflate($body);
684  }
685  }

◆ decodeGzip()

static decodeGzip (   $body)
static

Decode a gzip encoded message (when Content-encoding = gzip)

Currently requires PHP with zlib support

Parameters
string$body
Returns
string

Definition at line 639 of file Response.php.

640  {
641  if (! function_exists('gzinflate')) {
642  #require_once 'Zend/Http/Exception.php';
643  throw new Zend_Http_Exception(
644  'zlib extension is required in order to decode "gzip" encoding'
645  );
646  }
647 
648  return gzinflate(substr($body, 10));
649  }

◆ extractBody()

static extractBody (   $response_str)
static

Extract the body from a response string

Parameters
string$response_str
Returns
string

Definition at line 584 of file Response.php.

585  {
586  $parts = preg_split('|(?:\r\n){2}|m', $response_str, 2);
587  if (isset($parts[1])) {
588  return $parts[1];
589  }
590  return '';
591  }

◆ extractCode()

static extractCode (   $response_str)
static

Extract the response code from a response string

Parameters
string$response_str
Returns
int

Definition at line 449 of file Response.php.

450  {
451  preg_match("|^HTTP/[\d\.x]+ (\d+)|", $response_str, $m);
452 
453  if (isset($m[1])) {
454  return (int) $m[1];
455  } else {
456  return false;
457  }
458  }

◆ extractHeaders()

static extractHeaders (   $response_str)
static

Extract the headers from a response string

Parameters
string$response_str
Returns
array

Definition at line 500 of file Response.php.

501  {
502  $headers = array();
503 
504  // First, split body and headers. Headers are separated from the
505  // message at exactly the sequence "\r\n\r\n"
506  $parts = preg_split('|(?:\r\n){2}|m', $response_str, 2);
507  if (! $parts[0]) {
508  return $headers;
509  }
510 
511  // Split headers part to lines; "\r\n" is the only valid line separator.
512  $lines = explode("\r\n", $parts[0]);
513  unset($parts);
514  $last_header = null;
515 
516  foreach($lines as $index => $line) {
517  if ($index === 0 && preg_match('#^HTTP/\d+(?:\.\d+) [1-5]\d+#', $line)) {
518  // Status line; ignore
519  continue;
520  }
521 
522  if ($line == "") {
523  // Done processing headers
524  break;
525  }
526 
527  // Locate headers like 'Location: ...' and 'Location:...' (note the missing space)
528  if (preg_match("|^([a-zA-Z0-9\'`#$%&*+.^_\|\~!-]+):\s*(.*)|s", $line, $m)) {
529  unset($last_header);
530  $h_name = strtolower($m[1]);
531  $h_value = $m[2];
533 
534  if (isset($headers[$h_name])) {
535  if (! is_array($headers[$h_name])) {
536  $headers[$h_name] = array($headers[$h_name]);
537  }
538 
539  $headers[$h_name][] = ltrim($h_value);
540  $last_header = $h_name;
541  continue;
542  }
543 
544  $headers[$h_name] = ltrim($h_value);
545  $last_header = $h_name;
546  continue;
547  }
548 
549  // Identify header continuations
550  if (preg_match("|^[ \t](.+)$|s", $line, $m) && $last_header !== null) {
551  $h_value = trim($m[1]);
552  if (is_array($headers[$last_header])) {
553  end($headers[$last_header]);
554  $last_header_key = key($headers[$last_header]);
555 
556  $h_value = $headers[$last_header][$last_header_key] . $h_value;
558 
559  $headers[$last_header][$last_header_key] = $h_value;
560  continue;
561  }
562 
563  $h_value = $headers[$last_header] . $h_value;
565 
566  $headers[$last_header] = $h_value;
567  continue;
568  }
569 
570  // Anything else is an error condition
571  #require_once 'Zend/Http/Exception.php';
572  throw new Zend_Http_Exception('Invalid header line detected');
573  }
574 
575  return $headers;
576  }
$index
Definition: list.phtml:44

◆ extractMessage()

static extractMessage (   $response_str)
static

Extract the HTTP message from a response

Parameters
string$response_str
Returns
string

Definition at line 466 of file Response.php.

467  {
468  preg_match("|^HTTP/[\d\.x]+ \d+ ([^\r\n]+)|", $response_str, $m);
469 
470  if (isset($m[1])) {
471  return $m[1];
472  } else {
473  return false;
474  }
475  }

◆ extractVersion()

static extractVersion (   $response_str)
static

Extract the HTTP version from a response

Parameters
string$response_str
Returns
string

Definition at line 483 of file Response.php.

484  {
485  preg_match("|^HTTP/([\d\.x]+) \d+|", $response_str, $m);
486 
487  if (isset($m[1])) {
488  return $m[1];
489  } else {
490  return false;
491  }
492  }

◆ fromString()

static fromString (   $response_str)
static

Create a new Zend_Http_Response object from a string

Parameters
string$response_str
Returns
Zend_Http_Response

Definition at line 693 of file Response.php.

694  {
695  $code = self::extractCode($response_str);
696  $headers = self::extractHeaders($response_str);
697  $body = self::extractBody($response_str);
698  $version = self::extractVersion($response_str);
699  $message = self::extractMessage($response_str);
700 
702  }
static extractHeaders($response_str)
Definition: Response.php:500
static extractVersion($response_str)
Definition: Response.php:483
static extractBody($response_str)
Definition: Response.php:584
static extractMessage($response_str)
Definition: Response.php:466
static extractCode($response_str)
Definition: Response.php:449

◆ getBody()

getBody ( )

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 transfered on wire) use $this->getRawBody() instead.

Returns
string

Definition at line 258 of file Response.php.

259  {
260  $body = '';
261 
262  // Decode the body if it was transfer-encoded
263  switch (strtolower($this->getHeader('transfer-encoding'))) {
264 
265  // Handle chunked body
266  case 'chunked':
267  $body = self::decodeChunkedBody($this->body);
268  break;
269 
270  // No transfer encoding, or unknown encoding extension:
271  // return body as is
272  default:
273  $body = $this->body;
274  break;
275  }
276 
277  // Decode any content-encoding (gzip or deflate) if needed
278  switch (strtolower($this->getHeader('content-encoding'))) {
279 
280  // Handle gzip encoding
281  case 'gzip':
283  break;
284 
285  // Handle deflate encoding
286  case 'deflate':
288  break;
289 
290  default:
291  break;
292  }
293 
294  return $body;
295  }
static decodeGzip($body)
Definition: Response.php:639
static decodeChunkedBody($body)
Definition: Response.php:599
getHeader($header)
Definition: Response.php:357
static decodeDeflate($body)
Definition: Response.php:659

◆ getHeader()

getHeader (   $header)

Get a specific header as string, or null if it is not set

Parameters
string$header
Returns
string|array|null

Definition at line 357 of file Response.php.

358  {
359  $header = ucwords(strtolower($header));
360  if (! is_string($header) || ! isset($this->headers[$header])) return null;
361 
362  return $this->headers[$header];
363  }

◆ getHeaders()

getHeaders ( )

Get the response headers

Returns
array

Definition at line 346 of file Response.php.

347  {
348  return $this->headers;
349  }

◆ getHeadersAsString()

getHeadersAsString (   $status_line = true,
  $br = "\n" 
)

Get all headers as string

Parameters
boolean$status_lineWhether to return the first status line (IE "HTTP 200 OK")
string$brLine breaks (eg. "\n", "\r\n", "<br />")
Returns
string

Definition at line 372 of file Response.php.

373  {
374  $str = '';
375 
376  if ($status_line) {
377  $str = "HTTP/{$this->version} {$this->code} {$this->message}{$br}";
378  }
379 
380  // Iterate over the headers and stringify them
381  foreach ($this->headers as $name => $value)
382  {
383  if (is_string($value))
384  $str .= "{$name}: {$value}{$br}";
385 
386  elseif (is_array($value)) {
387  foreach ($value as $subval) {
388  $str .= "{$name}: {$subval}{$br}";
389  }
390  }
391  }
392 
393  return $str;
394  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ getMessage()

getMessage ( )

Return a message describing the HTTP response code (Eg. "OK", "Not Found", "Moved Permanently")

Returns
string

Definition at line 336 of file Response.php.

337  {
338  return $this->message;
339  }

◆ getRawBody()

getRawBody ( )

Get the raw response body (as transfered "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.

Returns
string

Definition at line 305 of file Response.php.

306  {
307  return $this->body;
308  }

◆ getStatus()

getStatus ( )

Get the HTTP response status code

Returns
int

Definition at line 325 of file Response.php.

326  {
327  return $this->code;
328  }

◆ getVersion()

getVersion ( )

Get the HTTP version of the response

Returns
string

Definition at line 315 of file Response.php.

316  {
317  return $this->version;
318  }

◆ isError()

isError ( )

Check whether the response is an error

Returns
boolean

Definition at line 206 of file Response.php.

207  {
208  $restype = floor($this->code / 100);
209  if ($restype == 4 || $restype == 5) {
210  return true;
211  }
212 
213  return false;
214  }

◆ isRedirect()

isRedirect ( )

Check whether the response is a redirection

Returns
boolean

Definition at line 236 of file Response.php.

237  {
238  $restype = floor($this->code / 100);
239  if ($restype == 3) {
240  return true;
241  }
242 
243  return false;
244  }

◆ isSuccessful()

isSuccessful ( )

Check whether the response in successful

Returns
boolean

Definition at line 221 of file Response.php.

222  {
223  $restype = floor($this->code / 100);
224  if ($restype == 2 || $restype == 1) { // Shouldn't 3xx count as success as well ???
225  return true;
226  }
227 
228  return false;
229  }

◆ responseCodeAsText()

static responseCodeAsText (   $code = null,
  $http11 = true 
)
static

A convenience function that returns a text representation of HTTP response codes. Returns 'Unknown' for unknown codes. Returns array of all codes, if $code is not specified.

Conforms to HTTP/1.1 as defined in RFC 2616 (except for 'Unknown') See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10 for reference

Parameters
int$codeHTTP response code
boolean$http11Use HTTP version 1.1
Returns
string

Definition at line 429 of file Response.php.

430  {
432  if (! $http11) $messages[302] = 'Moved Temporarily';
433 
434  if ($code === null) {
435  return $messages;
436  } elseif (isset($messages[$code])) {
437  return $messages[$code];
438  } else {
439  return 'Unknown';
440  }
441  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

Field Documentation

◆ $body

$body
protected

Definition at line 135 of file Response.php.

◆ $code

$code
protected

Definition at line 113 of file Response.php.

◆ $headers

$headers = array()
protected

Definition at line 128 of file Response.php.

◆ $message

$message
protected

Definition at line 121 of file Response.php.

◆ $messages

$messages
staticprotected

Definition at line 47 of file Response.php.

◆ $version

$version
protected

Definition at line 106 of file Response.php.


The documentation for this class was generated from the following file: