dotnet / systemweb-adapters

MIT License
337 stars 59 forks source link

Add more functions to `HttpCachePolicy` #507

Open TaoziZ03 opened 3 months ago

TaoziZ03 commented 3 months ago

Summary

Can I add more functions to HttpCachePolicy?

  1. SetNoStore(): add no-store header
  2. SetETag(string etag): add etag header
  3. UpdateHeaders(): better support no cache scenario, set expires = -1, pragma = no-cache and omit vary.

Motivation and goals

Our legacy code has a custom wrapper of HttCachePolicy and was modfied throughout whole request lifetime. None of attribute and middleware fit in existing design. To skip the design change and migrate to .net core first, I hope to add these functions.

In scope

A list of major scenarios, perhaps in priority order.

Out of scope

Scenarios you explicitly want to exclude.

Risks / unknowns

Please kindly point out other files I should modify.

Examples

Give brief examples of possible developer experiences (e.g., code they would write).

Don't be deeply concerned with how it would be implemented yet. Your examples could even be from other technology stacks.

twsouthwick commented 2 months ago

We'll take an implementation of this if you'd like to provide that

TaoziZ03 commented 2 months ago

I hope HttpCachePolicy could have the same behavior as Framework version. https://github.com/dotnet/systemweb-adapters/blob/649e7379bc4a4da69ca4167410324d2a4470d54d/src/Microsoft.AspNetCore.SystemWebAdapters/HttpCachePolicy.cs#L11

Framework reference: Get/SetNoStore() Get/SetETag() UpdateCachedHeaders()

Repro:

// Here set `vary` and `Expires`
httpContext.Reponse.SetVaryByCustom("accept-encoding");
httpContext.Reponse.SetExpires(2d);
httpContext.Response.Cache.SetETag("ETag1");
....
httpContext.Response..Cacheability = HttpCacheability.NoCache;
httpContext.Response.Cache.SetNoStore();

Expected headers

Cache-Control: no-store, no-cache
Pragma: no-cache

ETag, Expires and Vary will ignored because later set cachebility to NoCache.