Skip to content

Commit

Permalink
Add UseGzip property to StaticFilesModule
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Sep 12, 2016
1 parent 68d7f8d commit 8ca03e3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Unosquare.Labs.EmbedIO.Samples/StaticFilesSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ 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 = true;
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
27 changes: 25 additions & 2 deletions Unosquare.Labs.EmbedIO.Tests/StaticFilesModuleTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Unosquare.Labs.EmbedIO.Tests
using System.IO.Compression;
using System.Text;

namespace Unosquare.Labs.EmbedIO.Tests
{
using NUnit.Framework;
using System;
Expand Down Expand Up @@ -235,7 +238,7 @@ public void GetInvalidChunk()

Assert.AreEqual(response.StatusCode, HttpStatusCode.RequestedRangeNotSatisfiable, "Status Code RequestedRangeNotSatisfiable");
Assert.AreEqual(response.Headers["Content-Range"],
string.Format("bytes */{0}", remoteSize));
$"bytes */{remoteSize}");
return;
}

Expand All @@ -260,6 +263,26 @@ public void GetNotPartial()
}
}

[Test]
public void GetGzipCompressFile()
{
var request = (HttpWebRequest)WebRequest.Create(Resources.ServerAddress);
request.AutomaticDecompression = DecompressionMethods.GZip;

using (var response = (HttpWebResponse)request.GetResponse())
{
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");

var responseStream = response.GetResponseStream();
//Assert.IsTrue(response.ContentEncoding.ToLower().Contains("gzip"), "Request is gziped");
//var responseStream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
var reader = new StreamReader(responseStream, Encoding.Default);
var html = reader.ReadToEnd();

Assert.IsNotNull(html, "Data is not empty");
Assert.AreEqual(Resources.index, html);
}
}
[Test]
public void HeadIndex()
{
Expand Down
5 changes: 5 additions & 0 deletions Unosquare.Labs.EmbedIO.Tests/TestObjects/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public static string SetupStaticFolderInstance(string instanceName)
return folder;
}

/// <summary>
/// Creates the temporary binary file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="sizeInMb">The size in mb.</param>
public static void CreateTempBinaryFile(string fileName, int sizeInMb)
{
// Note: block size must be a factor of 1MB to avoid rounding errors :)
Expand Down
1 change: 1 addition & 0 deletions Unosquare.Labs.EmbedIO/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ private static Dictionary<string, string> ParseFormData(string requestBody)
/// <returns></returns>
public static MemoryStream Compress(this Stream buffer)
{
buffer.Position = 0;
var targetStream = new MemoryStream();

using (var compressor = new GZipStream(targetStream, CompressionMode.Compress, true))
Expand Down
11 changes: 10 additions & 1 deletion Unosquare.Labs.EmbedIO/Modules/StaticFilesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public class StaticFilesModule : WebModuleBase
/// </value>
public bool UseRamCache { get; set; }

/// <summary>
/// Gets or sets a value indicating whether [use gzip].
/// </summary>
/// <value>
/// <c>true</c> if [use gzip]; otherwise, <c>false</c>.
/// </value>
public bool UseGzip { get; set; }

/// <summary>
/// The default headers
/// </summary>
Expand Down Expand Up @@ -151,6 +159,7 @@ public StaticFilesModule(string fileSystemPath, Dictionary<string, string> heade
throw new ArgumentException($"Path '{fileSystemPath}' does not exist.");

this.FileSystemPath = fileSystemPath;
this.UseGzip = true;
#if DEBUG
// When debugging, disable RamCache
this.UseRamCache = false;
Expand Down Expand Up @@ -289,7 +298,7 @@ private bool HandleGet(HttpListenerContext context, WebServer server, bool sendB
}
else
{
if (context.RequestHeader(Constants.HeaderAcceptEncoding).Contains(Constants.HeaderCompressionGzip))
if (UseGzip && context.RequestHeader(Constants.HeaderAcceptEncoding).Contains(Constants.HeaderCompressionGzip))
{
// Perform compression if available
buffer = buffer.Compress();
Expand Down

0 comments on commit 8ca03e3

Please sign in to comment.