]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Utils.hs
Bugfix: parseWWWFormURLEncoded now replaces each '+' to ' ', that were previously...
[Lucu.git] / Network / HTTP / Lucu / Utils.hs
index f1c178d55a1b22983d1ec75907873186f4446686..dbc65ac17ad8a52f553af26e5506a33df9aab137 100644 (file)
@@ -1,3 +1,7 @@
+{-# LANGUAGE
+    BangPatterns
+  , UnicodeSyntax
+  #-}
 -- |Utility functions used internally in the Lucu httpd. These
 -- functions may be useful too for something else.
 module Network.HTTP.Lucu.Utils
@@ -9,11 +13,11 @@ module Network.HTTP.Lucu.Utils
     , parseWWWFormURLEncoded
     )
     where
-
 import Control.Monad
 import Data.List     hiding (last)
 import Network.URI
 import Prelude       hiding (last)
+import Prelude.Unicode
 
 -- |> splitBy (== ':') "ab:c:def"
 --  > ==> ["ab", "c", "def"]
@@ -61,13 +65,20 @@ quoteStr !str = concat (["\""] ++ map quote str ++ ["\""])
 
 -- |> parseWWWFormURLEncoded "aaa=bbb&ccc=ddd"
 --  > ==> [("aaa", "bbb"), ("ccc", "ddd")]
-parseWWWFormURLEncoded :: String -> [(String, String)]
+parseWWWFormURLEncoded ∷ String → [(String, String)]
 parseWWWFormURLEncoded src
-    | src == "" = []
-    | otherwise = do pairStr <- splitBy (\ c -> c == ';' || c == '&') src
-                     let (key, value) = break (== '=') pairStr
-                     return ( unEscapeString key
-                            , unEscapeString $ case value of
-                                                 ('=':val) -> val
-                                                 val       -> val
+    | null src  = []
+    | otherwise = do pairStr ← splitBy (\ c → c ≡ ';' ∨ c ≡ '&') src
+                     let (key, value) = break ( '=') pairStr
+                     return ( unescape key
+                            , unescape $ case value of
+                                           ('=':val) → val
+                                           val       → val
                             )
+    where
+      unescape ∷ String → String
+      unescape = unEscapeString ∘ map plusToSpace
+
+      plusToSpace ∷ Char → Char
+      plusToSpace '+' = ' '
+      plusToSpace c   = c