Skip to content

Commit 574d92d

Browse files
agrande-eamartinnormark
authored andcommitted
Ensure external sources for styles only process CSS responses when inlining stylesheets
1 parent e08037d commit 574d92d

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

PreMailer.Net/Benchmarks/Program.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
using AngleSharp;
22
using AngleSharp.Html.Parser;
33
using BenchmarkDotNet.Attributes;
4+
using BenchmarkDotNet.Configs;
5+
using BenchmarkDotNet.Jobs;
46
using BenchmarkDotNet.Running;
7+
using BenchmarkDotNet.Toolchains.InProcess.NoEmit;
58

69
public static class Program
710
{
811
public static void Main()
912
{
10-
BenchmarkRunner.Run<Realistic>();
13+
// Some local environments may run into issues with Windows Defender or
14+
// SentinelOne (and others) when running a benchmark. This ensures we
15+
// keep our toolchain within our process and stops the above apps from blocking
16+
// our benchmark process, but can slow the execution time.
17+
var avSafeConfig = DefaultConfig.Instance
18+
.AddJob(
19+
Job.ShortRun
20+
.WithToolchain(InProcessNoEmitToolchain.Instance)
21+
.WithIterationCount(100)
22+
);
23+
24+
BenchmarkRunner.Run<Realistic>(avSafeConfig);
1125
}
1226
}
1327

14-
[SimpleJob(invocationCount: 100, iterationCount: 100)]
1528
[MemoryDiagnoser]
1629
public class Realistic
1730
{
@@ -47,7 +60,7 @@ public void MoveCssInline_AllFlags()
4760
4861
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />
4962
<title>PreMailer Benchmark</title>
50-
63+
5164
</head>
5265
5366
<body bgcolor=""#123"">

PreMailer.Net/PreMailer.Net/Downloaders/WebDownloader.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using PreMailer.Net.Extensions;
12
using System;
23
using System.IO;
34
using System.Net;
@@ -8,7 +9,7 @@ namespace PreMailer.Net.Downloaders
89
public class WebDownloader : IWebDownloader
910
{
1011
private static IWebDownloader _sharedDownloader;
11-
12+
private const string CssMimeType = "text/css";
1213
public static IWebDownloader SharedDownloader
1314
{
1415
get
@@ -29,8 +30,16 @@ public static IWebDownloader SharedDownloader
2930
public string DownloadString(Uri uri)
3031
{
3132
var request = WebRequest.Create(uri);
33+
request.Headers.Add(HttpRequestHeader.Accept, CssMimeType);
34+
3235
using (var response = request.GetResponse())
3336
{
37+
// We only support this operation for CSS file/content types coming back
38+
// from the response. If we get something different, throw with the unsupported
39+
// content type in the message
40+
if(response.ParseContentType() != CssMimeType)
41+
throw new NotSupportedException($"The Uri type is giving a response in unsupported content type '{response.ContentType}'.");
42+
3443
switch (response)
3544
{
3645
case HttpWebResponse httpWebResponse:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Net;
3+
4+
namespace PreMailer.Net.Extensions
5+
{
6+
public static class WebResponseExtensions
7+
{
8+
public static string ParseContentType(this WebResponse response)
9+
{
10+
if(response == null)
11+
throw new NullReferenceException("Malformed response detected when parsing WebResponse Content-Type");
12+
13+
if(string.IsNullOrEmpty(response.ContentType))
14+
throw new NullReferenceException("Malformed Content-Type response detected when parsing WebResponse");
15+
16+
var results = response.ContentType.Split(';');
17+
18+
if(results.Length == 0)
19+
throw new FormatException("Malformed Content-Type response detected when parsing WebResponse");
20+
21+
return results[0];
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)