Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit d4ee8e5

Browse files
committed
Store content length as header instead of separate field
1 parent 4e7c959 commit d4ee8e5

File tree

5 files changed

+12
-15
lines changed

5 files changed

+12
-15
lines changed

src/Microsoft.AspNetCore.ResponseCaching/Internal/CacheEntry/CachedResponse.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,5 @@ public class CachedResponse : IResponseCacheEntry
1616
public IHeaderDictionary Headers { get; set; }
1717

1818
public Stream Body { get; set; }
19-
20-
public string BodyLengthString { get; set; }
2119
}
2220
}

src/Microsoft.AspNetCore.ResponseCaching/Internal/MemoryCachedResponse.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,5 @@ internal class MemoryCachedResponse
1818
public List<byte[]> BodySegments { get; set; }
1919

2020
public long BodyLength { get; set; }
21-
22-
public string BodyLengthString { get; set; }
2321
}
2422
}

src/Microsoft.AspNetCore.ResponseCaching/Internal/MemoryResponseCache.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public Task<IResponseCacheEntry> GetAsync(string key)
3535
StatusCode = memoryCachedResponse.StatusCode,
3636
Headers = memoryCachedResponse.Headers,
3737
Body = new SegmentReadStream(memoryCachedResponse.BodySegments, memoryCachedResponse.BodyLength),
38-
BodyLengthString = memoryCachedResponse.BodyLengthString
3938
});
4039
}
4140
else
@@ -60,8 +59,7 @@ public async Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan valid
6059
StatusCode = cachedResponse.StatusCode,
6160
Headers = cachedResponse.Headers,
6261
BodySegments = segmentStream.GetSegments(),
63-
BodyLength = segmentStream.Length,
64-
BodyLengthString = HeaderUtilities.FormatInt64(segmentStream.Length)
62+
BodyLength = segmentStream.Length
6563
},
6664
new MemoryCacheEntryOptions
6765
{

src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingMiddleware.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Globalization;
76
using System.Threading.Tasks;
87
using Microsoft.AspNetCore.Http;
98
using Microsoft.AspNetCore.Http.Features;
@@ -151,12 +150,6 @@ internal async Task<bool> TryServeCachedResponseAsync(ResponseCachingContext con
151150
var body = context.CachedResponse.Body;
152151
if (body.Length > 0)
153152
{
154-
// Add a content-length if required
155-
if (!response.ContentLength.HasValue && StringValues.IsNullOrEmpty(response.Headers[HeaderNames.TransferEncoding]))
156-
{
157-
response.Headers[HeaderNames.ContentLength] = context.CachedResponse.BodyLengthString;
158-
}
159-
160153
try
161154
{
162155
await body.CopyToAsync(response.Body, StreamUtilities.BodySegmentSize, context.HttpContext.RequestAborted);
@@ -292,6 +285,13 @@ internal async Task FinalizeCacheBodyAsync(ResponseCachingContext context)
292285
var bufferStream = context.ResponseCachingStream.GetBufferStream();
293286
if (!contentLength.HasValue || contentLength == bufferStream.Length)
294287
{
288+
var response = context.HttpContext.Response;
289+
// Add a content-length if required
290+
if (!response.ContentLength.HasValue && StringValues.IsNullOrEmpty(response.Headers[HeaderNames.TransferEncoding]))
291+
{
292+
context.CachedResponse.Headers[HeaderNames.ContentLength] = HeaderUtilities.FormatInt64(bufferStream.Length);
293+
}
294+
295295
context.CachedResponse.Body = bufferStream;
296296
_logger.LogResponseCached();
297297
await _cache.SetAsync(context.StorageVaryKey ?? context.BaseKey, context.CachedResponse, context.CachedResponseValidFor);

test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingMiddlewareTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,10 @@ public async Task FinalizeCacheBody_Cache_IfContentLengthAbsent()
668668
await context.HttpContext.Response.WriteAsync(new string('0', 10));
669669

670670
context.ShouldCacheResponse = true;
671-
context.CachedResponse = new CachedResponse();
671+
context.CachedResponse = new CachedResponse()
672+
{
673+
Headers = new HeaderDictionary()
674+
};
672675
context.BaseKey = "BaseKey";
673676
context.CachedResponseValidFor = TimeSpan.FromSeconds(10);
674677

0 commit comments

Comments
 (0)