Skip to content

Fix calculation of freshness lifetime in HttpResponse#419

Closed
arturobernalg wants to merge 4 commits intoapache:5.3.xfrom
arturobernalg:feature/calculateFreshnessLifetime
Closed

Fix calculation of freshness lifetime in HttpResponse#419
arturobernalg wants to merge 4 commits intoapache:5.3.xfrom
arturobernalg:feature/calculateFreshnessLifetime

Conversation

@arturobernalg
Copy link
Copy Markdown
Member

@arturobernalg arturobernalg commented Mar 4, 2023

This pull request adds a fix for the calculateFreshnessLifetime method in the HttpResponseCache class. The method calculates the freshness lifetime of a response based on the headers in the response and follows the algorithm for calculating the freshness lifetime described in RFC 7234, section 4.2.1.

The method takes into account the s-maxage, max-age, Expires, and Date headers as follows:

  • If the s-maxage directive is present in the Cache-Control header of the response, its value is used as the freshness lifetime for shared caches, which typically serve multiple users or clients.

  • If the max-age directive is present in the Cache-Control header of the response, its value is used as the freshness lifetime for private caches, which serve a single user or client.

  • If the Expires header is present in the response, its value is used as the expiration time of the response. The freshness lifetime is calculated as the difference between the expiration time and the time specified in the Date header of the response.

  • If none of the above headers are present or if the calculated freshness lifetime is invalid, a default value of 5 minutes is returned.

The existing implementation had a bug where it did not handle the case where the max-age directive was present in the Cache-Control header but the s-maxage directive was not. In this case, the method would incorrectly return the default freshness lifetime of 5 minutes instead of using the max-age value as the freshness lifetime. This pull request fixes the bug by adding a check for the s-maxage directive and returning the max-age value if the s-maxage directive is not present.

Motivation

The bug in the calculateFreshnessLifetime method could cause incorrect caching behavior, which could lead to degraded performance and unexpected results for clients. By fixing this bug, we ensure that the method correctly calculates the freshness lifetime of responses and that clients can rely on the cache to serve responses efficiently and accurately.

Changes Made

To fix the bug, we added a check for the s-maxage directive in the Cache-Control header and updated the return value of the method to use the max-age value if the s-maxage directive is not present.

@arturobernalg arturobernalg force-pushed the feature/calculateFreshnessLifetime branch from 80ee2d0 to 5f0f349 Compare March 4, 2023 17:35
@arturobernalg arturobernalg changed the base branch from master to 5.3.x March 4, 2023 18:51
garydgregory and others added 4 commits March 4, 2023 19:52
Update ResponseCachingPolicy to allow caching of responses to POST requests under certain circumstances, as specified in RFC 2616 and explained in more detail in draft-ietf-httpbis-p2-semantics-20#section-2.3.4. This change extends the cacheability of responses beyond GET and HEAD methods, improving the cache's efficiency and reducing network traffic.
@arturobernalg arturobernalg force-pushed the feature/calculateFreshnessLifetime branch from 5f0f349 to 7da5adb Compare March 4, 2023 18:52
@arturobernalg arturobernalg deleted the feature/calculateFreshnessLifetime branch March 4, 2023 18:53
@arturobernalg
Copy link
Copy Markdown
Member Author

closed in favor of #420 to 5.3.X branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants