-
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
59b5a15
Request CachePolicy isn't applied in HTTP request header
pedrobsaila 3d4bf86
handle other CachePolicy levels
pedrobsaila e272a65
manage max-stale cache policy case
pedrobsaila 9ed19f3
handle DefaultCachePolicy
pedrobsaila ac36aec
CachePolicy is nullable by default
pedrobsaila 32dffd6
explicitely set DefaultCachePolicy and DefaultCachePolicy tests execu…
pedrobsaila f499323
fix minor issues
pedrobsaila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||||||
|
@@ -689,7 +690,21 @@ public static int DefaultMaximumErrorResponseLength | |||||
get; set; | ||||||
} | ||||||
|
||||||
public static new RequestCachePolicy? DefaultCachePolicy { get; set; } = new RequestCachePolicy(RequestCacheLevel.BypassCache); | ||||||
private static RequestCachePolicy? _defaultCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache); | ||||||
private static bool _isDefaultCachePolicySet; | ||||||
|
||||||
public static new RequestCachePolicy? DefaultCachePolicy | ||||||
{ | ||||||
get | ||||||
{ | ||||||
return _defaultCachePolicy; | ||||||
} | ||||||
set | ||||||
{ | ||||||
_isDefaultCachePolicySet = true; | ||||||
_defaultCachePolicy = value; | ||||||
} | ||||||
} | ||||||
|
||||||
public DateTime IfModifiedSince | ||||||
{ | ||||||
|
@@ -1137,6 +1152,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 +1219,127 @@ private async Task<WebResponse> SendRequest(bool async) | |||||
} | ||||||
} | ||||||
|
||||||
private void AddCacheControlHeaders(HttpRequestMessage request) | ||||||
{ | ||||||
RequestCachePolicy? policy = GetApplicableCachePolicy(); | ||||||
|
||||||
if (policy != null && policy.Level != RequestCacheLevel.BypassCache) | ||||||
{ | ||||||
CacheControlHeaderValue? cacheControl = null; | ||||||
var pragmaHeaders = request.Headers.Pragma; | ||||||
|
||||||
if (policy is HttpRequestCachePolicy httpRequestCachePolicy) | ||||||
{ | ||||||
switch (httpRequestCachePolicy.Level) | ||||||
{ | ||||||
case HttpRequestCacheLevel.NoCacheNoStore: | ||||||
cacheControl = new CacheControlHeaderValue | ||||||
{ | ||||||
NoCache = true, | ||||||
NoStore = true | ||||||
}; | ||||||
pragmaHeaders.Add(new NameValueHeaderValue("no-cache")); | ||||||
break; | ||||||
case HttpRequestCacheLevel.Reload: | ||||||
cacheControl = new CacheControlHeaderValue | ||||||
{ | ||||||
NoCache = true | ||||||
}; | ||||||
pragmaHeaders.Add(new NameValueHeaderValue("no-cache")); | ||||||
break; | ||||||
case HttpRequestCacheLevel.CacheOnly: | ||||||
scalablecory marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
throw new WebException(SR.CacheEntryNotFound, WebExceptionStatus.CacheEntryNotFound); | ||||||
case HttpRequestCacheLevel.CacheOrNextCacheOnly: | ||||||
cacheControl = new CacheControlHeaderValue | ||||||
{ | ||||||
OnlyIfCached = true | ||||||
}; | ||||||
break; | ||||||
case HttpRequestCacheLevel.Default: | ||||||
if (httpRequestCachePolicy.MinFresh > TimeSpan.Zero) | ||||||
{ | ||||||
cacheControl = new CacheControlHeaderValue | ||||||
{ | ||||||
MinFresh = httpRequestCachePolicy.MinFresh | ||||||
}; | ||||||
} | ||||||
|
||||||
if (httpRequestCachePolicy.MaxAge != TimeSpan.MaxValue) | ||||||
{ | ||||||
cacheControl = new CacheControlHeaderValue | ||||||
{ | ||||||
MaxAge = httpRequestCachePolicy.MaxAge | ||||||
}; | ||||||
} | ||||||
|
||||||
if (httpRequestCachePolicy.MaxStale > TimeSpan.Zero) | ||||||
{ | ||||||
cacheControl = new CacheControlHeaderValue | ||||||
{ | ||||||
MaxStale = true, | ||||||
MaxStaleLimit = httpRequestCachePolicy.MaxStale | ||||||
}; | ||||||
} | ||||||
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.
|
||||||
|
||||||
break; | ||||||
case HttpRequestCacheLevel.Refresh: | ||||||
cacheControl = new CacheControlHeaderValue | ||||||
{ | ||||||
MaxAge = TimeSpan.Zero | ||||||
}; | ||||||
pragmaHeaders.Add(new NameValueHeaderValue("no-cache")); | ||||||
break; | ||||||
} | ||||||
} | ||||||
else | ||||||
{ | ||||||
switch (policy.Level) | ||||||
{ | ||||||
case RequestCacheLevel.NoCacheNoStore: | ||||||
cacheControl = new CacheControlHeaderValue | ||||||
{ | ||||||
NoCache = true, | ||||||
NoStore = true | ||||||
}; | ||||||
pragmaHeaders.Add(new NameValueHeaderValue("no-cache")); | ||||||
break; | ||||||
case RequestCacheLevel.Reload: | ||||||
cacheControl = new CacheControlHeaderValue | ||||||
{ | ||||||
NoCache = true | ||||||
}; | ||||||
pragmaHeaders.Add(new NameValueHeaderValue("no-cache")); | ||||||
break; | ||||||
case RequestCacheLevel.CacheOnly: | ||||||
throw new WebException(SR.CacheEntryNotFound, WebExceptionStatus.CacheEntryNotFound); | ||||||
} | ||||||
} | ||||||
|
||||||
if (cacheControl != null) | ||||||
{ | ||||||
request.Headers.CacheControl = cacheControl; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
private RequestCachePolicy? GetApplicableCachePolicy() | ||||||
{ | ||||||
if (CachePolicy != null) | ||||||
{ | ||||||
return CachePolicy; | ||||||
} | ||||||
else if (_isDefaultCachePolicySet && DefaultCachePolicy != null) | ||||||
{ | ||||||
return DefaultCachePolicy; | ||||||
} | ||||||
else if (WebRequest.DefaultCachePolicy != null) | ||||||
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.
Suggested change
|
||||||
{ | ||||||
return WebRequest.DefaultCachePolicy; | ||||||
} | ||||||
|
||||||
return null; | ||||||
} | ||||||
|
||||||
public override IAsyncResult BeginGetResponse(AsyncCallback? callback, object? state) | ||||||
{ | ||||||
CheckAbort(); | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We only
var
if the right hand side isnew T
. This makes sure the code is easy to read outside of an IDE.