Skip to content

Commit

Permalink
Add limit to Gzip to 4Mb
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Sep 12, 2016
1 parent 8ca03e3 commit 4f7257a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 2 additions & 3 deletions Unosquare.Labs.EmbedIO.Samples/StaticFilesSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Unosquare.Labs.EmbedIO.Modules;

/// <summary>
///
/// Sample helper
/// </summary>
public static class StaticFilesSample
{
Expand All @@ -23,7 +23,7 @@ public static string HtmlRootPath
// This lets you edit the files without restarting the server.
return Path.GetFullPath(Path.Combine(assemblyPath, "..\\..\\html"));
#else
// This is when you have deployed ythe server.
// This is when you have deployed the server.
return Path.Combine(assemblyPath, "html");
#endif
}
Expand All @@ -38,7 +38,6 @@ public static void Setup(WebServer server)
server.RegisterModule(new StaticFilesModule(HtmlRootPath));
// The static files module will cache small files in ram until it detects they have been modified.
server.Module<StaticFilesModule>().UseRamCache = false;
server.Module<StaticFilesModule>().UseGzip = false;
server.Module<StaticFilesModule>().DefaultExtension = ".html";
// We don't need to add the line below. The default document is always index.html.
//server.Module<StaticFilesWebModule>().DefaultDocument = "index.html";
Expand Down
9 changes: 8 additions & 1 deletion Unosquare.Labs.EmbedIO/Modules/StaticFilesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class StaticFilesModule : WebModuleBase
/// </summary>
private const int ChuckSize = 256*1024;

/// <summary>
/// The maximum gzip input length
/// </summary>
private const int MaxGzipInputLength = 4*1024*1024;

private readonly Dictionary<string, string> m_VirtualPaths =
new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

Expand Down Expand Up @@ -298,7 +303,9 @@ private bool HandleGet(HttpListenerContext context, WebServer server, bool sendB
}
else
{
if (UseGzip && context.RequestHeader(Constants.HeaderAcceptEncoding).Contains(Constants.HeaderCompressionGzip))
if (UseGzip &&
context.RequestHeader(Constants.HeaderAcceptEncoding).Contains(Constants.HeaderCompressionGzip) &&
buffer.Length < MaxGzipInputLength)
{
// Perform compression if available
buffer = buffer.Compress();
Expand Down

0 comments on commit 4f7257a

Please sign in to comment.