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

Fix: avoid matching capture pattern name ( Fixes #291) #301

Merged
merged 7 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions Web/Scotty/Route.hs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ matchRoute (Capture pat) req = go (T.split (=='/') pat) (compress $ T.split (==
| otherwise = Nothing -- request string is longer than pattern
go p [] prs | T.null (mconcat p) = Just prs -- in case pattern has trailing slashes
| otherwise = Nothing -- request string is not long enough
go (p:ps) (r:rs) prs | p == r = go ps rs prs -- equal literals, keeping checking
| T.null p = Nothing -- p is null, but r is not, fail
| T.head p == ':' = go ps rs $ (T.tail p, r) : prs -- p is a capture, add to params
| otherwise = Nothing -- both literals, but unequal, fail
go (p:ps) (r:rs) prs = case T.uncons p of
Just (':', name) -> go ps rs $ (name, r) : prs -- p is a capture, add to params
_ | p == r -> go ps rs prs -- equal literals, keeping checking
| otherwise -> Nothing -- both literals, but unequal, fail
compress ("":rest@("":_)) = compress rest
compress (x:xs) = x : compress xs
compress [] = []
Expand Down
3 changes: 3 additions & 0 deletions test/Web/ScottySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ spec = do
it ("properly handles extra slash routes for " ++ method ++ " requests") $ do
makeRequest "//scotty" `shouldRespondWith` 200

withApp (route "/:paramName" $ param "paramName" >>= text) $ do
jfraudeau marked this conversation as resolved.
Show resolved Hide resolved
it ("captures route parameters for " ++ method ++ " requests when parameter matches its name") $ do
makeRequest "/:paramName" `shouldRespondWith` ":paramName"
ocramz marked this conversation as resolved.
Show resolved Hide resolved
describe "addroute" $ do
forM_ availableMethods $ \method -> do
withApp (addroute method "/scotty" $ html "") $ do
Expand Down
Loading