Changeset 1338
- Timestamp:
- 09/06/06 17:39:38
- Files:
-
- trunk/cherrypy/_cperror.py (modified) (3 diffs)
- trunk/cherrypy/_cprequest.py (modified) (3 diffs)
- trunk/cherrypy/_cptree.py (modified) (6 diffs)
- trunk/cherrypy/lib/caching.py (modified) (1 diff)
- trunk/cherrypy/lib/cptools.py (modified) (1 diff)
- trunk/cherrypy/test/test_objectmapping.py (modified) (1 diff)
- trunk/cherrypy/test/test_proxy.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cperror.py
r1334 r1338 31 31 # 1. a URL relative to root (e.g. "/dummy") 32 32 # 2. a URL relative to the current path 33 # Note that any query string will be discarded.33 # Note that any query string will be discarded. 34 34 path = _urljoin(cherrypy.request.path_info, path) 35 35 … … 53 53 def __init__(self, urls, status=None): 54 54 import cherrypy 55 request = cherrypy.request 55 56 56 57 if isinstance(urls, basestring): … … 63 64 # 2. a URL relative to root (e.g. "/dummy") 64 65 # 3. a URL relative to the current path 65 # Note that any query string in browser_url will bediscarded.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) 67 68 abs_urls.append(url) 68 69 self.urls = abs_urls trunk/cherrypy/_cprequest.py
r1336 r1338 232 232 request.redirect_on_missing_slash): 233 233 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) 238 235 raise cherrypy.HTTPRedirect(new_url) 239 236 … … 248 245 # If pi == '/', don't redirect to ''! 249 246 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) 254 248 raise cherrypy.HTTPRedirect(new_url) 255 249 … … 657 651 tool._setup() 658 652 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 666 669 667 670 def process_body(self): trunk/cherrypy/_cptree.py
r1311 r1338 1 1 """CherryPy Application and Tree objects.""" 2 2 3 import os 3 4 import sys 4 5 import cherrypy 5 from cherrypy import _cpconfig, _cplogging 6 from cherrypy import _cpconfig, _cplogging, tools 6 7 from cherrypy._cperror import format_exc, bare_error 7 8 from cherrypy.lib import http … … 36 37 if self._script_name is None: 37 38 # None signals that the script name should be pulled from WSGI environ. 38 import cherrypy39 39 return cherrypy.request.wsgi_environ['SCRIPT_NAME'] 40 40 return self._script_name … … 218 218 # If mounted at "", add favicon.ico 219 219 if script_name == "" and root and not hasattr(root, "favicon_ico"): 220 import os221 from cherrypy import tools222 220 favicon = os.path.join(os.getcwd(), os.path.dirname(__file__), 223 221 "favicon.ico") … … 245 243 if path is None: 246 244 try: 247 import cherrypy248 245 path = cherrypy.request.path 249 246 except AttributeError: … … 260 257 path = path[:path.rfind("/")] 261 258 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. 264 261 265 262 If script_name is None, cherrypy.request.path will be used 266 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 267 """ 268 268 … … 272 272 return path 273 273 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) 276 278 277 279 def __call__(self, environ, start_response): trunk/cherrypy/lib/caching.py
r1291 r1338 28 28 29 29 def _key(self): 30 re turn 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)) 32 32 key = property(_key) 33 33 trunk/cherrypy/lib/cptools.py
r1330 r1338 230 230 sess[self.session_key] = username = self.anonymous() 231 231 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)) 233 233 return True 234 234 trunk/cherrypy/test/test_objectmapping.py
r1303 r1338 220 220 self.assertBody(url) 221 221 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)) 223 224 224 225 # Test that configs don't overwrite each other from diferent apps trunk/cherrypy/test/test_proxy.py
r1275 r1338 4 4 import cherrypy 5 5 6 script_names = ["", "/path/to/myapp"] 6 7 7 8 def setup_server(): … … 19 20 xhost.exposed = True 20 21 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 21 27 22 cherrypy.tree.mount(Root()) 28 for sn in script_names: 29 cherrypy.tree.mount(Root(), sn) 30 23 31 cherrypy.config.update({ 24 32 'environment': 'test_suite', … … 54 62 self.getPage("/xhost", headers=[('X-Host', 'http://www.yetanother.com')]) 55 63 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>.") 56 74 57 75

