match route using a splat. What does it do? - ruby-on-rails-4

I see this route:
match "*a", to: 'application#some_error_handler', via: :all
What does that do in Rails? Is that a splat "a"?

This is called route globbing and is explained in the Routing guide's section on route globbing:
Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example:
get 'photos/*other', to: 'photos#unknown'
This route would match photos/12 or /photos/long/path/to/12, setting params[:other] to "12" or "long/path/to/12". The fragments prefixed with a star are called "wildcard segments".
Wildcard segments can occur anywhere in a route. For example:
get 'books/*section/:title', to: 'books#show'

Related

How to use RegExp in a location path on my nginx config?

I have a navigation route that looks like:
/book/bookname-1234/
I have an API route to purchase the book at:
/book/1234/purchase
For the navigation route, I want my Nextjs server to handle it, but the API route I want be handled by our python server.
In my nginx config I have defined Nextjs routes with the catchall going to python.
For the navigation route, I have it set as:
location ~ /book/^[a-z]+(?:-[0-9]+)$ {
try_files $uri #nextjs;
}
I have also tried putting double quotes around the "/book/^[a-z]+(?:-[0-9]+)$"
What am I doing wrong here?
To match the url, you can use:
/book/[a-z]+-[0-9]+/$
If you want to reuse the capture group values, you can use 2 capture groups.
Note that there is an ending / in the example string, and using ^ denotes the end of the string.
/book/a-z]+-[0-9]+/$
Regex demo
Then you could use the 2 capture groups followed by the word purchase to get /book/1234/purchase

Yii2 - Url Manager - Specific rule failing

I have the following url:
http://example.com/user/login
If I input the url in the browser, it matches de rule:
'<module:user>/<slug:[\w\-]+>' => '<module>/<slug>'
If I create the url:
Yii::$app->urlManager->createAbsoluteUrl(["user/index", "slug" => "login"]);
It should create the same url as above but instead it creates:
http://example.com/user/index?slug=login
But If I change the rule to:
'<module:user>/<slug:[\w\-]+>' => '<module>/index'
It works ok, any ideas why? I guess for some reason:
It is passing slug empty to or
It is an invalid value.
Any ideas?
That is because slug is part of the route pattern: '<module>/<slug>'. So <slug:[\w\-]+> is not treated as a named GET param, but as a part of the route. That means that URL /user/something will point to route user/something without any GET params.
You should not use the same name for route patterns and named params. You should either use different name:
'<module:user>/<action:[\w\-]+>/<slug:[\w\-]+>' => '<module>/<action>'
Or hardcode action for specified rule (as you already did in second example):
'<module:user>/<slug:[\w\-]+>' => '<module>/index'
Note that this will also match standard actions, like user/view.

Extract domain with grok

I'd like to make the simplest grok filter, just to extract domain from url
For example, for the url
https://stackoverflow.com/questions/ask?title=grok%20extract%20url
I'd like to get the result
stackoverflow.com
I tried to do so with the filter
%{URIPROTO}://%{URIHOST:domain}
And it did extract me stackoverflow.com, but when I use a different url that has www at the start
for example
https://www.elastic.co/
the result is
www.elastic.co
is there a filter that could return me the domain alone, without www?
Thank you!
You can add a custom pattern like below :
SLD ([a-z0-9-]+.[a-z]{2,63})
This gives you second level domain name without subdomain. You can also add xn-- pattern like below for unicode domain names:
SLD ((xn--)?[a-z0-9-]+\.[a-z]{2,63})
Please check how you can add custom pattern to logstash documentation. Then, now, you can use this custom pattern like below:
%{URIPROTO}://(%{WORD:SUBDOMAIN}\.)?(%{SLD})
For %{WORD:SUBDOMAIN} this part, you can also write another regex to your custom pattern like below:
SUBDOMAIN ([a-z0-9-]{1,63})
At the end, your pattern file like this :
SLD ((xn--)?[a-z0-9-]+\.[a-z]{2,63})
SUBDOMAIN ([a-z0-9-]{1,63})
And your logstash conf like this:
filter {
grok {
patterns_dir => ["./patterns"]
match => { "uri" => "%{URIPROTO}://(%{SUBDOMAIN}\.)?(%{SLD})" }
}
}
Please define grok pattern:
HOSTNAME \b(?:[0-9A-Za-z][0-9A-Za-z-]{0,62})(?:\.(?:[0-9A-Za-z][0-9A-Za-z-]{0,62}))*(\.?|\b)
and use it:
%{URIPROTO}://%{HOSTNAME:domain}

