git.iko.soy/typeable/http-api-data
on master help sign in
Refactor decodeForm
f2a7554Nickolay Kudasov
src/Web/Internal/FormUrlEncoded.hs+12−15
@@ -295,21 +295,18 @@ encodeForm xs = BSL.intercalate "&" $ map (BSL.fromStrict . encodePair) $ toList
295-- >>> decodeForm "this=has=too=many=equals"
295-- >>> decodeForm "this=has=too=many=equals"
296-- Left "not a valid pair: this=has=too=many=equals"
296-- Left "not a valid pair: this=has=too=many=equals"
297decodeForm :: BSL.ByteString -> Either T.Text Form
297decodeForm :: BSL.ByteString -> Either T.Text Form
298−decodeForm "" = return mempty
298+decodeForm bs = toForm <$> traverse parsePair pairs
299−decodeForm q = do
299+ where
300− let xs :: [T.Text]
301− xs = T.splitOn "&" . decodeUtf8With lenientDecode . mconcat . BSL.toChunks $ q
300+ pairs = map (decodeUtf8With lenientDecode . BSL.toStrict) (BSL.split '&' bs)
301+
302+ unescape = T.pack . unEscapeString . T.unpack . T.replace "+" "%20"
303+
302− parsePair :: T.Text -> Either T.Text (T.Text, T.Text)
304+ parsePair :: T.Text -> Either T.Text (T.Text, T.Text)
303− parsePair p =
305+ parsePair p =
304− case T.splitOn "=" p of
306+ case T.splitOn "=" p of
305− [k,v] -> return ( unescape k
307+ [k, v] -> return (unescape k, unescape v)
306− , unescape v
307− )
308− [k] -> return ( unescape k, "" )
308+ [k] -> return (unescape k, "" )
309− _ -> Left $ "not a valid pair: " <> p
309+ _ -> Left $ "not a valid pair: " <> p
310− unescape :: T.Text -> T.Text
311− unescape = T.pack . unEscapeString . T.unpack . T.intercalate "%20" . T.splitOn "+"
312− toForm <$> mapM parsePair xs
313
310
314data Proxy3 a b c = Proxy3
311data Proxy3 a b c = Proxy3
315
312