Changeset 1350
- Timestamp:
- 09/11/06 12:26:06
- Files:
-
- trunk/cherrypy/__init__.py (modified) (1 diff)
- trunk/cherrypy/_cperror.py (modified) (1 diff)
- trunk/cherrypy/_cprequest.py (modified) (3 diffs)
- trunk/cherrypy/_cptree.py (modified) (2 diffs)
- trunk/cherrypy/lib/caching.py (modified) (1 diff)
- trunk/cherrypy/lib/cptools.py (modified) (1 diff)
- trunk/cherrypy/test/helper.py (modified) (2 diffs)
- trunk/cherrypy/test/test.py (modified) (1 diff)
- trunk/cherrypy/test/test_objectmapping.py (modified) (2 diffs)
- trunk/cherrypy/test/test_proxy.py (modified) (4 diffs)
- trunk/cherrypy/test/webtest.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r1335 r1350 150 150 return expose_ 151 151 152 def 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 152 209 153 210 # Set up config last so it can wrap other top-level objects trunk/cherrypy/_cperror.py
r1342 r1350 65 65 # 3. a URL relative to the current path 66 66 # Note that any query string in cherrypy.request is discarded. 67 url = _urljoin( request.url(), url)67 url = _urljoin(cherrypy.url(), url) 68 68 abs_urls.append(url) 69 69 self.urls = abs_urls trunk/cherrypy/_cprequest.py
r1342 r1350 232 232 request.redirect_on_missing_slash): 233 233 if pi[-1:] != '/': 234 new_url = request.url(pi + '/', request.query_string)234 new_url = cherrypy.url(pi + '/', request.query_string) 235 235 raise cherrypy.HTTPRedirect(new_url) 236 236 … … 245 245 # If pi == '/', don't redirect to ''! 246 246 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) 248 248 raise cherrypy.HTTPRedirect(new_url) 249 249 … … 651 651 tool._setup() 652 652 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 return657 (self.base + self.script_name + path_info + qs).658 If it does not start with a slash, this returns659 (self.base + self.script_name + self.path_info + path_info + qs).660 """661 if path_info == "":662 path_info = self.path_info663 if not path_info.startswith("/"):664 path_info = self.path_info + "/" + path_info665 666 if qs:667 qs = '?' + qs668 return self.base + self.script_name + path_info + qs669 670 653 def process_body(self): 671 654 """Convert request.rfile into request.params (or request.body).""" trunk/cherrypy/_cptree.py
r1342 r1350 72 72 if port != 80: 73 73 host += ":%s" % port 74 return scheme + host + self.script_name74 return scheme + host + (self.script_name or "/") 75 75 76 76 def wsgiapp(self, environ, start_response): … … 257 257 path = path[:path.rfind("/")] 258 258 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 used263 to find a script_name.264 265 If base is None, cherrypy.request.base will be used. Note that266 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 path273 274 if base is None:275 base = cherrypy.request.base276 277 return base + http.urljoin(script_name, path)278 279 259 def __call__(self, environ, start_response): 280 260 # If you're calling this, then you're probably setting SCRIPT_NAME trunk/cherrypy/lib/caching.py
r1338 r1350 29 29 def _key(self): 30 30 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)) 32 32 key = property(_key) 33 33 trunk/cherrypy/lib/cptools.py
r1342 r1350 235 235 sess[self.session_key] = username = self.anonymous() 236 236 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)) 238 238 return True 239 239 trunk/cherrypy/test/helper.py
r1340 r1350 31 31 32 32 script_name = "" 33 scheme = "http" 33 34 34 35 def prefix(self): … … 150 151 """Run __main__ as a test module, with webtest debugging.""" 151 152 if conf is None: 152 conf = { }153 conf = {'server.socket_host': '127.0.0.1'} 153 154 setConfig(conf) 154 155 try: trunk/cherrypy/test/test.py
r1345 r1350 72 72 webtest.WebCase.PORT = self.port 73 73 webtest.WebCase.harness = self 74 webtest.WebCase.scheme = self.scheme74 helper.CPWebCase.scheme = self.scheme 75 75 if self.scheme == "https": 76 76 webtest.WebCase.HTTP_CONN = httplib.HTTPSConnection trunk/cherrypy/test/test_objectmapping.py
r1345 r1350 81 81 script_name.exposed = True 82 82 83 def tree_url(self):84 return cherrypy. tree.url("/extra")85 tree_url.exposed = True83 def cherrypy_url(self): 84 return cherrypy.url("/extra") 85 cherrypy_url.exposed = True 86 86 87 87 def posparam(self, *vpath): … … 219 219 self.getPage("/dir1/dir2/script_name") 220 220 self.assertBody(url) 221 self.getPage("/dir1/dir2/ tree_url")221 self.getPage("/dir1/dir2/cherrypy_url") 222 222 self.assertBody("%s://%s:%s%s/extra" % 223 223 (self.scheme, self.HOST, self.PORT, prefix)) trunk/cherrypy/test/test_proxy.py
r1345 r1350 27 27 def newurl(self): 28 28 return ("Browse to <a href='%s'>this page</a>." 29 % cherrypy. tree.url("/this/new/page"))29 % cherrypy.url("/this/new/page")) 30 30 newurl.exposed = True 31 31 … … 47 47 self.getPage("/") 48 48 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())) 50 51 51 52 # Test X-Forwarded-Host (Apache 1.3.33+ and Apache 2) … … 71 72 self.assertBody("https://www.mydomain.com") 72 73 73 # Test tree.url()74 # Test cherrypy.url() 74 75 for sn in script_names: 76 # Test the value inside requests 75 77 self.getPage(sn + "/newurl") 76 78 self.assertBody("Browse to <a href='%s://www.mydomain.com" % self.scheme … … 80 82 self.assertBody("Browse to <a href='http://www.yetanother.com" 81 83 + 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)) 82 94 83 95 trunk/cherrypy/test/webtest.py
r1281 r1350 378 378 break 379 379 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))) 381 384 382 385 if method in methods_with_bodies:

