wsgi/handler.py
branchtls-sprint
changeset 1802 d628defebc17
parent 0 b97547f5f1fa
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
     1 """WSGI request handler for cubicweb
     1 """WSGI request handler for cubicweb
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 
     7 
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
     9 
     9 
    72         else:
    72         else:
    73             self.body = []
    73             self.body = []
    74 
    74 
    75     def __iter__(self):
    75     def __iter__(self):
    76         return iter(self.body)
    76         return iter(self.body)
    77     
    77 
    78 
    78 
    79 
    79 
    80 class CubicWebWSGIApplication(object):
    80 class CubicWebWSGIApplication(object):
    81     """This is the wsgi application which will be called by the
    81     """This is the wsgi application which will be called by the
    82     wsgi server with the WSGI ``environ`` and ``start_response``
    82     wsgi server with the WSGI ``environ`` and ``start_response``
    98 #         assert not self.https_url or self.https_url[-1] == '/'
    98 #         assert not self.https_url or self.https_url[-1] == '/'
    99         try:
    99         try:
   100             self.url_rewriter = self.appli.vreg.select_component('urlrewriter')
   100             self.url_rewriter = self.appli.vreg.select_component('urlrewriter')
   101         except ObjectNotFound:
   101         except ObjectNotFound:
   102             self.url_rewriter = None
   102             self.url_rewriter = None
   103         
   103 
   104     def _render(self, req):
   104     def _render(self, req):
   105         """this function performs the actual rendering
   105         """this function performs the actual rendering
   106         XXX missing: https handling, url rewriting, cache management,
   106         XXX missing: https handling, url rewriting, cache management,
   107                      authentication
   107                      authentication
   108         """
   108         """
   154             # no result, something went wrong...
   154             # no result, something went wrong...
   155             self.error('no data (%s)', req)
   155             self.error('no data (%s)', req)
   156             # 500 Internal server error
   156             # 500 Internal server error
   157             return self.redirect(req, req.build_url('error'))
   157             return self.redirect(req, req.build_url('error'))
   158         return WSGIResponse(200, req, result)
   158         return WSGIResponse(200, req, result)
   159         
   159 
   160     
   160 
   161     def __call__(self, environ, start_response):
   161     def __call__(self, environ, start_response):
   162         """WSGI protocol entry point"""
   162         """WSGI protocol entry point"""
   163         req = CubicWebWsgiRequest(environ, self.appli.vreg, self.base_url)
   163         req = CubicWebWsgiRequest(environ, self.appli.vreg, self.base_url)
   164         response = self._render(req)
   164         response = self._render(req)
   165         start_response(response.status, response.headers)
   165         start_response(response.status, response.headers)
   168     def redirect(self, req, location):
   168     def redirect(self, req, location):
   169         """convenience function which builds a redirect WSGIResponse"""
   169         """convenience function which builds a redirect WSGIResponse"""
   170         self.debug('redirecting to %s', location)
   170         self.debug('redirecting to %s', location)
   171         req.set_header('location', str(location))
   171         req.set_header('location', str(location))
   172         return WSGIResponse(303, req)
   172         return WSGIResponse(303, req)
   173         
   173 
   174     def request_auth(self, req, loggedout=False):
   174     def request_auth(self, req, loggedout=False):
   175         """returns the appropriate WSGIResponse to require the user to log in
   175         """returns the appropriate WSGIResponse to require the user to log in
   176         """
   176         """
   177 #         if self.https_url and req.base_url() != self.https_url:
   177 #         if self.https_url and req.base_url() != self.https_url:
   178 #             return self.redirect(self.https_url + 'login')
   178 #             return self.redirect(self.https_url + 'login')