on master help sign in
Mac, fix Gzip
commita2530fa0dde7bf5b8a3f86b3c2cab1081674d5b4 authorMichael Snoyman <michael@snoyman.com> dateWed 25 May 2011, 06:47 (15 years ago) parent241972f
3 files changed+29−5
Network/Wai/Handler/Launch.hs+21−3 test.hs+2−1 wai-handler-launch.cabal+6−1
Network/Wai/Handler/Launch.hs+21−3
@@ -12,11 +12,14 @@ import Foreign
1212import Foreign.C.String
1313import Control.Monad.IO.Class (liftIO)
1414import qualified Data.ByteString as S
15−import Data.Enumerator (($$), enumList)
15+import Data.Enumerator (($$), enumList, joinI)
1616import Blaze.ByteString.Builder (fromByteString)
1717#if !WINDOWS
1818import System.Cmd (rawSystem)
1919#endif
20+import Codec.Zlib.Enum (ungzip)
21+import Blaze.ByteString.Builder.Enumerator (builderToByteString)
22+import qualified Data.Enumerator.List as EL
2023
2124ping :: IORef Bool -> Middleware
2225ping var app req
@@ -31,15 +34,30 @@ ping var app req
3134 case lookup "content-type" headers of
3235 Just ct
3336 | "text/html" `S.isPrefixOf` ct -> do
34− enumList 1 [fromByteString "<script>setInterval(function(){var x;if(window.XMLHttpRequest){x=new XMLHttpRequest();}else{x=new ActiveXObject(\"Microsoft.XMLHTTP\");}x.open(\"GET\",\"/_ping\",false);x.send();},60000)</script>"] $$ f status headers
37+ let (isEnc, headers') = fixHeaders id headers
38+ let fixEnc x =
39+ if isEnc
40+ then joinI $ builderToByteString $$ joinI $ ungzip $$ joinI $ EL.map fromByteString $$ x
41+ else x
42+ fixEnc $ enumList 1 [fromByteString "<script>setInterval(function(){var x;if(window.XMLHttpRequest){x=new XMLHttpRequest();}else{x=new ActiveXObject(\"Microsoft.XMLHTTP\");}x.open(\"GET\",\"/_ping\",false);x.send();},60000)</script>"] $$ f status headers'
3543 _ -> f status headers
3644
45+fixHeaders front [] = (False, front [])
46+fixHeaders front (("content-encoding", "gzip"):rest) = (True, front rest)
47+fixHeaders front (x:xs) = fixHeaders (front . (:) x) xs
48+
3749#if WINDOWS
3850foreign import ccall "launch"
3951 launch :: IO ()
4052#else
4153launch :: IO ()
42−launch = rawSystem "xdg-open" ["http://localhost:4587/"] >> return ()
54+launch = forkIO (rawSystem
55+#if MAC
56+ "open"
57+#else
58+ "xdg-open"
59+#endif
60+ ["http://localhost:4587/"] >> return ()) >> return ()
4361#endif
4462
4563run :: Application -> IO ()
test.hs+2−1
@@ -2,5 +2,6 @@
22import Network.Wai
33import Network.HTTP.Types
44import Network.Wai.Handler.Launch
5+import Network.Wai.Middleware.Gzip
56
6−main = run $ \_ -> return $ responseLBS status200 [("Content-Type", "text/html; charset=utf8")] "HELLO THERE"
7+main = run $ gzip True $ \_ -> return $ responseLBS status200 [("Content-Type", "text/html; charset=utf8")] "HELLO THERE"
wai-handler-launch.cabal+6−1
@@ -20,9 +20,14 @@ Library
2020 , bytestring >= 0.9 && < 1
2121 , blaze-builder >= 0.2 && < 0.4
2222 , enumerator >= 0.4 && < 0.5
23+ , blaze-builder-enumerator>= 0.2 && < 0.3
24+ , zlib-enum >= 0.2 && < 0.3
2325 if os(windows)
2426 c-sources: windows.c
2527 cpp-options: -DWINDOWS
2628 extra-libraries: Shell32
2729 else
28− build-depends: process >= 1.0 && < 1.2
30+ if os(darwin)
31+ cpp-options: -DMAC
32+ else
33+ build-depends: process >= 1.0 && < 1.2