class _CookieElement(HeaderElement): #RE_TSPECIALS = re.compile(r'[ \(\)<>@,;:\\"\[\]\?=]') RE_TSPECIALS = re.compile(r'(?!)') def __init__(self, cookie_name, cookie_value, params=None): self.cookie_name = Unicode(cookie_name) self.cookie_value = Unicode(cookie_value) super(_CookieElement, self).__init__(self.value, params) @classmethod def parse(cls, elementstr): value, params = cls.parseparams(elementstr) cookie_name, cookie_value, __ = cls.parseparam(value) return cls(cookie_name, cookie_value, params) @classmethod def unescape_key(cls, key): key = key.strip() if key.lower() in ('httponly', 'secure', 'path', 'domain', 'max-age', 'expires'): return key.lower() return key @property def value(self): return b'%s=%s' % (self.cookie_name.encode('ISO8859-1'), self.cookie_value.encode('ISO8859-1')) @value.setter def value(self, value): self.cookie_name, self.cookie_value, __ = self.parseparam(value)
class _CookieElement(HeaderElement): #RE_TSPECIALS = re.compile(br'[ \(\)<>@,;:\\"\[\]\?=]') RE_TSPECIALS = re.compile(br'(?!)') def __init__(self, cookie_name, cookie_value, params=None): self.cookie_name = Unicode(cookie_name) self.cookie_value = Unicode(cookie_value) super(_CookieElement, self).__init__(self.value, params) @classmethod def parse(cls, elementstr): value, params = cls.parseparams(elementstr) cookie_name, cookie_value, __ = cls.parseparam(value) return cls(cookie_name, cookie_value, params) @classmethod def unescape_key(cls, key): key = key.strip() if key.lower() in ('httponly', 'secure', 'path', 'domain', 'max-age', 'expires'): return key.lower() return key @property def value(self): return b'%s=%s' % (self.cookie_name.encode('ISO8859-1'), self.cookie_value.encode('ISO8859-1')) @value.setter def value(self, value): self.cookie_name, self.cookie_value, __ = self.parseparam(value)
def set_body_content_encoding(self): if 'Content-Encoding' in self.message.headers: try: self.message.body.content_encoding = self.message.headers.element('Content-Encoding') self.message.body.content_encoding.codec except Invalid as exc: raise NOT_IMPLEMENTED(Unicode(exc))
def validate_request_uri_scheme(self): if self.message.uri.scheme: if self.message.uri.scheme not in ('http', 'https'): exc = InvalidURI(_(u'Invalid URL: wrong scheme')) raise BAD_REQUEST(Unicode(exc)) else: self.message.uri.scheme = self._default_scheme self.message.uri.host = self._default_host self.message.uri.port = self._default_port
def __parse_chunk_size(self): line, rest_chunk = self.buffer.split(self.line_end, 1) chunk_size = line.split(b";", 1)[0].strip() try: chunk_size = int(bytes(chunk_size), 16) if chunk_size < 0: raise ValueError except (ValueError, OverflowError): exc = InvalidHeader(_(u'Invalid chunk size: %r'), chunk_size.decode('ISO8859-1')) raise BAD_REQUEST(Unicode(exc)) else: return chunk_size, rest_chunk
def parse(self, data): u"""Appends the given data to the internal buffer and parses it as HTTP Request-Messages. :param data: data to parse :type data: bytes """ self.buffer.extend(data) try: return tuple(x for x in self._parse() if x is not None) except (InvalidHeader, InvalidLine, InvalidURI) as exc: raise BAD_REQUEST(Unicode(exc))
def parse_trailers(self): # TODO: the code is exactly the same as parse_headers but # we have to make sure no invalid header fields are send (only values told in Trailer header allowed) if self.buffer.startswith(self.line_end): self.buffer = self.buffer[len(self.line_end):] return False # no trailers trailer_end = self.line_end + self.line_end if trailer_end not in self.buffer: # not received yet return NOT_RECEIVED_YET trailers, self.buffer = self.buffer.split(trailer_end, 1) self.trailers = Headers() try: self.trailers.parse(bytes(trailers)) except InvalidHeader as exc: exc = InvalidHeader(_(u'Invalid trailers: %r'), Unicode(exc)) raise BAD_REQUEST(Unicode(exc)) self.merge_trailer_into_header() return False
def parse_startline(self): if CRLF not in self.buffer: if LF not in self.buffer: return NOT_RECEIVED_YET self.line_end = LF requestline, self.buffer = self.buffer.split(self.line_end, 1) # parse request line try: self.message.parse(bytes(requestline)) except (InvalidLine, InvalidURI) as exc: raise BAD_REQUEST(Unicode(exc))
def parse_headers(self): # empty headers? if self.buffer.startswith(self.line_end): self.buffer = self.buffer[len(self.line_end):] return False header_end = self.line_end + self.line_end if header_end not in self.buffer: # headers incomplete return NOT_RECEIVED_YET headers, self.buffer = self.buffer.split(header_end, 1) # parse headers if headers: try: self.message.headers.parse(bytes(headers)) except InvalidHeader as exc: raise BAD_REQUEST(Unicode(exc))
def path_segments(self): return [Unicode.replace(p, u'%2f', u'/') for p in self.path.split(u'/')]
def __init__(self, cookie_name, cookie_value, params=None): self.cookie_name = Unicode(cookie_name) self.cookie_value = Unicode(cookie_value) super(_CookieElement, self).__init__(self.value, params)
def quote(self, data, charset): return Percent.quote(Unicode(data).encode(self.encoding), charset)
def __eq__(self, other): if isinstance(other, Unicode): return Unicode(self) == other return bytes(self) == other