Merge branch 'master' of github.com:tparnell8/CompressR

This commit is contained in:
Tommy Parnell
2016-06-20 19:11:42 -04:00
20 changed files with 282 additions and 28 deletions

1
.gitignore vendored
View File

@@ -21,6 +21,7 @@
bld/
[Bb]in/
[Oo]bj/
nuget.exe
# Visual Studio 2015 cache/options directory
.vs/

View File

@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompressR.WebApi", "src\Com
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompressR.Sample", "src\CompressR.Sample\CompressR.Sample.csproj", "{2FA56DF3-C7B2-4070-B41A-FE328D96961C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompressR", "src\CompressR\CompressR.csproj", "{C94378C3-4AA8-4F49-8720-77D14B62F72E}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\CompressR.MVC\CompressR.MVC.projitems*{b8889368-e350-4b1e-82f5-ea537d6da6e9}*SharedItemsImports = 4
@@ -40,6 +42,10 @@ Global
{2FA56DF3-C7B2-4070-B41A-FE328D96961C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FA56DF3-C7B2-4070-B41A-FE328D96961C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FA56DF3-C7B2-4070-B41A-FE328D96961C}.Release|Any CPU.Build.0 = Release|Any CPU
{C94378C3-4AA8-4F49-8720-77D14B62F72E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C94378C3-4AA8-4F49-8720-77D14B62F72E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C94378C3-4AA8-4F49-8720-77D14B62F72E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C94378C3-4AA8-4F49-8720-77D14B62F72E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -4,10 +4,10 @@ var msbuild = require('gulp-msbuild');
var download = require("gulp-download");
var del = require('del');
var assemblyInfo = require('gulp-dotnet-assembly-info');
var version = '1.0.0';
var version = '1.2.0';
gulp.task('clean', ()=>{
return del['./**/bin', './**/obj', 'nuget.exe', 'nupkgs']
return del(['src/**/obj/', 'src/**/bin/Release', 'nuget.exe', 'nupkgs'])
});
gulp.task('downloadNuget', ['clean'], ()=>{
return download('https://dist.nuget.org/win-x86-commandline/latest/nuget.exe')
@@ -36,15 +36,17 @@ gulp.task('build', ['restore', 'patchAssemblyInfo'], ()=>{
});
gulp.task('pack', ['build'], ()=>{
return gulp.src(['src/CompressR.MVC4/*.csproj', 'src/CompressR.MVC5/*.csproj', 'src/CompressR.WebApi/*.csproj'])
return gulp.src(['src/CompressR.MVC4/*.csproj', 'src/CompressR.MVC5/*.csproj', 'src/CompressR.WebApi/*.csproj', 'src/CompressR/*.csproj'])
.pipe(nuget.pack({
build: false,
properties: 'configuration=release',
outputDirectory: './nupkgs'
symbols: true,
properties: 'configuration=Release',
outputDirectory: './nupkgs',
includeReferencedProjects: true
}));
});
gulp.task('publish', ['pack'], ()=>{
return gulp.src('./nupkgs/*.nupkg')
.pipe(nuget.push({ nuget: "nuget.exe", source: 'https://www.nuget.org/api/v2/package', apiKey: '158f98f2-f9e6-4490-9382-8b49ebda9cc7'}));
return gulp.src(['!./nupkgs/*.symbols.nupkg','./nupkgs/*.nupkg'])
.pipe(nuget.push({ nuget: "nuget.exe", source: 'https://www.nuget.org/api/v2/package', apiKey: process.env.nugetApiKey}));
});

BIN
nuget.exe

Binary file not shown.

View File

@@ -9,9 +9,16 @@ namespace CompressR.MVC
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class CompressAttribute : System.Web.Mvc.ActionFilterAttribute
{
private bool RequireCompression { get; set; }
public CompressAttribute(bool requireCompression = false)
{
RequireCompression = requireCompression;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
CompressFactory.Compress(filterContext);
CompressFactory.Compress(filterContext, RequireCompression);
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using CompressR.Exceptions;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
@@ -8,18 +9,28 @@ namespace CompressR.MVC
{
public static class CompressFactory
{
public static void Compress(string compression, System.Web.Mvc.ActionExecutingContext filterContext)
public static void Compress(string compression, System.Web.Mvc.ActionExecutingContext filterContext, bool requireCompression)
{
var context = filterContext.RequestContext.HttpContext;
var compressionAccepted = context.Request.Headers.Get(Constants.AcceptEncoding)?.Split(',').Trim().Any(a => string.Equals(a, compression, StringComparison.OrdinalIgnoreCase)) ?? false;
if (!compressionAccepted)
{
if (requireCompression)
{
throw new CompressRException("Compression required but client did not send accept header");
}
else
{
return;
}
}
HandleCompression(compression, filterContext);
}
public static void Compress(System.Web.Mvc.ActionExecutingContext filterContext)
public static void Compress(System.Web.Mvc.ActionExecutingContext filterContext, bool requireCompression)
{
var context = filterContext.RequestContext.HttpContext;
var compressionAlgorithm = context.Request.Headers.Get(Constants.AcceptEncoding)?.Split(',').Trim().Intersect(Constants.Compressors, StringComparer.OrdinalIgnoreCase)?.FirstOrDefault();
@@ -27,6 +38,10 @@ namespace CompressR.MVC
{
HandleCompression(compressionAlgorithm, filterContext);
}
else if(requireCompression)
{
throw new CompressRException("Compression required but client did not send accept header");
}
}
private static void HandleCompression(string compression, System.Web.Mvc.ActionExecutingContext filterContext)

View File

@@ -7,6 +7,13 @@ namespace CompressR.MVC
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class DeflateAttribute : System.Web.Mvc.ActionFilterAttribute
{
private bool RequireCompression { get; set; }
public DeflateAttribute(bool requireCompression = false)
{
RequireCompression = requireCompression;
}
/// <summary>
/// Override to compress the content that is generated by
/// an action method.
@@ -14,7 +21,7 @@ namespace CompressR.MVC
/// <param name="filterContext"></param>
public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
{
CompressFactory.Compress(Constants.Deflate, filterContext);
CompressFactory.Compress(Constants.Deflate, filterContext, RequireCompression);
}
}
}

View File

@@ -7,6 +7,13 @@ namespace CompressR.MVC
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class GzipAttribute : System.Web.Mvc.ActionFilterAttribute
{
private bool RequireCompression { get; set; }
public GzipAttribute(bool requireCompression = false)
{
RequireCompression = requireCompression;
}
/// <summary>
/// Override to compress the content that is generated by
/// an action method.
@@ -14,7 +21,7 @@ namespace CompressR.MVC
/// <param name="filterContext"></param>
public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
{
CompressFactory.Compress(Constants.Gzip, filterContext);
CompressFactory.Compress(Constants.Gzip, filterContext, RequireCompression);
}
}
}

View File

@@ -75,6 +75,12 @@
<None Include="CompressR.MVC4.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CompressR\CompressR.csproj">
<Project>{c94378c3-4aa8-4f49-8720-77d14b62f72e}</Project>
<Name>CompressR</Name>
</ProjectReference>
</ItemGroup>
<Import Project="..\CompressR.MVC\CompressR.MVC.projitems" Label="Shared" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -75,6 +75,12 @@
<None Include="CompressR.MVC5.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CompressR\CompressR.csproj">
<Project>{c94378c3-4aa8-4f49-8720-77d14b62f72e}</Project>
<Name>CompressR</Name>
</ProjectReference>
</ItemGroup>
<Import Project="..\CompressR.MVC\CompressR.MVC.projitems" Label="Shared" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -25,6 +25,7 @@
<UseGlobalApplicationHostFile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<WebGreaseLibPath>..\..\packages\WebGrease.1.5.2\lib</WebGreaseLibPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -178,6 +179,7 @@
<Content Include="favicon.ico" />
<Content Include="fonts\glyphicons-halflings-regular.svg" />
<Content Include="Global.asax" />
<Content Include="packages.config" />
<Content Include="fonts\glyphicons-halflings-regular.woff" />
<Content Include="fonts\glyphicons-halflings-regular.ttf" />
<Content Include="fonts\glyphicons-halflings-regular.eot" />
@@ -223,6 +225,7 @@
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\Shared\Error.cshtml" />
<Content Include="Views\Shared\_Layout.cshtml" />
<Content Include="Scripts\jquery-1.10.2.min.map" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CompressR.MVC5\CompressR.MVC5.csproj">
@@ -235,9 +238,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
<None Include="Project_Readme.html" />
<Content Include="Scripts\jquery-1.10.2.min.map" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>

View File

@@ -1,4 +1,5 @@
using System;
using CompressR.Exceptions;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
@@ -14,8 +15,19 @@ namespace CompressR.WebApi
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class CompressAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
private bool RequireCompression { get; set; }
public CompressAttribute(bool requireCompression = false)
{
RequireCompression = requireCompression;
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if(actionExecutedContext.Response.Content == null)
{
return;
}
var acceptedEncoding = actionExecutedContext
.Response
.RequestMessage
@@ -26,15 +38,26 @@ namespace CompressR.WebApi
.FirstOrDefault();
if (string.IsNullOrWhiteSpace(acceptedEncoding))
{
if (RequireCompression)
{
throw new CompressRException("Compression required but client did not send accept header");
}
else
{
return;
}
}
actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, acceptedEncoding);
}
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
if(actionExecutedContext.Response.Content == null)
{
return;
}
var acceptedEncoding = actionExecutedContext
.Response
.RequestMessage

View File

@@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CompressR.WebApi</RootNamespace>
<AssemblyName>CompressR.WebApi</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -63,6 +64,12 @@
<None Include="CompressR.WebApi.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CompressR\CompressR.csproj">
<Project>{c94378c3-4aa8-4f49-8720-77d14b62f72e}</Project>
<Name>CompressR</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@@ -14,8 +14,19 @@ namespace CompressR.WebApi
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class DeflateAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
private bool RequireCompression { get; set; }
public DeflateAttribute(bool requireCompression = false)
{
RequireCompression = requireCompression;
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if(actionExecutedContext.Response.Content == null)
{
return;
}
var acceptedEncoding = actionExecutedContext
.Response
.RequestMessage
@@ -24,7 +35,7 @@ namespace CompressR.WebApi
.Select(a => a.Value)
.Any(a => a.Equals(Constants.Deflate, StringComparison.OrdinalIgnoreCase));
if (acceptedEncoding)
if (!acceptedEncoding)
{
return;
}
@@ -33,6 +44,10 @@ namespace CompressR.WebApi
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
if(actionExecutedContext.Response.Content == null)
{
return;
}
var acceptedEncoding = actionExecutedContext
.Response
.RequestMessage
@@ -41,7 +56,7 @@ namespace CompressR.WebApi
.Select(a => a.Value)
.Any(a => a.Equals(Constants.Deflate, StringComparison.OrdinalIgnoreCase));
if (acceptedEncoding)
if (!acceptedEncoding)
{
return;
}

View File

@@ -13,8 +13,19 @@ namespace CompressR.WebApi
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class GzipAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
private bool RequireCompression { get; set; }
public GzipAttribute(bool requireCompression = false)
{
RequireCompression = requireCompression;
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if(actionExecutedContext.Response.Content == null)
{
return;
}
var acceptedEncoding = actionExecutedContext
.Response
.RequestMessage
@@ -23,7 +34,7 @@ namespace CompressR.WebApi
.Select(a => a.Value)
.Any(a => a.Equals(Constants.Gzip, StringComparison.OrdinalIgnoreCase));
if (acceptedEncoding)
if (!acceptedEncoding)
{
return;
}
@@ -32,6 +43,10 @@ namespace CompressR.WebApi
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
if(actionExecutedContext.Response.Content == null)
{
return;
}
var acceptedEncoding = actionExecutedContext
.Response
.RequestMessage
@@ -40,7 +55,7 @@ namespace CompressR.WebApi
.Select(a => a.Value)
.Any(a => a.Equals(Constants.Gzip, StringComparison.OrdinalIgnoreCase));
if (acceptedEncoding)
if (!acceptedEncoding)
{
return;
}

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
</packages>

View File

@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C94378C3-4AA8-4F49-8720-77D14B62F72E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CompressR</RootNamespace>
<AssemblyName>CompressR</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\CompressRException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="CompressR.nuspec" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<package>
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<licenseUrl>https://opensource.org/licenses/MIT</licenseUrl>
<projectUrl>https://github.com/tparnell8/CompressR</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Help you compress your actions</description>
<releaseNotes></releaseNotes>
<copyright>Copyright 2016</copyright>
<tags>Compression MVC</tags>
</metadata>
</package>

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace CompressR.Exceptions
{
public class CompressRException: Exception
{
public CompressRException()
{
}
public CompressRException(string message) : base(message) { }
public CompressRException(string message, Exception innerException) : base(message, innerException)
{
}
protected CompressRException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}

View File

@@ -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")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("CompressR")]
[assembly: AssemblyCopyright("Copyright © Microsoft 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("c94378c3-4aa8-4f49-8720-77d14b62f72e")]
// 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.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]