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

Changeset 1338

Show
Ignore:
Timestamp:
09/06/06 17:39:38
Author:
fumanchu
Message:

Improved tree.url() to include base. Also replaced request.browser_url with request.url().

Files:

Legend:

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

    r1334 r1338  
    3131        #  1. a URL relative to root (e.g. "/dummy") 
    3232        #  2. a URL relative to the current path 
    33         # Note that any querystring will be discarded. 
     33        # Note that any query string will be discarded. 
    3434        path = _urljoin(cherrypy.request.path_info, path) 
    3535         
     
    5353    def __init__(self, urls, status=None): 
    5454        import cherrypy 
     55        request = cherrypy.request 
    5556         
    5657        if isinstance(urls, basestring): 
     
    6364            #  2. a URL relative to root (e.g. "/dummy") 
    6465            #  3. a URL relative to the current path 
    65             # Note that any querystring in browser_url will be discarded. 
    66             url = _urljoin(cherrypy.request.browser_url, url) 
     66            # Note that any query string in cherrypy.request is discarded. 
     67            url = _urljoin(request.url(), url) 
    6768            abs_urls.append(url) 
    6869        self.urls = abs_urls 
  • trunk/cherrypy/_cprequest.py

    r1336 r1338  
    232232                              request.redirect_on_missing_slash): 
    233233            if pi[-1:] != '/': 
    234                 atoms = request.browser_url.split("?", 1) 
    235                 new_url = atoms.pop(0) + '/' 
    236                 if atoms: 
    237                     new_url += "?" + atoms[0] 
     234                new_url = request.url(pi + '/', request.query_string) 
    238235                raise cherrypy.HTTPRedirect(new_url) 
    239236     
     
    248245            # If pi == '/', don't redirect to ''! 
    249246            if pi[-1:] == '/' and pi != '/': 
    250                 atoms = request.browser_url.split("?", 1) 
    251                 new_url = atoms.pop(0)[:-1] 
    252                 if atoms: 
    253                     new_url += "?" + atoms[0] 
     247                new_url = request.url(pi[:-1], request.query_string) 
    254248                raise cherrypy.HTTPRedirect(new_url) 
    255249 
     
    657651                tool._setup() 
    658652     
    659     def _get_browser_url(self): 
    660         url = self.base + self.path 
    661         if self.query_string: 
    662             url += '?' + self.query_string 
    663         return url 
    664     browser_url = property(_get_browser_url, 
    665                            doc="The URL as entered in a browser (read-only).") 
     653    def url(self, path_info="", qs=""): 
     654        """Create an absolute URL for the given path_info. 
     655         
     656        If 'path_info' starts with a slash ('/'), this will return 
     657            (self.base + self.script_name + path_info + qs). 
     658        If it does not start with a slash, this returns 
     659            (self.base + self.script_name + self.path_info + path_info + qs). 
     660        """ 
     661        if path_info == "": 
     662            path_info = self.path_info 
     663        if not path_info.startswith("/"): 
     664            path_info = self.path_info + "/" + path_info 
     665         
     666        if qs: 
     667            qs = '?' + qs 
     668        return self.base + self.script_name + path_info + qs 
    666669     
    667670    def process_body(self): 
  • trunk/cherrypy/_cptree.py

    r1311 r1338  
    11"""CherryPy Application and Tree objects.""" 
    22 
     3import os 
    34import sys 
    45import cherrypy 
    5 from cherrypy import _cpconfig, _cplogging 
     6from cherrypy import _cpconfig, _cplogging, tools 
    67from cherrypy._cperror import format_exc, bare_error 
    78from cherrypy.lib import http 
     
    3637        if self._script_name is None: 
    3738            # None signals that the script name should be pulled from WSGI environ. 
    38             import cherrypy 
    3939            return cherrypy.request.wsgi_environ['SCRIPT_NAME'] 
    4040        return self._script_name 
     
    218218            # If mounted at "", add favicon.ico 
    219219            if script_name == "" and root and not hasattr(root, "favicon_ico"): 
    220                 import os 
    221                 from cherrypy import tools 
    222220                favicon = os.path.join(os.getcwd(), os.path.dirname(__file__), 
    223221                                       "favicon.ico") 
     
    245243        if path is None: 
    246244            try: 
    247                 import cherrypy 
    248245                path = cherrypy.request.path 
    249246            except AttributeError: 
     
    260257            path = path[:path.rfind("/")] 
    261258     
    262     def url(self, path, script_name=None): 
    263         """Return 'path', prefixed with script_name
     259    def url(self, path, script_name=None, base=None): 
     260        """Return 'path', prefixed with script_name and base
    264261         
    265262        If script_name is None, cherrypy.request.path will be used 
    266263        to find a script_name. 
     264         
     265        If base is None, cherrypy.request.base will be used. Note that 
     266        you can use cherrypy.tools.proxy to change this. 
    267267        """ 
    268268         
     
    272272                return path 
    273273         
    274         from cherrypy.lib import http 
    275         return http.urljoin(script_name, path) 
     274        if base is None: 
     275            base = cherrypy.request.base 
     276         
     277        return base + http.urljoin(script_name, path) 
    276278     
    277279    def __call__(self, environ, start_response): 
  • trunk/cherrypy/lib/caching.py

    r1291 r1338  
    2828     
    2929    def _key(self): 
    30         return cherrypy.request.config.get("tools.caching.key", 
    31                                            cherrypy.request.browser_url
     30        request = cherrypy.request 
     31        return request.config.get("tools.caching.key", request.url(qs=request.query_string)
    3232    key = property(_key) 
    3333     
  • trunk/cherrypy/lib/cptools.py

    r1330 r1338  
    230230            sess[self.session_key] = username = self.anonymous() 
    231231        if not username: 
    232             cherrypy.response.body = self.login_screen(request.browser_url
     232            cherrypy.response.body = self.login_screen(request.url(qs=request.query_string)
    233233            return True 
    234234         
  • trunk/cherrypy/test/test_objectmapping.py

    r1303 r1338  
    220220            self.assertBody(url) 
    221221            self.getPage("/dir1/dir2/tree_url") 
    222             self.assertBody(prefix + "/extra") 
     222            self.assertBody("http://%s:%s%s/extra" % 
     223                            (self.HOST, self.PORT, prefix)) 
    223224             
    224225            # Test that configs don't overwrite each other from diferent apps 
  • trunk/cherrypy/test/test_proxy.py

    r1275 r1338  
    44import cherrypy 
    55 
     6script_names = ["", "/path/to/myapp"] 
    67 
    78def setup_server(): 
     
    1920        xhost.exposed = True 
    2021        xhost._cp_config = {'tools.proxy.local': 'X-Host'} 
     22         
     23        def newurl(self): 
     24            return ("Browse to <a href='%s'>this page</a>." 
     25                    % cherrypy.tree.url("/this/new/page")) 
     26        newurl.exposed = True 
    2127     
    22     cherrypy.tree.mount(Root()) 
     28    for sn in script_names: 
     29        cherrypy.tree.mount(Root(), sn) 
     30     
    2331    cherrypy.config.update({ 
    2432        'environment': 'test_suite', 
     
    5462        self.getPage("/xhost", headers=[('X-Host', 'http://www.yetanother.com')]) 
    5563        self.assertHeader('Location', "http://www.yetanother.com/blah") 
     64         
     65        # Test tree.url() 
     66        for sn in script_names: 
     67            self.getPage(sn + "/newurl") 
     68            self.assertBody("Browse to <a href='http://www.mydomain.com" 
     69                            + sn + "/this/new/page'>this page</a>.") 
     70            self.getPage(sn + "/newurl", headers=[('X-Forwarded-Host', 
     71                                                   'http://www.yetanother.com')]) 
     72            self.assertBody("Browse to <a href='http://www.yetanother.com" 
     73                            + sn + "/this/new/page'>this page</a>.") 
    5674 
    5775 

Hosted by WebFaction

Log in as guest/cpguest to create tickets