Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1769

Show
Ignore:
Timestamp:
10/26/07 21:14:08
Author:
fumanchu
Message:

Fix for #725 (cherrypy.url should default to emitting server-relative URL's). It doesn't default (that would break backward compatibility), but at least there's an option for it now.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/__init__.py

    r1768 r1769  
    419419            return expose_ 
    420420 
    421 def url(path="", qs="", script_name=None, base=None, relative=False): 
     421def url(path="", qs="", script_name=None, base=None, relative=None): 
    422422    """Create an absolute URL for the given path. 
    423423     
     
    438438    original browser URL (assuming no internal redirections). 
    439439     
    440     If relative is False (the default), the output will be an absolute URL 
    441     (usually including the scheme, host, vhost, and script_name). 
    442     If relative is True, the output will instead be a URL that is relative 
    443     to the current request path, perhaps including '..' atoms. 
     440    If relative is None or not provided, request.app.relative_urls will 
     441    be used (if available, else False). If False, the output will be an 
     442    absolute URL (including the scheme, host, vhost, and script_name). 
     443    If True, the output will instead be a URL that is relative to the 
     444    current request path, perhaps including '..' atoms. If relative is 
     445    the string 'server', the output will instead be a URL that is 
     446    relative to the server root; i.e., it will start with a slash. 
    444447    """ 
    445448    if qs: 
     
    495498    # At this point, we should have a fully-qualified absolute URL. 
    496499     
    497     if relative: 
     500    if relative is None: 
     501        relative = getattr(request.app, "relative_urls", False) 
     502     
     503    # See http://www.ietf.org/rfc/rfc2396.txt 
     504    if relative == 'server': 
     505        # "A relative reference beginning with a single slash character is 
     506        # termed an absolute-path reference, as defined by <abs_path>..." 
     507        # This is also sometimes called "server-relative". 
     508        newurl = '/' + '/'.join(newurl.split('/', 3)[3:]) 
     509    elif relative: 
     510        # "A relative reference that does not begin with a scheme name 
     511        # or a slash character is termed a relative-path reference." 
    498512        old = url().split('/')[:-1] 
    499513        new = newurl.split('/') 
  • trunk/cherrypy/_cptree.py

    r1752 r1769  
    4343    request_class = _cprequest.Request 
    4444    response_class = _cprequest.Response 
     45     
     46    relative_urls = False 
    4547     
    4648    def __init__(self, root, script_name=""): 
  • trunk/cherrypy/test/test_core.py

    r1768 r1769  
    7777         
    7878        def index(self, path_info, relative=None): 
    79             return cherrypy.url(path_info, relative=bool(relative)) 
     79            if relative != 'server': 
     80                relative = bool(relative) 
     81            return cherrypy.url(path_info, relative=relative) 
    8082         
    8183        def leaf(self, path_info, relative=None): 
    82             return cherrypy.url(path_info, relative=bool(relative)) 
     84            if relative != 'server': 
     85                relative = bool(relative) 
     86            return cherrypy.url(path_info, relative=relative) 
    8387     
    8488     
     
    10771081        self.getPage('/baseurl?path_info=/ab&relative=True') 
    10781082        self.assertBody('ab') 
     1083         
     1084        # absolute-path references ("server-relative") 
     1085        # Input relative to current 
     1086        self.getPage('/url/leaf?path_info=page1&relative=server') 
     1087        self.assertBody('/url/page1') 
     1088        self.getPage('/url/?path_info=page1&relative=server') 
     1089        self.assertBody('/url/page1') 
     1090        # Input is 'absolute'; that is, relative to script_name 
     1091        self.getPage('/url/leaf?path_info=/page1&relative=server') 
     1092        self.assertBody('/page1') 
     1093        self.getPage('/url/?path_info=/page1&relative=server') 
     1094        self.assertBody('/page1') 
    10791095 
    10801096 

Hosted by WebFaction

Log in as guest/cpguest to create tickets