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

Commit

Permalink
Automatic merge from TeamCity build #20, branch release/2.0.0. Releas…
Browse files Browse the repository at this point in the history
…e 2.0.0 is ready.
  • Loading branch information
ciadmin committed Aug 8, 2016
2 parents e9836aa + 647d830 commit 99bf91c
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/Client/ClientCompressionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ClientCompressionHandler : DelegatingHandler
/// The HTTP content operations
/// </summary>
private readonly HttpContentOperations httpContentOperations;

/// <summary>
/// Initializes a new instance of the <see cref="ClientCompressionHandler" /> class.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Core/Compressors/BaseCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public abstract class BaseCompressor : ICompressor
/// <summary>Manager for stream.</summary>
private readonly IStreamManager streamManager;

/// <summary>Initializes a new instance of the <see cref="BaseCompressor" /> class.</summary>
protected BaseCompressor()
{
this.streamManager = StreamManager.Instance;
}

/// <summary>Initializes a new instance of the <see cref="BaseCompressor" /> class.</summary>
/// <param name="streamManager">The stream manager.</param>
protected BaseCompressor(IStreamManager streamManager)
Expand Down
7 changes: 7 additions & 0 deletions src/Core/Compressors/DeflateCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
/// </summary>
public class DeflateCompressor : BaseCompressor
{
/// <summary>
/// Initializes a new instance of the <see cref="DeflateCompressor" /> class.
/// </summary>
public DeflateCompressor()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DeflateCompressor" /> class.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Compressors/GZipCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
/// </summary>
public class GZipCompressor : BaseCompressor
{
/// <summary>Initializes a new instance of the <see cref="GZipCompressor" /> class.</summary>
public GZipCompressor()
{
}

/// <summary>Initializes a new instance of the <see cref="GZipCompressor" /> class.</summary>
/// <param name="streamManager">Manager for stream.</param>
public GZipCompressor(IStreamManager streamManager)
Expand Down
1 change: 1 addition & 0 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Interfaces\IStreamManager.cs" />
<Compile Include="Models\CompressedContent.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StreamManager.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Net.Http">
Expand Down
8 changes: 4 additions & 4 deletions src/Core/Extensions/HttpContentHeaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public static void CopyTo(
bool handleContentEncoding = true,
bool handleChangedValues = false)
{
// Remove headers we are going to rewrite and headers with null values
// Copy all other headers unless they have been modified and we want to preserve that
foreach (var header in source)
{
try
{
if (!handleContentLength && header.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase))
{
return;
continue;
}

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

if (!handleChangedValues)
Expand All @@ -41,7 +41,7 @@ public static void CopyTo(
{
if (target.GetValues(header.Key).Any(targetValue => header.Value.Any(originalValue => originalValue != targetValue)))
{
return;
continue;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Core/Models/CompressedContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public CompressedContent(HttpContent content, ICompressor compressor)
{
if (content == null)
{
throw new ArgumentNullException("content");
throw new ArgumentNullException(nameof(content));
}

if (compressor == null)
{
throw new ArgumentNullException("compressor");
throw new ArgumentNullException(nameof(compressor));
}

this.originalContent = content;
Expand All @@ -64,11 +64,11 @@ protected override bool TryComputeLength(out long length)
/// <param name="stream">The target stream.</param>
/// <param name="context">Information about the transport (channel binding token, for example). This parameter may be null.</param>
/// <returns>Returns <see cref="T:System.Threading.Tasks.Task" />.The task object representing the asynchronous operation.</returns>
protected async override Task SerializeToStreamAsync(Stream stream, TransportContext context)
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
throw new ArgumentNullException(nameof(stream));
}

// Read and compress stream
Expand Down
16 changes: 16 additions & 0 deletions src/Core/StreamManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace System.Net.Http.Extensions.Compression.Core
{
using System.IO;
using System.Net.Http.Extensions.Compression.Core.Interfaces;

/// <summary>Manager for streams.</summary>
public class StreamManager : IStreamManager
{
public static IStreamManager Instance { get; } = new StreamManager();

public Stream GetStream(string tag = null)
{
return new MemoryStream();
}
}
}
9 changes: 8 additions & 1 deletion src/Server/BaseServerCompressionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected BaseServerCompressionHandler(HttpMessageHandler innerHandler, int cont
this.Compressors = compressors;
this.contentSizeThreshold = contentSizeThreshold;
this.httpContentOperations = new HttpContentOperations();
this.streamManager = new StreamManager();
this.streamManager = new RecyclableStreamManager();

this.enableCompression = enableCompression ?? (x =>
{
Expand Down Expand Up @@ -223,6 +223,13 @@ orderby quality descending
{
try
{
// Only compress response if not already compressed
if (response.Content?.Headers.ContentEncoding != null
&& response.Content.Headers.ContentEncoding.Contains(compressor.EncodingType))
{
return;
}

// Only compress response if size is larger than treshold (if set)
if (this.contentSizeThreshold == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
using Microsoft.IO;

/// <summary>Manager for streams.</summary>
public class StreamManager : IStreamManager
public class RecyclableStreamManager : IStreamManager
{
private readonly RecyclableMemoryStreamManager recyclableMemoryStreamManager;

/// <summary>Initializes a new instance of the <see cref="StreamManager" /> class.</summary>
public StreamManager()
/// <summary>Initializes a new instance of the <see cref="RecyclableStreamManager" /> class.</summary>
public RecyclableStreamManager()
{
this.recyclableMemoryStreamManager = new RecyclableMemoryStreamManager();
}

public static IStreamManager Instance { get; } = new StreamManager();
public static IStreamManager Instance { get; } = new RecyclableStreamManager();

public Stream GetStream(string tag = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<Compile Include="BaseServerCompressionHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServerCompressionHandler.cs" />
<Compile Include="StreamManager.cs" />
<Compile Include="RecyclableStreamManager.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
2 changes: 1 addition & 1 deletion src/Server/ServerCompressionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ServerCompressionHandler : BaseServerCompressionHandler
/// Initializes a new instance of the <see cref="ServerCompressionHandler" /> class.
/// </summary>
public ServerCompressionHandler()
: base(null, 860, new GZipCompressor(StreamManager.Instance), new DeflateCompressor(StreamManager.Instance))
: base(null, 860, new GZipCompressor(RecyclableStreamManager.Instance), new DeflateCompressor(RecyclableStreamManager.Instance))
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tests/App_Start/WebApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void Register(HttpConfiguration config)
defaults: new { id = RouteParameter.Optional });

// Add compression message handler
config.MessageHandlers.Insert(0, new ServerCompressionHandler(0, new GZipCompressor(StreamManager.Instance), new DeflateCompressor(StreamManager.Instance)));
config.MessageHandlers.Insert(0, new ServerCompressionHandler(0, new GZipCompressor(RecyclableStreamManager.Instance), new DeflateCompressor(RecyclableStreamManager.Instance)));

// Configure error details policy
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
Expand Down
2 changes: 2 additions & 0 deletions src/Tests/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public async Task<HttpResponseMessage> GetCustomHeader()
public async Task<HttpResponseMessage> GetRedirect()
{
var response = this.Request.CreateResponse(HttpStatusCode.Redirect);
response.Content = new ByteArrayContent(new byte[1024]);
response.Content.Headers.ContentLength = 1024;
response.Headers.Location = new Uri($"{this.Request.RequestUri.Scheme}://{this.Request.RequestUri.Authority}/api/test");

return response;
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Tests/Common/TestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public async void Get_WhenResponseHeaderIsModified_ShouldReturnModifiedResponse(

Console.Write(await response.ToTestString());

Assert.AreEqual(29, response.Content.Headers.ContentLength);
Assert.AreEqual("http://localhost:55399/api/test", response.Headers.Location.ToString());
}

Expand Down
27 changes: 16 additions & 11 deletions src/Tests/Tests/Facade/OwinHostTests.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
namespace Tests.Tests.Facade
{
using System;
using System.Net.Http;
using System.Net.Http.Extensions.Compression.Client;
using System.Net.Http.Extensions.Compression.Core.Compressors;
using System.Net.Http.Headers;
using System.Text;
using System.Web.Http;

using global::Tests.Extensions;
using global::Tests.Handlers;
using global::Tests.Models;
using global::Tests.Tests.Common;

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.WebApi.Extensions.Compression.Server;
using Microsoft.AspNet.WebApi.Extensions.Compression.Server.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Testing;

using Newtonsoft.Json;

using NUnit.Framework;
using Owin;
using System;
using System.Net.Http;
using System.Net.Http.Extensions.Compression.Client;
using System.Net.Http.Extensions.Compression.Core.Compressors;
using System.Net.Http.Headers;
using System.Text;
using System.Web.Http;

using Microsoft.AspNet.WebApi.Extensions.Compression.Server;
using Owin;

[TestFixture]
public class OwinHostTests : TestFixture
Expand All @@ -36,7 +40,7 @@ public void TestFixtureSetup()
[SetUp]
public void SetUp()
{
var client = new HttpClient(new TraceMessageHandler(new ClientCompressionHandler(this.server.Handler, new GZipCompressor(StreamManager.Instance), new DeflateCompressor(StreamManager.Instance))))
var client = new HttpClient(new TraceMessageHandler(new ClientCompressionHandler(this.server.Handler, new GZipCompressor(), new DeflateCompressor())))
{
BaseAddress = new Uri("http://localhost:55399")
};
Expand All @@ -63,6 +67,7 @@ public async void Post_WhenHeaderIsModifiedOnServer_ShouldReturnModifiedHeader()

Assert.AreEqual(loginModel.ReturnUrl, response.Headers.Location.ToString());
}

public class OwinStartup
{
public void Configuration(IAppBuilder appBuilder)
Expand All @@ -72,7 +77,7 @@ public void Configuration(IAppBuilder appBuilder)
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });

// Add compression message handler
config.MessageHandlers.Insert(0, new OwinServerCompressionHandler(0, new GZipCompressor(StreamManager.Instance), new DeflateCompressor(StreamManager.Instance)));
config.MessageHandlers.Insert(0, new OwinServerCompressionHandler(0, new GZipCompressor(), new DeflateCompressor()));

appBuilder.UseCookieAuthentication(
new CookieAuthenticationOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
{
using global::Tests.Extensions;
using global::Tests.Tests.Common;

using Microsoft.AspNet.WebApi.Extensions.Compression.Server;
using NUnit.Framework;
using System;
using System.Net.Http;
using System.Net.Http.Extensions.Compression.Client;
using System.Net.Http.Extensions.Compression.Core;
using System.Net.Http.Extensions.Compression.Core.Compressors;
using System.Net.Http.Headers;
using System.Web.Http;
Expand Down Expand Up @@ -40,7 +42,7 @@ public void TestFixtureSetUp()
[SetUp]
public void SetUp()
{
var client = new HttpClient(new ClientCompressionHandler(this.server, 0, new GZipCompressor(StreamManager.Instance), new DeflateCompressor(StreamManager.Instance)));
var client = new HttpClient(new ClientCompressionHandler(this.server, 0, new GZipCompressor(), new DeflateCompressor()));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NUnit.Framework;
using System.Net.Http;
using System.Net.Http.Extensions.Compression.Client;
using System.Net.Http.Extensions.Compression.Core;
using System.Net.Http.Extensions.Compression.Core.Compressors;
using System.Net.Http.Headers;
using System.Web.Http;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void TestFixtureSetUp()
[SetUp]
public void SetUp()
{
var client = new HttpClient(new ClientCompressionHandler(this.server, 0, new GZipCompressor(StreamManager.Instance), new DeflateCompressor(StreamManager.Instance)));
var client = new HttpClient(new ClientCompressionHandler(this.server, 0, new GZipCompressor(), new DeflateCompressor()));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));

Expand Down

0 comments on commit 99bf91c

Please sign in to comment.