Coder Social home page Coder Social logo

jackjet / microsoft.aspnet.webapi.messagehandlers.compression Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azzlack/microsoft.aspnet.webapi.messagehandlers.compression

0.0 2.0 0.0 1.18 MB

Drop-in module for ASP.Net WebAPI that enables GZip and Deflate support

License: Apache License 2.0

C# 99.90% ASP 0.10%

microsoft.aspnet.webapi.messagehandlers.compression's Introduction

Compression support for ASP.NET WebAPI and HttpClient

TeamCity Build Status

Drop-in module for ASP.Net WebAPI that enables GZip and Deflate support.
This module is based on this blog post by Ben Foster which in turn is based on this blog post by Kiran Challa.
This code improves on their work by adding several new options, as well as fixing some issues with the original code.

Package Version Downloads
Microsoft.AspNet.WebApi.Extensions.Compression.Server NuGet Package Version NuGet Package Downloads
System.Net.Http.Extensions.Compression.Client NuGet Package Version NuGet Package Downloads

How to use

Server side

You need to add the compression handler as the last applied message handler on outgoing requests, and the first one on incoming requests.
To do that, just add the following line to your App_Start\WebApiConfig.cs file after adding all your other message handlers:

GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));

This will insert the ServerCompressionHandler to the request pipeline as the first on incoming requests, and the last on outgoing requests.

Client side

JavaScript

If you are doing your requests with JavaScript you probably don't have to do do anything.
Just make sure the gzip and deflate values are included in the Accept-Encoding header. (Most browsers do this by default)

C#

You need to apply the following code when creating your HttpClient.

var client = new HttpClient(new ClientCompressionHandler(new GZipCompressor(), new DeflateCompressor()));

client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));

Thats it! You should now immediately start experiencing smaller payloads when doing GET, POST, PUT, etc.

Advanced use

Skip compression of requests/responses that are smaller than a specified value

By default, both ServerCompressionHandler and ClientCompressionHandler compress everything larger than 860 bytes.
However, this can be overriden by inserting a threshold as the first parameter like this:

var serverCompressionHandler = new ServerCompressionHandler(4096, new GZipCompressor(), new DeflateCompressor());
var clientCompressionHandler = new ClientCompressionHandler(4096, new GZipCompressor(), new DeflateCompressor());

The above code will skip compression for any request/response that is smaller than 4096 bytes / 4 kB.

Disable compression for endpoint

It is possible to disable compression for a specific endpoint. Just add the [Compression(Enabled = false)] attribute to your endpoint method. (Or the whole controller if you want to disable for all endpoints in it)

OWIN Authentication

When using the OWIN Authentication pipeline, you might encounter errors saying that Server cannot append header after http headers have been sent. This is a bug with OWIN, but as of this moment it has not been fixed.

The workaround is to install the Microsoft.AspNet.WebApi.Extensions.Compression.Server.Owin package and use the included OwinServerCompressionHandler instead of the default ServerCompressionHandler. This class contains code to detect whether the headers have been sent already and prevent any attempts at compression.

Version history

2.0.3 (current)

2.0.0

  • Added attribute for disable compression for certain routes
  • Fixed clearing of non-standard properties when compressing and decompressing
  • Stop trying to compress requests/responses with no content
  • Properly copy HTTP headers from the content that is going to be compressed
  • Fixed 504 timeout error when returning ByteArrayContent from an ApiController
  • Fixed bug with content stream sometimes being disposed before returning
  • Added better test coverage
  • Changed default threshold for compression to 860 bytes (what Akamai uses)
  • Now reports proper Content-Length
  • Simplified usage
  • Added support for setting a minimum content size for compressing
  • First release, basic compression of server responses and client requests
  • Did not support compressing POSTs and PUTs

microsoft.aspnet.webapi.messagehandlers.compression's People

Contributors

azzlack avatar abrenneke avatar cheesebaron avatar gentledepp avatar brettbialer avatar charlesnrice avatar danielcrenna avatar wiltodelta avatar sebastianstehle avatar altumano avatar coni2k avatar turbodrubin avatar

Watchers

James Cloos avatar Jackey avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.