+-- >>> quoteStr "ab\"c"
+-- "\"ab\\\"c\""
+quoteStr ∷ Ascii → AsciiBuilder
+quoteStr str = A.toAsciiBuilder "\"" ⊕
+ go (A.toByteString str) (∅) ⊕
+ A.toAsciiBuilder "\""
+ where
+ go ∷ ByteString → AsciiBuilder → AsciiBuilder
+ go bs ab
+ = case BS.break (≡ '"') bs of
+ (x, y)
+ | BS.null y
+ → ab ⊕ b2ab x
+ | otherwise
+ → go (BS.tail y)
+ (ab ⊕ b2ab x ⊕ A.toAsciiBuilder "\\\"")
+
+ b2ab ∷ ByteString → AsciiBuilder
+ b2ab = A.toAsciiBuilder ∘ A.unsafeFromByteString
+
+-- |>>> parseWWWFormURLEncoded "aaa=bbb&ccc=ddd"
+-- [("aaa", "bbb"), ("ccc", "ddd")]
+parseWWWFormURLEncoded ∷ Ascii → [(ByteString, ByteString)]
+parseWWWFormURLEncoded src
+ -- THINKME: We could gain some performance by using attoparsec
+ -- here.
+ | src ≡ "" = []
+ | otherwise = do pairStr ← splitBy (\ c → c ≡ ';' ∨ c ≡ '&') (A.toString src)
+ let (key, value) = break (≡ '=') pairStr
+ return ( unescape key
+ , unescape $ case value of
+ ('=':val) → val
+ val → val
+ )
+ where
+ unescape ∷ String → ByteString
+ unescape = BS.pack ∘ unEscapeString ∘ (plusToSpace <$>)
+
+ plusToSpace ∷ Char → Char
+ plusToSpace '+' = ' '
+ plusToSpace c = c
+
+-- |>>> uriHost "http://example.com/foo/bar"
+-- "example.com"
+uriHost ∷ URI → Host
+{-# INLINE uriHost #-}
+uriHost = CI.mk ∘ T.pack ∘ uriRegName ∘ fromJust ∘ uriAuthority
+
+-- |>>> uriPathSegments "http://example.com/foo/bar"
+-- ["foo", "bar"]
+uriPathSegments ∷ URI → Path
+uriPathSegments uri
+ = let reqPathStr = uriPath uri
+ reqPath = [ unEscapeString x
+ | x ← splitBy (≡ '/') reqPathStr, (¬) (null x) ]
+ in
+ BS.pack <$> reqPath
+
+-- |>>> trim " ab c d "
+-- "ab c d"
+trim ∷ String → String
+trim = reverse ∘ f ∘ reverse ∘ f