From 4bdeb448412c1d092d6bc94820488db04e764b3f Mon Sep 17 00:00:00 2001 From: Zachary Juang Date: Sun, 17 Dec 2023 09:49:42 -0500 Subject: [PATCH] read from queryString instead of rawQueryString (#360) * read from queryString instead of rawQueryString * use decodeUtf8Lenient * add test --- Web/Scotty/Route.hs | 9 +++------ test/Web/ScottySpec.hs | 12 +++++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Web/Scotty/Route.hs b/Web/Scotty/Route.hs index 8a9cb04..474a876 100644 --- a/Web/Scotty/Route.hs +++ b/Web/Scotty/Route.hs @@ -11,9 +11,6 @@ import Control.Monad.IO.Class (MonadIO(..)) import UnliftIO (MonadUnliftIO(..)) import qualified Control.Monad.State as MS -import qualified Data.ByteString.Char8 as B - -import Data.Maybe (fromMaybe) import Data.String (fromString) import qualified Data.Text as T @@ -160,14 +157,14 @@ mkEnv :: MonadIO m => BodyInfo -> Request -> [Param] -> RouteOptions -> m Action mkEnv bodyInfo req captureps opts = do (formps, bodyFiles) <- liftIO $ getFormParamsAndFilesAction req bodyInfo opts let - queryps = parseEncodedParams $ rawQueryString req + queryps = parseEncodedParams $ queryString req bodyFiles' = [ (decodeUtf8Lenient k, fi) | (k,fi) <- bodyFiles ] responseInit <- liftIO $ newTVarIO defaultScottyResponse return $ Env req captureps formps queryps (getBodyAction bodyInfo opts) (getBodyChunkAction bodyInfo) bodyFiles' responseInit -parseEncodedParams :: B.ByteString -> [Param] -parseEncodedParams bs = [ (k, fromMaybe "" v) | (k,v) <- parseQueryText bs ] +parseEncodedParams :: Query -> [Param] +parseEncodedParams qs = [ ( decodeUtf8Lenient k, maybe "" decodeUtf8Lenient v) | (k,v) <- qs ] {- | Match requests using a regular expression. Named captures are not yet supported. diff --git a/test/Web/ScottySpec.hs b/test/Web/ScottySpec.hs index af577d3..a55eb6b 100644 --- a/test/Web/ScottySpec.hs +++ b/test/Web/ScottySpec.hs @@ -11,7 +11,7 @@ import Data.String import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Encoding as TLE import Network.HTTP.Types -import Network.Wai (Application, responseLBS) +import Network.Wai (Application, Request(queryString), responseLBS) import qualified Control.Exception.Lifted as EL import qualified Control.Exception as E @@ -174,6 +174,16 @@ spec = do it "Responds with a 302 Redirect" $ do get "/a" `shouldRespondWith` 302 { matchHeaders = ["Location" <:> "/b"] } + describe "middleware" $ do + context "rewrites the query string" $ do + withApp (do + Scotty.middleware $ \app req sendResponse -> + app req{queryString = [("query", Just "haskell")]} sendResponse + Scotty.matchAny "/search" $ queryParam "query" >>= text + ) $ do + it "returns query parameter with given name" $ do + get "/search" `shouldRespondWith` "haskell" + describe "captureParam" $ do withApp ( do