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

Changeset 879

Show
Ignore:
Timestamp:
12/23/05 15:29:16
Author:
fumanchu
Message:

Fix for #55. The before_main filter hook can now raise InternalRedirect?.

Files:

Legend:

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

    r865 r879  
    8989                        self.processBody() 
    9090                     
    91                     applyFilters('before_main') 
    92                     if self.executeMain: 
    93                         self.main() 
     91                    # Loop to allow for InternalRedirect. 
     92                    while True: 
     93                        try: 
     94                            applyFilters('before_main') 
     95                            if self.executeMain: 
     96                                self.main() 
     97                            break 
     98                        except cherrypy.InternalRedirect, ir: 
     99                            self.objectPath = ir.path 
    94100                     
    95101                    applyFilters('before_finalize') 
     
    232238            path = self.objectPath 
    233239         
    234         while True: 
    235             try: 
    236                 page_handler, object_path, virtual_path = self.mapPathToObject(path) 
    237                  
    238                 # Decode any leftover %2F in the virtual_path atoms. 
    239                 virtual_path = [x.replace("%2F", "/") for x in virtual_path] 
    240                  
    241                 # Remove "root" from object_path and join it to get objectPath 
    242                 self.objectPath = '/' + '/'.join(object_path[1:]) 
    243                 try: 
    244                     body = page_handler(*virtual_path, **self.params) 
    245                 except Exception, x: 
    246                     x.args = x.args + (page_handler,) 
    247                     raise 
    248                 cherrypy.response.body = body 
    249                 return 
    250             except cherrypy.InternalRedirect, x: 
    251                 # Try again with the new path 
    252                 path = x.path 
     240        page_handler, object_path, virtual_path = self.mapPathToObject(path) 
     241         
     242        # Decode any leftover %2F in the virtual_path atoms. 
     243        virtual_path = [x.replace("%2F", "/") for x in virtual_path] 
     244         
     245        # Remove "root" from object_path and join it to get objectPath 
     246        self.objectPath = '/' + '/'.join(object_path[1:]) 
     247        try: 
     248            body = page_handler(*virtual_path, **self.params) 
     249        except Exception, x: 
     250            x.args = x.args + (page_handler,) 
     251            raise 
     252        cherrypy.response.body = body 
    253253     
    254254    def mapPathToObject(self, objectpath): 
  • trunk/cherrypy/test/test_core.py

    r859 r879  
    104104        raise cherrypy.HTTPRedirect("proxy", 305) 
    105105     
    106     def internal(self): 
     106    def stringify(self): 
     107        return str(cherrypy.HTTPRedirect("/")) 
     108 
     109 
     110class LoginFilter: 
     111     
     112    def before_main(self): 
     113        if cherrypy.config.get("auth.on", False): 
     114            if not getattr(cherrypy.request, "login", None): 
     115                raise cherrypy.InternalRedirect("/internalredirect/login") 
     116 
     117class InternalRedirect(Test): 
     118     
     119    _cp_filters = [LoginFilter()] 
     120     
     121    def index(self): 
    107122        raise cherrypy.InternalRedirect("/") 
    108123     
    109     def internal2(self, user_id): 
     124    def petshop(self, user_id): 
    110125        if user_id == "parrot": 
    111126            # Trade it for a slug when redirecting 
     
    119134            raise cherrypy.InternalRedirect('/image/getImagesByUser') 
    120135     
    121     def stringify(self): 
    122         return str(cherrypy.HTTPRedirect("/")) 
     136    def secure(self): 
     137        return "Welcome!" 
     138     
     139    def login(self): 
     140        return "Please log in" 
    123141 
    124142 
     
    306324class NadsatFilter: 
    307325     
    308     def beforeFinalize(self): 
     326    def before_finalize(self): 
    309327        self.ended = False 
    310328        def nadsat_it_up(body): 
     
    351369    '/params': { 
    352370        'server.log_file': log_file, 
     371    }, 
     372    '/internalredirect/secure': { 
     373        'auth.on': True, 
    353374    }, 
    354375    '/error': { 
     
    549570         
    550571        # InternalRedirect 
    551         self.getPage("/redirect/internal") 
     572        self.getPage("/internalredirect/") 
    552573        self.assertBody('hello') 
    553574        self.assertStatus('200 OK') 
    554575         
    555         self.getPage("/redirect/internal2?user_id=Sir-not-appearing-in-this-film") 
     576        self.getPage("/internalredirect/petshop?user_id=Sir-not-appearing-in-this-film") 
    556577        self.assertBody('0 images for Sir-not-appearing-in-this-film') 
    557578        self.assertStatus('200 OK') 
    558579         
    559         self.getPage("/redirect/internal2?user_id=parrot") 
     580        self.getPage("/internalredirect/petshop?user_id=parrot") 
    560581        self.assertBody('0 images for slug') 
    561582        self.assertStatus('200 OK') 
    562583         
    563         self.getPage("/redirect/internal2?user_id=terrier") 
     584        self.getPage("/internalredirect/petshop?user_id=terrier") 
    564585        self.assertBody('0 images for fish') 
     586        self.assertStatus('200 OK') 
     587         
     588        self.getPage("/internalredirect/secure") 
     589        self.assertBody('Please log in') 
    565590        self.assertStatus('200 OK') 
    566591         

Hosted by WebFaction

Log in as guest/cpguest to create tickets