url handling

15 views
Skip to first unread message

xrfang

unread,
Nov 8, 2009, 3:35:31 AM11/8/09
to web.py
I have the following code in my index.py

urls = (
"/tasks/?$", "signin",
"/tasks/list", "listing",
"/tasks/post", "post",
... ...
)

Now having a problem I am debugging, the question I am asking now is
related to the problem. Please see the first line: "/tasks/?$" ... My
intention is to let it match either of the following

http://localhost/tasks
http://localhost/tasks/

I found, if I write "/tasks", then it won't work, because the browser
(or nginx) always send webpy the url http://localhost/tasks/, even if
I type in "http://localhost/tasks" (without the /).

Now I started to wonder, shall I put $ at the end of the regex? I did
that, it is still correct even for urls like:
http://localhost/tasks/?msg=Invalid%20username%20or%20password

In another word: regex matching is AFTER urlcracking, i.e., only the
PATH is being matched and parameters of the url is stripped off,
right? Which is to say, the regex we write in url() block is ALWAYS a
full-match and there is no need to write ^ or $?

I think it is full match, because, if it is not, then url like /tasks/
blahblah will also be matched my the first item?

Could you please help me confirm this?

Thank you!

Alessandro Agosto

unread,
Nov 8, 2009, 6:03:36 AM11/8/09
to web.py


On Nov 8, 9:35 am, xrfang <xrf...@gmail.com> wrote:
> I have the following code in my index.py
>
> urls = (
>     "/tasks/?$", "signin",
>     "/tasks/list", "listing",
>     "/tasks/post", "post",
>     ... ...
> )
>
> Now having a problem I am debugging, the question I am asking now is
> related to the problem. Please see the first line: "/tasks/?$" ... My
> intention is to let it match either of the following
>
> http://localhost/taskshttp://localhost/tasks/
>
> I found, if I write "/tasks", then it won't work, because the browser
> (or nginx) always send webpy the urlhttp://localhost/tasks/, even if
> I type in "http://localhost/tasks" (without the /).
>
> Now I started to wonder, shall I put $ at the end of the regex? I did
> that, it is still correct even for urls like:http://localhost/tasks/?msg=Invalid%20username%20or%20password
>
> In another word: regex matching is AFTER urlcracking, i.e., only the
> PATH is being matched and parameters of the url is stripped off,
> right?  Which is to say, the regex we write in url() block is ALWAYS a
> full-match and there is no need to write ^ or $?
>
> I think it is full match, because, if it is not, then url like /tasks/
> blahblah will also be matched my the first item?
>
> Could you please help me confirm this?
>
> Thank you!
Hi,
have you seen the "Hello World" tutorial? (http://webpy.org/cookbook/
helloworld)
The author discussed how to make a redirect to make urls with a slash
working.
So if your app receive a request to http://www.example.com/tasks/
redirect to the url /tasks. I'm using this solution without any
problem.
Regarding the URLs question, please wait someone who can respond with
certainty.
Greetings,
Alessandro.

xrfang

unread,
Nov 8, 2009, 8:58:06 AM11/8/09
to web.py
Thank you for the info, but, that's not my problem I know how to make
urls with or without "/" work :) I just need to know how the regex
matching is done, in detail, so that I can handle more complex cases
in the future.
> So if your app receive a request tohttp://www.example.com/tasks/

Greg Milby

unread,
Nov 8, 2009, 12:35:48 PM11/8/09
to we...@googlegroups.com
xfrang - have you figured out a good way to print the links on the pages you're producing?  my plan (at the moment) is to just use the url variable and prepend the web address to the url in a for loop - just add it to the return variable.

Angelo Gladding

unread,
Nov 8, 2009, 12:49:48 PM11/8/09
to we...@googlegroups.com
web.application .32

line 232:

fn, args = self._match(self.mapping, web.ctx.path)

line 427:

_match() adds ^ and $ respectively to each end of the URI pattern

So its trying to match your updated pattern against web.ctx.path.
--
Angelo Gladding
ang...@gladding.name
http://angelo.gladding.name/
E69E 47E8 5C3A 96E5 C70F
D931 F35C ACBA 6F39 9611

xrfang

unread,
Nov 8, 2009, 6:25:51 PM11/8/09
to web.py
I am sorry Greg -- I do not understand what you mean by "print the
links on the page you are producing"?? What links? If you have any
problem, I suggest you post it so that people can help you :)

xrfang

unread,
Nov 8, 2009, 6:49:56 PM11/8/09
to web.py
Thank you! I am adding this to the cookbook.
> ang...@gladding.namehttp://angelo.gladding.name/
Reply all
Reply to author
Forward
0 new messages