Skip to content

Commit

Permalink
Merge pull request #134 from PhyxionNL/header-dictionary
Browse files Browse the repository at this point in the history
Switch to IHeaderDictionary
  • Loading branch information
Shazwazza committed Nov 30, 2021
2 parents 0204943 + 1b60395 commit 391ed15
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
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

0 comments on commit 391ed15

Please sign in to comment.