wai-handler-launch-1.3.1.4/0000755000000000000000000000000012110452450013501 5ustar0000000000000000wai-handler-launch-1.3.1.4/wai-handler-launch.cabal0000644000000000000000000000273212110452450020134 0ustar0000000000000000Name: wai-handler-launch Version: 1.3.1.4 Synopsis: Launch a web app in the default browser. Description: This handles cross-platform launching and inserts Javascript code to ping the server. When the server no longer receives pings, it shuts down. License: MIT License-file: LICENSE Author: Michael Snoyman Maintainer: michael@snoyman.com Category: Web Build-type: Simple Cabal-version: >=1.6 Library Exposed-modules: Network.Wai.Handler.Launch build-depends: base >= 4 && < 5 , wai >= 1.3 && < 1.5 , warp >= 1.3 && < 1.4 , http-types >= 0.7 , transformers >= 0.2.2 && < 0.4 , bytestring >= 0.9.1.4 , blaze-builder >= 0.2.1.4 && < 0.4 , conduit >= 0.5 && < 1.1 , blaze-builder-conduit >= 0.5 && < 1.1 , zlib-conduit >= 0.5 && < 1.1 if os(windows) c-sources: windows.c cpp-options: -DWINDOWS extra-libraries: Shell32 else build-depends: process >= 1.0 && < 1.2 if os(darwin) cpp-options: -DMAC source-repository head type: git location: git://github.com/yesodweb/wai.git wai-handler-launch-1.3.1.4/LICENSE0000644000000000000000000000207512110452450014512 0ustar0000000000000000Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. wai-handler-launch-1.3.1.4/Setup.lhs0000644000000000000000000000016212110452450015310 0ustar0000000000000000#!/usr/bin/env runhaskell > module Main where > import Distribution.Simple > main :: IO () > main = defaultMain wai-handler-launch-1.3.1.4/windows.c0000644000000000000000000000051012110452450015333 0ustar0000000000000000#include #include #include void launch(int port, char *s) { int len = 8 + strlen("http://127.0.0.1:") + strlen(s); char *buff = malloc(len); snprintf(buff, len, "http://127.0.0.1:%d/%s", port, s); ShellExecute(NULL, "open", buff, NULL, NULL, SW_SHOWNORMAL); free(buff); } wai-handler-launch-1.3.1.4/Network/0000755000000000000000000000000012110452450015132 5ustar0000000000000000wai-handler-launch-1.3.1.4/Network/Wai/0000755000000000000000000000000012110452450015652 5ustar0000000000000000wai-handler-launch-1.3.1.4/Network/Wai/Handler/0000755000000000000000000000000012110452450017227 5ustar0000000000000000wai-handler-launch-1.3.1.4/Network/Wai/Handler/Launch.hs0000644000000000000000000001167312110452450021005 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ForeignFunctionInterface #-} {-# LANGUAGE CPP #-} module Network.Wai.Handler.Launch ( run , runUrl , runUrlPort ) where import Network.Wai import Network.HTTP.Types import qualified Network.Wai.Handler.Warp as Warp import Data.IORef import Control.Concurrent (forkIO, threadDelay) import Control.Monad.IO.Class (liftIO) import qualified Data.ByteString as S import Blaze.ByteString.Builder (fromByteString) #if WINDOWS import Foreign import Foreign.C.String #else import System.Cmd (rawSystem) #endif import Data.Conduit.Zlib (decompressFlush, WindowBits (WindowBits)) import Data.Conduit.Blaze (builderToByteStringFlush) import Data.Conduit import qualified Data.Conduit.List as CL ping :: IORef Bool -> Middleware ping var app req | pathInfo req == ["_ping"] = do liftIO $ writeIORef var True return $ responseLBS status200 [] "" | otherwise = do res <- app req let isHtml hs = case lookup "content-type" hs of Just ct -> "text/html" `S.isPrefixOf` ct Nothing -> False case res of ResponseFile _ hs _ _ | not $ isHtml hs -> return res ResponseBuilder _ hs _ | not $ isHtml hs -> return res ResponseSource _ hs _ | not $ isHtml hs -> return res _ -> do let (s, hs, body) = responseSource res let (isEnc, headers') = fixHeaders id hs let headers'' = filter (\(x, _) -> x /= "content-length") headers' let fixEnc src = if isEnc then src $= decompressFlush (WindowBits 31) else src return $ ResponseSource s headers'' $ fixEnc (body $= builderToByteStringFlush) $= insideHead $= CL.map (fmap fromByteString) toInsert :: S.ByteString toInsert = "" insideHead :: Conduit (Flush S.ByteString) (ResourceT IO) (Flush S.ByteString) insideHead = loop' (S.empty, whole) where loop' state = await >>= maybe (close state) (push' state) whole = "" push' state (Chunk x) = push state x push' state Flush = yield Flush >> loop' state push (held, atFront) x | atFront `S.isPrefixOf` x = do let y = S.drop (S.length atFront) x mapM_ (yield . Chunk) [held, atFront, toInsert, y] CL.map id | whole `S.isInfixOf` x = do let (before, rest) = S.breakSubstring whole x let after = S.drop (S.length whole) rest mapM_ (yield . Chunk) [held, before, whole, toInsert, after] CL.map id | x `S.isPrefixOf` atFront = do let held' = held `S.append` x atFront' = S.drop (S.length x) atFront loop' (held', atFront') | otherwise = do let (held', atFront', x') = getOverlap whole x mapM_ (yield . Chunk) [held, x'] loop' (held', atFront') close (held, _) = mapM_ yield [Chunk held, Chunk toInsert] getOverlap :: S.ByteString -> S.ByteString -> (S.ByteString, S.ByteString, S.ByteString) getOverlap whole x = go whole where go piece | S.null piece = ("", whole, x) | piece `S.isSuffixOf` x = let x' = S.take (S.length x - S.length piece) x atFront = S.drop (S.length piece) whole in (piece, atFront, x') | otherwise = go $ S.init piece fixHeaders :: ([Header] -> [Header]) -> [Header] -> (Bool, [Header]) fixHeaders front [] = (False, front []) fixHeaders front (("content-encoding", "gzip"):rest) = (True, front rest) fixHeaders front (x:xs) = fixHeaders (front . (:) x) xs #if WINDOWS foreign import ccall "launch" launch' :: Int -> CString -> IO () #endif launch :: Int -> String -> IO () #if WINDOWS launch port s = withCString s $ launch' port #else launch port s = forkIO (rawSystem #if MAC "open" #else "xdg-open" #endif ["http://127.0.0.1:" ++ show port ++ "/" ++ s] >> return ()) >> return () #endif run :: Application -> IO () run = runUrl "" runUrl :: String -> Application -> IO () runUrl = runUrlPort 4587 runUrlPort :: Int -> String -> Application -> IO () runUrlPort port url app = do x <- newIORef True _ <- forkIO $ Warp.runSettings Warp.defaultSettings { Warp.settingsPort = port , Warp.settingsOnException = const $ return () , Warp.settingsHost = "*4" } $ ping x app launch port url loop x loop :: IORef Bool -> IO () loop x = do let seconds = 120 threadDelay $ 1000000 * seconds b <- readIORef x if b then writeIORef x False >> loop x else return ()