def __init__(self, version, header_entries, body, trailer_entries=None, remark=None): super(Message, self).__init__() self.version = (HTTPVersion(force_unicode(version)) if version is not None else None) self.header_entries = [HeaderEntry(k, v) for k, v in header_entries] self.body = bytes(body) if okay(body) else body self.trailer_entries = [HeaderEntry(k, v) for k, v in trailer_entries or []] self.rebuild_headers() self.annotations = {} self.remark = remark
def parse_header_fields(stream): """Parse a block of HTTP/1.x header fields. :param stream: The :class:`~httpolice.parse.Stream` from which to parse. :return: A list of :class:`HeaderEntry`. :raises: :class:`ParseError` """ entries = [] while True: name = stream.maybe_consume_regex(rfc7230.field_name) if name is None: break stream.consume_regex(b':') stream.consume_regex(rfc7230.OWS) vs = [] while True: v = stream.maybe_consume_regex(rfc7230.field_content) if v is None: if stream.maybe_consume_regex(rfc7230.obs_fold): stream.complain(1016) vs.append(b' ') else: break else: vs.append(v.encode('iso-8859-1')) # back to bytes value = b''.join(vs) stream.consume_regex(rfc7230.OWS) _parse_line_ending(stream) entries.append(HeaderEntry(FieldName(name), value)) _parse_line_ending(stream) return entries
def parse_header_fields(stream): """Parse a block of HTTP/1.x header fields. :param stream: The :class:`~httpolice.stream.Stream` from which to parse. :return: A list of :class:`HeaderEntry`. :raises: :class:`ParseError` """ entries = [] while stream.peek() not in [b'\r', b'\n', b'']: with stream.parsing(header_field): pos = stream.tell() line = stream.readline(decode=False) (name, colon, v) = line.partition(b':') if not colon: raise stream.error(pos) vs = [v] while stream.peek() in [b' ', b'\t']: stream.complain(1016) vs.append(b' ' + stream.readline(decode=False).lstrip(b' \t')) name = FieldName(name.decode('iso-8859-1')) value = b''.join(vs).strip(b' \t') entries.append(HeaderEntry(name, value)) return entries