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

Commit 212c264

Browse files
committed
Use strongly typed headers.
1 parent 82be0d3 commit 212c264

File tree

8 files changed

+70
-181
lines changed

8 files changed

+70
-181
lines changed

src/Microsoft.AspNet.StaticFiles/Constants.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,6 @@ internal static class Constants
1111
internal const string SendFileVersionKey = "sendfile.Version";
1212
internal const string SendFileVersion = "1.0";
1313

14-
internal const string Location = "Location";
15-
internal const string IfMatch = "If-Match";
16-
internal const string IfNoneMatch = "If-None-Match";
17-
internal const string IfModifiedSince = "If-Modified-Since";
18-
internal const string IfUnmodifiedSince = "If-Unmodified-Since";
19-
internal const string IfRange = "If-Range";
20-
internal const string Range = "Range";
21-
internal const string ContentRange = "Content-Range";
22-
internal const string LastModified = "Last-Modified";
23-
internal const string ETag = "ETag";
24-
25-
internal const string HttpDateFormat = "r";
26-
27-
internal const string TextHtmlUtf8 = "text/html; charset=utf-8";
28-
2914
internal const int Status200Ok = 200;
3015
internal const int Status206PartialContent = 206;
3116
internal const int Status304NotModified = 304;

src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
64
using System.Threading.Tasks;
75
using Microsoft.AspNet.Builder;
8-
using Microsoft.AspNet.FileSystems;
96
using Microsoft.AspNet.Hosting;
107
using Microsoft.AspNet.Http;
8+
using Microsoft.Net.Http.Headers;
119

