Skip to content
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

read from queryString instead of rawQueryString #360

Merged
merged 4 commits into from
Dec 17, 2023
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
9 changes: 3 additions & 6 deletions Web/Scotty/Route.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
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

Expand All @@ -23,7 +20,7 @@
import qualified Text.Regex as Regex

import Web.Scotty.Action
import Web.Scotty.Internal.Types (RoutePattern(..), RouteOptions, ActionEnv(..), ActionT, ScottyState(..), ScottyT(..), ErrorHandler, Middleware, BodyInfo, handler, addRoute, defaultScottyResponse)

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.3

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

The import of ‘ActionT’
import Web.Scotty.Util (decodeUtf8Lenient)
import Web.Scotty.Body (cloneBodyInfo, getBodyAction, getBodyChunkAction, getFormParamsAndFilesAction)

Expand Down Expand Up @@ -160,14 +157,14 @@
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.
Expand Down
12 changes: 11 additions & 1 deletion test/Web/ScottySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading