GC_Http::chunkTransferDecode()

最后更新于:2021-12-01 11:14:05

GC_( string$body)

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

参数

$body

(string) (Required) Body content.

响应

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

源文件

文件: gc-includes/class-http.php

	public static function chunkTransferDecode( $body ) { // phpcs:ignore GeChiUI.NamingConventions.ValidFunctionName.MethodNameInvalid
		// The body is not chunked encoded or is malformed.
		if ( ! preg_match( '/^([0-9a-f]+)[^rn]*rn/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]+)[^rn]*rn/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;
			}
		}
	}