Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When configured to use \yii\web\JsonResponseFormatter and yii\filters\HttpCache 304 responses include a response body #18199

Closed
darrylkuhn opened this issue Jul 22, 2020 · 1 comment
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug
Milestone

Comments

@darrylkuhn
Copy link

When the app is configured to configured return JSON formatted payloads using \yii\web\JsonResponseFormatter and configured to perform caching with yii\filters\HttpCache the response includes a body of null when the cache is hit. However per rfc7232

A 304 response cannot contain a message-body; it is always terminated by the first empty line after the header fields.

This would be a problem for 204 No Content responses as well however \yii\web\Response::prepare() explicitly sets the content of the response to '' when the status is 204. It seems the same ought to be done for 304s as well. If this change is welcome I'd be happy to open a PR.

What steps will reproduce the problem?

  1. Configure your response formatter to support json
Yii::$app->response->formatters[\yii\web\Response::FORMAT_JSON] = \yii\web\JsonResponseFormatter::class;
  1. Configure your controller to implement yii\filters\HttpCache
public function behaviors()
{
    $behaviors = parent::behaviors();

    $behaviors[] = [
        'class' => 'yii\filters\HttpCache',
        'only' => ['view'],
        'etagSeed' => function ($action, $params) {
            return 'static-etag';
        },
    ];

    return $behaviors;
}
  1. Make a request using the etag
curl -v --location --request GET 'http://localhost/example/1' --header 'If-None-Match: "ywR/RvOEhT8oO8ayzGOORj2GzbM"' --header 'Accept: application/json' --header 'Content-Type: application/json'

What is the expected result?

The expected result is a response that with a 304 status and no response body.

What do you get instead?

A response which includes the response body null. This is indicated by curl error message:

* Excess found in a non pipelined read: excess = 4 url = /example/1 (zero-length body)

Additional info

Q A
Yii version 2.0.16
PHP version 7.1.17
Operating system alpine3.4
@samdark samdark added status:ready for adoption Feel free to implement this issue. type:bug Bug labels Aug 5, 2020
@rad8329
Copy link
Contributor

rad8329 commented Nov 14, 2020

I'll handle this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug
Projects
None yet
Development

No branches or pull requests

3 participants