1210
namespace Microsoft.AspNet.StaticFiles
1311
{
@@ -65,7 +63,7 @@ public Task Invoke(HttpContext context)
6563
if (!Helpers.PathEndsInSlash(context.Request.Path))
6664
{
6765
context.Response.StatusCode = 301;
68-
context.Response.Headers[Constants.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
66+
context.Response.Headers[HeaderNames.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
6967
return Constants.CompletedTask;
7068
}
7169

src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Threading.Tasks;
76
using Microsoft.AspNet.Builder;
87
using Microsoft.AspNet.FileSystems;
98
using Microsoft.AspNet.Hosting;
109
using Microsoft.AspNet.Http;
10+
using Microsoft.Net.Http.Headers;
1111

1212
namespace Microsoft.AspNet.StaticFiles
1313
{
@@ -57,7 +57,7 @@ public Task Invoke(HttpContext context)
5757
if (!Helpers.PathEndsInSlash(context.Request.Path))
5858
{
5959
context.Response.StatusCode = 301;
60-
context.Response.Headers[Constants.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
60+
context.Response.Headers[HeaderNames.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
6161
return Constants.CompletedTask;
6262
}
6363

src/Microsoft.AspNet.StaticFiles/Helpers.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Globalization;
6-
using System.IO;
75
using Microsoft.AspNet.Http;
86

97
namespace Microsoft.AspNet.StaticFiles
@@ -45,15 +43,5 @@ internal static bool TryMatchPath(HttpContext context, PathString matchUrl, bool
4543
}
4644
return false;
4745
}
48-
49-
internal static bool TryParseHttpDate(string dateString, out DateTimeOffset parsedDate)
50-
{
51-
return DateTimeOffset.TryParseExact(dateString, Constants.HttpDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate);
52-
}
53-
54-
internal static string ResolveRootPath(string webRoot, PathString path)
55-
{
56-
return Path.GetFullPath(Path.Combine(webRoot, path.Value ?? string.Empty));
57-
}
5846
}
5947
}

src/Microsoft.AspNet.StaticFiles/HtmlDirectoryFormatter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace Microsoft.AspNet.StaticFiles
1818
/// </summary>
1919
public class HtmlDirectoryFormatter : IDirectoryFormatter
2020
{
21+
private const string TextHtmlUtf8 = "text/html; charset=utf-8";
22+
2123
/// <summary>
2224
/// Generates an HTML view for a directory.
2325
/// </summary>
@@ -32,7 +34,7 @@ public virtual Task GenerateContentAsync(HttpContext context, IEnumerable<IFileI
3234
throw new ArgumentNullException("contents");
3335
}
3436

35-
context.Response.ContentType = Constants.TextHtmlUtf8;
37+
context.Response.ContentType = TextHtmlUtf8;
3638

3739
if (Helpers.IsHeadMethod(context.Request.Method))
3840
{

src/Microsoft.AspNet.StaticFiles/Infrastructure/RangeHelpers.cs

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

44
using System;
55
using System.Collections.Generic;
6-
using System.Globalization;
7-
using System.Linq;
6+
using Microsoft.Net.Http.Headers;
87

98
namespace Microsoft.AspNet.StaticFiles.Infrastructure
109
{
1110
internal static class RangeHelpers
1211
{
13-
// Examples:
14-
// bytes=0-499
15-
// bytes=500-
16-
// bytes=-500
17-
// bytes=0-0,-1
18-
// bytes=500-600,601-999
19-
// Any individual bad range fails the whole parse and the header should be ignored.
20-
internal static bool TryParseRanges(string rangeHeader, out IList<Tuple<long?, long?>> parsedRanges)
21-
{
22-
parsedRanges = null;
23-
if (string.IsNullOrWhiteSpace(rangeHeader)
24-
|| !rangeHeader.StartsWith("bytes=", StringComparison.OrdinalIgnoreCase))
25-
{
26-
return false;
27-
}
28-
29-
string[] subRanges = rangeHeader.Substring("bytes=".Length).Replace(" ", string.Empty).Split(',');
30-
31-
List<Tuple<long?, long?>> ranges = new List<Tuple<long?, long?>>();
32-
33-
for (int i = 0; i < subRanges.Length; i++)
34-
{
35-
long? first = null, second = null;
36-
string subRange = subRanges[i];
37-
int dashIndex = subRange.IndexOf('-');
38-
if (dashIndex < 0)
39-
{
40-
return false;
41-
}
42-
else if (dashIndex == 0)
43-
{
44-
// -500
45-
string remainder = subRange.Substring(1);
46-
if (!TryParseLong(remainder, out second))
47-
{
48-
return false;
49-
}
50-
}
51-
else if (dashIndex == (subRange.Length - 1))
52-
{
53-
// 500-
54-
string remainder = subRange.Substring(0, subRange.Length - 1);
55-
if (!TryParseLong(remainder, out first))
56-
{
57-
return false;
58-
}
59-
}
60-
else
61-
{
62-
// 0-499
63-
string firstString = subRange.Substring(0, dashIndex);
64-
string secondString = subRange.Substring(dashIndex + 1, subRange.Length - dashIndex - 1);
65-
if (!TryParseLong(firstString, out first) || !TryParseLong(secondString, out second)
66-
|| first.Value > second.Value)
67-
{
68-
return false;
69-
}
70-
}
71-
72-
ranges.Add(new Tuple<long?, long?>(first, second));
73-
}
74-
75-
if (ranges.Count > 0)
76-
{
77-
parsedRanges = ranges;
78-
return true;
79-
}
80-
return false;
81-
}
82-
83-
private static bool TryParseLong(string input, out long? result)
84-
{
85-
int temp;
86-
if (!string.IsNullOrWhiteSpace(input)
87-
&& int.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out temp))
88-
{
89-
result = temp;
90-
return true;
91-
}
92-
result = null;
93-
return false;
94-
}
95-
9612
// 14.35.1 Byte Ranges - If a syntactically valid byte-range-set includes at least one byte-range-spec whose
9713
// first-byte-pos is less than the current length of the entity-body, or at least one suffix-byte-range-spec
9814
// with a non-zero suffix-length, then the byte-range-set is satisfiable.
9915
// Adjusts ranges to be absolute and within bounds.
100-
internal static IList<Tuple<long, long>> NormalizeRanges(IList<Tuple<long?, long?>> ranges, long length)
16+
internal static IList<RangeItemHeaderValue> NormalizeRanges(ICollection<RangeItemHeaderValue> ranges, long length)
10117
{
102-
IList<Tuple<long, long>> normalizedRanges = new List<Tuple<long, long>>(ranges.Count);
103-
for (int i = 0; i < ranges.Count; i++)
18+
IList<RangeItemHeaderValue> normalizedRanges = new List<RangeItemHeaderValue>(ranges.Count);
19+
foreach (var range in ranges)
10420
{
105-
Tuple<long?, long?> range = ranges[i];
106-
long? start = range.Item1, end = range.Item2;
21+
long? start = range.From;
22+
long? end = range.To;
10723

10824
// X-[Y]
10925
if (start.HasValue)
@@ -131,7 +47,7 @@ internal static IList<Tuple<long, long>> NormalizeRanges(IList<Tuple<long?, long
13147
start = length - bytes;
13248
end = start + bytes - 1;
13349
}
134-
normalizedRanges.Add(new Tuple<long, long>(start.Value, end.Value));
50+
normalizedRanges.Add(new RangeItemHeaderValue(start.Value, end.Value));
13551
}
13652
return normalizedRanges;
13753
}

src/Microsoft.AspNet.StaticFiles/Microsoft.AspNet.StaticFiles.kproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
@@ -14,4 +14,9 @@
1414
<SchemaVersion>2.0</SchemaVersion>
1515
</PropertyGroup>
1616
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
17-
</Project>
17+
<ProjectExtensions>
18+
<VisualStudio>
19+
<UserProperties project_1json__JSONSchema="http://www.asp.net/media/4878834/project.json" />
20+
</VisualStudio>
21+
</ProjectExtensions>
22+
</Project>

0 commit comments

Comments
 (0)