WP_Http::chunkTransferDecode() WordPress Method

The WP_Http::chunkTransferDecode() function is used to decode a chunked transfer-encoded string. This function is useful for decoding strings received from an HTTP server that uses chunked transfer-encoding.

WP_Http::chunkTransferDecode( string $body ) #

Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.


Description

Based off the HTTP http_encoding_dechunk function.


Top ↑

Parameters

$body

(string)(Required)Body content.


Top ↑

Return

(string) Chunked decoded body on success or raw body on failure.


Top ↑

Source

File: wp-includes/class-wp-http.php

	public static function chunkTransferDecode( $body ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
		// The body is not chunked encoded or is malformed.
		if ( ! preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', trim( $body ) ) ) {
			return $body;
		}

		$parsed_body = '';

		// We'll be altering $body, so need a backup in case of error.
		$body_original = $body;

		while ( true ) {
			$has_chunk = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $body, $match );
			if ( ! $has_chunk || empty( $match[1] ) ) {
				return $body_original;
			}

			$length       = hexdec( $match[1] );
			$chunk_length = strlen( $match[0] );

			// Parse out the chunk of data.
			$parsed_body .= substr( $body, $chunk_length, $length );

			// Remove the chunk from the raw data.
			$body = substr( $body, $length + $chunk_length );

			// End of the document.
			if ( '0' === trim( $body ) ) {
				return $parsed_body;
			}
		}
	}


Top ↑

Changelog

Changelog
VersionDescription
2.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.