-
-
Notifications
You must be signed in to change notification settings - Fork 446
Description
I'm aware that the ordering of query parameters is outside the scope of this project, as it clearly states in the README. However, this isn't about ordering. If the route ends with a parameter and it is followed by a query string, the parameter gets the ?. Using the Node.js REPL, I can replicate this problem.
> p2r = require('path-to-regexp');
> re = p2r('/whatever/:moo?query=str')
/^\/whatever(?:\/([^\/]+?))?query\=str(?:\/)?$/i
> '/whatever/moo?query=str'.match(re)
[
'/whatever/moo?query=str',
'moo?',
index: 0,
input: '/whatever/moo?query=str',
groups: undefined
]
The result of capturing moo? is broken. It appears that the regular expression constructed should look like the following.
/^\/whatever(?:\/([^\/?]+?))?\?query\=str(?:\/)?$/i
I would suggest updating the regular expression so the question mark can not be included in the path portion, which changes (?:\/([^\/]+?))? into (?:\/([^\/?]+?))?. This should be done for safety, since question marks are technically not part of the path as per the RFC, section 3.
Also, it would be nice to clearly update the documentation to indicate one of two positions:
-
If the path to parse contains query parameters, one must escape the question mark, changing
"/path?query=str"into"/path\\?query=str"(two backslashes = send a literal backslash). This isn't really explained in the optional section. -
Query strings are not supported at all and they must be removed before trying to parse the path. This appears to be the opinion stated in issues URL parameters behind a variable #47 and query parameters and RegExp's involving path parameter #91, but the documentation currently indicates that the library doesn't manage the ordering of query strings as opposed to query strings being forbidden.