7 -- |Utility functions used internally in this package.
8 module Network.HTTP.Lucu.Utils
11 , parseWWWFormURLEncoded
26 import Control.Applicative
28 import Data.Ascii (Ascii, CIAscii, AsciiBuilder)
29 import qualified Data.Ascii as A
30 import Data.ByteString (ByteString)
31 import qualified Data.ByteString.Char8 as Strict
32 import qualified Data.ByteString.Lazy.Internal as Lazy
34 import Data.List hiding (last)
36 import qualified Data.Map as M
37 import Data.Monoid.Unicode
39 import Data.Text (Text)
40 import qualified Data.Text as T
42 import Data.Time.Clock.POSIX
43 import Language.Haskell.TH.Lib
44 import Language.Haskell.TH.Syntax
46 import Prelude hiding (last)
47 import Prelude.Unicode
48 import System.Directory
49 import System.Time (ClockTime(..))
51 -- |>>> splitBy (== ':') "ab:c:def"
53 splitBy ∷ (a → Bool) → [a] → [[a]]
54 {-# INLINEABLE splitBy #-}
56 = case break isSep src of
58 (first, _sep:rest) → first : splitBy isSep rest
60 -- |>>> quoteStr "abc"
63 -- >>> quoteStr "ab\"c"
65 quoteStr ∷ Ascii → AsciiBuilder
66 quoteStr str = A.toAsciiBuilder "\"" ⊕
67 go (A.toByteString str) (∅) ⊕
70 go ∷ Strict.ByteString → AsciiBuilder → AsciiBuilder
72 = case Strict.break (≡ '"') bs of
78 (ab ⊕ b2ab x ⊕ A.toAsciiBuilder "\\\"")
80 b2ab ∷ Strict.ByteString → AsciiBuilder
81 b2ab = A.toAsciiBuilder ∘ A.unsafeFromByteString
83 -- |>>> parseWWWFormURLEncoded "aaa=bbb&ccc=ddd"
84 -- [("aaa", "bbb"), ("ccc", "ddd")]
85 parseWWWFormURLEncoded ∷ Ascii → [(ByteString, ByteString)]
86 parseWWWFormURLEncoded src
87 -- THINKME: We could gain some performance by using attoparsec
90 | otherwise = do pairStr ← splitBy (\ c → c ≡ ';' ∨ c ≡ '&') (A.toString src)
91 let (key, value) = break (≡ '=') pairStr
93 , unescape $ case value of
98 unescape ∷ String → ByteString
99 unescape = Strict.pack ∘ unEscapeString ∘ map plusToSpace
101 plusToSpace ∷ Char → Char
102 plusToSpace '+' = ' '
105 -- |>>> splitPathInfo "http://example.com/foo/bar"
107 splitPathInfo ∷ URI → [ByteString]
109 = let reqPathStr = uriPath uri
110 reqPath = [unEscapeString x | x ← splitBy (≡ '/') reqPathStr, (¬) (null x)]
112 map Strict.pack reqPath
114 -- |>>> trim " ab c d "
116 trim ∷ String → String
117 trim = reverse ∘ f ∘ reverse ∘ f
119 f = dropWhile isSpace
121 -- |Get the modification time of a given file.
122 getLastModified ∷ FilePath → IO UTCTime
123 getLastModified = (clockTimeToUTC <$>) ∘ getModificationTime
125 clockTimeToUTC ∷ ClockTime → UTCTime
126 clockTimeToUTC (TOD sec picoSec)
127 = posixSecondsToUTCTime
129 $ sec % 1 + picoSec % (1000 ⋅ 1000 ⋅ 1000 ⋅ 1000)
131 -- |Convert a 'ByteString' to an 'Exp' representing it as a literal.
132 liftByteString ∷ ByteString → Q Exp
134 = [| Strict.pack $(litE $ stringL $ Strict.unpack bs) |]
136 -- |Convert a 'Lazy.ByteString' to an 'Exp' representing it as a
138 liftLazyByteString ∷ Lazy.ByteString → Q Exp
139 liftLazyByteString = Lazy.foldrChunks f [| Lazy.Empty |]
141 f ∷ ByteString → Q Exp → Q Exp
142 f bs e = [| Lazy.Chunk $(liftByteString bs) $e |]
144 -- |Convert an 'Ascii' to an 'Exp' representing it as a literal.
145 liftAscii ∷ Ascii → Q Exp
146 liftAscii a = [| A.unsafeFromByteString $(liftByteString $ A.toByteString a) |]
148 -- |Convert a 'CIAscii' to an 'Exp' representing it as a literal.
149 liftCIAscii ∷ CIAscii → Q Exp
150 liftCIAscii a = [| A.toCIAscii $(liftAscii $ A.fromCIAscii a) |]
152 -- |Convert a 'Text' to an 'Exp' representing it as a literal.
153 liftText ∷ Text → Q Exp
154 liftText t = [| T.pack $(litE $ stringL $ T.unpack t) |]
156 -- |Convert an arbitrary 'Map' to an 'Exp' representing it as a
157 -- literal, using a given key lifter and a value lifter.
158 liftMap ∷ Eq k ⇒ (k → Q Exp) → (v → Q Exp) → Map k v → Q Exp
159 liftMap liftK liftV m
160 | M.null m = [| M.empty |]
161 | otherwise = [| M.fromDistinctAscList $(liftPairs (M.toAscList m)) |]
163 liftPairs = listE ∘ map liftPair
164 liftPair (k, v) = tupE [liftK k, liftV v]
166 -- |Convert an 'UTCTime' to an 'Exp' representing it as a literal.
167 liftUTCTime ∷ UTCTime → Q Exp
168 liftUTCTime (UTCTime {..})
169 = [| UTCTime $(liftDay utctDay) $(liftDiffTime utctDayTime) |]
171 liftDay ∷ Day → Q Exp
172 liftDay (ModifiedJulianDay {..})
173 = [| ModifiedJulianDay $(lift toModifiedJulianDay) |]
175 liftDiffTime ∷ DiffTime → Q Exp
176 liftDiffTime dt = [| fromRational ($n % $d) ∷ DiffTime |]
179 n = lift $ numerator $ toRational dt
180 d = lift $ denominator $ toRational dt