Path Expression When Parameter is Not Last Component
JUSTINMKAUFMAN opened this issue · comments
This works well to route a profile get request with a trailing 'userid' param:
"/api/v1/profile/:userid"
...but how do I write it when the userid param is in the middle, like:
"/api/v1/:userid/profile"
?
Params in the middle should work. In the demo app I tried the following:
If you change line 69 in TelegraphDemo.swift from
server.route(.GET, "hello/:name", serverHandleHello)
to
server.route(.GET, "hello/:name/test", serverHandleHello)
Then you should get the following results to these requests:
- https://localhost:9000/hello/bob => 404
- https://localhost:9000/hello/bob/test => 200, "Hello Bob"
- https://localhost:9000/hello/bob/testing => 404
There might be a few edge cases with trailing slashes, because regexes are a bit of pain 🙂
Can you try to add (/)
at the end of the route specification?
For example: /api/v1/:userid/profile(/)
That should allow the slash at the end to be optional.
@Zyphrax I figured out the cause of the bug.
If the requestor passes an =
character in a parameter-type path component (e.g. api/v1/eifjiihgrio=
) and you have multiple routes, some with params in the same places (e.g. api/v1/:userid
and api/v1/:userid/:pref/ok
), none of the associated routes work.
Must be something wrong with the regex parser?
Not a good solution, but to assist w/ debugging, adding this handler fixes the issue:
public class HTTPRequestPathHandler: HTTPRequestHandler {
public func respond(to request: HTTPRequest, nextHandler: HTTPRequest.Handler) throws -> HTTPResponse? {
request.uri.path = request.uri.path.replacingOccurrences(of: "=", with: "")
return try nextHandler(request)
}
}
@JUSTINMKAUFMAN A few minutes ago I pushed a commit with a few changes to HTTPRoute
. I've removed some of the validation code to allow more complex paths and special characters.
Can you give the code on the master
branch a spin for me?
@JUSTINMKAUFMAN Does the commit fix the HTTPRoute
issues?
@Zyphrax Yes it does, thank you! Looking forward to the next release!
@JUSTINMKAUFMAN Version 0.27 is now available! 🎉