Skip to content

Commit

Permalink
Fix: avoid matching capture pattern name ( Fixes #291) (#301)
Browse files Browse the repository at this point in the history
* Fix: avoid matching capture pattern name

* Add test

* Fix: replace obsolete use of param

* Add changelog entry
  • Loading branch information
jfraudeau authored Dec 23, 2023
1 parent cb0e4c9 commit bddd9de
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Web/Scotty/Route.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ matchRoute (Capture pat) req = go (T.split (=='/') pat) (compress $ "":pathInfo
| 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 changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## next [????.??.??]

### Fixes
* Path parameters with value matching the parameter name prefixed by colon will properly populate `pathParams` with their literal value : `/:param` will match `/:param` and add a `Param` with value `("param", ":param")` (#301)


## 0.21 [2023.12.17]
### New
Expand Down
2 changes: 2 additions & 0 deletions test/Web/ScottySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec = do
makeRequest "//scotty" `shouldRespondWith` 200

withApp (route "/:paramName" $ captureParam "paramName" >>= text) $ do
it ("captures route parameters for " ++ method ++ " requests when parameter matches its name") $ do
makeRequest "/:paramName" `shouldRespondWith` ":paramName"
it ("captures route parameters for " ++ method ++ " requests with url encoded '/' in path") $ do
makeRequest "/a%2Fb" `shouldRespondWith` "a/b"

Expand Down

0 comments on commit bddd9de

Please sign in to comment.