Changeset 1303
- Timestamp:
- 08/30/06 19:02:25
- Files:
-
- trunk/cherrypy/_cprequest.py (modified) (4 diffs)
- trunk/cherrypy/lib/cptools.py (modified) (1 diff)
- trunk/cherrypy/test/test_objectmapping.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cprequest.py
r1302 r1303 186 186 187 187 # Try successive objects (reverse order) 188 for i in xrange(len(object_trail) - 1, -1, -1): 188 num_candidates = len(object_trail) - 1 189 for i in xrange(num_candidates, -1, -1): 189 190 190 191 name, candidate, nodeconf, curpath = object_trail[i] … … 203 204 204 205 # Uncomment the next line to restrict positional params to "default". 205 # if i < len(object_trail)- 2: continue206 # if i < num_candidates - 2: continue 206 207 207 208 # Try the current leaf. 208 209 if getattr(candidate, 'exposed', False): 209 210 request.config = set_conf() 210 if i == len(object_trail) - 1: 211 # We found the extra ".index". Check if the original path 212 # had a trailing slash (otherwise, do a redirect). 213 if path[-1:] != '/': 214 atoms = request.browser_url.split("?", 1) 215 new_url = atoms.pop(0) + '/' 216 if atoms: 217 new_url += "?" + atoms[0] 218 raise cherrypy.HTTPRedirect(new_url) 211 if i == num_candidates: 212 # We found the extra ".index". Check that path_info 213 # has a trailing slash (otherwise, do a redirect). 214 self.check_missing_slash() 215 else: 216 # We're not at an 'index' handler. Check that path_info 217 # had NO trailing slash (if it did, do a redirect). 218 self.check_extra_slash() 219 219 return candidate, names[i:-1] 220 220 … … 222 222 request.config = set_conf() 223 223 return None, [] 224 225 def check_missing_slash(self): 226 """Redirect if path_info has no trailing slash (if configured).""" 227 request = cherrypy.request 228 pi = request.path_info 229 230 # Must use config here because tool_up probably hasn't run yet. 231 if request.config.get("request.redirect_on_missing_slash", 232 request.redirect_on_missing_slash): 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] 238 raise cherrypy.HTTPRedirect(new_url) 239 240 def check_extra_slash(self): 241 """Redirect if path_info has trailing slash (if configured).""" 242 request = cherrypy.request 243 pi = request.path_info 244 245 # Must use config here because tool_up hasn't run yet. 246 if request.config.get("request.redirect_on_extra_slash", 247 request.redirect_on_extra_slash): 248 # If pi == '/', don't redirect to ''! 249 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] 254 raise cherrypy.HTTPRedirect(new_url) 224 255 225 256 … … 300 331 config = None 301 332 recursive_redirect = False 333 redirect_on_extra_slash = False 334 redirect_on_missing_slash = True 302 335 303 336 hookpoints = ['on_start_resource', 'before_request_body', trunk/cherrypy/lib/cptools.py
r1294 r1303 220 220 request = cherrypy.request 221 221 222 # Guard against running twice. 222 223 if hasattr(request, "virtual_prefix"): 223 224 return trunk/cherrypy/test/test_objectmapping.py
r1297 r1303 65 65 return "myMethod from dir1, path_info is:" + repr(cherrypy.request.path_info) 66 66 myMethod.exposed = True 67 myMethod._cp_config = {'request.redirect_on_extra_slash': True} 67 68 68 69 def default(self, *params): … … 92 93 def default(self): 93 94 return "default for dir3, not exposed" 94 95 95 96 96 class Dir4: … … 179 179 self.assertBody('index for dir2, path is:%s/dir1/dir2/' % prefix) 180 180 181 # Test omitted trailing slash (should be redirected by default). 181 182 self.getPage("/dir1/dir2") 182 self.assert _(self.status in ('302 Found', '303 See Other'))183 self.assertStatus((302, 303)) 183 184 self.assertHeader('Location', 'http://%s:%s%s/dir1/dir2/' 184 185 % (self.HOST, self.PORT, prefix)) 185 186 187 # Test extra trailing slash (should be redirected if configured). 188 self.getPage("/dir1/myMethod/") 189 self.assertStatus((302, 303)) 190 self.assertHeader('Location', 'http://%s:%s%s/dir1/myMethod' 191 % (self.HOST, self.PORT, prefix)) 192 193 # Test that default method must be exposed in order to match. 186 194 self.getPage("/dir1/dir2/dir3/dir4/index") 187 195 self.assertBody("default for dir1, param is:('dir2', 'dir3', 'dir4', 'index')")

