Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snap.Util.GZip: fix Vary header handling #337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Snap/Util/GZip.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
17 changes: 17 additions & 0 deletions test/Snap/Util/GZip/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Control.Applicative ((<$>))
tests :: [Test]
tests = [ testIdentity1
, testIdentity1_charset
, testIdentity1_vary
, testIdentity2
, testIdentity3
, testIdentity4
Expand Down Expand Up @@ -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
Expand Down