-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Request CachePolicy isn't applied in HTTP request header #60913
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
.NET no longer has a local cache, so this can always throw a |
||
} | ||
} | ||
} | ||
} | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem correct: if I explicitly want my request to bypass cache, this will end up using the default policy (which might be to cache). I think correct behavior is to stop at the first non-null policy, and return null if that policy says bypass. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, can you make sure The docs say it bypasses all caches "between client and server", and it's not clear if this needs to set some header related to proxies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I did it because the DefaultCachePolicy is initialized with BypassCache in WebRequest and HttpWebRequest classes by default. if a user sets WebRequest.DefaultCachePolicy and didn't do it for HttpWebRequest.DefaultCachePolicy we will presume that no cache gonna be applied, which may seem misleading for the user There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It sounds like we need to update those properties to track "explicitly set" vs "default" state. Pull the first one that is explicitly set. |
||
|
||
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.
CacheControl
will always benull
here. I would:And accessing
request.Headers.CacheControl
over and over will do non-trivial work, so I'd use this local variable in the switch body and then after setting everything: