wsgi/handler.py
changeset 5223 6abd6e3599f4
parent 4721 8f63691ccb7f
child 5423 e15abfdcce38
equal deleted inserted replaced
5216:4f4369e63f5e 5223:6abd6e3599f4
     7 """
     7 """
     8 
     8 
     9 __docformat__ = "restructuredtext en"
     9 __docformat__ = "restructuredtext en"
    10 
    10 
    11 from cubicweb import AuthenticationError
    11 from cubicweb import AuthenticationError
    12 from cubicweb.web import (NotFound, Redirect, DirectResponse, StatusResponse,
    12 from cubicweb.web import Redirect, DirectResponse, StatusResponse, LogOut
    13                           ExplicitLogin)
       
    14 from cubicweb.web.application import CubicWebPublisher
    13 from cubicweb.web.application import CubicWebPublisher
    15 from cubicweb.wsgi.request import CubicWebWsgiRequest
    14 from cubicweb.wsgi.request import CubicWebWsgiRequest
    16 
    15 
    17 # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    16 # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    18 STATUS_CODE_TEXT = {
    17 STATUS_CODE_TEXT = {
   111             # activate realm-based auth
   110             # activate realm-based auth
   112             realm = self.config['realm']
   111             realm = self.config['realm']
   113             req.set_header('WWW-Authenticate', [('Basic', {'realm' : realm })], raw=False)
   112             req.set_header('WWW-Authenticate', [('Basic', {'realm' : realm })], raw=False)
   114         try:
   113         try:
   115             self.appli.connect(req)
   114             self.appli.connect(req)
   116         except AuthenticationError:
       
   117             return self.request_auth(req)
       
   118         except Redirect, ex:
   115         except Redirect, ex:
   119             return self.redirect(req, ex.location)
   116             return self.redirect(req, ex.location)
   120         path = req.path
   117         path = req.path
   121         if not path or path == "/":
   118         if not path or path == "/":
   122             path = 'view'
   119             path = 'view'
   124             result = self.appli.publish(path, req)
   121             result = self.appli.publish(path, req)
   125         except DirectResponse, ex:
   122         except DirectResponse, ex:
   126             return WSGIResponse(200, req, ex.response)
   123             return WSGIResponse(200, req, ex.response)
   127         except StatusResponse, ex:
   124         except StatusResponse, ex:
   128             return WSGIResponse(ex.status, req, ex.content)
   125             return WSGIResponse(ex.status, req, ex.content)
   129         except NotFound:
   126         except AuthenticationError:  # must be before AuthenticationError
   130             result = self.appli.notfound_content(req)
       
   131             return WSGIResponse(404, req, result)
       
   132         except ExplicitLogin:  # must be before AuthenticationError
       
   133             return self.request_auth(req)
   127             return self.request_auth(req)
   134         except AuthenticationError:
   128         except LogOut:
   135             if self.config['auth-mode'] == 'cookie':
   129             if self.config['auth-mode'] == 'cookie':
   136                 # in cookie mode redirecting to the index view is enough :
   130                 # in cookie mode redirecting to the index view is enough :
   137                 # either anonymous connection is allowed and the page will
   131                 # either anonymous connection is allowed and the page will
   138                 # be displayed or we'll be redirected to the login form
   132                 # be displayed or we'll be redirected to the login form
   139                 msg = req._('you have been logged out')
   133                 msg = req._('you have been logged out')