def _parse_request_heading(stream, scheme=None): beginning = stream.point try: with stream: method_ = Method(stream.consume_regex(rfc7230.method)) stream.consume_regex(SP) target = stream.consume_regex(b'[^\\s]+', u'request target') stream.consume_regex(SP) version_ = HTTPVersion(stream.consume_regex(rfc7230.HTTP_version)) _parse_line_ending(stream) entries = parse_header_fields(stream) except ParseError as e: stream.sane = False stream.complain(1006, error=e) return Unavailable else: req = Request(scheme, method_, target, version_, entries, body=None, remark=u'from %s, offset %d' % (stream.name, beginning)) stream.dump_complaints(req.complain, place=u'request heading') return req
def _parse_response_heading(req, stream): beginning = stream.point try: with stream: version_ = HTTPVersion(stream.consume_regex(rfc7230.HTTP_version)) stream.consume_regex(SP) status = StatusCode(stream.consume_regex(rfc7230.status_code)) stream.consume_regex(SP) reason = stream.consume_regex(rfc7230.reason_phrase) _parse_line_ending(stream) entries = parse_header_fields(stream) except ParseError as e: stream.complain(1009, error=e) stream.sane = False return Unavailable else: resp = Response(version_, status, reason, entries, body=None, remark=u'from %s, offset %d' % (stream.name, beginning)) resp.request = req stream.dump_complaints(resp.complain, place=u'response heading') return resp
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_request_heading(stream, scheme=None): beginning = stream.tell() with stream.parsing(request_line): line = stream.readline() pieces = line.split(u' ') if len(pieces) != 3 or not HTTP_VERSION.match(pieces[2]): raise stream.error(beginning) method = Method(pieces[0]) target = pieces[1] version_ = HTTPVersion(pieces[2]) entries = parse_header_fields(stream) with stream.parsing(HTTP_message): stream.readlineend() req = Request(scheme, method, target, version_, entries, body=None, remark=u'from %s, offset %d' % (stream.name, beginning)) stream.dump_complaints(req.complain, place=u'request heading') return req
def _parse_response_heading(req, stream): beginning = stream.tell() with stream.parsing(status_line): line = stream.readline() pieces = line.split(u' ', 2) if len(pieces) != 3 or \ not HTTP_VERSION.match(pieces[0]) or \ not STATUS_CODE.match(pieces[1]): raise stream.error(beginning) version_ = HTTPVersion(pieces[0]) status = StatusCode(pieces[1]) reason = pieces[2] entries = parse_header_fields(stream) with stream.parsing(HTTP_message): stream.readlineend() resp = Response( version_, status, reason, entries, body=None, remark=u'from %s, offset %d' % (stream.name, beginning)) resp.request = req stream.dump_complaints(resp.complain, place=u'response heading') return resp