diff --git a/src/Snap/Util/GZip.hs b/src/Snap/Util/GZip.hs index ea463679..cc83a68c 100644 --- a/src/Snap/Util/GZip.hs +++ b/src/Snap/Util/GZip.hs @@ -30,7 +30,7 @@ import Data.Set (Set) import qualified Data.Set as Set (fromList, member) import Data.Typeable (Typeable) import Prelude (Either (..), Eq (..), IO, Show (show), id, not, ($), ($!), (&&), (++), (||)) -import Snap.Core (MonadSnap, clearContentLength, finishWith, getHeader, getRequest, getResponse, modifyResponse, modifyResponseBody, setHeader) +import Snap.Core (MonadSnap, HasHeaders, clearContentLength, finishWith, getHeader, getRequest, getResponse, modifyResponse, modifyResponseBody, setHeader, addHeader) import Snap.Internal.Debug (debug) import Snap.Internal.Parsing (fullyParse) import System.IO.Streams (OutputStream) @@ -176,15 +176,17 @@ compressibleMimeTypes = Set.fromList [ "application/x-font-truetype" , "text/plain" , "text/xml" ] - - +updateVaryHeader :: HasHeaders a => a -> a +updateVaryHeader resp = case getHeader "Accept-Encoding" resp of + Just "*" -> resp + _ -> addHeader "Vary" "Accept-Encoding" resp ------------------------------------------------------------------------------ gzipCompression :: MonadSnap m => ByteString -> m () gzipCompression ce = modifyResponse f where f r = setHeader "Content-Encoding" ce $ - setHeader "Vary" "Accept-Encoding" $ + updateVaryHeader $ clearContentLength $ modifyResponseBody gcompress r @@ -194,7 +196,7 @@ compressCompression :: MonadSnap m => ByteString -> m () compressCompression ce = modifyResponse f where f r = setHeader "Content-Encoding" ce $ - setHeader "Vary" "Accept-Encoding" $ + updateVaryHeader $ clearContentLength $ modifyResponseBody ccompress r diff --git a/test/Snap/Util/GZip/Tests.hs b/test/Snap/Util/GZip/Tests.hs index 2e639627..e01ab86a 100644 --- a/test/Snap/Util/GZip/Tests.hs +++ b/test/Snap/Util/GZip/Tests.hs @@ -40,6 +40,7 @@ import Control.Applicative ((<$>)) tests :: [Test] tests = [ testIdentity1 , testIdentity1_charset + , testIdentity1_vary , testIdentity2 , testIdentity3 , testIdentity4 @@ -240,6 +241,22 @@ testIdentity1_charset = testProperty "gzip/identity1_charset" $ let s1 = GZip.decompress $ L.fromChunks [body] assertEqual "" s s1 +testIdentity1_vary :: Test +testIdentity1_vary = testProperty "gzip/identity1_vary" $ + monadicIO $ forAllM arbitrary prop + where + prop :: L.ByteString -> PropertyM IO () + prop s = liftQ $ do + (!_,!rsp) <- goGZip (seqSnap $ withCompression $ do + modifyResponse $ setHeader "Vary" "Origin" + textPlain s) + assertEqual "" (Just "gzip") $ getHeader "Content-Encoding" rsp + assertEqual "" (Just "Origin,Accept-Encoding") $ getHeader "Vary" rsp + + body <- Test.getResponseBody rsp + let s1 = GZip.decompress $ L.fromChunks [body] + assertEqual "" s s1 + ------------------------------------------------------------------------------ testIdentity2 :: Test