@@ -66,8 +66,6 @@ module Servant.API.ContentTypes
6666 , AllMime(..)
6767 , AllMimeRender(..)
6868 , AllMimeUnrender(..)
69− , FromFormUrlEncoded(..)
70− , ToFormUrlEncoded(..)
7169 , eitherDecodeLenient
7270 , canHandleAcceptH
7371 ) where
@@ -82,10 +80,8 @@ import Data.Attoparsec.ByteString.Char8 (endOfInput, parseOnly,
8280import qualified Data.ByteString as BS
8381import Data.ByteString.Lazy (ByteString, fromStrict,
8482 toStrict)
85−import qualified Data.ByteString.Lazy as B
8683import qualified Data.ByteString.Lazy.Char8 as BC
8784import Data.Maybe (isJust)
88−import Data.Monoid.Compat
8985import Data.String.Conversions (cs)
9086import qualified Data.Text as TextS
9187import qualified Data.Text.Encoding as TextS
@@ -94,8 +90,9 @@ import qualified Data.Text.Lazy.Encoding as TextL
9490import Data.Typeable
9591import GHC.Generics (Generic)
9692import qualified Network.HTTP.Media as M
97−import Network.URI (escapeURIString,
98− isUnreserved, unEscapeString)
93+import Web.FormUrlEncoded (FromForm, ToForm,
94+ urlEncodeAsForm,
95+ urlDecodeAsForm)
9996import Prelude ()
10097import Prelude.Compat
10198
@@ -290,12 +287,12 @@ instance OVERLAPPABLE_
290287 ToJSON a => MimeRender JSON a where
291288 mimeRender _ = encode
292289
293−-- | @encodeFormUrlEncoded . toFormUrlEncoded@
290+-- | @urlEncodeAsForm@
294291-- Note that the @mimeUnrender p (mimeRender p x) == Right x@ law only
295292-- holds if every element of x is non-null (i.e., not @("", "")@)
296293instance OVERLAPPABLE_
297− ToFormUrlEncoded a => MimeRender FormUrlEncoded a where
298− mimeRender _ = encodeFormUrlEncoded . toFormUrlEncoded
294+ ToForm a => MimeRender FormUrlEncoded a where
295+ mimeRender _ = urlEncodeAsForm
299296
300297-- | `TextL.encodeUtf8`
301298instance MimeRender PlainText TextL.Text where
@@ -348,11 +345,11 @@ eitherDecodeLenient input =
348345instance FromJSON a => MimeUnrender JSON a where
349346 mimeUnrender _ = eitherDecodeLenient
350347
351−-- | @decodeFormUrlEncoded >=> fromFormUrlEncoded@
348+-- | @urlDecodeAsForm@
352349-- Note that the @mimeUnrender p (mimeRender p x) == Right x@ law only
353350-- holds if every element of x is non-null (i.e., not @("", "")@)
354−instance FromFormUrlEncoded a => MimeUnrender FormUrlEncoded a where
355− mimeUnrender _ = decodeFormUrlEncoded >=> fromFormUrlEncoded
351+instance FromForm a => MimeUnrender FormUrlEncoded a where
352+ mimeUnrender _ = left TextS.unpack . urlDecodeAsForm
356353
357354-- | @left show . TextL.decodeUtf8'@
358355instance MimeUnrender PlainText TextL.Text where
@@ -375,49 +372,6 @@ instance MimeUnrender OctetStream BS.ByteString where
375372 mimeUnrender _ = Right . toStrict
376373
377374
378−--------------------------------------------------------------------------
379−-- * FormUrlEncoded
380−
381−-- | A type that can be converted to @application/x-www-form-urlencoded@
382−class ToFormUrlEncoded a where
383− toFormUrlEncoded :: a -> [(TextS.Text, TextS.Text)]
384−
385−instance ToFormUrlEncoded [(TextS.Text, TextS.Text)] where
386− toFormUrlEncoded = id
387−
388−-- | A type that can be converted from @application/x-www-form-urlencoded@,
389−-- with the possibility of failure.
390−class FromFormUrlEncoded a where
391− fromFormUrlEncoded :: [(TextS.Text, TextS.Text)] -> Either String a
392−
393−instance FromFormUrlEncoded [(TextS.Text, TextS.Text)] where
394− fromFormUrlEncoded = return
395−
396−encodeFormUrlEncoded :: [(TextS.Text, TextS.Text)] -> ByteString
397−encodeFormUrlEncoded xs =
398− let escape :: TextS.Text -> ByteString
399− escape = cs . escapeURIString isUnreserved . cs
400− encodePair :: (TextS.Text, TextS.Text) -> ByteString
401− encodePair (k, "") = escape k
402− encodePair (k, v) = escape k <> "=" <> escape v
403− in B.intercalate "&" $ map encodePair xs
404−
405−decodeFormUrlEncoded :: ByteString -> Either String [(TextS.Text, TextS.Text)]
406−decodeFormUrlEncoded "" = return []
407−decodeFormUrlEncoded q = do
408− let xs :: [TextS.Text]
409− xs = TextS.splitOn "&" . cs $ q
410− parsePair :: TextS.Text -> Either String (TextS.Text, TextS.Text)
411− parsePair p =
412− case TextS.splitOn "=" p of
413− [k,v] -> return ( unescape k
414− , unescape v
415− )
416− [k] -> return ( unescape k, "" )
417− _ -> Left $ "not a valid pair: " <> cs p
418− unescape :: TextS.Text -> TextS.Text
419− unescape = cs . unEscapeString . cs . TextS.intercalate "%20" . TextS.splitOn "+"
420− mapM parsePair xs
421375
422376-- $setup
423377-- >>> import Servant.API