Edgewall Software

Ticket #3655: irequestfilter-r3717.diff

File irequestfilter-r3717.diff, 3.9 KB (added by cboos, 6 years ago)

Updated patch, integrating feedback from cmlenz

  • trac/web/api.py

     
    467467    requests, before and/or after they are processed by the main handler.""" 
    468468 
    469469    def pre_process_request(req, handler): 
    470         """Do any pre-processing the request might need; typically adding 
    471         values to req.hdf, or redirecting. 
     470        """Called after initial handler selection, and can be used to change 
     471        the selected handler or redirect request. 
    472472         
    473473        Always returns the request handler, even if unchanged. 
    474474        """ 
  • trac/web/main.py

     
    172172                        chosen_handler = handler 
    173173                        break 
    174174 
    175             for f in self.filters: 
    176                 chosen_handler = f.pre_process_request(req, chosen_handler) 
    177                  
     175            chosen_handler = self._pre_process_request(req, chosen_handler) 
    178176        except: 
    179177            early_error = sys.exc_info() 
    180178             
     
    201199        # Prepare HDF for the clearsilver template 
    202200        try: 
    203201            use_template = getattr(chosen_handler, 'use_template', True) 
     202            req.hdf = None 
    204203            if use_template: 
    205204                chrome = Chrome(self.env) 
    206205                req.hdf = HDFWrapper(loadpaths=chrome.get_all_templates_dirs()) 
     
    212211                raise 
    213212 
    214213        if early_error: 
     214            try: 
     215                self._post_process_request(req) 
     216            except Exception, e: 
     217                self.log.exception(e) 
    215218            raise early_error[0], early_error[1], early_error[2] 
    216219 
    217220        # Process the request and render the template 
    218221        try: 
    219222            try: 
    220                 resp = chosen_handler.process_request(req) 
    221                 if resp: 
    222                     for filter_ in reversed(self.filters): 
    223                         resp = filter_.post_process_request(req, *resp) 
    224                     template, content_type = resp 
    225                     req.display(template, content_type or 'text/html') 
    226                 else: 
    227                     for filter_ in reversed(self.filters): 
    228                         filter_.post_process_request(req, None, None) 
     223                try: 
     224                    resp = chosen_handler.process_request(req) 
     225                    if resp: 
     226                        template, content_type = \ 
     227                                  self._post_process_request(req, *resp) 
     228                        req.display(template, content_type or 'text/html') 
     229                    else: 
     230                        self._post_process_request(req) 
     231                except: 
     232                    err = sys.exc_info() 
     233                    try: 
     234                        self._post_process_request(req) 
     235                    except Exception, e: 
     236                        self.log.exception(e) 
     237                    raise err[0], err[1], err[2] 
    229238            except PermissionError, e: 
    230239                raise HTTPForbidden(to_unicode(e)) 
    231240            except TracError, e: 
     
    235244            if req.session: 
    236245                req.session.save() 
    237246 
     247    def _pre_process_request(self, req, chosen_handler): 
     248        for f in self.filters: 
     249            chosen_handler = f.pre_process_request(req, chosen_handler) 
     250        return chosen_handler 
     251                 
     252    def _post_process_request(self, req, template=None, content_type=None): 
     253        for f in reversed(self.filters): 
     254            template, content_type = f.post_process_request(req, template, 
     255                                                            content_type) 
     256        return template, content_type 
    238257 
     258 
    239259def dispatch_request(environ, start_response): 
    240260    """Main entry point for the Trac web interface. 
    241261