Require compression via attribute (#1)

* Require compression via attribute

* Added compressr exception
This commit is contained in:
Brian Kennedy
2016-06-20 10:21:42 -04:00
committed by Tommy Parnell
parent 61d8fc7923
commit dd922411ae
8 changed files with 140 additions and 2 deletions

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

@@ -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

@@ -1,4 +1,5 @@
using System;
using CompressR.Exceptions;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
@@ -14,6 +15,13 @@ 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)
{
var acceptedEncoding = actionExecutedContext
@@ -27,7 +35,10 @@ namespace CompressR.WebApi
if (string.IsNullOrWhiteSpace(acceptedEncoding))
{
return;
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);

View File

@@ -64,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

@@ -0,0 +1,54 @@
<?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>
<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,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CompressR.Exceptions
{
public class CompressRException: Exception
{
public CompressRException(string message) : base(message) { }
}
}

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")]