-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Request CachePolicy isn't applied in HTTP request header #60913
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
Changes from 5 commits
59b5a15
3d4bf86
e272a65
9ed19f3
ac36aec
32dffd6
f499323
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| using System.IO; | ||
| using System.Net.Cache; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| using System.Net.Security; | ||
| using System.Net.Sockets; | ||
| using System.Runtime.Serialization; | ||
|
|
@@ -1137,6 +1138,8 @@ private async Task<WebResponse> SendRequest(bool async) | |
| request.Headers.Host = Host; | ||
| } | ||
|
|
||
| AddCacheControlHeaders(request); | ||
|
|
||
| // Copy the HttpWebRequest request headers from the WebHeaderCollection into HttpRequestMessage.Headers and | ||
| // HttpRequestMessage.Content.Headers. | ||
| foreach (string headerName in _webHeaderCollection) | ||
|
|
@@ -1202,6 +1205,100 @@ private async Task<WebResponse> SendRequest(bool async) | |
| } | ||
| } | ||
|
|
||
| private void AddCacheControlHeaders(HttpRequestMessage request) | ||
| { | ||
| RequestCachePolicy? policy = GetApplicableCachePolicy(); | ||
|
|
||
| if (policy != null) | ||
| { | ||
| if (request.Headers.CacheControl == null) | ||
| { | ||
| request.Headers.CacheControl = new CacheControlHeaderValue(); | ||
| } | ||
|
|
||
| if (policy is HttpRequestCachePolicy httpRequestCachePolicy) | ||
| { | ||
| switch (httpRequestCachePolicy.Level) | ||
| { | ||
| case HttpRequestCacheLevel.NoCacheNoStore: | ||
| request.Headers.CacheControl.NoCache = true; | ||
| request.Headers.CacheControl.NoStore = true; | ||
| request.Headers.Pragma.Add(new NameValueHeaderValue("no-cache")); | ||
| break; | ||
| case HttpRequestCacheLevel.Reload: | ||
| request.Headers.CacheControl.NoCache = true; | ||
| request.Headers.Pragma.Add(new NameValueHeaderValue("no-cache")); | ||
| break; | ||
| case HttpRequestCacheLevel.CacheOnly: | ||
scalablecory marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case HttpRequestCacheLevel.CacheOrNextCacheOnly: | ||
| request.Headers.CacheControl.OnlyIfCached = true; | ||
| break; | ||
| case HttpRequestCacheLevel.Default: | ||
| if (httpRequestCachePolicy.MinFresh > TimeSpan.Zero) | ||
| { | ||
| request.Headers.CacheControl.MinFresh = httpRequestCachePolicy.MinFresh; | ||
| } | ||
|
|
||
| if (httpRequestCachePolicy.MaxAge != TimeSpan.MaxValue) | ||
| { | ||
| request.Headers.CacheControl.MaxAge = httpRequestCachePolicy.MaxAge; | ||
| } | ||
|
|
||
| if (httpRequestCachePolicy.MaxStale > TimeSpan.Zero) | ||
| { | ||
| request.Headers.CacheControl.MaxStale = true; | ||
| request.Headers.CacheControl.MaxStaleLimit = httpRequestCachePolicy.MaxStale; | ||
| } | ||
|
|
||
| break; | ||
| case HttpRequestCacheLevel.Refresh: | ||
| request.Headers.CacheControl.MaxAge = TimeSpan.Zero; | ||
| request.Headers.Pragma.Add(new NameValueHeaderValue("no-cache")); | ||
| break; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| switch (policy.Level) | ||
| { | ||
| case RequestCacheLevel.NoCacheNoStore: | ||
| request.Headers.CacheControl.NoCache = true; | ||
| request.Headers.CacheControl.NoStore = true; | ||
| request.Headers.Pragma.Add(new NameValueHeaderValue("no-cache")); | ||
| break; | ||
| case RequestCacheLevel.Reload: | ||
| request.Headers.CacheControl.NoCache = true; | ||
| request.Headers.Pragma.Add(new NameValueHeaderValue("no-cache")); | ||
| break; | ||
| case RequestCacheLevel.CacheOnly: | ||
| request.Headers.CacheControl.OnlyIfCached = true; | ||
| break; | ||
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private RequestCachePolicy? GetApplicableCachePolicy() | ||
| { | ||
| if (CachePolicy != null) | ||
| { | ||
| return CachePolicy; | ||
| } | ||
| else if (IsDefaultCachePolicySet(DefaultCachePolicy)) | ||
| { | ||
| return DefaultCachePolicy; | ||
| } | ||
| else if (IsDefaultCachePolicySet(WebRequest.DefaultCachePolicy)) | ||
| { | ||
| return WebRequest.DefaultCachePolicy; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static bool IsDefaultCachePolicySet(RequestCachePolicy? policy) => policy != null | ||
| && policy.Level != RequestCacheLevel.BypassCache; | ||
|
||
|
|
||
| public override IAsyncResult BeginGetResponse(AsyncCallback? callback, object? state) | ||
| { | ||
| CheckAbort(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CacheControlwill always benullhere. I would:And accessing
request.Headers.CacheControlover and over will do non-trivial work, so I'd use this local variable in the switch body and then after setting everything: