librelist archives

« back to archive

host/route matching

host/route matching

From:
Mathijs Kwik
Date:
2011-08-12 @ 11:04
Hi all,

I looked at the documentation for how matching works.
I thought of 2 cases where I'm not sure if they are possible to write.

As I understand, hostname is matched in reverse, meaning the pattern
has to be at the beginning of the string.
If I want to listen to example.com, example.net and example.org, and
have them respond the same, do I have to declare 3 hosts?
Or can I use some wildcard like "example.(com|net|org)"?

Furthermore, I want to listen to example.com, and all its subdomains.
This works by default, but as a side-effect, requests for
someexample.com are also picked up.
Although it is not usually the case that someexample.com's dns points
to the same machine, I would still like to stricten my config to only
serve on hosts I really want.
Using (.+).example.com rules out example.com without a subdomain. I
tried "(.+.)?example.com" but that's probably too regexpy and didn't
work.
Do I have to use 2 hosts here as well?
So if I want subdomains and the com/net/org combo, I need to declare 6 hosts?
Not a real issue, since I can share routes as a variable, but still I
hope I'm overlooking something, because fitting it on 1 line is nicer.

Also, documentation mentions prefixes should be unique.
What if I (for some contrived reason) have a jpg handling backend, did
I understand correctly that this won't work?:
"/images/(.⋆.jpg)": jpg_handler,
"/images/(.*.png)": png_handler
If so, how should this be handled then?

I know this second example is somewhat contrived, but still the first
seems like a real use case.

Thanks for any help,
Mathijs

Re: [mongrel2] host/route matching

From:
Zed A. Shaw
Date:
2011-08-12 @ 16:55
On Fri, Aug 12, 2011 at 01:04:34PM +0200, Mathijs Kwik wrote:
> Hi all,
> 
> I looked at the documentation for how matching works.
> I thought of 2 cases where I'm not sure if they are possible to write.
> 
> As I understand, hostname is matched in reverse, meaning the pattern
> has to be at the beginning of the string.
> If I want to listen to example.com, example.net and example.org, and
> have them respond the same, do I have to declare 3 hosts?

Yep, but you can put the routes to for all three in a variable so it's
really just:

Host(name="example.net", routes=example_routes)
Host(name="example.org", routes=example_routes)
Host(name="example.com", routes=example_routes)

That's all I do.  I'm going to be crafting up a Redirect() handler too
so that you can do the usual bouncing of people somewhere.

> Or can I use some wildcard like "example.(com|net|org)"?

Nope, it's optimized for the more common case of wildcard matching on
subdomains.

> Furthermore, I want to listen to example.com, and all its subdomains.
> This works by default, but as a side-effect, requests for
> someexample.com are also picked up.

> Do I have to use 2 hosts here as well?

So you want:

example.com -> somewhere
subdomain.example.com -> differentplace
badexample.com -> 404

I think there's a way, but yeah easiest is just do two hosts.  They're
actually fairly cheap and solve lots of these confusing problems quicker
than trying to work out a complex regex.

Make sure the subdmain one is: (.+).example.com, and the other is just
example.com.

> So if I want subdomains and the com/net/org combo, I need to declare 6 hosts?

That'd be easier than trying to tease out the perfect regex.  The
matching algorithm in Mongrel2 is designed to be really fast and
explicit at the cost of some flexibility.

> Also, documentation mentions prefixes should be unique.
> What if I (for some contrived reason) have a jpg handling backend, did
> I understand correctly that this won't work?:
> "/images/(.⋆.jpg)": jpg_handler,
> "/images/(.*.png)": png_handler

Have your handler do it.  Again, the Mongrel2 matching isn't trying to
do the app's work, it's just trying to route requests for 95% of the
cases.  Most of the time, if you're doing the above, then you're trying
to make Mongrel2 be your app's full routing system.  That sort of works,
and I do it, but if you do then you now have to change your routes to
fit how Mongrel2 likes them:  explicit and simple.

So I'd change the above routes to just route everything in /images/ to
the images handler and let it do its thing.

-- 
Zed A. Shaw
http://zedshaw.com/

Re: [mongrel2] host/route matching

From:
Mathijs Kwik
Date:
2011-08-12 @ 17:09
yeah I found that routes can be a variable. in that case, heaving
separate hosts is way more readable indeed.

Thanks

On Fri, Aug 12, 2011 at 6:55 PM, Zed A. Shaw <zedshaw@zedshaw.com> wrote:
> On Fri, Aug 12, 2011 at 01:04:34PM +0200, Mathijs Kwik wrote:
>> Hi all,
>>
>> I looked at the documentation for how matching works.
>> I thought of 2 cases where I'm not sure if they are possible to write.
>>
>> As I understand, hostname is matched in reverse, meaning the pattern
>> has to be at the beginning of the string.
>> If I want to listen to example.com, example.net and example.org, and
>> have them respond the same, do I have to declare 3 hosts?
>
> Yep, but you can put the routes to for all three in a variable so it's
> really just:
>
> Host(name="example.net", routes=example_routes)
> Host(name="example.org", routes=example_routes)
> Host(name="example.com", routes=example_routes)
>
> That's all I do.  I'm going to be crafting up a Redirect() handler too
> so that you can do the usual bouncing of people somewhere.
>
>> Or can I use some wildcard like "example.(com|net|org)"?
>
> Nope, it's optimized for the more common case of wildcard matching on
> subdomains.
>
>> Furthermore, I want to listen to example.com, and all its subdomains.
>> This works by default, but as a side-effect, requests for
>> someexample.com are also picked up.
>
>> Do I have to use 2 hosts here as well?
>
> So you want:
>
> example.com -> somewhere
> subdomain.example.com -> differentplace
> badexample.com -> 404
>
> I think there's a way, but yeah easiest is just do two hosts.  They're
> actually fairly cheap and solve lots of these confusing problems quicker
> than trying to work out a complex regex.
>
> Make sure the subdmain one is: (.+).example.com, and the other is just
> example.com.
>
>> So if I want subdomains and the com/net/org combo, I need to declare 6 hosts?
>
> That'd be easier than trying to tease out the perfect regex.  The
> matching algorithm in Mongrel2 is designed to be really fast and
> explicit at the cost of some flexibility.
>
>> Also, documentation mentions prefixes should be unique.
>> What if I (for some contrived reason) have a jpg handling backend, did
>> I understand correctly that this won't work?:
>> "/images/(.⋆.jpg)": jpg_handler,
>> "/images/(.*.png)": png_handler
>
> Have your handler do it.  Again, the Mongrel2 matching isn't trying to
> do the app's work, it's just trying to route requests for 95% of the
> cases.  Most of the time, if you're doing the above, then you're trying
> to make Mongrel2 be your app's full routing system.  That sort of works,
> and I do it, but if you do then you now have to change your routes to
> fit how Mongrel2 likes them:  explicit and simple.
>
> So I'd change the above routes to just route everything in /images/ to
> the images handler and let it do its thing.
>
> --
> Zed A. Shaw
> http://zedshaw.com/
>