@@ -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 ()