X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FUtils.hs;h=3d38b8b3aec36c9dff990cb4c7e66d05995ad27e;hb=a362be1c8664306b970c32e1df9b62081498feb1;hp=ec4b6727f60567613549604a1c0e209d6d2a7727;hpb=db4b61223e0d8b34079d3b190fb3e3644b0b4866;p=Lucu.git diff --git a/Network/HTTP/Lucu/Utils.hs b/Network/HTTP/Lucu/Utils.hs index ec4b672..3d38b8b 100644 --- a/Network/HTTP/Lucu/Utils.hs +++ b/Network/HTTP/Lucu/Utils.hs @@ -1,15 +1,14 @@ {-# LANGUAGE - BangPatterns - , OverloadedStrings + OverloadedStrings , UnicodeSyntax #-} -- |Utility functions used internally in the Lucu httpd. These -- functions may be useful too for something else. module Network.HTTP.Lucu.Utils ( splitBy - , joinWith , quoteStr , parseWWWFormURLEncoded + , splitPathInfo , show3 ) where @@ -29,22 +28,11 @@ import Prelude.Unicode -- |> splitBy (== ':') "ab:c:def" -- > ==> ["ab", "c", "def"] splitBy ∷ (a → Bool) → [a] → [[a]] +{-# INLINEABLE splitBy #-} splitBy isSep src - = case break isSep src - of (last , [] ) → [last] - (first, _sep:rest) → first : splitBy isSep rest - --- |> joinWith ":" ["ab", "c", "def"] --- > ==> "ab:c:def" -joinWith ∷ Ascii → [AsciiBuilder] → AsciiBuilder -{-# INLINEABLE joinWith #-} -joinWith sep = flip go (∅) - where - go ∷ [AsciiBuilder] → AsciiBuilder → AsciiBuilder - {-# INLINE go #-} - go [] ab = ab - go (x:[]) ab = ab ⊕ x - go (x:xs) ab = go xs (ab ⊕ A.toAsciiBuilder sep ⊕ x) + = case break isSep src of + (last , [] ) → [last] + (first, _sep:rest) → first : splitBy isSep rest -- |> quoteStr "abc" -- > ==> "\"abc\"" @@ -89,6 +77,15 @@ parseWWWFormURLEncoded src plusToSpace '+' = ' ' plusToSpace c = c +-- |> splitPathInfo "http://example.com/foo/bar" +-- > ==> ["foo", "bar"] +splitPathInfo ∷ URI → [ByteString] +splitPathInfo uri + = let reqPathStr = uriPath uri + reqPath = [unEscapeString x | x ← splitBy (≡ '/') reqPathStr, (¬) (null x)] + in + map BS.pack reqPath + -- |> show3 5 -- > ==> "005" show3 ∷ Integral n ⇒ n → AsciiBuilder @@ -99,3 +96,5 @@ show3 = A.unsafeFromBuilder ∘ go | i ≥ 0 ∧ i < 100 = B.fromByteString "0" ⊕ BT.integral i | i ≥ 0 ∧ i < 1000 = BT.integral i | otherwise = error ("show3: the integer i must satisfy 0 <= i < 1000: " ⧺ show i) +-- FIXME: Drop this function as soon as possible, to eliminate the +-- dependency on blaze-textual.