Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to IHeaderDictionary #134

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Smidge.Core/IRequestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Primitives;
using Microsoft.Extensions.Primitives;
using Smidge.Models;
using System.Collections.Generic;

Expand Down Expand Up @@ -31,4 +31,4 @@ public interface IRequestHelper

bool IsExternalRequestPath(string path);
}
}
}
1 change: 1 addition & 0 deletions src/Smidge.Core/Smidge.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Content Include="..\..\assets\logo-nuget.png" Link="logo-nuget.png" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Composite" Version="5.0.0" />
Expand Down
28 changes: 20 additions & 8 deletions src/Smidge/RequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ public RequestHelper(IWebsiteInfo siteInfo)

public bool IsExternalRequestPath(string path)
{
if ((path.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| path.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
|| path.StartsWith("//", StringComparison.OrdinalIgnoreCase)))
if ((path.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
path.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ||
path.StartsWith("//", StringComparison.OrdinalIgnoreCase)))
{
return true;
}

return false;
}

Expand All @@ -42,7 +43,8 @@ public string Content(IWebFile file)
}

var filePath = Content(file.FilePath);
if (filePath == null) return null;
if (filePath == null)
return null;

var requestPath = file.RequestPath != null ? Content(file.RequestPath) : string.Empty;

Expand Down Expand Up @@ -79,12 +81,12 @@ public string Content(string path)
PathString pathBase = _siteInfo.GetBasePath();
return pathBase.Add(new PathString(path.Substring(1))).Value;
}

return path;
}

/// <summary>
/// Check what kind of compression to use. Need to select the first available compression
/// Check what kind of compression to use. Need to select the first available compression
/// from the header value as this is how .Net performs caching by compression so we need to follow
/// this process.
/// If IE 6 is detected, we will ignore compression as it's known that some versions of IE 6
Expand All @@ -94,11 +96,21 @@ public CompressionType GetClientCompression(IDictionary<string, StringValues> he
{
var type = CompressionType.None;

if (headers.TryGetValue(HeaderNames.AcceptEncoding, out StringValues acceptEncoding))
if (headers is not IHeaderDictionary headerDictionary)
{
headerDictionary = new HeaderDictionary(headers.Count);
foreach ((var key, StringValues stringValues) in headers)
{
headerDictionary[key] = stringValues;
}
}

var acceptEncoding = headerDictionary.GetCommaSeparatedValues(HeaderNames.AcceptEncoding);
if (acceptEncoding.Length > 0)
{
// Prefer in order: Brotli, GZip, Deflate.
// https://www.iana.org/assignments/http-parameters/http-parameters.xml#http-content-coding-registry
for (var i = 0; i < acceptEncoding.Count; i++)
for (var i = 0; i < acceptEncoding.Length; i++)
{
var encoding = acceptEncoding[i].Trim();

Expand Down