Skip to content

Commit 812ab5d

Browse files
authored
Merge pull request #1464 from spencerjanssen/unicode-username-1339
Preserve non-ASCII input in form2json forms
2 parents 5ee3e26 + 14d77ff commit 812ab5d

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/Distribution/Server/Framework/HtmlFormWrapper.hs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{-# LANGUAGE FlexibleContexts #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
24
module Distribution.Server.Framework.HtmlFormWrapper (
35
htmlFormWrapperHack,
46
rqRealMethod,
@@ -16,6 +18,7 @@ import qualified Data.Text as T
1618
import qualified Data.Aeson.Key as Key
1719
import qualified Data.Aeson.KeyMap as KeyMap
1820
import Control.Concurrent.MVar
21+
import Data.Text.Encoding (decodeUtf8Lenient, encodeUtf8)
1922

2023
import Distribution.Server.Framework.HappstackUtils (showContentType)
2124

@@ -149,7 +152,7 @@ requestFormDataAsJSON = do
149152
let keyvals = [ (k, v)
150153
| (k, Input { inputValue = Right v }) <- fromMaybe [] mbody
151154
, case k of '_':_ -> False; _ -> True ]
152-
paths = [ parsePathTmpl (BS8.unpack v) k
155+
paths = [ parsePathTmpl (decodeUtf8Lenient (BS8.toStrict v)) k
153156
| (k, v) <- keyvals ]
154157
case accumJPaths paths of
155158
Nothing -> return $ Left (zip keyvals paths )
@@ -158,28 +161,29 @@ requestFormDataAsJSON = do
158161
data JPath = JField T.Text JPath | JVal JSON.Value | JPathErr
159162
deriving Show
160163

161-
parsePathTmpl :: String -> String -> JPath
164+
parsePathTmpl :: T.Text -> String -> JPath
162165
parsePathTmpl v = parseKey
163166
where
164167
parseKey s =
165168
case break (\c -> c == '.' || c == '=') s of
166-
("%f", '.':r) -> JField (T.pack v) (parseKey r)
169+
("%f", '.':r) -> JField v (parseKey r)
167170
(c@(_:_), '.':r) -> JField (T.pack c) (parseKey r)
168-
("%f", '=':r) -> JField (T.pack v) (parseVal r)
171+
("%f", '=':r) -> JField v (parseVal r)
169172
(c@(_:_), '=':r) -> JField (T.pack c) (parseVal r)
170173
_ -> JPathErr
171-
parseVal "%s" = JVal (JSON.String (T.pack v))
172-
parseVal "%n" | [(n,"")] <- reads v = JVal (JSON.Number (fromIntegral (n :: Int)))
174+
parseVal :: String -> JPath
175+
parseVal "%s" = JVal (JSON.String v)
176+
parseVal "%n" | [(n,"")] <- reads (T.unpack v) = JVal (JSON.Number (fromIntegral (n :: Int)))
173177
parseVal "%v" | Just j <- parseJVal v = JVal j
174-
parseVal s | Just j <- parseJVal s = JVal j
178+
parseVal s | Just j <- parseJVal (T.pack s) = JVal j
175179
parseVal _ = JPathErr
176180

177181
parseJVal "true" = Just (JSON.Bool True)
178182
parseJVal "false" = Just (JSON.Bool False)
179183
parseJVal "null" = Just JSON.Null
180-
parseJVal s | [(str,"")] <- reads s = Just (JSON.String (T.pack str))
181-
parseJVal s | [(n, "")] <- reads s = Just (JSON.Number (fromIntegral (n :: Int)))
182-
parseJVal s = JSON.decode (BS8.pack s)
184+
parseJVal s | [(str,"")] <- reads (T.unpack s) = Just (JSON.String (T.pack str))
185+
parseJVal s | [(n, "")] <- reads (T.unpack s) = Just (JSON.Number (fromIntegral (n :: Int)))
186+
parseJVal s = JSON.decodeStrict (encodeUtf8 s)
183187

184188
accumJPaths :: [JPath] -> Maybe JSON.Value
185189
accumJPaths js = f JSON.Null

0 commit comments

Comments
 (0)