Host name in routes.py

119 views
Skip to first unread message

Mladen Milankovic

unread,
Mar 19, 2009, 12:56:25 PM3/19/09
to web2py Web Framework
Hi.

My name is Mladen Milankovic, and I'm a first time poster. I've been working
with web2py for a couple of months (reading mailing list from that time), and
around a year with python.

I like writing applications in web2py, because it's easy and fast... I came
from php. :)

Wanted to use web2py through apache. At first I used it through mod_proxy, but
wanted to go to wsgi. I manage several domain names and needed to access
different application from web2py as if they run through the root, without the
application/controller/function. I managed to make it work like needed through
mod_proxy, but rewrite through wsgi was hard for me. routes.py was much
easier, at least for me, but I could only setup one set of rewrite
instructions in it.

I saw AchipAs post in "web2py and GAE" thread about modifying the routes.py so
it can be given parameter about host, so different host names have different
rewrite instructions. Unfortunately this was the last post I saw about this
topic.

Thought to give it a try and write a patch that will do that. I tested it
directly and through proxy and wsgi. It's backward compatible. Didn't have any
problems with it. Further testing needed.

I went with AchipAs solution of third parameter to every rewrite instruction:
example
routes_in = (('.*:/favicon.ico', '/examples/static/favicon.ico'),
('.*:/robots.txt', '/examples/static/robots.txt'),
('.*:/robots.txt', '/myapp/static/robots.txt', 'mydomain.com'),
('.*:/', '/myapp/default/index', 'mydomain.com')
)
Now when /robots.txt is called through mydomain.com it will give the one from
/myapp/static/, and when called through any other the one from
/examples/static/. Also mydomain.com/ will give /myapp/default/index page.
Same rules apply for routes_out and routes_onerror. Host name can be written
in regular expression, and instructions with host name will be prioritized.
They will be checked first.

Modified files:
/gloun/rewrite.py
/gluon/main.py

Unfortunately, had to modify gluon/main.py, too. It contains check_error_route
function, as I read in the comment because of IE. Tried it only with Firefox.

Found on net how to create svn patch. It created against svn, revision 796.

regards
mmlado

routes.py.host.diff.zip

Yarko Tymciurak

unread,
Mar 19, 2009, 1:33:02 PM3/19/09
to web...@googlegroups.com
Hi Mmlado -

Welcome, and thanks for the patch.

