def done(self) -> None: if not self.response.complete: if self.response.http_error: problem = self.response.http_error.desc else: problem = "" self.add_base_note("", RANGE_SUBREQ_PROBLEM, problem=problem) return if self.response.status_code == "206": c_e = "content-encoding" if ("gzip" in self.base.response.parsed_headers.get(c_e, []) == "gzip" not in self.response.parsed_headers.get(c_e, [])): self.add_base_note( "header-accept-ranges header-content-encoding", RANGE_NEG_MISMATCH) return self.check_missing_hdrs( [ "date", "cache-control", "content-location", "etag", "expires", "vary", ], MISSING_HDRS_206, ) if self.response.parsed_headers.get( "etag", None) == self.base.response.parsed_headers.get( "etag", None): if self.response.payload == self.range_target: self.base.partial_support = True self.add_base_note("header-accept-ranges", RANGE_CORRECT) else: # the body samples are just bags of bits self.base.partial_support = False self.add_base_note( "header-accept-ranges", RANGE_INCORRECT, range="bytes=%s-%s" % (self.range_start, self.range_end), range_expected=display_bytes(self.range_target), range_expected_bytes=f_num(len(self.range_target)), range_received=display_bytes(self.response.payload), range_received_bytes=f_num(self.response.payload_len), ) else: self.add_base_note("header-accept-ranges", RANGE_CHANGED) elif self.response.status_code == self.base.response.status_code: self.base.partial_support = False self.add_base_note("header-accept-ranges", RANGE_FULL) else: self.add_base_note( "header-accept-ranges", RANGE_STATUS, range_status=self.response.status_code, enc_range_status=self.response.status_code or "(unknown)", )
def done(self) -> None: if not self.response.complete: if self.response.http_error: problem = self.response.http_error.desc else: problem = "" self.add_base_note('', RANGE_SUBREQ_PROBLEM, problem=problem) return if self.response.status_code == '206': c_e = 'content-encoding' if 'gzip' in self.base.response.parsed_headers.get(c_e, []) == \ 'gzip' not in self.response.parsed_headers.get(c_e, []): self.add_base_note( 'header-accept-ranges header-content-encoding', RANGE_NEG_MISMATCH) return self.check_missing_hdrs([ 'date', 'cache-control', 'content-location', 'etag', 'expires', 'vary' ], MISSING_HDRS_206) if self.response.parsed_headers.get('etag', None) == \ self.base.response.parsed_headers.get('etag', None): if self.response.payload == self.range_target: self.base.partial_support = True self.add_base_note('header-accept-ranges', RANGE_CORRECT) else: # the body samples are just bags of bits self.base.partial_support = False self.add_base_note( 'header-accept-ranges', RANGE_INCORRECT, range="bytes=%s-%s" % (self.range_start, self.range_end), range_expected=display_bytes(self.range_target), range_expected_bytes=f_num(len(self.range_target)), range_received=display_bytes(self.response.payload), range_received_bytes=f_num(self.response.payload_len)) else: self.add_base_note('header-accept-ranges', RANGE_CHANGED) # TODO: address 416 directly elif self.response.status_code == self.base.response.status_code: self.base.partial_support = False self.add_base_note('header-accept-ranges', RANGE_FULL) else: self.add_base_note('header-accept-ranges', RANGE_STATUS, range_status=self.response.status_code, enc_range_status=self.response.status_code or '(unknown)')
def done(self) -> None: if not self.response.complete: if self.response.http_error: problem = self.response.http_error.desc else: problem = "" self.add_base_note('', RANGE_SUBREQ_PROBLEM, problem=problem) return if self.response.status_code == '206': c_e = 'content-encoding' if 'gzip' in self.base.response.parsed_headers.get(c_e, []) == \ 'gzip' not in self.response.parsed_headers.get(c_e, []): self.add_base_note('header-accept-ranges header-content-encoding', RANGE_NEG_MISMATCH) return self.check_missing_hdrs(['date', 'cache-control', 'content-location', 'etag', 'expires', 'vary'], MISSING_HDRS_206) if self.response.parsed_headers.get('etag', None) == \ self.base.response.parsed_headers.get('etag', None): if self.response.payload == self.range_target: self.base.partial_support = True self.add_base_note('header-accept-ranges', RANGE_CORRECT) else: # the body samples are just bags of bits self.base.partial_support = False self.add_base_note('header-accept-ranges', RANGE_INCORRECT, range="bytes=%s-%s" % (self.range_start, self.range_end), range_expected=display_bytes(self.range_target), range_expected_bytes=f_num(len(self.range_target)), range_received=display_bytes(self.response.payload), range_received_bytes=f_num(self.response.payload_len)) else: self.add_base_note('header-accept-ranges', RANGE_CHANGED) # TODO: address 416 directly elif self.response.status_code == self.base.response.status_code: self.base.partial_support = False self.add_base_note('header-accept-ranges', RANGE_FULL) else: self.add_base_note('header-accept-ranges', RANGE_STATUS, range_status=self.response.status_code, enc_range_status=self.response.status_code or '(unknown)')
def _process_content_codings(self, chunk: bytes) -> bytes: """ Decode a chunk according to the message's content-encoding header. Currently supports gzip. """ content_codings = self.parsed_headers.get("content-encoding", []) content_codings.reverse() for coding in content_codings: if coding in ["gzip", "x-gzip"] and self._decode_ok: if not self._in_gzip_body: self._gzip_header_buffer += chunk try: chunk = self._read_gzip_header( self._gzip_header_buffer) self._in_gzip_body = True except IndexError: return b"" # not a full header yet except IOError as gzip_error: self.add_note( "header-content-encoding", BAD_GZIP, gzip_error=str(gzip_error), ) self._decode_ok = False return b"" try: chunk = self._gzip_processor.decompress(chunk) except zlib.error as zlib_error: self.add_note( "header-content-encoding", BAD_ZLIB, zlib_error=str(zlib_error), ok_zlib_len=f_num(self.payload_len), chunk_sample=display_bytes(chunk), ) self._decode_ok = False return b"" else: # we can't handle other codecs, so punt on body processing. self._decode_ok = False return b"" self._md5_post_processor.update(chunk) self.decoded_len += len(chunk) return chunk
def _process_content_codings(self, chunk: bytes) -> bytes: """ Decode a chunk according to the message's content-encoding header. Currently supports gzip. """ content_codings = self.parsed_headers.get('content-encoding', []) content_codings.reverse() for coding in content_codings: if coding in ['gzip', 'x-gzip'] and self._decode_ok: if not self._in_gzip_body: self._gzip_header_buffer += chunk try: chunk = self._read_gzip_header(self._gzip_header_buffer) self._in_gzip_body = True except IndexError: return b'' # not a full header yet except IOError as gzip_error: self.add_note('header-content-encoding', BAD_GZIP, gzip_error=str(gzip_error)) self._decode_ok = False return b'' try: chunk = self._gzip_processor.decompress(chunk) except zlib.error as zlib_error: self.add_note( 'header-content-encoding', BAD_ZLIB, zlib_error=str(zlib_error), ok_zlib_len=f_num(self.payload_len), chunk_sample=display_bytes(chunk) ) self._decode_ok = False return b'' else: # we can't handle other codecs, so punt on body processing. self._decode_ok = False return b'' self._md5_post_processor.update(chunk) self.decoded_len += len(chunk) return chunk