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

Changeset 1350

Show
Ignore:
Timestamp:
09/11/06 12:26:06
Author:
fumanchu
Message:

Collapsed request.url and tree.url into a single cherrypy.url function.

Files:

Legend:

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

    r1335 r1350  
    150150            return expose_ 
    151151 
     152def url(path="", qs="", script_name=None, base=None): 
     153    """Create an absolute URL for the given path. 
     154     
     155    If 'path' starts with a slash ('/'), this will return 
     156        (base + script_name + path + qs). 
     157    If it does not start with a slash, this returns 
     158        (base + script_name [+ request.path_info] + path + qs). 
     159     
     160    If script_name is None, cherrypy.request will be used 
     161    to find a script_name, if available. 
     162     
     163    If base is None, cherrypy.request.base will be used if available. 
     164    Note that you can use cherrypy.tools.proxy to change this. 
     165     
     166    Finally, note that this function can be used to obtain an absolute URL 
     167    for the current request path (minus the querystring) by passing no args. 
     168    """ 
     169    if qs: 
     170        qs = '?' + qs 
     171     
     172    if request.app: 
     173        if path == "": 
     174            path = request.path_info 
     175        if not path.startswith("/"): 
     176            path = request.path_info + "/" + path 
     177        if script_name is None: 
     178            script_name = request.app.script_name 
     179        if base is None: 
     180            base = request.base 
     181         
     182        return base + script_name + path + qs 
     183    else: 
     184        # No request.app (we're being called outside a request). 
     185        # We'll have to guess the base from server.* attributes. 
     186        # This will produce very different results from the above 
     187        # if you're using vhosts or tools.proxy. 
     188        if base is None: 
     189            f = server.socket_file 
     190            if f: 
     191                base = f 
     192            else: 
     193                host = server.socket_host 
     194                if not host: 
     195                    import socket 
     196                    host = socket.gethostname() 
     197                port = server.socket_port 
     198                if (port in (443, 8443) or server.ssl_certificate): 
     199                    scheme = "https" 
     200                    if port != 443: 
     201                        host += ":%s" % port 
     202                else: 
     203                    scheme = "http" 
     204                    if port != 80: 
     205                        host += ":%s" % port 
     206                base = "%s://%s" % (scheme, host) 
     207        path = (script_name or "") + path 
     208        return base + path + qs 
    152209 
    153210# Set up config last so it can wrap other top-level objects 
  • trunk/cherrypy/_cperror.py

    r1342 r1350  
    6565            #  3. a URL relative to the current path 
    6666            # Note that any query string in cherrypy.request is discarded. 
    67             url = _urljoin(request.url(), url) 
     67            url = _urljoin(cherrypy.url(), url) 
    6868            abs_urls.append(url) 
    6969        self.urls = abs_urls 
  • trunk/cherrypy/_cprequest.py

    r1342 r1350  
    232232                              request.redirect_on_missing_slash): 
    233233            if pi[-1:] != '/': 
    234                 new_url = request.url(pi + '/', request.query_string) 
     234                new_url = cherrypy.url(pi + '/', request.query_string) 
    235235                raise cherrypy.HTTPRedirect(new_url) 
    236236     
     
    245245            # If pi == '/', don't redirect to ''! 
    246246            if pi[-1:] == '/' and pi != '/': 
    247                 new_url = request.url(pi[:-1], request.query_string) 
     247                new_url = cherrypy.url(pi[:-1], request.query_string) 
    248248                raise cherrypy.HTTPRedirect(new_url) 
    249249 
     
    651651                tool._setup() 
    652652     
    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 
    669      
    670653    def process_body(self): 
    671654        """Convert request.rfile into request.params (or request.body).""" 
  • trunk/cherrypy/_cptree.py

    r1342 r1350  
    7272        if port != 80: 
    7373            host += ":%s" % port 
    74         return scheme + host + self.script_name 
     74        return scheme + host + (self.script_name or "/") 
    7575     
    7676    def wsgiapp(self, environ, start_response): 
     
    257257            path = path[:path.rfind("/")] 
    258258     
    259     def url(self, path, script_name=None, base=None): 
    260         """Return 'path', prefixed with script_name and base. 
    261          
    262         If script_name is None, cherrypy.request will be used 
    263         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. 
    267         """ 
    268          
    269         if script_name is None: 
    270             script_name = self.script_name() 
    271             if script_name is None: 
    272                 return path 
    273          
    274         if base is None: 
    275             base = cherrypy.request.base 
    276          
    277         return base + http.urljoin(script_name, path) 
    278      
    279259    def __call__(self, environ, start_response): 
    280260        # If you're calling this, then you're probably setting SCRIPT_NAME 
  • trunk/cherrypy/lib/caching.py

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

    r1342 r1350  
    235235            sess[self.session_key] = username = self.anonymous() 
    236236        if not username: 
    237             cherrypy.response.body = self.login_screen(request.url(qs=request.query_string)) 
     237            cherrypy.response.body = self.login_screen(cherrypy.url(qs=request.query_string)) 
    238238            return True 
    239239         
  • trunk/cherrypy/test/helper.py

    r1340 r1350  
    3131     
    3232    script_name = "" 
     33    scheme = "http" 
    3334     
    3435    def prefix(self): 
     
    150151    """Run __main__ as a test module, with webtest debugging.""" 
    151152    if conf is None: 
    152         conf = {
     153        conf = {'server.socket_host': '127.0.0.1'
    153154    setConfig(conf) 
    154155    try: 
  • trunk/cherrypy/test/test.py

    r1345 r1350  
    7272        webtest.WebCase.PORT = self.port 
    7373        webtest.WebCase.harness = self 
    74         webtest.WebCase.scheme = self.scheme 
     74        helper.CPWebCase.scheme = self.scheme 
    7575        if self.scheme == "https": 
    7676            webtest.WebCase.HTTP_CONN = httplib.HTTPSConnection 
  • trunk/cherrypy/test/test_objectmapping.py

    r1345 r1350  
    8181        script_name.exposed = True 
    8282         
    83         def tree_url(self): 
    84             return cherrypy.tree.url("/extra") 
    85         tree_url.exposed = True 
     83        def cherrypy_url(self): 
     84            return cherrypy.url("/extra") 
     85        cherrypy_url.exposed = True 
    8686         
    8787        def posparam(self, *vpath): 
     
    219219            self.getPage("/dir1/dir2/script_name") 
    220220            self.assertBody(url) 
    221             self.getPage("/dir1/dir2/tree_url") 
     221            self.getPage("/dir1/dir2/cherrypy_url") 
    222222            self.assertBody("%s://%s:%s%s/extra" % 
    223223                            (self.scheme, self.HOST, self.PORT, prefix)) 
  • trunk/cherrypy/test/test_proxy.py

    r1345 r1350  
    2727        def newurl(self): 
    2828            return ("Browse to <a href='%s'>this page</a>." 
    29                     % cherrypy.tree.url("/this/new/page")) 
     29                    % cherrypy.url("/this/new/page")) 
    3030        newurl.exposed = True 
    3131     
     
    4747        self.getPage("/") 
    4848        self.assertHeader('Location', 
    49                           "%s://www.mydomain.com%s/dummy" % (self.scheme, self.prefix())) 
     49                          "%s://www.mydomain.com%s/dummy" % 
     50                          (self.scheme, self.prefix())) 
    5051         
    5152        # Test X-Forwarded-Host (Apache 1.3.33+ and Apache 2) 
     
    7172        self.assertBody("https://www.mydomain.com") 
    7273         
    73         # Test tree.url() 
     74        # Test cherrypy.url() 
    7475        for sn in script_names: 
     76            # Test the value inside requests 
    7577            self.getPage(sn + "/newurl") 
    7678            self.assertBody("Browse to <a href='%s://www.mydomain.com" % self.scheme 
     
    8082            self.assertBody("Browse to <a href='http://www.yetanother.com" 
    8183                            + sn + "/this/new/page'>this page</a>.") 
     84             
     85            # Test the value outside requests 
     86            port = "" 
     87            if self.scheme == "http" and self.PORT != 80: 
     88                port = ":%s" % self.PORT 
     89            elif self.scheme == "https" and self.PORT != 443: 
     90                port = ":%s" % self.PORT 
     91            self.assertEqual(cherrypy.url("/this/new/page", script_name=sn), 
     92                             "%s://127.0.0.1%s%s/this/new/page" 
     93                             % (self.scheme, port, sn)) 
    8294 
    8395 
  • trunk/cherrypy/test/webtest.py

    r1281 r1350  
    378378            break 
    379379    if not found: 
    380         headers.append(("Host", "%s:%s" % (host, port))) 
     380        if port == 80: 
     381            headers.append(("Host", host)) 
     382        else: 
     383            headers.append(("Host", "%s:%s" % (host, port))) 
    381384     
    382385    if method in methods_with_bodies: 

Hosted by WebFaction

Log in as guest/cpguest to create tickets