Skip to content

Commit

Permalink
Merge pull request #128 from PhyxionNL/fix-headers
Browse files Browse the repository at this point in the history
Check if request headers exist
  • Loading branch information
Shazwazza committed Oct 6, 2021
2 parents a366c35 + cfbc4d0 commit 0ba3ae5
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/Smidge/RequestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -93,23 +93,14 @@ public string Content(string path)
public CompressionType GetClientCompression(IDictionary<string, StringValues> headers)
{
var type = CompressionType.None;
var agentHeader = (string)headers[HeaderNames.UserAgent];
if (agentHeader != null && agentHeader.Contains("MSIE 6"))
{
return type;
}

string acceptEncoding = headers[HeaderNames.AcceptEncoding];

if (!string.IsNullOrEmpty(acceptEncoding))
if (headers.TryGetValue(HeaderNames.AcceptEncoding, out StringValues acceptEncoding))
{
string[] acceptedEncodings = acceptEncoding.Split(',');

// 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 < acceptedEncodings.Length; i++)
for (var i = 0; i < acceptEncoding.Count; i++)
{
var encoding = acceptedEncodings[i].Trim();
var encoding = acceptEncoding[i].Trim();

CompressionType parsed = CompressionType.Parse(encoding);

Expand All @@ -118,21 +109,21 @@ public CompressionType GetClientCompression(IDictionary<string, StringValues> he
{
return CompressionType.Brotli;
}

// Not pack200-gzip.
if (parsed == CompressionType.GZip)
{
type = CompressionType.GZip;
}

if (type != CompressionType.GZip && parsed == CompressionType.Deflate)
{
type = CompressionType.Deflate;
}
}
}
}

return type;
}
}
}
}

0 comments on commit 0ba3ae5

Please sign in to comment.