Skip to content

Commit 795e939

Browse files
no-longer-on-githu-bowickstrom
authored andcommitted
Move application/x-www-form-urlencoded parsing into separate module
1 parent 38bb49a commit 795e939

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/Hyper/Form.purs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ import Data.MediaType.Common (applicationFormURLEncoded)
2323
import Data.Monoid (class Monoid)
2424
import Data.Newtype (class Newtype, unwrap)
2525
import Data.StrMap (lookup)
26-
import Data.String (split, joinWith, Pattern(Pattern))
27-
import Data.Traversable (sequence)
28-
import Data.Tuple (Tuple(Tuple))
29-
import Global (decodeURIComponent)
26+
import Data.String (Pattern(Pattern), split)
27+
import Data.Tuple (Tuple)
3028
import Hyper.Conn (Conn)
29+
import Hyper.Form.Urlencoded (parseUrlencoded)
3130
import Hyper.Middleware (Middleware)
3231
import Hyper.Middleware.Class (getConn)
3332
import Hyper.Request (class Request, class ReadableBody, getRequestData, readBody)
@@ -60,24 +59,6 @@ parseContentMediaType = split (Pattern ";")
6059
>>> head
6160
>>> map MediaType
6261

63-
64-
toTuple :: Array String -> Either String (Tuple String (Maybe String))
65-
toTuple kv =
66-
case kv of
67-
[key] ->
68-
pure (Tuple (decodeURIComponent key) Nothing)
69-
[key, value] ->
70-
pure (Tuple (decodeURIComponent key) (Just (decodeURIComponent value)))
71-
parts ->
72-
throwError ("Invalid form key-value pair: " <> joinWith " " parts)
73-
74-
75-
splitPairs :: String Either String (Array (Tuple String (Maybe String)))
76-
splitPairs = split (Pattern "&")
77-
>>> map (split (Pattern "="))
78-
>>> map toTuple
79-
>>> sequence
80-
8162
parseForm forall m req res c
8263
. Monad m
8364
=> Request req m
@@ -95,7 +76,7 @@ parseForm = do
9576
Nothing ->
9677
ipure (Left "Missing or invalid content-type header.")
9778
Just mediaType | mediaType == applicationFormURLEncoded ->
98-
ipure (Form <$> splitPairs body)
79+
ipure (Form <$> parseUrlencoded body)
9980
Just mediaType ->
10081
ipure (Left ("Cannot parse media of type: " <> show mediaType))
10182
where bind = ibind

src/Hyper/Form/Urlencoded.purs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- | Parser for the `application/x-www-form-urlencoded` format, commonly used
2+
-- | for query strings and POST request bodies.
3+
module Hyper.Form.Urlencoded
4+
( parseUrlencoded
5+
) where
6+
7+
import Prelude
8+
import Control.Monad.Error.Class (throwError)
9+
import Data.Either (Either)
10+
import Data.Maybe (Maybe(Just, Nothing))
11+
import Data.String (split, joinWith, Pattern(Pattern))
12+
import Data.Traversable (sequence)
13+
import Data.Tuple (Tuple(Tuple))
14+
import Global (decodeURIComponent)
15+
16+
toTuple :: Array String -> Either String (Tuple String (Maybe String))
17+
toTuple kv =
18+
case kv of
19+
[key] ->
20+
pure (Tuple (decodeURIComponent key) Nothing)
21+
[key, value] ->
22+
pure (Tuple (decodeURIComponent key) (Just (decodeURIComponent value)))
23+
parts ->
24+
throwError ("Invalid form key-value pair: " <> joinWith " " parts)
25+
26+
27+
parseUrlencoded :: String Either String (Array (Tuple String (Maybe String)))
28+
parseUrlencoded = split (Pattern "&")
29+
>>> map (split (Pattern "="))
30+
>>> map toTuple
31+
>>> sequence

0 commit comments

Comments
 (0)