@@ -114,7 +114,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
114
114
// a Content-Range header is needed with the new length
115
115
ignoreRange = true ;
116
116
properties = await blob . GetPropertiesAsync ( ) . ConfigureAwait ( false ) ;
117
- response . Headers . Append ( "Content-Range" , $ "bytes */{ properties . Value . ContentLength } ") ;
117
+ response . Headers . Append ( HeaderNames . ContentRange , $ "bytes */{ properties . Value . ContentLength } ") ;
118
118
}
119
119
catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . NotModified )
120
120
{
@@ -128,15 +128,15 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
128
128
// and not a request failed with status NotModified :(
129
129
catch ( Exception ex ) when ( ex . Message == "The condition specified using HTTP conditional header(s) is not met." )
130
130
{
131
- if ( blobRequestConditions != null
132
- && ( blobRequestConditions . IfMatch . HasValue || blobRequestConditions . IfUnmodifiedSince . HasValue ) )
131
+ if ( blobRequestConditions != null &&
132
+ ( blobRequestConditions . IfMatch . HasValue || blobRequestConditions . IfUnmodifiedSince . HasValue ) )
133
133
{
134
134
// If-Range or If-Unmodified-Since is not met
135
135
// if the resource has been modified, we need to send the whole file back with a 200 OK
136
136
// a Content-Range header is needed with the new length
137
137
ignoreRange = true ;
138
138
properties = await blob . GetPropertiesAsync ( ) . ConfigureAwait ( false ) ;
139
- response . Headers . Append ( "Content-Range" , $ "bytes */{ properties . Value . ContentLength } ") ;
139
+ response . Headers . Append ( HeaderNames . ContentRange , $ "bytes */{ properties . Value . ContentLength } ") ;
140
140
}
141
141
else
142
142
{
@@ -155,13 +155,12 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
155
155
156
156
var responseHeaders = response . GetTypedHeaders ( ) ;
157
157
158
- responseHeaders . CacheControl =
159
- new CacheControlHeaderValue
160
- {
161
- Public = true ,
162
- MustRevalidate = true ,
163
- MaxAge = _maxAge ,
164
- } ;
158
+ responseHeaders . CacheControl = new CacheControlHeaderValue
159
+ {
160
+ Public = true ,
161
+ MustRevalidate = true ,
162
+ MaxAge = _maxAge ,
163
+ } ;
165
164
166
165
responseHeaders . LastModified = properties . Value . LastModified ;
167
166
@@ -170,12 +169,10 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
170
169
responseHeaders . ETag = entityTagHeaderValue ;
171
170
}
172
171
173
- responseHeaders . Append ( HeaderNames . Vary , "Accept-Encoding" ) ;
172
+ responseHeaders . Append ( HeaderNames . Vary , HeaderNames . AcceptEncoding ) ;
174
173
175
174
var requestHeaders = request . GetTypedHeaders ( ) ;
176
-
177
175
var rangeHeader = requestHeaders . Range ;
178
-
179
176
if ( ! ignoreRange && rangeHeader != null )
180
177
{
181
178
if ( ! ValidateRanges ( rangeHeader . Ranges , properties . Value . ContentLength ) )
@@ -228,6 +225,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
228
225
return ;
229
226
}
230
227
}
228
+
231
229
response . StatusCode = ( int ) HttpStatusCode . OK ;
232
230
response . ContentType = properties . Value . ContentType ;
233
231
responseHeaders . ContentLength = properties . Value . ContentLength ;
@@ -239,11 +237,11 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
239
237
240
238
private static BlobRequestConditions ? GetAccessCondition ( HttpRequest request )
241
239
{
242
- var range = request . Headers [ " Range" ] ;
240
+ var range = request . Headers [ HeaderNames . Range ] ;
243
241
if ( string . IsNullOrEmpty ( range ) )
244
242
{
245
243
// etag
246
- var ifNoneMatch = request . Headers [ "If-None-Match" ] ;
244
+ var ifNoneMatch = request . Headers [ HeaderNames . IfNoneMatch ] ;
247
245
if ( ! string . IsNullOrEmpty ( ifNoneMatch ) )
248
246
{
249
247
return new BlobRequestConditions
@@ -252,7 +250,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
252
250
} ;
253
251
}
254
252
255
- var ifModifiedSince = request . Headers [ "If-Modified-Since" ] ;
253
+ var ifModifiedSince = request . Headers [ HeaderNames . IfModifiedSince ] ;
256
254
if ( ! string . IsNullOrEmpty ( ifModifiedSince ) &&
257
255
DateTimeOffset . TryParse ( ifModifiedSince , CultureInfo . InvariantCulture , DateTimeStyles . None , out DateTimeOffset ifModifiedSinceDate ) )
258
256
{
@@ -266,7 +264,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
266
264
{
267
265
// handle If-Range header, it can be either an etag or a date
268
266
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Range and https://tools.ietf.org/html/rfc7233#section-3.2
269
- var ifRange = request . Headers [ "If-Range" ] ;
267
+ var ifRange = request . Headers [ HeaderNames . IfRange ] ;
270
268
if ( ! string . IsNullOrEmpty ( ifRange ) )
271
269
{
272
270
if ( DateTimeOffset . TryParse ( ifRange , CultureInfo . InvariantCulture , DateTimeStyles . None , out DateTimeOffset ifRangeDate ) )
@@ -285,7 +283,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
285
283
}
286
284
}
287
285
288
- var ifUnmodifiedSince = request . Headers [ "If-Unmodified-Since" ] ;
286
+ var ifUnmodifiedSince = request . Headers [ HeaderNames . IfUnmodifiedSince ] ;
289
287
if ( ! string . IsNullOrEmpty ( ifUnmodifiedSince ) &&
290
288
DateTimeOffset . TryParse ( ifUnmodifiedSince , CultureInfo . InvariantCulture , DateTimeStyles . None , out DateTimeOffset ifUnmodifiedSinceDate ) )
291
289
{
@@ -302,14 +300,21 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
302
300
private static bool ValidateRanges ( ICollection < RangeItemHeaderValue > ranges , long length )
303
301
{
304
302
if ( ranges . Count == 0 )
303
+ {
305
304
return false ;
305
+ }
306
306
307
307
foreach ( var range in ranges )
308
308
{
309
309
if ( range . From > range . To )
310
+ {
310
311
return false ;
312
+ }
313
+
311
314
if ( range . To >= length )
315
+ {
312
316
return false ;
317
+ }
313
318
}
314
319
315
320
return true ;
@@ -348,8 +353,7 @@ private static ContentRangeHeaderValue GetRangeHeader(BlobProperties properties,
348
353
return new ContentRangeHeaderValue ( from , to , properties . ContentLength ) ;
349
354
}
350
355
351
- private static async Task DownloadRangeToStreamAsync ( BlobClient blob , BlobProperties properties ,
352
- Stream outputStream , ContentRangeHeaderValue contentRange , CancellationToken cancellationToken )
356
+ private static async Task DownloadRangeToStreamAsync ( BlobClient blob , BlobProperties properties , Stream outputStream , ContentRangeHeaderValue contentRange , CancellationToken cancellationToken )
353
357
{
354
358
var offset = contentRange . From . GetValueOrDefault ( 0L ) ;
355
359
var length = properties . ContentLength ;
@@ -370,8 +374,7 @@ private static async Task DownloadRangeToStreamAsync(BlobClient blob, BlobProper
370
374
await DownloadRangeToStreamAsync ( blob , outputStream , offset , length , cancellationToken ) . ConfigureAwait ( false ) ;
371
375
}
372
376
373
- private static async Task DownloadRangeToStreamAsync ( BlobClient blob , Stream outputStream ,
374
- long offset , long length , CancellationToken cancellationToken )
377
+ private static async Task DownloadRangeToStreamAsync ( BlobClient blob , Stream outputStream , long offset , long length , CancellationToken cancellationToken )
375
378
{
376
379
try
377
380
{
0 commit comments