Skip to content

Commit

Permalink
fix ContentEncoding bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lanlanlee2008 committed Nov 30, 2016
1 parent 302570c commit 6835af8
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/OutputCacheModuleAsync/OutputCacheHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public async Task<object> GetAsCacheVaryAsync(CachedVary cachedVary) {
if (index > -1) {
identityIsAcceptable = false;
// the client Accept-Encoding header contains an encoding that's in the VaryByContentEncoding list
item = await GetAsync(key + contentEncodings[index]);
item = await GetAsync(key);
if (item != null) {
continue;
}
Expand Down Expand Up @@ -345,7 +345,7 @@ public bool IsResponseCacheable() {
return false;
}
return cache.VaryByContentEncodings.GetContentEncodings() == null ||
IsCacheableEncoding(_context.Response.ContentEncoding,
IsCacheableEncoding(_context.Request.Headers[HttpHeaders.AcceptEncoding],
cache.VaryByContentEncodings);
}

Expand Down Expand Up @@ -633,13 +633,23 @@ private CacheItemPolicy GetCacheItemPolicy(CacheDependency dependency) {
return cacheItemPolicy;
}

private bool IsCacheableEncoding(Encoding contentEncoding, HttpCacheVaryByContentEncodings varyByContentEncodings) {
private bool IsCacheableEncoding(string headerContentEncodings, HttpCacheVaryByContentEncodings varyByContentEncodings) {
// return true if we are not varying by content encoding.
if (varyByContentEncodings == null) {
return true;
}
// return true if there is no Content-Encoding header or the Content-Encoding header is listed
return contentEncoding == null || varyByContentEncodings.GetContentEncodings().Any(varyByContentEncoding => varyByContentEncoding.Equals(contentEncoding.ToString(), StringComparison.OrdinalIgnoreCase));
// return true if there is no Content-Encoding header
if (headerContentEncodings == null) {
return true;
}
// return true if the Content-Encoding header is listed within varyByContentEncodings
string[] headerContentEncodingCollection = headerContentEncodings.Split(new Char[] { ',' });
foreach (string headerContentEncoding in headerContentEncodingCollection) {
if (varyByContentEncodings.GetContentEncodings().Any(varyByContentEncoding => varyByContentEncoding.Equals(headerContentEncoding, StringComparison.OrdinalIgnoreCase))) {
return true;
}
}
return false;
}

private bool ContainsNonShareableCookies() {
Expand Down Expand Up @@ -948,9 +958,14 @@ private string CreateOutputCachedItemKey(string path, string verb, CachedVary ca
if (contentEncodings == null) {
return sb.ToString();
}
string coding = _context.Response.HeaderEncoding.ToString();
if (contentEncodings.Any(t => t.Equals(coding,StringComparison.OrdinalIgnoreCase))) {
sb.Append(coding);
if (_context.Request.Headers[HttpHeaders.AcceptEncoding] != null) {
string[] headerContentEncodingCollection = _context.Request.Headers[HttpHeaders.AcceptEncoding].Split(new char[] { ',' });
foreach (string headerContentEncoding in headerContentEncodingCollection) {
if (contentEncodings.Any(t => t.Equals(headerContentEncoding, StringComparison.OrdinalIgnoreCase))) {
sb.Append(headerContentEncoding);
break;
}
}
}
// The key must end in "E", or the VaryByContentEncoding feature will break. Unfortunately,
// there was no good way to encapsulate the logic within this routine. See the code in
Expand Down Expand Up @@ -1073,8 +1088,9 @@ private bool CheckIfNoneMatch(HttpCachePolicySettings settings) {
return true;
}
}
return false;
}
return false;
return true;
}

private bool CheckIfModifiedSince(HttpCachePolicySettings settings) {
Expand Down

0 comments on commit 6835af8

Please sign in to comment.