[albatross-users] AppContext.redirect_url() question

Matt Goodall matt at pollenation.net
Wed Jul 9 02:29:57 EST 2003


I'm probably being really stupid and missing something obvious but this 
is the bit from redirect_url() I'm confused about:

        this_path = urlparse.urlparse(self.request.get_uri())[2]
        base_path = urlparse.urlparse(self.app.base_url())[2]
        pos = this_path.find(base_path)
        new_base = this_path[:pos + len(base_path)]

Firstly, this_path and base_path are normally the same up to the name of 
the script and so pos will be 0. The only time pos can be non-zero is 
when the URLs don't match at all or this_path contains an extra path at 
the start. I can't think of a situation where this_path has the extra path.

So ... that makes new_base the same as base_path and so I don't 
understand why you cannot simply add the redirect location to the 
base_path. You can all humiliate me now and tell me that I can't read 
simple code if you want ;-).

My big question is why bother with pos and the this_path slice? What is 
it needed for?

Anyway, that is just me trying to figure out what's going on. More 
importantly, at least for me, when I put a mod_python app behind a 
mod_rewrite rule redirect_url() fails since mod_python's 
Request.get_uri() returns the *rewritten* path which may have no 
relationship to the application's base_url. That causes this_path.find() 
to return -1 and after that things get a little messy ;-).

I have been playing around with using PATH_INFO (in CGI speak) as an 
alternative to decoding the URLs. A set of patches follow, I would be 
interested in your thoughts.

Cheers, Matt

Dave Cole wrote:

>>>>>>"Matt" == Matt Goodall <matt at pollenation.net> writes:
>>>>>>            
>>>>>>
>
>Matt> Hi, Can anyone explain why the method in the subject is fiddling
>Matt> around with uri paths? It looks like it's trying to cope with
>Matt> the situation when the base uri and the request uri differ but I
>Matt> don't understand why and it's probably not quite right,
>Matt> i.e. what happens if the two URIs do not match correctly? When
>Matt> would the two URIs be different anyway?
>
>All of that code (which could probably be improved) is trying to
>ensure that only absolute redirect URLs come out of the application.
>
>You can see what it is trying to do by placing this application in
>your cgi-bin directory and pointing your browser at it.
>
>- - redirect.py - - - - - - - - - - - - - - - - - - - - - - - - - - -
>#!/usr/bin/python
>import sys
>import os
>
>if __name__ == '__main__':
>    host = os.environ.get('HTTP_HOST')
>    script_name = os.environ.get('SCRIPT_NAME')
>    uri = os.environ.get('REQUEST_URI')
>
>    page = uri[len(script_name):]
>    sys.stderr.write('uri="%s", page="%s"\n' % (uri, page))
>    if page == '/there':
>        print 'Content-Type: text/plain'
>        print
>        print 'You are there'
>    else:
>        # This works perfectly
>        loc = 'http://%s%s/there' % (host, script_name)
>        # These all cause all sorts of strife
>        #loc = '/%s/there' % script_name
>        #loc = 'there'
>        print 'Location:', loc
>        print 'Status:', '301 Moved Permanently'
>        print
>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
>The Albatross code is just trying to figure out how to construct an
>absolute location for the requested redirect.  It also tries to avoid
>fiddling with a URL that is already absolute.
>
>Matt> Also, why does Albatross not make use of path info
>Matt> (os.environ['PATH_INFO'] in cgi, req.path_info in mod_python;
>Matt> not sure for BaseHTTPServer)? I think path info could simplify
>Matt> the parsing and building or URLs which has been causing me a few
>Matt> problems today when I popped a mod_python app behind a
>Matt> mod_rewrite rule :(.
>
>I am more than happy to change the current code if you figure out a
>better way to do it.
>
>- Dave
>
>  
>

-- 
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenationinternet.com
e: matt at pollenationinternet.com
t: +44 (0)113 2252500





More information about the Albatross-users mailing list