How to configure Fiddler's Autoresponder to "map" a host to a folder?

I'm already using Fiddler to intercept requests for specific remote files while I'm working on them (so I can tweak them locally without touching the published contents).
i.e. I use many rules like this
match: regex:(?insx).+/some_file([?a-z0-9-=&]+\.)*
respond: c:\somepath\some_file
This works perfectly.
What I'd like to do now is taking this a step further, with something like this
match: regex:http://some_dummy_domain/(anything)?(anything)
respond: c:\somepath\(anything)?(anything)
or, in plain text,
Intercept any http request to 'some_dummy_domain', go inside 'c:\somepath' and grab the file with the same path and name that was requested originally. Query string should pass through.
Some scenarios to further clarify:
http://some_domain/somefile --> c:\somepath\somefile
http://some_domain/path1/somefile --> c:\somepath\path1\somefile
http://some_domain/path1/somefile?querystring --> c:\somepath\path1\somefile?querystring
I tried to leverage what I already had:
match: regex:(?insx).+//some_dummy_domain/([?a-z0-9-=&]+\.)*
respond: ...
Basically, I'm looking for //some_dummy_domain/ in requests. This seems to match correctly when testing, but I'm missing how to respond.
Can Fiddler use matches in responses, and how could I set this up properly ?
I tried to respond c:\somepath\$1 but Fiddler seems to treat it verbatim:
match: regex:(?insx).+//some_domain/([?a-z0-9-=&]+\.)*
respond: c:\somepath\$1
request: http://some_domain/index.html
response: c:\somepath\$1html <-----------
The problem is your use of insx at the front of your expression; the n means that you want to require explicitly-named capture groups, meaning that a group $1 isn't automatically created. You can either omit the n or explicitly name the capture group.
From the Fiddler Book:
Use RegEx Replacements in Action Text
Fiddler’s AutoResponder permits you to use regular expression group replacements to map text from the Match Condition into the Action Text. For instance, the rule:
Match Text: REGEX:.+/assets/(.*)
Action Text: http://example.com/mockup/$1
...maps a request for http://example.com/assets/Test1.gif to http://example.com/mockup/Test1.gif.
The following rule:
Match Text: REGEX:.+example\.com.*
Action Text: http://proxy.webdbg.com/p.cgi?url=$0
...rewrites the inbound URL so that all URLs containing example.com are passed as a URL parameter to a page on proxy.webdbg.com.
Match Text: REGEX:(?insx).+/assets/(?'fname'[^?]*).*
Action Text C:\src\${fname}
...maps a request for http://example.com/‌assets/img/1.png?bunnies to C:\src\‌img\‌1.png.

Why is my route not matching any action

Using play framework, I'm trying to match a route using a regular expression.
What I wanted is to use one action that maps all this urls:
mydomain.com/my-post-title-123
mydomain.com/another-post-title-124
mydomain.com/a-third-post-title-125
get this "123, 124 and 125" from the end of the url so the controller can use it. Basically ignore whatever post tile comes in and only use the number at the end.
I have the following on my routes.conf
GET /$postId<\d$> controllers.Posts.viewPost(postId: Int)
But I get the error page "Action not found"
You are missing the url prefix and "+" in the regex in the routes definition. Here is my route configuration and it works fine
#Regex test
GET /$prefix<.*>$postId<\d+$> controllers.Application.viewPost(prefix:String,postId: Int)
Controllers.Application.viewPost
def viewPost(prefix:String,postId:Int) = Action{
Ok("the post id is: "+postId+" the prefix is:"+prefix)
}
and the output will be
the post id is: 123 the prefix is "whatever/prefix/you/give"
** tested, it works.