Massimo is on a trip (and I'm sure he'll look at it soon as he can, but may not be for over a week - not sure if he'll have internet where he is - ah!  spring break!).

I'll look at your patch later.

Looking forward to hearing more from you!

Regards,
Yarko

mmlado

unread,
Apr 16, 2009, 11:36:43 AM4/16/09
to web2py Web Framework
bump!

Looks like no one, except me, needs this functionality. :(
Or if there's something to fix/change please tell me.

regards
mmlado

mdipierro

unread,
Apr 16, 2009, 12:06:54 PM4/16/09
to web2py Web Framework
Oops...

This was implemented in 1.61 but with a slightly different syntax than
you proposed:

routes_in=(('^127.0.0.1:https://localhost:post /hello','/admin'))

i.e. re-route all https POST requests from 127.0.0.1 to
http_host=="localhost" to /admin.
This should work with most web server may break in case of proxy.
The old syntax is still supported for backward compatibility.

Massimo

Yarko Tymciurak

unread,
Apr 16, 2009, 1:12:13 PM4/16/09
to web...@googlegroups.com
On Thu, Apr 16, 2009 at 11:06 AM, mdipierro <mdip...@cs.depaul.edu> wrote:

Oops...

This was implemented in 1.61 but with a slightly different syntax than
you proposed:

routes_in=(('^127.0.0.1:https://localhost:post /hello','/admin'))

This is really obtuse, hard to read in an understandable way;

Can you explain more?  


i.e. re-route all https POST requests from 127.0.0.1 to
http_host=="localhost" to /admin.

Huh?     *127.0.0.1   maps to "any POST request on HTTPS" --- is that correct?  

   I ask because it reads more like   *127.... --->  "any" 127 request  to ": https://localhost"

   I'm just trying to understand how to parse this with my eyes, make sense of it.

   For example,   how do I write "any 127.0.0.1 request map to https://localhost"?

   And (finally) the "[_space_]/hello", " /admin"   mapping.... is not clear to me... not sure what this is saying..
   Is this any 127 request that is https, that is a post, that roots from /hello  
   will map to (?)  https /admin

A bit confused,
Yarko

mdipierro

unread,
Apr 16, 2009, 1:57:47 PM4/16/09
to web2py Web Framework
A request comes in and you want to redirect it to a controller action
filtering by
- IP of client (for example 127.0.0.1)
- requested protocol (for example http or http "(http|https)""
- requested hostname (for example www.web2py.com)
- type of request (for example get or post "(get|post)")
- requested URL (for example "/test")

You would capture it with

"^[client]:[protocol]://[hostname]:[method] [url]$"

in the example

"^127.0.0.1:(http|https)://www.web2py.com:(get|post) /test"

Now you can map this into for example "/welcome/default/index" with
routes.py

routes_in=(("^127.0.0.1:(http|https)://www.web2py.com:(get|post) /
test", "/welcome/default/index"),)

I can see writing an entire book in this only.
Can you figure out what this does?

routes_in=(("^127.0.0.1:$a://www.web2py.com:(get|post) /test/$b", "/
welcome/default/$b?method=$a"),)

and this?

routes_in=(("^127.0.0.1:$a://www.web2py.com:(get|post) /test/(?
P<b>.*)", "/welcome/default/$b?method=$a"),)

I know it is ugly but 1) it works; 2) it very powerful when compared
with routes on rails and urls in Django; 3) is is backward compatible.

Does not anybody remember that I never wanted to add this to web2py?
Now you know why.

Massimo

On Apr 16, 12:12 pm, Yarko Tymciurak <yark...@gmail.com> wrote:
> On Thu, Apr 16, 2009 at 11:06 AM, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > Oops...
>
> > This was implemented in 1.61 but with a slightly different syntax than
> > you proposed:
>
> > routes_in=(('^127.0.0.1:https://localhost:post/hello','/admin'))
>
> This is really obtuse, hard to read in an understandable way;
>
> Can you explain more?
>
>
>
> > i.e. re-route all https POST requests from 127.0.0.1 to
> > http_host=="localhost" to /admin.
>
> Huh? *127.0.0.1 maps to "any POST request on HTTPS" --- is that
> correct?
>
> I ask because it reads more like *127.... ---> "any" 127 request to
> ":https://localhost"
>
> I'm just trying to understand how to parse this with my eyes, make sense
> of it.
>
> For example, how do I write "any 127.0.0.1 request map tohttps://localhost"?

Mladen Milankovic

unread,
Apr 16, 2009, 2:14:40 PM4/16/09
to web...@googlegroups.com
Hi.

Thank you very much. This is more powerful and more useful then what I did. I
see I have a lot to learn.

regards
mmlado

mdipierro

unread,
Apr 16, 2009, 2:55:53 PM4/16/09
to web2py Web Framework
Your code was actually helpful to get this done. Thank you.

Yarko Tymciurak

unread,
Apr 16, 2009, 6:03:36 PM4/16/09
to web...@googlegroups.com
On Thu, Apr 16, 2009 at 12:57 PM, mdipierro <mdip...@cs.depaul.edu> wrote:

A request comes in and you want to redirect it to a controller action
filtering by
- IP of client (for example 127.0.0.1)
- requested protocol (for example http or http "(http|https)""
- requested hostname (for example www.web2py.com)
- type of request (for example get or post "(get|post)")
- requested URL (for example "/test")

You would capture it with

 "^[client]:[protocol]://[hostname]:[method] [url]$"

Perfect:  this was the missing piece (I think)... just one question now:

Looking at request vars shown on http://www.web2py.com/examples/simple_examples/hello6.

this would match with: (????):

client: --> request.client (129.188.33.25)
protocol --> actual_server_protocol (HTTP/1.1???)
hosthame --> http_x_forwarded_host (www.web2py.com)?  http_x_forwarded_server?
method --> request_method (GET)
url (ahem... this is just part of the url) --> path_info (/examples/simple_examples/hello6)



in the example

 "^127.0.0.1:(http|https)://www.web2py.com:(get|post) /test"

Now you can map this into for example "/welcome/default/index" with
routes.py

 routes_in=(("^127.0.0.1:(http|https)://www.web2py.com:(get|post) /
test", "/welcome/default/index"),)

I can see writing an entire book in this only.
Can you figure out what this does?

 routes_in=(("^127.0.0.1:$a://www.web2py.com:(get|post) /test/$b", "/
welcome/default/$b?method=$a"),)

Yes..


and this?

 routes_in=(("^127.0.0.1:$a://www.web2py.com:(get|post) /test/(?
P<b>.*)", "/welcome/default/$b?method=$a"),)

yes ( you can mix references (?P<b>) and $b?  Cool!)....
 


I know it is ugly but 1) it works; 2) it very powerful when compared
with routes on rails and urls in Django; 3) is is backward compatible.

It's only ugly if it's not obvious - the explanation you provide (at the top) helps A LOT! ;-)
 

mdipierro

unread,
Apr 16, 2009, 6:15:47 PM4/16/09
to web2py Web Framework

> Looking at request vars shown onhttp://www.web2py.com/examples/simple_examples/hello6.
>
> this would match with: (????):
>
> client: --> request.client (129.188.33.25)
> protocol --> actual_server_protocol (HTTP/1.1???)
> hosthame --> http_x_forwarded_host (www.web2py.com)?
> http_x_forwarded_server?
> method --> request_method (GET)
> url (ahem... this is just part of the url) --> path_info
> (/examples/simple_examples/hello6)

protocol is just "http" or "https" or both "https?" (using regex).

The version of the protocol (/1.1) is not used.

Mladen Milankovic

unread,
Apr 17, 2009, 6:14:33 AM4/17/09
to web...@googlegroups.com
Hi.

I'm testing things out and I think got a bug.

I'm trying to get root on localhost to show /my_app/default/index page and not
the example, so I wrote:

routes_in = (('^.*?:https?://localhost:[a-z]+ /$', '/my_app/default/index'), )

It didn't work, and I throw in some logging. I watched what will be the key
before it's converted to regex. It gave:
^.*?:https?:https?://[^:/]+:[a-z]+ //localhost:[a-z]+ /$

Problem is in
line 45, in rewrite.py

if k.find(':/') > 0:

This line is determining if the definition is in the old format:
'.*:/favicon.ico'.
Problem is that in the new definition :/ can be found again, but it has a
different meaning.

It can be done in many different ways. I used this one:

if k.find(':/') > 0 and not k.find('://'):

This way it will match only the old definition and skip the new one.

regards
mmlado

On Thursday 16 April 2009 19:57:47 mdipierro wrote:

Michal Jursa

unread,
Apr 17, 2009, 7:10:41 AM4/17/09
to web...@googlegroups.com
Correct me if i'm wrong, but port numbers are usually nubers so this
regexp cannot match i think as you have [a-z]+ after : in url. Isn't
this your trouble?

routes_in = ('^.*?:https?://localhost:[a-z]+/$','/my_app/default/index'), )

Michal

mdipierro

unread,
Apr 17, 2009, 7:39:31 AM4/17/09
to web2py Web Framework
There is no port number in routes in because it would not make sense.
If the server got the request the port matched already.

Massimo

Michal Jursa

unread,
Apr 17, 2009, 8:38:31 AM4/17/09
to web...@googlegroups.com
Oh, i see, I misunderstood that part of route .. My fault, sorry...

Michal

Álvaro Justen [Turicas]

unread,
Apr 20, 2009, 2:16:17 PM4/20/09
to web...@googlegroups.com
On Thu, Apr 16, 2009 at 2:57 PM, mdipierro <mdip...@cs.depaul.edu> wrote:
> I know it is ugly but 1) it works; 2) it very powerful when compared
> with routes on rails and urls in Django; 3) is is backward compatible.
>
> Does not anybody remember that I never wanted to add this to web2py?
> Now you know why.

Why you do not recommend using routes.py, like in:
http://www.web2py.com/AlterEgo/default/show/42
?

I think this is very useful, including for create nice and more
readable URLs (I don't like a "/default/" or some long URLs created by
default).
Does routes.py decreases web2py performance in an way that its use is
discouraged?

--
Álvaro Justen
Peta5 - Telecomunicações e Software Livre
21 3021-6001 / 9898-0141
http://www.peta5.com.br/

mdipierro

unread,
Apr 20, 2009, 2:41:16 PM4/20/09
to web2py Web Framework
Do no not not recommend it any more. There was a time when I though
that one should use Apache mod_rewrite and such feature did not belong
in web2py. Now that it is there and I see how people use it, I agree
there is value in it. We should all thank user voltron for insisting
that we add that it.

Massimo

On Apr 20, 1:16 pm, Álvaro Justen [Turicas] <alvarojus...@gmail.com>
wrote:

Álvaro Justen [Turicas]

unread,
Apr 20, 2009, 3:08:09 PM4/20/09
to web...@googlegroups.com
On Mon, Apr 20, 2009 at 3:41 PM, mdipierro <mdip...@cs.depaul.edu> wrote:
> Do no not not recommend it any more. There was a time when I though
> that one should use Apache mod_rewrite and such feature did not belong
> in web2py. Now that it is there and I see how people use it, I agree
> there is value in it. We should all thank user voltron for insisting
> that we add that it.

How I can change wiki?
I want to change the begin of http://www.web2py.com/AlterEgo/default/show/42
to:
"web2py supports URL rerwite - it's a very useful feature.
You can use Apache (or lighttpd) + mod_proxy (or mod_rewrite) for this
purpose but using web2py's rewrite you have more flexiblity.

***routes.py syntax changed so much in early releases (like 1.61).
This page have to be updated.***

To use this feature just create a new file in web2py/ called routes.py
and write something like this in it..."

mdipierro

unread,
Apr 20, 2009, 5:24:53 PM4/20/09
to web2py Web Framework
email me and I give you the code

On Apr 20, 2:08 pm, Álvaro Justen [Turicas] <alvarojus...@gmail.com>
wrote:
> On Mon, Apr 20, 2009 at 3:41 PM, mdipierro <mdipie...@cs.depaul.edu> wrote:
> > Do no not not recommend it any more. There was a time when I though
> > that one should use Apache mod_rewrite and such feature did not belong
> > in web2py. Now that it is there and I see how people use it, I agree
> > there is value in it. We should all thank user voltron for insisting
> > that we add that it.
>
> How I can change wiki?
> I want to change the begin ofhttp://www.web2py.com/AlterEgo/default/show/42

Álvaro Justen [Turicas]

unread,
Apr 20, 2009, 6:34:48 PM4/20/09
to web...@googlegroups.com
On Mon, Apr 20, 2009 at 3:41 PM, mdipierro <mdip...@cs.depaul.edu> wrote:
> Do no not not recommend it any more. There was a time when I though
> that one should use Apache mod_rewrite and such feature did not belong
> in web2py. Now that it is there and I see how people use it, I agree
> there is value in it. We should all thank user voltron for insisting
> that we add that it.

So, if it is a nice feature, why it generates a warning if I use routes.py? :-P
WARNING:root:URL rewrite is on. configuration in routes.py

mdipierro

unread,
Apr 20, 2009, 6:45:39 PM4/20/09
to web2py Web Framework
Because people forget about routes.py files.

Massimo

On Apr 20, 5:34 pm, Álvaro Justen [Turicas] <alvarojus...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages