I'm trying to build my own URL route matching engine, trying to match routes using regular expressions.
For example, let's consider the scenario where a server application allows to set custom parameterized routes and then execute a function when the route it's being invoked by an HTTP request. The developer could create the following routes:
/users/:id/doSomething
/hello/world
/:format/convert
And each one of them would be associated with a different request handler/function.
Now, on an incoming request, the server should be able to match the requested path to the proper handler. So for example if a client application requests http://myservice.com/users/john/doSomething, the server should be able to tell that the requested URL belongs to the /users/{id}/doSomething route definition, and then execute the associated handler.
Personally they way I would build the route matcher would be to take the requested URL, loop over the route definitions and, if a definition matches the requested URL, execute the handler. The tricky part is the dynamic parameters matching.
How would you build a regular expression that matches the URL segments?
EDIT:
I'm currently using the following regular expression to match segments: ([^/\?])+.
For example to check if a request path belongs to the first route I would match it against:
/users/([^/])+/doSomething
Which is a very permissive regex.
Solution
First concentrate on how the developer could create the routes. What must she type for entering dynamic parameters ? Then writing the dynamic parameters matching will be easier.
Example
In Java, I recently worked with Jersey. Here is how one can define an url route:
/api/{id:[\dA-F]+}.{type:(?:xml|json|csv)}
Some expected urls:
/api/EF123.csv
/api/ABC.json
/api/1234567890.xml
The matcher would parse the route provided by the developer for finding dynamic parameter using a regex like this:
{([^:]+)\s*:\s*(.+?)(?<!\\)}
Check the demo: http://regex101.com/r/iH1gY3
Once done, the matcher can build the regex below on the fly for matching the route:
/api/[\dA-F]+\.(?:xml|json|csv)
Related
I have a website and I'd like to redirect traffic based on a dynamic product ID in the url.
This I'm doing with help of Google Optimize Redirect Test. Setup is based on a Regex to actually fetch the Original page which I want to redirect to an other page.
https://domain.de/en/products/brand/product/a002p00001EuNgzAAF should redirect to https://domain.de/en/our-products/brand/product/a002p00001EuNgzAAF
How can i setup the regex so that I can use the 18 digit product ID as an identifier?
I alread tried to setup a regex like
https:\/\/domain\.de\/en\/products($|\?.*) plus trying to add the expression (\d{18}) but failed in putting it in the right position.
I am trying to create a regular expression for my Spring controller method that matches all of the paths in my web application, except It also needs to exclude any traffic with the prefix “/websock” that comes in for my websocket server. Here is the method:
#GetMapping(value = "/{path:[^\\.]*}")
public String redirect(#PathVariable(value="path") String path) {
LOGGER.debug("In redirect path:" + path);
return "forward:/";
}
I have tried a lot of examples from the web I have not been able to find one that does what I need. My examples have either accepted all traffic (including the websocket prefix), or that have excluded some of the http traffic.
The first reg-ex was
"/{path:[^\\.]*}"
which matches URLs such “http://localhost:8080/abc”. But, it was failing to match http://localhost:8080/abc/def.
My second attempt using
"/**/{path:[^\\.]*}"
corrected that issue but then matched everything including my websocket traffic.
I tried the following, which I saw from other questions, was supposed to exclude traffic starting with "/websock". But instead it failed to match anything including the “abc” URLs above:
"/{path:[^\\.]*}", "/**/{path:^(?!websock).*}/{path:[^\\.]*}".
What am I doing wrong with my reg-ex?
I have some versioned folders of site files that we are handling through IIS. What I need to do is create a URL Rewrite that will redirect traffic from all requests that don't match the most recent version, TO that most recent version. I'm having a difficult time as RegEx are not my specialty and I have been working on it for the last week.
Here's an example of what I need.
Most recent version:
https://testurl.com/v4/#
Older Versions:
https://testurl.com/#
https://testurl.com/v2/#
https://testurl.com/v3/#
These urls have other routes off of the base as well (ex. https://testurl/v3/#/rout1)
I'm needing a regular expression that will say "Any requested url that is does not contain the /v4/ to REDIRECT to the https://testurl/v4/#
Can someone point me in the right direction?
This regex will capture all domains of the form that you've listed. Capture group 1 will contain the actual route, eg / or /rout1.
/^https\:\/\/testurl\.com(?:\/v\d)?\/#(.*)$/
You can see it illustrated here: http://regexr.com/3fgo8
Is it possible to first load CSV, then use RegEx on it and use that data extracted for HTTP Request? At this moment I have RegEx's prepared, but it doesn't seems to work. When I use reference to them in HTTP Request it doesn't see this variable. It looks like my RegEx ain't used before HTTP Request.
My RegEx is prepared with settings checked:
Apply to: JMeter Variable
Field to check: Body
Regular Expression Extractor is a PostProcessor, it means that it is executed after the request. If you need to apply the regular expression to a JMeter Variable in the time of the request I would suggest to consider __regexFunction() instead.
JMeter Functions can be placed anywhere in the script and they are evaluated directly when they are called so it seems it is something you're looking for.
For extended information on JMeter Functions check out How to Use JMeter Functions posts series
I want to use ACL for authorization (https://www.npmjs.com/package/acl) but i can't use it with complex urls like http://domain.com/api/v1/resource/:id1/anotherResource/:id2
Using node js, express can handle finding right function from the url from request's originalUrl or something else. I need to put regular expressions statements of requested urls into configuration of ACL.
I debugged express step by step and found regex form of url on 84th line of Layer.js but I need to know every regex form of url to use ACL module as a middleware.
Is there any way to get regexp string (like in the picture below) of url to use in middleware?
Use path-to-regexp module which is used internally by expressjs (actually you were very close to finding it while debugging)