Snap.Util.GZip

snap-core-1.0.5.1: Snap: A Haskell Web Framework (core interfaces and types)

Safe Haskell | None
Language | Haskell2010

Snap.Util.GZip

Description

Helpers for running a `Snap` web handler with compression.

Synopsis

- withCompression :: MonadSnap m => m a -> m ()
- withCompression' :: MonadSnap m => Set ByteString -> m a -> m ()
- noCompression :: MonadSnap m => m ()
- data BadAcceptEncodingException
- compressibleMimeTypes :: Set ByteString

# Documentation

withCompression #

Arguments

:: MonadSnap m |
=> m a | the web handler to run
-> m () |

Runs a `Snap` web handler with compression if available.

If the client has indicated support for `gzip` or `deflate` in its `Accept-Encoding` header, and the `Content-Type` in the response is one of the following types:

-     application/x-javascript

-     application/json

-     text/css

-     text/html

-     text/javascript

-     text/plain

-     text/xml

-     application/x-font-truetype

Then the given handler's output stream will be compressed, `Content-Encoding` will be set in the output headers, and the `Content-Length` will be cleared if it was set. (We can't process the stream in O(1) space if the length is known beforehand.)

The wrapped handler will be run to completion, and then the `Response` that's contained within the `Snap` monad state will be passed to `finishWith` to prevent further processing.

Example:

    ghci> :set -XOverloadedStrings
    ghci> import qualified Data.Map as M
    ghci> import qualified Snap.Test as T
    ghci> let r = T.get "/" M.empty >> T.addHeader "Accept-Encoding" "gzip,deflate"
    ghci> let h = modifyResponse (setContentType "text/plain") >> writeBS "some text"
    ghci> T.runHandler r h
    HTTP/1.1 200 OK
    content-type: text/plain
    server: Snap/test
    date: Fri, 08 Aug 2014 15:40:45 GMT

    some text
    ghci> T.runHandler r (withCompression h)
    HTTP/1.1 200 OK
    content-type: text/plain
    vary: Accept-Encoding
    content-encoding: gzip
    server: Snap/test
    date: Fri, 08 Aug 2014 15:40:10 GMT

withCompression' #

Arguments

:: MonadSnap m |
=> Set ByteString | set of compressible MIME types
-> m a | the web handler to run
-> m () |

The same as `withCompression`, with control over which MIME types to compress.

noCompression :: MonadSnap m => m () #

Turn off compression by setting "Content-Encoding: identity" in the response headers. `withCompression` is a no-op when a content-encoding is already set.

data BadAcceptEncodingException #

Thrown when the 'Accept-Encoding' request header has invalid format.

#### Instances

Instances details

Exception BadAcceptEncodingException # |
Instance details Defined in Snap.Util.GZip Methods toException :: BadAcceptEncodingException -> SomeException # fromException :: SomeException -> Maybe BadAcceptEncodingException # displayException :: BadAcceptEncodingException -> String # backtraceDesired :: BadAcceptEncodingException -> Bool #
Show BadAcceptEncodingException # |
Instance details Defined in Snap.Util.GZip Methods showsPrec :: Int -> BadAcceptEncodingException -> ShowS # show :: BadAcceptEncodingException -> String # showList :: [BadAcceptEncodingException] -> ShowS #

compressibleMimeTypes :: Set ByteString #