wppaste
WordPress

Requests::parse_response( string $headers, string $url, array $req_headers, array $req_data, array $options ): WpOrg\Requests\Response

Source
wp-includes/Requests/src/Requests.php:723
HTTP response parser

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$headersstring
Full response text including headers and body
$urlstring
Original request URL
$req_headersarray
Original $headers array passed to request(), in case we need to follow redirects
$req_dataarray
Original $data array passed to request(), in case we need to follow redirects
$optionsarray
Original $options array passed to request(), in case we need to follow redirects

Return value

WpOrg\Requests\Response

Performance profile

How much work a call to Requests::parse_response() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
11–170

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 224.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 14 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost Requests::parse_response() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$options11–27none
$options && empty($matches)46–60explode(), array_shift(), preg_match()
$options && !empty($matches) && !isset($value)87–123explode(), array_shift(), preg_match(), ->is_redirect()
$options && !empty($matches)94–130explode(), array_shift(), preg_match(), ::decompress(), ->is_redirect()
$options && !empty($matches)96–132explode(), array_shift(), preg_match(), ::decode_chunked(), ->is_redirect()
$options && !empty($matches) && isset($value)103–139explode(), array_shift(), preg_match(), ::decode_chunked(), ::decompress(), ->is_redirect()
$options && !empty($matches) && ->is_redirect()119–147explode(), array_shift(), preg_match(), ->is_redirect(), ->dispatch(), ::request()
$options && !empty($matches) && ->is_redirect()126–154explode(), array_shift(), preg_match(), ::decompress(), ->is_redirect(), ->dispatch(), ::request()
$options && !empty($matches) && ->is_redirect()128–156explode(), array_shift(), preg_match(), ::decode_chunked(), ->is_redirect(), ->dispatch(), ::request()
$options && !empty($matches) && ->is_redirect()129–154explode(), array_shift(), preg_match(), ->is_redirect(), ::Iri(), ->dispatch(), ::request()
$options && !empty($matches) && isset($value) && ->is_redirect()135–163explode(), array_shift(), preg_match(), ::decode_chunked(), ::decompress(), ->is_redirect(), ->dispatch(), ::request()
$options && !empty($matches) && ->is_redirect()136–161explode(), array_shift(), preg_match(), ::decompress(), ->is_redirect(), ::Iri(), ->dispatch(), ::request()
2 further outcomes, up to 170 instructions
$options && !empty($matches) && ->is_redirect()138–163explode(), array_shift(), preg_match(), ::decode_chunked(), ->is_redirect(), ::Iri(), ->dispatch(), ::request()
$options && !empty($matches) && isset($value) && ->is_redirect()145–170explode(), array_shift(), preg_match(), ::decode_chunked(), ::decompress(), ->is_redirect(), ::Iri(), ->dispatch(), ::request()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev22411–17020
8.522411–17020
8.422411–1702026 fewer instructions than PHP 8.3
8.325011–19220
8.225011–19220
8.125011–19220
7.425011–19220

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

  • \WpOrg\Requests\Response::__construct()
  • \WpOrg\Requests\Exception::__construct()
  • \WpOrg\Requests\Requests::decode_chunked()
  • \WpOrg\Requests\Requests::decompress()
  • \WpOrg\Requests\Iri::absolutize()
  • \WpOrg\Requests\Requests::request()

Source code

	protected static function parse_response($headers, $url, $req_headers, $req_data, $options) {		$return = new Response();		if (!$options['blocking']) {			return $return;		} 		$return->raw  = $headers;		$return->url  = (string) $url;		$return->body = ''; 		if (!$options['filename']) {			$pos = strpos($headers, "\r\n\r\n");			if ($pos === false) {				// Crap!				throw new Exception('Missing header/body separator', 'requests.no_crlf_separator');			} 			$headers = substr($return->raw, 0, $pos);			// Headers will always be separated from the body by two new lines - `\n\r\n\r`.			$body = substr($return->raw, $pos + 4);			if (!empty($body)) {				$return->body = $body;			}		} 		// Pretend CRLF = LF for compatibility (RFC 2616, section 19.3)		$headers = str_replace("\r\n", "\n", $headers);		// Unfold headers (replace [CRLF] 1*( SP | HT ) with SP) as per RFC 2616 (section 2.2)		$headers = preg_replace('/\n[ \t]/', ' ', $headers);		$headers = explode("\n", $headers);		preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches);		if (empty($matches)) {			throw new Exception('Response could not be parsed', 'noversion', $headers);		} 		$return->protocol_version = (float) $matches[1];		$return->status_code      = (int) $matches[2];		if ($return->status_code >= 200 && $return->status_code < 300) {			$return->success = true;		} 		foreach ($headers as $header) {			list($key, $value) = explode(':', $header, 2);			$value             = trim($value);			preg_replace('#(\s+)#i', ' ', $value);			$return->headers[$key] = $value;		} 		if (isset($return->headers['transfer-encoding'])) {			$return->body = self::decode_chunked($return->body);			unset($return->headers['transfer-encoding']);		} 		if (isset($return->headers['content-encoding'])) {			$return->body = self::decompress($return->body);		} 		//fsockopen and cURL compatibility		if (isset($return->headers['connection'])) {			unset($return->headers['connection']);		} 		$options['hooks']->dispatch('requests.before_redirect_check', [&$return, $req_headers, $req_data, $options]); 		if ($return->is_redirect() && $options['follow_redirects'] === true) {			if (isset($return->headers['location']) && $options['redirected'] < $options['redirects']) {				if ($return->status_code === 303) {					$options['type'] = self::GET;				} 				$options['redirected']++;				$location = $return->headers['location'];				if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) {					// relative redirect, for compatibility make it absolute					$location = Iri::absolutize($url, $location);					$location = $location->uri;				} 				$hook_args = [					&$location,

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/Requests/src/Requests.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.