This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
FileResultExecutorBase.cs
349 lines (309 loc) · 13.7 KB
/
FileResultExecutorBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Mvc.Infrastructure
{
public class FileResultExecutorBase
{
private const string AcceptRangeHeaderValue = "bytes";
protected const int BufferSize = 64 * 1024;
public FileResultExecutorBase(ILogger logger)
{
Logger = logger;
}
internal enum PreconditionState
{
Unspecified,
NotModified,
ShouldProcess,
PreconditionFailed
}
protected ILogger Logger { get; }
protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody) SetHeadersAndLog(
ActionContext context,
FileResult result,
long? fileLength,
DateTimeOffset? lastModified = null,
EntityTagHeaderValue etag = null)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
SetContentType(context, result);
SetContentDispositionHeader(context, result);
Logger.FileResultExecuting(result.FileDownloadName);
var request = context.HttpContext.Request;
var httpRequestHeaders = request.GetTypedHeaders();
var response = context.HttpContext.Response;
var httpResponseHeaders = response.GetTypedHeaders();
if (lastModified.HasValue)
{
httpResponseHeaders.LastModified = lastModified;
}
if (etag != null)
{
httpResponseHeaders.ETag = etag;
}
var serveBody = !HttpMethods.IsHead(request.Method);
var preconditionState = GetPreconditionState(context, httpRequestHeaders, lastModified, etag);
if (preconditionState == PreconditionState.NotModified)
{
serveBody = false;
response.StatusCode = StatusCodes.Status304NotModified;
}
else if (preconditionState == PreconditionState.PreconditionFailed)
{
serveBody = false;
response.StatusCode = StatusCodes.Status412PreconditionFailed;
}
if (fileLength.HasValue)
{
SetAcceptRangeHeader(context);
// Assuming the request is not a range request, the Content-Length header is set to the length of the entire file.
// If the request is a valid range request, this header is overwritten with the length of the range as part of the
// range processing (see method SetContentLength).
response.ContentLength = fileLength.Value;
if (HttpMethods.IsHead(request.Method) || HttpMethods.IsGet(request.Method))
{
if ((preconditionState == PreconditionState.Unspecified ||
preconditionState == PreconditionState.ShouldProcess))
{
if (IfRangeValid(context, httpRequestHeaders, lastModified, etag))
{
return SetRangeHeaders(context, httpRequestHeaders, fileLength.Value);
}
}
}
}
return (range: null, rangeLength: 0, serveBody: serveBody);
}
private static void SetContentType(ActionContext context, FileResult result)
{
var response = context.HttpContext.Response;
response.ContentType = result.ContentType;
}
private static void SetContentDispositionHeader(ActionContext context, FileResult result)
{
if (!string.IsNullOrEmpty(result.FileDownloadName))
{
// From RFC 2183, Sec. 2.3:
// The sender may want to suggest a filename to be used if the entity is
// detached and stored in a separate file. If the receiving MUA writes
// the entity to a file, the suggested filename should be used as a
// basis for the actual filename, where possible.
var contentDisposition = new ContentDispositionHeaderValue("attachment");
contentDisposition.SetHttpFileName(result.FileDownloadName);
context.HttpContext.Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();
}
}
private static void SetAcceptRangeHeader(ActionContext context)
{
var response = context.HttpContext.Response;
response.Headers[HeaderNames.AcceptRanges] = AcceptRangeHeaderValue;
}
private static PreconditionState GetEtagMatchState(
IList<EntityTagHeaderValue> etagHeader,
EntityTagHeaderValue etag,
PreconditionState matchFoundState,
PreconditionState matchNotFoundState)
{
if (etagHeader != null && etagHeader.Any())
{
var state = matchNotFoundState;
foreach (var entityTag in etagHeader)
{
if (entityTag.Equals(EntityTagHeaderValue.Any) || entityTag.Compare(etag, useStrongComparison: true))
{
state = matchFoundState;
break;
}
}
return state;
}
return PreconditionState.Unspecified;
}
internal static bool IfRangeValid(
ActionContext context,
RequestHeaders httpRequestHeaders,
DateTimeOffset? lastModified = null,
EntityTagHeaderValue etag = null)
{
// 14.27 If-Range
var ifRange = httpRequestHeaders.IfRange;
if (ifRange != null)
{
// If the validator given in the If-Range header field matches the
// current validator for the selected representation of the target
// resource, then the server SHOULD process the Range header field as
// requested. If the validator does not match, the server MUST ignore
// the Range header field.
if (ifRange.LastModified.HasValue)
{
if (lastModified.HasValue && lastModified > ifRange.LastModified)
{
return false;
}
}
else if (etag != null && ifRange.EntityTag != null && !ifRange.EntityTag.Compare(etag, useStrongComparison: true))
{
return false;
}
}
return true;
}
// Internal for testing
internal static PreconditionState GetPreconditionState(
ActionContext context,
RequestHeaders httpRequestHeaders,
DateTimeOffset? lastModified = null,
EntityTagHeaderValue etag = null)
{
var ifMatchState = PreconditionState.Unspecified;
var ifNoneMatchState = PreconditionState.Unspecified;
var ifModifiedSinceState = PreconditionState.Unspecified;
var ifUnmodifiedSinceState = PreconditionState.Unspecified;
// 14.24 If-Match
var ifMatch = httpRequestHeaders.IfMatch;
if (etag != null)
{
ifMatchState = GetEtagMatchState(
etagHeader: ifMatch,
etag: etag,
matchFoundState: PreconditionState.ShouldProcess,
matchNotFoundState: PreconditionState.PreconditionFailed);
}
// 14.26 If-None-Match
var ifNoneMatch = httpRequestHeaders.IfNoneMatch;
if (etag != null)
{
ifNoneMatchState = GetEtagMatchState(
etagHeader: ifNoneMatch,
etag: etag,
matchFoundState: PreconditionState.NotModified,
matchNotFoundState: PreconditionState.ShouldProcess);
}
var now = DateTimeOffset.UtcNow;
// 14.25 If-Modified-Since
var ifModifiedSince = httpRequestHeaders.IfModifiedSince;
if (lastModified.HasValue && ifModifiedSince.HasValue && ifModifiedSince <= now)
{
var modified = ifModifiedSince < lastModified;
ifModifiedSinceState = modified ? PreconditionState.ShouldProcess : PreconditionState.NotModified;
}
// 14.28 If-Unmodified-Since
var ifUnmodifiedSince = httpRequestHeaders.IfUnmodifiedSince;
if (lastModified.HasValue && ifUnmodifiedSince.HasValue && ifUnmodifiedSince <= now)
{
var unmodified = ifUnmodifiedSince >= lastModified;
ifUnmodifiedSinceState = unmodified ? PreconditionState.ShouldProcess : PreconditionState.PreconditionFailed;
}
var state = GetMaxPreconditionState(ifMatchState, ifNoneMatchState, ifModifiedSinceState, ifUnmodifiedSinceState);
return state;
}
private static PreconditionState GetMaxPreconditionState(params PreconditionState[] states)
{
var max = PreconditionState.Unspecified;
for (var i = 0; i < states.Length; i++)
{
if (states[i] > max)
{
max = states[i];
}
}
return max;
}
private static (RangeItemHeaderValue range, long rangeLength, bool serveBody) SetRangeHeaders(
ActionContext context,
RequestHeaders httpRequestHeaders,
long fileLength)
{
var response = context.HttpContext.Response;
var httpResponseHeaders = response.GetTypedHeaders();
// Range may be null for empty range header, invalid ranges, parsing errors, multiple ranges
// and when the file length is zero.
var (isRangeRequest, range) = RangeHelper.ParseRange(
context.HttpContext,
httpRequestHeaders,
fileLength);
if (!isRangeRequest)
{
return (range: null, rangeLength: 0, serveBody: true);
}
if (range == null)
{
// 14.16 Content-Range - A server sending a response with status code 416 (Requested range not satisfiable)
// SHOULD include a Content-Range field with a byte-range-resp-spec of "*". The instance-length specifies
// the current length of the selected resource. e.g. */length
response.StatusCode = StatusCodes.Status416RangeNotSatisfiable;
httpResponseHeaders.ContentRange = new ContentRangeHeaderValue(fileLength);
return (range: null, rangeLength: 0, serveBody: false);
}
httpResponseHeaders.ContentRange = new ContentRangeHeaderValue(
range.From.Value,
range.To.Value,
fileLength);
response.StatusCode = StatusCodes.Status206PartialContent;
// Overwrite the Content-Length header for valid range requests with the range length.
var rangeLength = SetContentLength(context, range);
return (range, rangeLength, serveBody: true);
}
private static long SetContentLength(ActionContext context, RangeItemHeaderValue range)
{
var start = range.From.Value;
var end = range.To.Value;
var length = end - start + 1;
var response = context.HttpContext.Response;
response.ContentLength = length;
return length;
}
protected static ILogger CreateLogger<T>(ILoggerFactory factory)
{
if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}
return factory.CreateLogger<T>();
}
protected static async Task WriteFileAsync(HttpContext context, Stream fileStream, RangeItemHeaderValue range, long rangeLength)
{
var outputStream = context.Response.Body;
using (fileStream)
{
try
{
if (range == null)
{
await StreamCopyOperation.CopyToAsync(fileStream, outputStream, count: null, bufferSize: BufferSize, cancel: context.RequestAborted);
}
else
{
fileStream.Seek(range.From.Value, SeekOrigin.Begin);
await StreamCopyOperation.CopyToAsync(fileStream, outputStream, rangeLength, BufferSize, context.RequestAborted);
}
}
catch (OperationCanceledException)
{
// Don't throw this exception, it's most likely caused by the client disconnecting.
// However, if it was cancelled for any other reason we need to prevent empty responses.
context.Abort();
}
}
}
}
}