Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Yii Framework 2 Change Log
- Bug #20453: Fix PHPStan/Psalm types in `yii\web\View` (max-s-lab)
- Enh #20461: Add PHPStan/Psalm annotations for `yii\filters\auth\AuthInterface` (max-s-lab)
- Bug #20459: Fix return type in `RequestParserInterface::parse` (max-s-lab)
- Enh #20477: Added Content-Encoding(aka gzip) support to request body (lucaswitch)

- Bug #20475: Fix `Formatter` class `asScientific()` method for PHP `8.5` `sprintf` precision change (`6` to `0`) (terabytesoftw)

2.0.53 June 27, 2025
Expand Down
22 changes: 21 additions & 1 deletion framework/web/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@
* @var HeaderCollection Collection of request headers.
*/
private $_headers;
/** @var boolean Enable automatically inflate post requests with gzip/deflate Content-Encoding headers */
public $gzip = false;


/**
Expand Down Expand Up @@ -562,6 +564,24 @@

private $_rawBody;

/**
* Inflate the raw body on needed.
* @return void
*/
private function tryInflateRawBody()
{
if ($this->gzip) {
$contentEncoding = $this->headers->get('Content-Encoding', '', true);
if ($contentEncoding === 'gzip' || $contentEncoding === 'x-gzip') {
// gzip (RFC 1952)

Check failure on line 576 in framework/web/Request.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

Spaces must be used for alignment; tabs are not allowed
$this->_rawBody = gzdecode($this->_rawBody);
} elseif ($contentEncoding === 'deflate') {
// raw deflate (RFC 1951)
$this->_rawBody = gzinflate($this->_rawBody);
}
}
}

/**
* Returns the raw HTTP request body.
* @return string the request body
Expand All @@ -570,8 +590,8 @@
{
if ($this->_rawBody === null) {
$this->_rawBody = file_get_contents('php://input');
$this->tryInflateRawBody();
}

return $this->_rawBody;
}

Expand Down
44 changes: 43 additions & 1 deletion tests/framework/web/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace yiiunit\framework\web;

use ReflectionClass;
use yii\web\Request;
use yiiunit\TestCase;

Expand Down Expand Up @@ -564,7 +565,7 @@ public function testGetScriptUrlWithEmptyServer()
$_SERVER = [];

$this->expectException(\yii\base\InvalidConfigException::class);

$request->getScriptUrl();
}

Expand Down Expand Up @@ -1410,4 +1411,45 @@ public function testForwardedNotTrusted()
$this->assertSame('10.0.0.1', $request->userIP, 'User IP fail!');
$this->assertSame('http://yiiframework.com', $request->hostInfo, 'Host info fail!');
}

public function testGzipGetRawBody()
{
$request = new Request(['gzip' => true]);
$reflection = new ReflectionClass($request);
$tryInflateRawBody = $reflection->getMethod('tryInflateRawBody');
$rawBody = $reflection->getProperty('_rawBody');
$rawBody->setAccessible(true);
$tryInflateRawBody->setAccessible(true);

$testString = 'hello!';
// Test gzip as described in RFC 1950, Content-Encoding: gzip or x-gzip
$request->headers->add('Content-encoding','gzip');
$rawBody->setValue($request, gzencode($testString));
$tryInflateRawBody->invoke($request);
$this->assertSame($testString, $rawBody->getValue($request), 'Could not deflate correctly');
$request->headers->add('Content-encoding','x-gzip');
$rawBody->setValue($request, gzencode($testString));
$tryInflateRawBody->invoke($request);
$this->assertSame($testString, $rawBody->getValue($request), 'Could not deflate correctly');

// Test deflate as described in RFC 1951, Content-Encoding: deflate
$request = new Request(['gzip' => true]);
$request->headers->add('Content-encoding','deflate');
$rawBody->setValue($request, gzdeflate($testString));
$tryInflateRawBody->invoke($request);
$this->assertSame($testString, $rawBody->getValue($request), 'Could not deflate correctly');

// Test backwards compatibility
$request = new Request(['gzip' => false]);
$request->headers->add('Content-encoding','deflate');
$rawBody->setValue($request, gzdeflate($testString));
$tryInflateRawBody->invoke($request);
$this->assertSame(gzdeflate($testString), $rawBody->getValue($request), 'Gzip flag not working as expected');

$request = new Request();
$request->headers->add('Content-encoding','deflate');
$rawBody->setValue($request, gzdeflate($testString));
$tryInflateRawBody->invoke($request);
$this->assertSame(gzdeflate($testString), $rawBody->getValue($request), 'Gzip flag not working as expected');
}
}
Loading