wsgi/request.py
branchtls-sprint
changeset 1802 d628defebc17
parent 0 b97547f5f1fa
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
     3 NOTE: each docstring tagged with ``COME FROM DJANGO`` means that
     3 NOTE: each docstring tagged with ``COME FROM DJANGO`` means that
     4 the code has been taken (or adapted) from Djanco source code :
     4 the code has been taken (or adapted) from Djanco source code :
     5   http://www.djangoproject.com/
     5   http://www.djangoproject.com/
     6 
     6 
     7 :organization: Logilab
     7 :organization: Logilab
     8 :copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     8 :copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     9 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     9 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
    10 """
    10 """
    11 
    11 
    12 __docformat__ = "restructuredtext en"
    12 __docformat__ = "restructuredtext en"
    13 
    13 
    23 
    23 
    24 
    24 
    25 class CubicWebWsgiRequest(CubicWebRequestBase):
    25 class CubicWebWsgiRequest(CubicWebRequestBase):
    26     """most of this code COMES FROM DJANO
    26     """most of this code COMES FROM DJANO
    27     """
    27     """
    28     
    28 
    29     def __init__(self, environ, vreg, base_url=None):
    29     def __init__(self, environ, vreg, base_url=None):
    30         self.environ = environ
    30         self.environ = environ
    31         self.path = environ['PATH_INFO']
    31         self.path = environ['PATH_INFO']
    32         self.method = environ['REQUEST_METHOD'].upper()
    32         self.method = environ['REQUEST_METHOD'].upper()
    33         self._headers = dict([(normalize_header(k[5:]), v) for k, v in self.environ.items()
    33         self._headers = dict([(normalize_header(k[5:]), v) for k, v in self.environ.items()
    40             for fdef in files.itervalues():
    40             for fdef in files.itervalues():
    41                 fdef[0] = unicode(fdef[0], self.encoding)
    41                 fdef[0] = unicode(fdef[0], self.encoding)
    42             self.form.update(files)
    42             self.form.update(files)
    43         # prepare output headers
    43         # prepare output headers
    44         self.headers_out = {}
    44         self.headers_out = {}
    45         
    45 
    46     def __repr__(self):
    46     def __repr__(self):
    47         # Since this is called as part of error handling, we need to be very
    47         # Since this is called as part of error handling, we need to be very
    48         # robust against potentially malformed input.
    48         # robust against potentially malformed input.
    49         form = pformat(self.form)
    49         form = pformat(self.form)
    50         meta = pformat(self.environ)
    50         meta = pformat(self.environ)
    51         return '<CubicWebWsgiRequest\FORM:%s,\nMETA:%s>' % \
    51         return '<CubicWebWsgiRequest\FORM:%s,\nMETA:%s>' % \
    52             (form, meta)
    52             (form, meta)
    53 
    53 
    54     ## cubicweb request interface ################################################
    54     ## cubicweb request interface ################################################
    55     
    55 
    56     def base_url(self):
    56     def base_url(self):
    57         return self._base_url
    57         return self._base_url
    58 
    58 
    59     def http_method(self):
    59     def http_method(self):
    60         """returns 'POST', 'GET', 'HEAD', etc."""
    60         """returns 'POST', 'GET', 'HEAD', etc."""
    61         return self.method
    61         return self.method
    62     
    62 
    63     def relative_path(self, includeparams=True):
    63     def relative_path(self, includeparams=True):
    64         """return the normalized path of the request (ie at least relative
    64         """return the normalized path of the request (ie at least relative
    65         to the application's root, but some other normalization may be needed
    65         to the application's root, but some other normalization may be needed
    66         so that the returned path may be used to compare to generated urls
    66         so that the returned path may be used to compare to generated urls
    67 
    67 
    72         path = path[1:] # remove leading '/'
    72         path = path[1:] # remove leading '/'
    73         if includeparams:
    73         if includeparams:
    74             qs = self.environ.get('QUERY_STRING')
    74             qs = self.environ.get('QUERY_STRING')
    75             if qs:
    75             if qs:
    76                 return '%s?%s' % (path, qs)
    76                 return '%s?%s' % (path, qs)
    77         
    77 
    78         return path
    78         return path
    79 
    79 
    80     def get_header(self, header, default=None):
    80     def get_header(self, header, default=None):
    81         """return the value associated with the given input HTTP header,
    81         """return the value associated with the given input HTTP header,
    82         raise KeyError if the header is not set
    82         raise KeyError if the header is not set
    83         """
    83         """
    84         return self._headers.get(normalize_header(header), default)
    84         return self._headers.get(normalize_header(header), default)
    85     
    85 
    86     def set_header(self, header, value, raw=True):
    86     def set_header(self, header, value, raw=True):
    87         """set an output HTTP header"""
    87         """set an output HTTP header"""
    88         assert raw, "don't know anything about non-raw headers for wsgi requests"
    88         assert raw, "don't know anything about non-raw headers for wsgi requests"
    89         self.headers_out[header] = value
    89         self.headers_out[header] = value
    90 
    90 
    91     def add_header(self, header, value):
    91     def add_header(self, header, value):
    92         """add an output HTTP header"""
    92         """add an output HTTP header"""
    93         self.headers_out[header] = value
    93         self.headers_out[header] = value
    94     
    94 
    95     def remove_header(self, header):
    95     def remove_header(self, header):
    96         """remove an output HTTP header"""
    96         """remove an output HTTP header"""
    97         self.headers_out.pop(header, None)
    97         self.headers_out.pop(header, None)
    98 
    98 
    99     def header_if_modified_since(self):
    99     def header_if_modified_since(self):
   100         """If the HTTP header If-modified-since is set, return the equivalent
   100         """If the HTTP header If-modified-since is set, return the equivalent
   101         mx date time value (GMT), else return None
   101         mx date time value (GMT), else return None
   102         """
   102         """
   103         return None
   103         return None
   104         
   104 
   105     ## wsgi request helpers ###################################################
   105     ## wsgi request helpers ###################################################
   106     
   106 
   107     def application_uri(self):
   107     def application_uri(self):
   108         """Return the application's base URI (no PATH_INFO or QUERY_STRING)
   108         """Return the application's base URI (no PATH_INFO or QUERY_STRING)
   109 
   109 
   110         see python2.5's wsgiref.util.application_uri code
   110         see python2.5's wsgiref.util.application_uri code
   111         """
   111         """
   121             else:
   121             else:
   122                 if environ['SERVER_PORT'] != '80':
   122                 if environ['SERVER_PORT'] != '80':
   123                     url += ':' + environ['SERVER_PORT']
   123                     url += ':' + environ['SERVER_PORT']
   124         url += quote(environ.get('SCRIPT_NAME') or '/')
   124         url += quote(environ.get('SCRIPT_NAME') or '/')
   125         return url
   125         return url
   126         
   126 
   127     def get_full_path(self):
   127     def get_full_path(self):
   128         return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + self.environ.get('QUERY_STRING', '')) or '')
   128         return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + self.environ.get('QUERY_STRING', '')) or '')
   129 
   129 
   130     def is_secure(self):
   130     def is_secure(self):
   131         return 'wsgi.url_scheme' in self.environ \
   131         return 'wsgi.url_scheme' in self.environ \