def response(self, conn, src, dst): keep_alive = True resp = None conn = conn.copy() start_line = yield while keep_alive and start_line is not None: body = "" headers = MultiOrderedDict() try: resp = httputil.ResponseStartLine(*start_line.split()) except ValueError: if start_line != "": self.log("Error: Malformed response start line: '{}'", start_line) start_line = yield continue while True: header_line = yield if header_line is None: self.log("Warning: Terminated early?") return if not header_line.strip(): break self.parse_header_line(headers, header_line.strip()) if resp.version != "RTSP/1.0": self.log("Unknown version! '{}'", resp.version) if "content-length" in headers: try: content_length = int(headers.last("content-length")) except ValueError: content_length = None self.log("Warning: invalid content length '{}'", headers.last('content-length')) else: content_length = 0 if header_line is not None: conn["lbl_disable"](dst) while len(body) < content_length or content_length is None: data = yield if data is None: break body += data if "content-encoding" in headers: encoding = headers.last("content-encoding") if encoding in self.ENCODERS: body = self.ENCODERS[encoding](body) conn["lbl_enable"](dst) conn["rtsp_headers"] = headers conn["rtsp_response"] = resp start_line = yield self.bubble(dst, conn, body)
def request(self, conn, src, dst): keep_alive = True req = None conn = conn.copy() req_line = yield while keep_alive and req_line is not None: body = "" headers = MultiOrderedDict() try: req = httputil.parse_request_start_line(req_line.strip()) except httputil.HTTPInputError: if req_line != "": self.log("HTTP Error: Malformed request start line: '{}'", req_line) req_line = yield continue while True: header_line = yield if header_line is None: break if not header_line.strip(): break self.parse_header_line(headers, header_line.strip()) if req.version == "HTTP/1.0": keep_alive = headers.last("connection", "").lower().strip() == "keep-alive" else: keep_alive = headers.last("connection", "").lower().strip() != "close" if "content-length" in headers: try: content_length = int(headers.last("content-length")) except ValueError: content_length = None else: content_length = None if req.method != "POST": content_length = content_length or 0 if header_line is not None: #body += conn["lbl_buffers"][dst] #conn["lbl_buffers"][dst] = "" conn["lbl_disable"](dst) while len(body) < content_length or content_length is None: data = yield if data is None: break body += data if "content-encoding" in headers: conn["http_decoded"] = False encoding = headers.last("content-encoding") self.log("req encoding: {}", encoding) if encoding in self.DECODERS: try: body = self.DECODERS[encoding](body) conn["http_decoded"] = True except: self.log("Unable to decode content '{}' len={}/{}", encoding, len(body), content_length) else: conn["http_decoded"] = True conn["lbl_enable"](dst) conn["http_headers"] = headers conn["http_request"] = req req_line = yield self.bubble(dst, conn, body)