Skip to content

Set Date header in Response constructor already #22036

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

Closed
wants to merge 2 commits into from
Closed

Set Date header in Response constructor already #22036

wants to merge 2 commits into from

Conversation

mpdude
Copy link
Contributor

@mpdude mpdude commented Mar 17, 2017

Q A
Branch? 2.8
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Setting the Date header in the Response constructor has been removed in #14912 and changed to a more lazy approach in getDate().

That way, methods like getAge(), getTtl() or isFresh() cause side effects as they eventually call getDate() and the Request "starts to age" once you call them.

I don't know if this would be a nice test, but current behaviour is

        $response = new Response();
        $response->setSharedMaxAge(10);
        sleep(20);
        $this->assertTrue($response->isFresh());
        sleep(5);
        $this->assertTrue($response->isFresh());
        sleep(5);
        $this->assertFalse($response->isFresh());

A particular weird case is the isCacheable() method, because it calls isFresh() only under certain conditions, like particular status codes, no ETag present etc. This symptom is also described under "Cause of the problem" in #19390, however the problem is worked around there in other ways.

So, this PR suggests to effectively revert #14912.

Additionally, I'd like to suggest to move this special handling of the Date header into the ResponseHeaderBag. If the ResponseHeaderBag guards that we always have the Date, we would not need special logic in sendHeaders() and could also take care of #14912 (comment).

@xabbuh
Copy link
Member

xabbuh commented Mar 17, 2017

Not sure I understand the actual this is trying to fix. Can you explain this a bit more?

@mpdude
Copy link
Contributor Author

mpdude commented Mar 17, 2017

@xabbuh I'll try 😄

Every Response needs a Date: header. #14906 asked to postpone the initialization to as late as possible which was done in #14912. It now happens in getDate() or sendHeaders() only.

As the date is needed for age calculations, it is implicitly initialized when you call getAge(), getTtl() or isFresh().

My first point is that this is ugly API-wise because these getters/issers now change the Response state in a subtle way. Also it feels weird that a Response does not "get old" unless you once call one of the above methods. After that, getTtl() will decrement every second and isFresh() will eventually expire.

Second, HttpCache relies on the age calculation functions. But because the lazy initialization happens in getDate() or sendHeaders() and HttpCache may happen to call neither one, chances are the date remains uninitialized – this was reported in #19390.

[More precisely, when the Reponse has an ETag set, Response::isCacheable returns true because of isValidateable(), without touching the date through isFresh().]

#19390 fixed this by adding an extra safeguard in HttpCache::store(), but I think that's just a workaround for the underlying cause.

We could remove the additional initializations in getDate() or sendHeaders() but then we'd need to make sure otherwise that the header remains present even if you remove it (I have an idea how to do so). [Updated: See #22124]

Does that make it clearer?

@fabpot
Copy link
Member

fabpot commented Mar 22, 2017

Thank you @mpdude.

fabpot added a commit that referenced this pull request Mar 22, 2017
This PR was squashed before being merged into the 2.8 branch (closes #22036).

Discussion
----------

Set Date header in Response constructor already

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Setting the `Date` header in the `Response` constructor has been removed in #14912 and changed to a more lazy approach in `getDate()`.

That way, methods like `getAge()`, `getTtl()` or `isFresh()` cause side effects as they eventually call `getDate()` and the Request "starts to age" once you call them.

I don't know if this would be a nice test, but current behaviour is

```php
        $response = new Response();
        $response->setSharedMaxAge(10);
        sleep(20);
        $this->assertTrue($response->isFresh());
        sleep(5);
        $this->assertTrue($response->isFresh());
        sleep(5);
        $this->assertFalse($response->isFresh());
```

A particular weird case is the `isCacheable()` method, because it calls `isFresh()` only under certain conditions, like particular status codes, no `ETag` present etc. This symptom is also described under "Cause of the problem" in #19390, however the problem is worked around there in other ways.

So, this PR suggests to effectively revert #14912.

Additionally, I'd like to suggest to move this special handling of the `Date` header into the `ResponseHeaderBag`. If the `ResponseHeaderBag` guards that we always have the `Date`, we would not need special logic in `sendHeaders()` and could also take care of #14912 (comment).

Commits
-------

3a7fa7e Set Date header in Response constructor already
@fabpot fabpot closed this Mar 22, 2017
@mpdude mpdude deleted the response-always-date branch March 22, 2017 21:44
This was referenced Apr 5, 2017
nicolas-grekas added a commit that referenced this pull request Apr 7, 2017
…pdonadio)

This PR was squashed before being merged into the 3.2 branch (closes #22304).

Discussion
----------

Moved $this->setDate() before the deprecation handling.

| Q             | A
| ------------- | ---
| Branch?       | 3.2 <!-- see comment below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no?
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

<!--
- Bug fixes must be submitted against the lowest branch where they apply
  (lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the master branch.
- Please fill in this template according to the PR you're about to submit.
- Replace this comment by a description of what your PR is solving.
-->

#22036 merged the patch into the wrong place; the `$this->setDate()` happens after the deprecation handling, so it does not always get called.

See also https://www.drupal.org/node/2712647?page=1#comment-12025349

Commits
-------

b6df4e7 Moved ->setDate() before the deprecation handling.
symfony-splitter pushed a commit to symfony/http-foundation that referenced this pull request Apr 7, 2017
…pdonadio)

This PR was squashed before being merged into the 3.2 branch (closes #22304).

Discussion
----------

Moved $this->setDate() before the deprecation handling.

| Q             | A
| ------------- | ---
| Branch?       | 3.2 <!-- see comment below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no?
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

<!--
- Bug fixes must be submitted against the lowest branch where they apply
  (lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the master branch.
- Please fill in this template according to the PR you're about to submit.
- Replace this comment by a description of what your PR is solving.
-->

symfony/symfony#22036 merged the patch into the wrong place; the `$this->setDate()` happens after the deprecation handling, so it does not always get called.

See also https://www.drupal.org/node/2712647?page=1#comment-12025349

Commits
-------

b6df4e7 Moved ->setDate() before the deprecation handling.
fabpot added a commit that referenced this pull request Jun 16, 2017
…seHeaderBag (mpdude)

This PR was squashed before being merged into the 3.4 branch (closes #22124).

Discussion
----------

Shift responsibility for keeping Date header to ResponseHeaderBag

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

This is an improvement over #22036. It shifts responsibility for preserving a `Date` header to the `ResponseHeaderBag`.

We already have similar logic there for the `Cache-Control` header.

Commits
-------

5d83836 Shift responsibility for keeping Date header to ResponseHeaderBag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants