def range(self): try: value = self.env['HTTP_RANGE'] if '=' in value: unit, sep, req_range = value.partition('=') else: msg = "The value must be prefixed with a" + \ " range unit, e.g. 'bytes='" raise HTTPInvalidHeader('Range', msg) except KeyError: return None if ',' in req_range: msg = 'The value must be a continuous range.' raise HTTPInvalidHeader('Range', msg) try: first, sep, last = req_range.partition('-') if not sep: raise ValueError() if first: return (int(first), int(last or -1)) elif last: return (-int(last), -1) else: msg = 'The range offsets are missing.' raise HTTPInvalidHeader(msg, 'Range') except ValueError: msg = ('Range must be formatted according to RFC 7233.') raise HTTPInvalidHeader('Range', msg)
def content_length(self): # CONTENT_LENGTH try/catch cheaper and faster try: length = int(self.env['CONTENT_LENGTH']) if length < 0: raise HTTPInvalidHeader('Content-Length', 'Negative Length not allowed.') return length except KeyError: return 0 except ValueError: raise HTTPInvalidHeader('Content-Length', 'Not an integer')
def get_header_as_datetime(self, header, required=False, obs_date=False): """Return an HTTP header with HTTP-Date values as a datetime. Args: name (str): Header name, case-insensitive (e.g., 'Date') Keyword Args: required (bool): raise HTTPBadRequest (default False) obs_date (bool): Support obs-date formats according to RFC 7231 (e.g. Sunday, 06-Nov-94 08:49:37 GMT) (default False). Returns: datetime: The value of the specified header if it exists, or None if the header is not found and is not required. Raises: HTTPBadRequest: The header was not found in the request, but it was required. HttpInvalidHeader: The header contained a malformed/invalid value. """ try: http_date = self.get_header(header, required=required) if http_date is not None: return to_gmt(http_date, src=TimezoneGMT()) except TypeError: return None except ValueError: msg = "It must be formatted according to RFC 7231, Section 7.1.1.1" raise HTTPInvalidHeader(header, msg)
def range_unit(self): try: value = self.env['HTTP_RANGE'] if '=' in value: unit, sep, req_range = value.partition('=') return unit else: msg = "The value must be prefixed with a" + \ " range unit, e.g. 'bytes='" raise HTTPInvalidHeader('Range', msg) except KeyError: return None