6 -- |Utility functions used internally in the Lucu httpd. These
7 -- functions may be useful too for something else.
8 module Network.HTTP.Lucu.Utils
11 , parseWWWFormURLEncoded
20 import Blaze.ByteString.Builder.ByteString as B
21 import Blaze.Text.Int as BT
23 import Data.Ascii (Ascii, CIAscii, AsciiBuilder)
24 import qualified Data.Ascii as A
25 import Data.ByteString (ByteString)
26 import qualified Data.ByteString.Char8 as BS
28 import Data.List hiding (last)
30 import qualified Data.Map as M
31 import Data.Monoid.Unicode
32 import Data.Text (Text)
33 import qualified Data.Text as T
34 import Language.Haskell.TH.Lib
35 import Language.Haskell.TH.Syntax
37 import Prelude hiding (last)
38 import Prelude.Unicode
40 -- |>>> splitBy (== ':') "ab:c:def"
42 splitBy ∷ (a → Bool) → [a] → [[a]]
43 {-# INLINEABLE splitBy #-}
45 = case break isSep src of
47 (first, _sep:rest) → first : splitBy isSep rest
49 -- |>>> quoteStr "abc"
52 -- >>> quoteStr "ab\"c"
54 quoteStr ∷ Ascii → AsciiBuilder
55 quoteStr str = A.toAsciiBuilder "\"" ⊕
56 go (A.toByteString str) (∅) ⊕
59 go ∷ BS.ByteString → AsciiBuilder → AsciiBuilder
61 = case BS.break (≡ '"') bs of
63 | BS.null y → ab ⊕ b2ab x
64 | otherwise → go (BS.tail y) (ab ⊕ b2ab x
65 ⊕ A.toAsciiBuilder "\\\"")
67 b2ab ∷ BS.ByteString → AsciiBuilder
68 b2ab = A.toAsciiBuilder ∘ A.unsafeFromByteString
70 -- |>>> parseWWWFormURLEncoded "aaa=bbb&ccc=ddd"
71 -- [("aaa", "bbb"), ("ccc", "ddd")]
72 parseWWWFormURLEncoded ∷ Ascii → [(ByteString, ByteString)]
73 parseWWWFormURLEncoded src
74 -- THINKME: We could gain some performance by using attoparsec
77 | otherwise = do pairStr ← splitBy (\ c → c ≡ ';' ∨ c ≡ '&') (A.toString src)
78 let (key, value) = break (≡ '=') pairStr
80 , unescape $ case value of
85 unescape ∷ String → ByteString
86 unescape = BS.pack ∘ unEscapeString ∘ map plusToSpace
88 plusToSpace ∷ Char → Char
92 -- |>>> splitPathInfo "http://example.com/foo/bar"
94 splitPathInfo ∷ URI → [ByteString]
96 = let reqPathStr = uriPath uri
97 reqPath = [unEscapeString x | x ← splitBy (≡ '/') reqPathStr, (¬) (null x)]
103 show3 ∷ Integral n ⇒ n → AsciiBuilder
104 {-# INLINEABLE show3 #-}
105 show3 = A.unsafeFromBuilder ∘ go
107 go i | i ≥ 0 ∧ i < 10 = B.fromByteString "00" ⊕ BT.digit i
108 | i ≥ 0 ∧ i < 100 = B.fromByteString "0" ⊕ BT.integral i
109 | i ≥ 0 ∧ i < 1000 = BT.integral i
110 | otherwise = error ("show3: the integer i must satisfy 0 <= i < 1000: " ⧺ show i)
111 -- FIXME: Drop this function as soon as possible, to eliminate the
112 -- dependency on blaze-textual.
114 -- |>>> trim " ab c d "
116 trim ∷ String → String
117 trim = reverse ∘ f ∘ reverse ∘ f
119 f = dropWhile isSpace
121 -- |Convert a 'CIAscii' to an 'Exp' representing it as a literal.
122 liftCIAscii ∷ CIAscii → Q Exp
123 liftCIAscii a = [| A.toCIAscii (A.unsafeFromString $(strLit a)) |]
125 strLit ∷ CIAscii → Q Exp
126 strLit = liftString ∘ A.toString ∘ A.fromCIAscii
128 -- |Convert a 'Text' to an 'Exp' representing it as a literal.
129 liftText ∷ Text → Q Exp
130 liftText t = [| T.pack $(strLit t) |]
132 strLit ∷ Text → Q Exp
133 strLit = liftString ∘ T.unpack
135 -- |Convert an arbitrary 'Map' to an 'Exp' representing it as a
136 -- literal, using a given key lifter and a value lifter.
137 liftMap ∷ Eq k ⇒ (k → Q Exp) → (v → Q Exp) → Map k v → Q Exp
138 liftMap liftK liftV m = [| M.fromAscList $(liftPairs $ M.toAscList m) |]
140 liftPairs = listE ∘ map liftPair
141 liftPair (k, v) = tupE [liftK k, liftV v]