Giter Site home page Giter Site logo

qvalues's Introduction

QValues

Utility classes for correctly parsing Accept-Encodings

Utility classes for RFC 2616 compliant parsing of the Accept-Encoding, Accept-Charset and Accept-Language header.

Background

Dave Transom's blog post on What's wrong with Request.Headers["Accept-Encoding"].Contains("gzip")? goes into detail on why parsing Accept-Encoding with String#contains is a malpractice that has some downsides you should be aware of.

His article also contains some nice utility classes that does what thousand of sites do wrong, which is what I have put up here, along with some bug fixes.

Typical usage

/// load encodings from header
QValueList encodings = new QValueList(Request.Headers["Accept-Encoding"]);

/// get the types we can handle, can be accepted and
/// in the defined client preference
QValue preferred = encodings.FindPreferred("gzip", "deflate", "identity");

/// if none of the preferred values were found, but the
/// client can accept wildcard encodings, we'll default
/// to Gzip.
if (preferred.IsEmpty && encodings.AcceptWildcard && encodings.Find("gzip").IsEmpty)
  preferred = new QValue("gzip");

// handle the preferred encoding
switch (preferred.Name)
{
  case "gzip":
      Response.AppendHeader("Content-Encoding", "gzip");
      Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);
    break;
  case "deflate":
      Response.AppendHeader("Content-Encoding", "deflate");
      Response.Filter = new DeflateStream(Response.Filter, CompressionMode.Compress);
    break;
  case "identity":
  default:
    break;
}

qvalues's People

Contributors

fatso83 avatar davetransom avatar

Watchers

James Cloos avatar  avatar

Forkers

ejhnsn teja286

qvalues's Issues

Missing a license

@davetransom: mind if I add a license so that it is clear that people can actually use the code, even though you already said so?

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.