wsgi/handler.py
changeset 8312 6c2119509fac
parent 8309 48ef505aa9f9
child 8314 cfd6ab461849
equal deleted inserted replaced
8311:76a44a0d7f4b 8312:6c2119509fac
   110 #         assert not self.https_url or self.https_url[-1] == '/'
   110 #         assert not self.https_url or self.https_url[-1] == '/'
   111         self.url_rewriter = self.appli.vreg['components'].select_or_none('urlrewriter')
   111         self.url_rewriter = self.appli.vreg['components'].select_or_none('urlrewriter')
   112 
   112 
   113     def _render(self, req):
   113     def _render(self, req):
   114         """this function performs the actual rendering
   114         """this function performs the actual rendering
   115         XXX missing: https handling, url rewriting, cache management,
       
   116                      authentication
       
   117         """
   115         """
   118         if self.base_url is None:
   116         if self.base_url is None:
   119             self.base_url = self.config._base_url = req.base_url()
   117             self.base_url = self.config._base_url = req.base_url()
   120         # XXX https handling needs to be implemented
       
   121         if req.authmode == 'http':
       
   122             # activate realm-based auth
       
   123             realm = self.config['realm']
       
   124             req.set_header('WWW-Authenticate', [('Basic', {'realm' : realm })], raw=False)
       
   125         try:
   118         try:
   126             self.appli.connect(req)
   119             path = req.path
   127         except Redirect, ex:
   120             result = self.appli.handle_request(req, path)
   128             return self.redirect(req, ex.location)
       
   129         try:
       
   130             result = self.appli.publish(path, req)
       
   131         except DirectResponse, ex:
   121         except DirectResponse, ex:
   132             return WSGIResponse(200, req, ex.response)
   122             return ex.response
   133         except StatusResponse, ex:
   123         return WSGIResponse(req.status_out, req, result)
   134             return WSGIResponse(ex.status, req, ex.content)
       
   135         except AuthenticationError:  # must be before AuthenticationError
       
   136             return self.request_auth(req)
       
   137         except LogOut:
       
   138             if self.config['auth-mode'] == 'cookie':
       
   139                 # in cookie mode redirecting to the index view is enough :
       
   140                 # either anonymous connection is allowed and the page will
       
   141                 # be displayed or we'll be redirected to the login form
       
   142                 msg = req._('you have been logged out')
       
   143 #                 if req.https:
       
   144 #                     req._base_url =  self.base_url
       
   145 #                     req.https = False
       
   146                 url = req.build_url('view', vid='index', __message=msg)
       
   147                 return self.redirect(req, url)
       
   148             else:
       
   149                 # in http we have to request auth to flush current http auth
       
   150                 # information
       
   151                 return self.request_auth(req, loggedout=True)
       
   152         except Redirect, ex:
       
   153             return self.redirect(req, ex.location)
       
   154         if not result:
       
   155             # no result, something went wrong...
       
   156             self.error('no data (%s)', req)
       
   157             # 500 Internal server error
       
   158             return self.redirect(req, req.build_url('error'))
       
   159         return WSGIResponse(200, req, result)
       
   160 
   124 
   161 
   125 
   162     def __call__(self, environ, start_response):
   126     def __call__(self, environ, start_response):
   163         """WSGI protocol entry point"""
   127         """WSGI protocol entry point"""
   164         req = CubicWebWsgiRequest(environ, self.appli.vreg)
   128         req = CubicWebWsgiRequest(environ, self.appli.vreg)
   165         response = self._render(req)
   129         response = self._render(req)
   166         start_response(response.status, response.headers)
   130         start_response(response.status, response.headers)
   167         return response.body
   131         return response.body
   168 
   132 
   169     def redirect(self, req, location):
       
   170         """convenience function which builds a redirect WSGIResponse"""
       
   171         self.debug('redirecting to %s', location)
       
   172         req.set_header('location', str(location))
       
   173         return WSGIResponse(303, req)
       
   174 
   133 
   175     def request_auth(self, req, loggedout=False):
       
   176         """returns the appropriate WSGIResponse to require the user to log in
       
   177         """
       
   178 #         if self.https_url and req.base_url() != self.https_url:
       
   179 #             return self.redirect(self.https_url + 'login')
       
   180         if self.config['auth-mode'] == 'http':
       
   181             code = 401 # UNAUTHORIZED
       
   182         else:
       
   183             code = 403 # FORBIDDEN
       
   184         if loggedout:
       
   185 #             if req.https:
       
   186 #                 req._base_url =  self.base_url
       
   187 #                 req.https = False
       
   188             content = self.appli.loggedout_content(req)
       
   189         else:
       
   190             content = self.appli.need_login_content(req)
       
   191         return WSGIResponse(code, req, content)
       
   192 
   134 
   193     # these are overridden by set_log_methods below
   135     # these are overridden by set_log_methods below
   194     # only defining here to prevent pylint from complaining
   136     # only defining here to prevent pylint from complaining
   195     info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
   137     info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
   196 
   138