Skip to content

Fixed encoding of spaces #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ New features:

Bugfixes:

- Fixed encoding of spaces. Spaces must be replaced by `+` instead of `%20` (#24 by @dederer)

Other improvements:

## [v6.0.0](https://github.com/purescript-contrib/purescript-form-urlencoded/releases/tag/v6.0.0) - 2021-02-26
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Use the `encode` function to properly encode an array of key-value pairs, and th
> [ Tuple "hey" Nothing
> , Tuple "Oh" (Just "Let's go!")
> ]
Just "hey&Oh=Let's%20go!"
Just "hey&Oh=Let's+go!"

> decode "a=aa&b=bb"
Just (FormURLEncoded [(Tuple "a" (Just "aa")),(Tuple "b" (Just "bb"))])
Expand Down
10 changes: 5 additions & 5 deletions src/Data/FormURLEncoded.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Data.String (joinWith, split) as String
import Data.String.Pattern (Pattern(..))
import Data.Traversable (traverse)
import Data.Tuple (Tuple(..))
import JSURI (decodeURIComponent, encodeURIComponent)
import JSURI (decodeFormURLComponent, encodeFormURLComponent)

-- | `FormURLEncoded` is an ordered list of key-value pairs with possible duplicates.
newtype FormURLEncoded = FormURLEncoded (Array (Tuple String (Maybe String)))
Expand All @@ -35,14 +35,14 @@ encode :: FormURLEncoded -> Maybe String
encode = map (String.joinWith "&") <<< traverse encodePart <<< toArray
where
encodePart = case _ of
Tuple k Nothing -> encodeURIComponent k
Tuple k (Just v) -> (\key val -> key <> "=" <> val) <$> encodeURIComponent k <*> encodeURIComponent v
Tuple k Nothing -> encodeFormURLComponent k
Tuple k (Just v) -> (\key val -> key <> "=" <> val) <$> encodeFormURLComponent k <*> encodeFormURLComponent v

-- | Decode `FormURLEncoded` from `application/x-www-form-urlencoded`.
decode :: String -> Maybe FormURLEncoded
decode = map FormURLEncoded <<< traverse decodePart <<< String.split (Pattern "&")
where
decodePart = String.split (Pattern "=") >>> case _ of
[k, v] -> (\key val -> Tuple key $ Just val) <$> decodeURIComponent k <*> decodeURIComponent v
[k] -> Tuple <$> decodeURIComponent k <*> pure Nothing
[k, v] -> (\key val -> Tuple key $ Just val) <$> decodeFormURLComponent k <*> decodeFormURLComponent v
[k] -> Tuple <$> decodeFormURLComponent k <*> pure Nothing
_ -> Nothing
4 changes: 4 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ main = do

testDecode "a=b&%8A=c" Nothing

testDecode "a+b=aa+bb" (Just $ FormURLEncoded [ Tuple "a b" $ Just "aa bb" ])

testEncode (FormURLEncoded [ Tuple "a" $ Just "aa", Tuple "b" $ Just "bb" ]) $ Just "a=aa&b=bb"

testEncode (FormURLEncoded [ Tuple "this" $ Just "this=this" ]) $ Just "this=this%3Dthis"
Expand All @@ -41,6 +43,8 @@ main = do
])
(Just "&x=x&&y=y&z=")

testEncode (FormURLEncoded [ Tuple "a b" $ Just "aa bb" ]) $ Just "a+b=aa+bb"

where

testDecode :: String -> Maybe FormURLEncoded -> Effect Unit
Expand Down