Skip to content

Commit 12828cc

Browse files
Use HeaderNames constants and fix formatting
1 parent c5b394b commit 12828cc

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/Umbraco.StorageProviders.AzureBlob/AzureBlobFileSystemMiddleware.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
114114
// a Content-Range header is needed with the new length
115115
ignoreRange = true;
116116
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}");
118118
}
119119
catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.NotModified)
120120
{
@@ -128,15 +128,15 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
128128
// and not a request failed with status NotModified :(
129129
catch (Exception ex) when (ex.Message == "The condition specified using HTTP conditional header(s) is not met.")
130130
{
131-
if (blobRequestConditions != null
132-
&& (blobRequestConditions.IfMatch.HasValue || blobRequestConditions.IfUnmodifiedSince.HasValue))
131+
if (blobRequestConditions != null &&
132+
(blobRequestConditions.IfMatch.HasValue || blobRequestConditions.IfUnmodifiedSince.HasValue))
133133
{
134134
// If-Range or If-Unmodified-Since is not met
135135
// if the resource has been modified, we need to send the whole file back with a 200 OK
136136
// a Content-Range header is needed with the new length
137137
ignoreRange = true;
138138
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}");
140140
}
141141
else
142142
{
@@ -155,13 +155,12 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
155155

156156
var responseHeaders = response.GetTypedHeaders();
157157

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+
};
165164

166165
responseHeaders.LastModified = properties.Value.LastModified;
167166

@@ -170,12 +169,10 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
170169
responseHeaders.ETag = entityTagHeaderValue;
171170
}
172171

173-
responseHeaders.Append(HeaderNames.Vary, "Accept-Encoding");
172+
responseHeaders.Append(HeaderNames.Vary, HeaderNames.AcceptEncoding);
174173

175174
var requestHeaders = request.GetTypedHeaders();
176-
177175
var rangeHeader = requestHeaders.Range;
178-
179176
if (!ignoreRange && rangeHeader != null)
180177
{
181178
if (!ValidateRanges(rangeHeader.Ranges, properties.Value.ContentLength))
@@ -228,6 +225,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
228225
return;
229226
}
230227
}
228+
231229
response.StatusCode = (int)HttpStatusCode.OK;
232230
response.ContentType = properties.Value.ContentType;
233231
responseHeaders.ContentLength = properties.Value.ContentLength;
@@ -239,11 +237,11 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
239237

240238
private static BlobRequestConditions? GetAccessCondition(HttpRequest request)
241239
{
242-
var range = request.Headers["Range"];
240+
var range = request.Headers[HeaderNames.Range];
243241
if (string.IsNullOrEmpty(range))
244242
{
245243
// etag
246-
var ifNoneMatch = request.Headers["If-None-Match"];
244+
var ifNoneMatch = request.Headers[HeaderNames.IfNoneMatch];
247245
if (!string.IsNullOrEmpty(ifNoneMatch))
248246
{
249247
return new BlobRequestConditions
@@ -252,7 +250,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
252250
};
253251
}
254252

255-
var ifModifiedSince = request.Headers["If-Modified-Since"];
253+
var ifModifiedSince = request.Headers[HeaderNames.IfModifiedSince];
256254
if (!string.IsNullOrEmpty(ifModifiedSince) &&
257255
DateTimeOffset.TryParse(ifModifiedSince, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTimeOffset ifModifiedSinceDate))
258256
{
@@ -266,7 +264,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
266264
{
267265
// handle If-Range header, it can be either an etag or a date
268266
// 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];
270268
if (!string.IsNullOrEmpty(ifRange))
271269
{
272270
if (DateTimeOffset.TryParse(ifRange, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTimeOffset ifRangeDate))
@@ -285,7 +283,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
285283
}
286284
}
287285

288-
var ifUnmodifiedSince = request.Headers["If-Unmodified-Since"];
286+
var ifUnmodifiedSince = request.Headers[HeaderNames.IfUnmodifiedSince];
289287
if (!string.IsNullOrEmpty(ifUnmodifiedSince) &&
290288
DateTimeOffset.TryParse(ifUnmodifiedSince, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTimeOffset ifUnmodifiedSinceDate))
291289
{
@@ -302,14 +300,21 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
302300
private static bool ValidateRanges(ICollection<RangeItemHeaderValue> ranges, long length)
303301
{
304302
if (ranges.Count == 0)
303+
{
305304
return false;
305+
}
306306

307307
foreach (var range in ranges)
308308
{
309309
if (range.From > range.To)
310+
{
310311
return false;
312+
}
313+
311314
if (range.To >= length)
315+
{
312316
return false;
317+
}
313318
}
314319

315320
return true;
@@ -348,8 +353,7 @@ private static ContentRangeHeaderValue GetRangeHeader(BlobProperties properties,
348353
return new ContentRangeHeaderValue(from, to, properties.ContentLength);
349354
}
350355

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)
353357
{
354358
var offset = contentRange.From.GetValueOrDefault(0L);
355359
var length = properties.ContentLength;
@@ -370,8 +374,7 @@ private static async Task DownloadRangeToStreamAsync(BlobClient blob, BlobProper
370374
await DownloadRangeToStreamAsync(blob, outputStream, offset, length, cancellationToken).ConfigureAwait(false);
371375
}
372376

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)
375378
{
376379
try
377380
{

0 commit comments

Comments
 (0)