diff --git a/CompressR.sln b/CompressR.sln index 3763cc9..b23f048 100644 --- a/CompressR.sln +++ b/CompressR.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.25123.0 +VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "CompressR.MVC", "src\CompressR.MVC\CompressR.MVC.shproj", "{13E4DCA3-5D4D-4796-B3CF-AD4620E2A2F1}" EndProject @@ -25,11 +25,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompressR.WebApiUnitTests", "src\CompressR.WebApiUnitTests\CompressR.WebApiUnitTests.csproj", "{61D97564-ACF6-4396-AB96-2D29CDCC3814}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompressR.Owin", "src\CompressR.Owin\CompressR.Owin.csproj", "{4B84A645-A7E3-446E-83B5-5F56D18B47DF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompressR.Owin.Sample", "src\CompressR.Owin.Sample\CompressR.Owin.Sample.csproj", "{C6DD2187-F71A-4DA3-99B0-47A8CCF10853}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution - src\CompressR.MVC\CompressR.MVC.projitems*{b8889368-e350-4b1e-82f5-ea537d6da6e9}*SharedItemsImports = 4 src\CompressR.MVC\CompressR.MVC.projitems*{13e4dca3-5d4d-4796-b3cf-ad4620e2a2f1}*SharedItemsImports = 13 src\CompressR.MVC\CompressR.MVC.projitems*{256f0ecd-0310-4de9-8334-31a3d3867925}*SharedItemsImports = 4 + src\CompressR.MVC\CompressR.MVC.projitems*{b8889368-e350-4b1e-82f5-ea537d6da6e9}*SharedItemsImports = 4 EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -64,6 +68,14 @@ Global {61D97564-ACF6-4396-AB96-2D29CDCC3814}.Debug|Any CPU.Build.0 = Debug|Any CPU {61D97564-ACF6-4396-AB96-2D29CDCC3814}.Release|Any CPU.ActiveCfg = Release|Any CPU {61D97564-ACF6-4396-AB96-2D29CDCC3814}.Release|Any CPU.Build.0 = Release|Any CPU + {4B84A645-A7E3-446E-83B5-5F56D18B47DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B84A645-A7E3-446E-83B5-5F56D18B47DF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B84A645-A7E3-446E-83B5-5F56D18B47DF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B84A645-A7E3-446E-83B5-5F56D18B47DF}.Release|Any CPU.Build.0 = Release|Any CPU + {C6DD2187-F71A-4DA3-99B0-47A8CCF10853}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6DD2187-F71A-4DA3-99B0-47A8CCF10853}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6DD2187-F71A-4DA3-99B0-47A8CCF10853}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6DD2187-F71A-4DA3-99B0-47A8CCF10853}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Readme.md b/Readme.md index a6ca96a..ea615c7 100644 --- a/Readme.md +++ b/Readme.md @@ -59,4 +59,23 @@ public class ValuesController : ApiController } } +``` + +## Owin + +You can add gzip support through a middleware. Install the package: `Install-Package CompressR.Owin`. Then add `app.Use();` to your `Startup.cs`. + + +```csharp + + public void Configuration(IAppBuilder app) + { + app.Use(); + app.Run(context => + { + context.Response.ContentType = "text/plain"; + return context.Response.WriteAsync("Hello World!"); + }); + } + ``` \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index 0bbf32b..22e7be1 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -5,7 +5,7 @@ var download = require("gulp-download"); var del = require('del'); var assemblyInfo = require('gulp-dotnet-assembly-info'); var xunit = xunit = require('gulp-xunit-runner'); -var version = '1.5.1'; +var version = '1.5.2'; gulp.task('clean', ()=>{ return del(['src/**/obj/', 'src/**/bin/Release', 'nuget.exe', 'nupkgs']) @@ -44,7 +44,11 @@ gulp.task('test', ['build'], function () { })); }); gulp.task('pack', ['test'], ()=>{ - return gulp.src(['src/CompressR.MVC4/*.csproj', 'src/CompressR.MVC5/*.csproj', 'src/CompressR.WebApi/*.csproj', 'src/CompressR/*.csproj'], {read: false}) + return gulp.src(['src/CompressR.MVC4/*.csproj', + 'src/CompressR.MVC5/*.csproj', + 'src/CompressR.WebApi/*.csproj', + 'src/CompressR/*.csproj', + 'src/CompressR.Owin/*.csproj'], {read: false}) .pipe(nuget.pack({ build: false, symbols: true, diff --git a/src/CompressR.Owin.Sample/CompressR.Owin.Sample.csproj b/src/CompressR.Owin.Sample/CompressR.Owin.Sample.csproj new file mode 100644 index 0000000..40d5f26 --- /dev/null +++ b/src/CompressR.Owin.Sample/CompressR.Owin.Sample.csproj @@ -0,0 +1,141 @@ + + + + + + + Debug + AnyCPU + + + 2.0 + {C6DD2187-F71A-4DA3-99B0-47A8CCF10853} + {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + CompressR.Owin.Sample + CompressR.Owin.Sample + v4.5.2 + true + + + + + + + + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + ..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + True + + + + ..\..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll + True + + + ..\..\packages\Microsoft.Owin.Host.SystemWeb.3.0.1\lib\net45\Microsoft.Owin.Host.SystemWeb.dll + True + + + ..\..\packages\Owin.1.0\lib\net40\Owin.dll + True + + + + + + + + + + + + + + + + + + + + + + Web.config + + + Web.config + + + + + + + + + + + + {4b84a645-a7e3-446e-83b5-5f56d18b47df} + CompressR.Owin + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + + + + True + True + 42206 + / + http://localhost:42206/ + False + False + + + False + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/src/CompressR.Owin.Sample/Properties/AssemblyInfo.cs b/src/CompressR.Owin.Sample/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..10aaf09 --- /dev/null +++ b/src/CompressR.Owin.Sample/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("CompressR.Owin.Sample")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Tommy Parnell")] +[assembly: AssemblyProduct("CompressR.Owin.Sample")] +[assembly: AssemblyCopyright("Copyright © Tommy Parnell 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("c6dd2187-f71a-4da3-99b0-47a8ccf10853")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.5.2")] +[assembly: AssemblyFileVersion("1.5.2")] diff --git a/src/CompressR.Owin.Sample/Startup.cs b/src/CompressR.Owin.Sample/Startup.cs new file mode 100644 index 0000000..bb925f2 --- /dev/null +++ b/src/CompressR.Owin.Sample/Startup.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using Owin; + +namespace CompressR.Owin.Sample +{ + public class Startup + { + public void Configuration(IAppBuilder app) + { + app.Use(); + app.Run(context => + { + context.Response.ContentType = "text/plain"; + return context.Response.WriteAsync("Hello World!"); + }); + } + } +} \ No newline at end of file diff --git a/src/CompressR.Owin.Sample/Web.Debug.config b/src/CompressR.Owin.Sample/Web.Debug.config new file mode 100644 index 0000000..2e302f9 --- /dev/null +++ b/src/CompressR.Owin.Sample/Web.Debug.config @@ -0,0 +1,30 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/CompressR.Owin.Sample/Web.Release.config b/src/CompressR.Owin.Sample/Web.Release.config new file mode 100644 index 0000000..c358444 --- /dev/null +++ b/src/CompressR.Owin.Sample/Web.Release.config @@ -0,0 +1,31 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/CompressR.Owin.Sample/Web.config b/src/CompressR.Owin.Sample/Web.config new file mode 100644 index 0000000..b976e20 --- /dev/null +++ b/src/CompressR.Owin.Sample/Web.config @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/CompressR.Owin.Sample/packages.config b/src/CompressR.Owin.Sample/packages.config new file mode 100644 index 0000000..f26cc08 --- /dev/null +++ b/src/CompressR.Owin.Sample/packages.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/CompressR.Owin/CompressR.Owin.csproj b/src/CompressR.Owin/CompressR.Owin.csproj new file mode 100644 index 0000000..fa0e567 --- /dev/null +++ b/src/CompressR.Owin/CompressR.Owin.csproj @@ -0,0 +1,65 @@ + + + + + Debug + AnyCPU + {4B84A645-A7E3-446E-83B5-5F56D18B47DF} + Library + Properties + CompressR.Owin + CompressR.Owin + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll + True + + + ..\..\packages\Owin.1.0\lib\net40\Owin.dll + True + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/CompressR.Owin/CompressR.Owin.nuspec b/src/CompressR.Owin/CompressR.Owin.nuspec new file mode 100644 index 0000000..254363a --- /dev/null +++ b/src/CompressR.Owin/CompressR.Owin.nuspec @@ -0,0 +1,17 @@ + + + + $id$ + $version$ + $title$ + $author$ + $author$ + https://opensource.org/licenses/MIT + https://github.com/tparnell8/CompressR + false + Compress owin actions! + + Copyright 2016 + Compression Owin + + \ No newline at end of file diff --git a/src/CompressR.Owin/CompressionMiddleware.cs b/src/CompressR.Owin/CompressionMiddleware.cs new file mode 100644 index 0000000..afdedb4 --- /dev/null +++ b/src/CompressR.Owin/CompressionMiddleware.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Owin; + +namespace CompressR.Owin +{ + public struct Constants + { + public const string Gzip = "gzip"; + public const string Deflate = "deflate"; + public const string ContentEncoding = "Content-Encoding"; + public const string AcceptEncoding = "Accept-Encoding"; + public static readonly string[] Compressors = { Gzip, Deflate }; + } + + public class GzipMiddleware : OwinMiddleware + { + public GzipMiddleware(OwinMiddleware next) : base(next) + { + } + + public async override Task Invoke(IOwinContext context) + { + if(!context.Request.Headers.Keys.Contains(Constants.AcceptEncoding)) + { + await Next.Invoke(context); + } + var types = context.Request.Headers[Constants.AcceptEncoding].Split(','); + if(types.Contains(Constants.Gzip, StringComparer.OrdinalIgnoreCase)) + { + var responseStream = context.Response.Body; + using(var memStream = new MemoryStream()) + { + context.Response.Body = memStream; + await Next?.Invoke(context); + context.Response.Headers[Constants.ContentEncoding] = Constants.Gzip; + using(var gzipStream = new GZipStream(responseStream, CompressionLevel.Optimal)) + { + memStream.WriteTo(gzipStream); + await gzipStream.FlushAsync(); + } + + await responseStream.FlushAsync(); + + } + } + } + } +} diff --git a/src/CompressR.Owin/Properties/AssemblyInfo.cs b/src/CompressR.Owin/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..352ff42 --- /dev/null +++ b/src/CompressR.Owin/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("CompressR.Owin")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Tommy Parnell")] +[assembly: AssemblyProduct("CompressR.Owin")] +[assembly: AssemblyCopyright("Copyright © Tommy Parnell 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("4b84a645-a7e3-446e-83b5-5f56d18b47df")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.5.2")] +[assembly: AssemblyVersion("1.5.2")] +[assembly: AssemblyFileVersion("1.5.2")] diff --git a/src/CompressR.Owin/packages.config b/src/CompressR.Owin/packages.config new file mode 100644 index 0000000..8742a6b --- /dev/null +++ b/src/CompressR.Owin/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file