WpOrg\Requests

Requests::parse_response()protected staticWP 1.0

HTTP response parser

Метод класса: Requests{}

Хуков нет.

Возвращает

\WpOrg\Requests\Response.

Использование

$result = Requests::parse_response( $headers, $url, $req_headers, $req_data, $options );
$headers(строка) (обязательный)
Full response text including headers and body
$url(строка) (обязательный)
Original request URL
$req_headers(массив) (обязательный)
Original $headers array passed to request(), in case we need to follow redirects
$req_data(массив) (обязательный)
Original $data array passed to request(), in case we need to follow redirects
$options(массив) (обязательный)
Original $options array passed to request(), in case we need to follow redirects

Код Requests::parse_response() WP 6.5.2

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,
				&$req_headers,
				&$req_data,
				&$options,
				$return,
			];
			$options['hooks']->dispatch('requests.before_redirect', $hook_args);
			$redirected            = self::request($location, $req_headers, $req_data, $options['type'], $options);
			$redirected->history[] = $return;
			return $redirected;
		} elseif ($options['redirected'] >= $options['redirects']) {
			throw new Exception('Too many redirects', 'toomanyredirects', $return);
		}
	}

	$return->redirects = $options['redirected'];

	$options['hooks']->dispatch('requests.after_request', [&$return, $req_headers, $req_data, $options]);
	return $return;
}