Skip to content

Commit 4f7257a

Browse files
committed
Add limit to Gzip to 4Mb
1 parent 8ca03e3 commit 4f7257a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Unosquare.Labs.EmbedIO.Samples/StaticFilesSample.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Unosquare.Labs.EmbedIO.Modules;
55

66
/// <summary>
7-
///
7+
/// Sample helper
88
/// </summary>
99
public static class StaticFilesSample
1010
{
@@ -23,7 +23,7 @@ public static string HtmlRootPath
2323
// This lets you edit the files without restarting the server.
2424
return Path.GetFullPath(Path.Combine(assemblyPath, "..\\..\\html"));
2525
#else
26-
// This is when you have deployed ythe server.
26+
// This is when you have deployed the server.
2727
return Path.Combine(assemblyPath, "html");
2828
#endif
2929
}
@@ -38,7 +38,6 @@ public static void Setup(WebServer server)
3838
server.RegisterModule(new StaticFilesModule(HtmlRootPath));
3939
// The static files module will cache small files in ram until it detects they have been modified.
4040
server.Module<StaticFilesModule>().UseRamCache = false;
41-
server.Module<StaticFilesModule>().UseGzip = false;
4241
server.Module<StaticFilesModule>().DefaultExtension = ".html";
4342
// We don't need to add the line below. The default document is always index.html.
4443
//server.Module<StaticFilesWebModule>().DefaultDocument = "index.html";

Unosquare.Labs.EmbedIO/Modules/StaticFilesModule.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class StaticFilesModule : WebModuleBase
1919
/// </summary>
2020
private const int ChuckSize = 256*1024;
2121

22+
/// <summary>
23+
/// The maximum gzip input length
24+
/// </summary>
25+
private const int MaxGzipInputLength = 4*1024*1024;
26+
2227
private readonly Dictionary<string, string> m_VirtualPaths =
2328
new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
2429

@@ -298,7 +303,9 @@ private bool HandleGet(HttpListenerContext context, WebServer server, bool sendB
298303
}
299304
else
300305
{
301-
if (UseGzip && context.RequestHeader(Constants.HeaderAcceptEncoding).Contains(Constants.HeaderCompressionGzip))
306+
if (UseGzip &&
307+
context.RequestHeader(Constants.HeaderAcceptEncoding).Contains(Constants.HeaderCompressionGzip) &&
308+
buffer.Length < MaxGzipInputLength)
302309
{
303310
// Perform compression if available
304311
buffer = buffer.Compress();

0 commit comments

Comments
 (0)