happstack-server-7.5.1.1: Web related tools and services.

Safe HaskellNone
LanguageHaskell2010

Happstack.Server.Internal.Compression

Description

Filter for compressing the Response body.

Synopsis

Documentation

compressedResponseFilter Source #

Arguments

:: (FilterMonad Response m, MonadPlus m, WebMonad Response m, ServerMonad m) 
=> m String

name of the encoding chosen

reads the Accept-Encoding header. Then, if possible will compress the response body with methods gzip or deflate.

This function uses standardEncodingHandlers. If you want to provide alternative handers (perhaps to change compression levels), see compressedResponseFilter'

main =
  simpleHTTP nullConf $
     do str <- compressedResponseFilter
        return $ toResponse ("This response compressed using: " ++ str)

compressedResponseFilter' Source #

Arguments

:: (FilterMonad Response m, MonadPlus m, WebMonad Response m, ServerMonad m) 
=> [(String, String -> Bool -> m ())]

compression filter assoc list

-> m String

name of the encoding chosen

reads the Accept-Encoding header. Then, if possible will compress the response body using one of the supplied filters.

A filter function takes two arguments. The first is a String with the value to be used as the 'Content-Encoding' header. The second is Bool which indicates if the compression filter is allowed to fallback to identity.

This is important if the resource being sent using sendfile, since sendfile does not provide a compression option. If identity is allowed, then the file can be sent uncompressed using sendfile. But if identity is not allowed, then the filter will need to return error 406.

You should probably always include the identity and * encodings as acceptable.

myFilters :: (FilterMonad Response m) => [(String, String -> Bool -> m ()]
myFilters = [ ("gzip"    , gzipFilter)
            , ("identity", identityFilter)
            , ("*"       , starFilter)
            ]

main =
  simpleHTTP nullConf $
     do let filters =
str <- compressedResponseFilter'
        return $ toResponse ("This response compressed using: " ++ str)

compressWithFilter Source #

Arguments

:: FilterMonad Response m 
=> (ByteString -> ByteString)

function to compress the body

-> String

encoding to use for Content-Encoding header

-> Bool

fallback to identity for SendFile

-> m () 

Ignore the Accept-Encoding header in the Request and attempt to compress the body of the response using the supplied compressor.

We can not compress files being transfered using SendFile. If identity is an allowed encoding, then just return the Response unmodified. Otherwise we return 406 Not Acceptable.

see also: gzipFilter, deflateFilter, identityFilter, starFilter, compressedResponseFilter'

gzipFilter Source #

Arguments

:: FilterMonad Response m 
=> String

encoding to use for Content-Encoding header

-> Bool

fallback to identity for SendFile

-> m () 

Ignore the Accept-Encoding header in the Request and attempt to compress the body of the response with gzip.

calls compressWithFilter using compress.

see also: compressedResponseFilter

deflateFilter Source #

Arguments

:: FilterMonad Response m 
=> String

encoding to use for Content-Encoding header

-> Bool

fallback to identity for SendFile

-> m () 

Ignore the Accept-Encoding header in the Request and attempt compress the body of the response with zlib's deflate method

calls compressWithFilter using compress.

see also: compressedResponseFilter

identityFilter Source #

Arguments

:: FilterMonad Response m 
=> String

encoding to use for Content-Encoding header

-> Bool

fallback to identity for SendFile (irrelavant for this filter)

-> m () 

compression filter for the identity encoding (aka, do nothing)

see also: compressedResponseFilter

starFilter Source #

Arguments

:: FilterMonad Response m 
=> String

encoding to use for Content-Encoding header

-> Bool

fallback to identity for SendFile (irrelavant for this filter)

-> m () 

compression filter for the * encoding

This filter always fails.

encodings :: GenParser Char st [(String, Maybe Double)] Source #

a parser for the Accept-Encoding header

standardEncodingHandlers :: FilterMonad Response m => [(String, String -> Bool -> m ())] Source #

an assoc list of encodings and their corresponding compression functions.

e.g.

[("gzip", gzipFilter), ("identity", identityFilter), ("*",starFilter)]