diff --git a/.gitignore b/.gitignore index 3a2238d..e1af492 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ bld/ [Bb]in/ [Oo]bj/ +nuget.exe # Visual Studio 2015 cache/options directory .vs/ diff --git a/CompressR.sln b/CompressR.sln index b9ea086..9a3d652 100644 --- a/CompressR.sln +++ b/CompressR.sln @@ -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 diff --git a/gulpfile.js b/gulpfile.js index e556892..14c9882 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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})); }); \ No newline at end of file diff --git a/nuget.exe b/nuget.exe deleted file mode 100644 index 6bb79fe..0000000 Binary files a/nuget.exe and /dev/null differ diff --git a/src/CompressR.MVC/CompressAttribute.cs b/src/CompressR.MVC/CompressAttribute.cs index 7bfd3d8..1f7dde9 100644 --- a/src/CompressR.MVC/CompressAttribute.cs +++ b/src/CompressR.MVC/CompressAttribute.cs @@ -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); } } } \ No newline at end of file diff --git a/src/CompressR.MVC/CompressFactory.cs b/src/CompressR.MVC/CompressFactory.cs index efa6351..4e4c399 100644 --- a/src/CompressR.MVC/CompressFactory.cs +++ b/src/CompressR.MVC/CompressFactory.cs @@ -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) { - return; + 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) diff --git a/src/CompressR.MVC/DeflateAttribute.cs b/src/CompressR.MVC/DeflateAttribute.cs index 7bf2de2..c280353 100644 --- a/src/CompressR.MVC/DeflateAttribute.cs +++ b/src/CompressR.MVC/DeflateAttribute.cs @@ -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; + } + /// /// Override to compress the content that is generated by /// an action method. @@ -14,7 +21,7 @@ namespace CompressR.MVC /// public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext) { - CompressFactory.Compress(Constants.Deflate, filterContext); + CompressFactory.Compress(Constants.Deflate, filterContext, RequireCompression); } } } \ No newline at end of file diff --git a/src/CompressR.MVC/GzipAttribute.cs b/src/CompressR.MVC/GzipAttribute.cs index badb05a..c270ff4 100644 --- a/src/CompressR.MVC/GzipAttribute.cs +++ b/src/CompressR.MVC/GzipAttribute.cs @@ -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; + } + /// /// Override to compress the content that is generated by /// an action method. @@ -14,7 +21,7 @@ namespace CompressR.MVC /// public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext) { - CompressFactory.Compress(Constants.Gzip, filterContext); + CompressFactory.Compress(Constants.Gzip, filterContext, RequireCompression); } } } \ No newline at end of file diff --git a/src/CompressR.MVC4/CompressR.MVC4.csproj b/src/CompressR.MVC4/CompressR.MVC4.csproj index a1432da..50fa99e 100644 --- a/src/CompressR.MVC4/CompressR.MVC4.csproj +++ b/src/CompressR.MVC4/CompressR.MVC4.csproj @@ -75,6 +75,12 @@ + + + {c94378c3-4aa8-4f49-8720-77d14b62f72e} + CompressR + + + \ No newline at end of file diff --git a/src/CompressR/CompressR.nuspec b/src/CompressR/CompressR.nuspec new file mode 100644 index 0000000..a580997 --- /dev/null +++ b/src/CompressR/CompressR.nuspec @@ -0,0 +1,17 @@ + + + + $id$ + $version$ + $title$ + $author$ + $author$ + https://opensource.org/licenses/MIT + https://github.com/tparnell8/CompressR + false + Help you compress your actions + + Copyright 2016 + Compression MVC + + \ No newline at end of file diff --git a/src/CompressR/Exceptions/CompressRException.cs b/src/CompressR/Exceptions/CompressRException.cs new file mode 100644 index 0000000..dff11d3 --- /dev/null +++ b/src/CompressR/Exceptions/CompressRException.cs @@ -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) + { + } + } +} diff --git a/src/CompressR/Properties/AssemblyInfo.cs b/src/CompressR/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4d2376d --- /dev/null +++ b/src/CompressR/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")] +[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")]