14 import qualified Codec.Binary.UTF8.String as UTF8
16 import Control.Arrow.ArrowList
17 import qualified Data.ByteString.Lazy as Lazy (ByteString)
18 import qualified Data.ByteString.Lazy.Char8 as L8 hiding (ByteString)
20 import Network.HTTP.Lucu
22 import System.IO.Unsafe
25 yesOrNo :: Bool -> String
30 trueOrFalse :: Bool -> String
31 trueOrFalse True = "true"
32 trueOrFalse False = "false"
35 parseYesOrNo :: ArrowChoice a => a String Bool
37 = proc str -> do case str of
38 "yes" -> returnA -< True
39 "no" -> returnA -< False
40 _ -> returnA -< error ("Expected yes or no: " ++ str)
43 maybeA :: (ArrowList a, ArrowChoice a) => a b c -> a b (Maybe c)
47 [] -> returnA -< Nothing
48 (x:_) -> returnA -< Just x
51 deleteIfEmpty :: (ArrowList a, ArrowChoice a) => a String String
53 = proc str -> do case str of
58 chomp :: String -> String
59 chomp = reverse . snd . break (/= '\n') . reverse
62 guessMIMEType :: Lazy.ByteString -> MIMEType
63 guessMIMEType = read . unsafePerformIO . magicString magic . L8.unpack
66 magic = unsafePerformIO
67 $ do m <- magicOpen [MagicMime]
72 isSafeChar :: Char -> Bool
75 | isReserved c = False
76 | c > ' ' && c <= '~' = True
80 mkQueryString :: [(String, String)] -> String
82 mkQueryString ((k, v) : xs) = encode k ++ "=" ++ encode v ++
86 ';' : mkQueryString(xs)
88 encode :: String -> String
89 encode = escapeURIString isSafeChar . UTF8.encodeString