Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Started fixing #11
Browse files Browse the repository at this point in the history
  • Loading branch information
azzlack committed Jan 27, 2016
1 parent 867ff8c commit 3d38d51
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<Compile Include="Compressors\BaseCompressor.cs" />
<Compile Include="Compressors\DeflateCompressor.cs" />
<Compile Include="Compressors\GZipCompressor.cs" />
<Compile Include="Extensions\HttpContentHeaderExtensions.cs" />
<Compile Include="HttpContentOperations.cs" />
<Compile Include="Interfaces\ICompressor.cs" />
<Compile Include="Models\CompressedContent.cs" />
Expand Down
58 changes: 58 additions & 0 deletions src/Core/Extensions/HttpContentHeaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace System.Net.Http.Extensions.Compression.Core.Extensions
{
using System.Diagnostics;
using System.Linq;
using System.Net.Http.Headers;

public static class HttpContentHeaderExtensions
{
/// <summary>Copies all headers from the source to the target.</summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="handleContentLength">true to handle content length.</param>
/// <param name="handleContentEncoding">true to handle content encoding.</param>
/// <param name="handleChangedValues">true to handle changed values.</param>
public static void CopyTo(
this HttpContentHeaders source,
HttpContentHeaders target,
bool handleContentLength = true,
bool handleContentEncoding = true,
bool handleChangedValues = false)
{
// Remove headers we are going to rewrite and headers with null values
foreach (var header in source)
{
try
{
if (!handleContentLength && header.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase))
{
return;
}

if (!handleContentEncoding && header.Key.Equals("Content-Encoding", StringComparison.OrdinalIgnoreCase))
{
return;
}

if (!handleChangedValues)
{
// If values have changed, dont update it
if (target.Contains(header.Key))
{
if (target.GetValues(header.Key).Any(targetValue => header.Value.Any(originalValue => originalValue != targetValue)))
{
return;
}
}
}

target.Add(header.Key, header.Value);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
}
}
26 changes: 2 additions & 24 deletions src/Core/HttpContentOperations.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace System.Net.Http.Extensions.Compression.Core
{
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Net.Http.Extensions.Compression.Core.Extensions;
using System.Net.Http.Extensions.Compression.Core.Interfaces;
using System.Threading.Tasks;

Expand Down Expand Up @@ -34,30 +33,9 @@ public async Task<HttpContent> DecompressContent(HttpContent compressedContent,
var decompressedContent = new StreamContent(decompressedContentStream);

// Copy content headers so we know what got sent back
this.CopyHeaders(compressedContent, decompressedContent);
compressedContent.Headers.CopyTo(decompressedContent.Headers);

return decompressedContent;
}

/// <summary>
/// Copies the HTTP headers onto the new response.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="output">The output.</param>
public void CopyHeaders(HttpContent input, HttpContent output)
{
// All other headers
foreach (var header in input.Headers)
{
try
{
output.Headers.Add(header.Key, header.Value);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
}
}
24 changes: 2 additions & 22 deletions src/Core/Models/CompressedContent.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace System.Net.Http.Extensions.Compression.Core.Models
{
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Extensions.Compression.Core.Extensions;
using System.Net.Http.Extensions.Compression.Core.Interfaces;
using System.Threading.Tasks;

Expand Down Expand Up @@ -87,26 +86,7 @@ protected async override Task SerializeToStreamAsync(Stream stream, TransportCon
/// </summary>
private void CopyHeaders()
{
// Remove headers we are going to rewrite and headers with null values
var headers =
this.originalContent.Headers.Where(
x =>
x.Value != null
&& !x.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase)
&& !x.Key.Equals("Content-Encoding", StringComparison.OrdinalIgnoreCase)).ToList();

// Copy the other headers
foreach (var header in headers)
{
try
{
this.Headers.Add(header.Key, header.Value);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
this.originalContent.Headers.CopyTo(this.Headers, false, false);

// Content-Encoding: {content-encodings}
this.Headers.ContentEncoding.Add(this.compressor.EncodingType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void TestFixtureSetUp()

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

config.MessageHandlers.Insert(0, new ServerCompressionHandler(32, new GZipCompressor(), new DeflateCompressor()));
config.MessageHandlers.Insert(0, new ServerCompressionHandler(4096, new GZipCompressor(), new DeflateCompressor()));

this.server = new HttpServer(config);

Expand Down Expand Up @@ -61,5 +61,17 @@ public async void Get_WhenResponseSizeIsLessThanTreshold_ShouldReturnUncompresse
Assert.AreEqual(response.Content.Headers.ContentLength, content.Length);
Assert.IsFalse(response.Content.Headers.ContentEncoding.Contains("gzip"));
}

[Test]
public async void Get_WhenResponseSizeIsLargerThanTreshold_ShouldReturnCompressedContent()
{
var response = await this.Client.GetAsync("http://localhost:55399/api/file/image");

var content = await response.Content.ReadAsStringAsync();

Assert.IsTrue(response.IsSuccessStatusCode);
Assert.AreNotEqual(response.Content.Headers.ContentLength, content.Length);
Assert.IsTrue(response.Content.Headers.ContentEncoding.Contains("gzip"));
}
}
}

0 comments on commit 3d38d51

Please sign in to comment.