def _write_to_stream(self, response): """Write response body to a write-able stream. .. note: This method assumes that the ``_stream`` attribute is set on the current download. Args: response (~requests.Response): The HTTP response object. Raises: ~google.resumable_media.common.DataCorruption: If the download's checksum doesn't agree with server-computed checksum. """ # `_get_expected_checksum()` may return None even if a checksum was # requested, in which case it will emit an info log _MISSING_CHECKSUM. # If an invalid checksum type is specified, this will raise ValueError. expected_checksum, checksum_object = _helpers._get_expected_checksum( response, self._get_headers, self.media_url, checksum_type=self.checksum) with response: # NOTE: In order to handle compressed streams gracefully, we try # to insert our checksum object into the decompression stream. If # the stream is indeed compressed, this will delegate the checksum # object to the decoder and return a _DoNothingHash here. local_checksum_object = _add_decoder(response.raw, checksum_object) body_iter = response.iter_content( chunk_size=_request_helpers._SINGLE_GET_CHUNK_SIZE, decode_unicode=False) for chunk in body_iter: self._stream.write(chunk) local_checksum_object.update(chunk) if expected_checksum is None: return else: actual_checksum = _helpers.prepare_checksum_digest( checksum_object.digest()) if actual_checksum != expected_checksum: msg = _CHECKSUM_MISMATCH.format( self.media_url, expected_checksum, actual_checksum, checksum_type=self.checksum.upper(), ) raise common.DataCorruption(response, msg)
def test__w_header_missing(self, _LOGGER, checksum): headers = {} response = _mock_response(headers=headers) def _get_headers(response): return response.headers url = "https://example.com/" expected_checksum, checksum_obj = _helpers._get_expected_checksum( response, _get_headers, url, checksum_type=checksum) assert expected_checksum is None assert isinstance(checksum_obj, _helpers._DoNothingHash) expected_msg = _helpers._MISSING_CHECKSUM.format( url, checksum_type=checksum.upper()) _LOGGER.info.assert_called_once_with(expected_msg)
def _write_to_stream(self, response): """Write response body to a write-able stream. .. note: This method assumes that the ``_stream`` attribute is set on the current download. Args: response (~requests.Response): The HTTP response object. Raises: ~google.resumable_media.common.DataCorruption: If the download's checksum doesn't agree with server-computed checksum. """ # `_get_expected_checksum()` may return None even if a checksum was # requested, in which case it will emit an info log _MISSING_CHECKSUM. # If an invalid checksum type is specified, this will raise ValueError. expected_checksum, checksum_object = _helpers._get_expected_checksum( response, self._get_headers, self.media_url, checksum_type=self.checksum) with response: body_iter = response.raw.stream( _request_helpers._SINGLE_GET_CHUNK_SIZE, decode_content=False) for chunk in body_iter: self._stream.write(chunk) checksum_object.update(chunk) response._content_consumed = True if expected_checksum is None: return else: actual_checksum = _helpers.prepare_checksum_digest( checksum_object.digest()) if actual_checksum != expected_checksum: msg = _CHECKSUM_MISMATCH.format( self.media_url, expected_checksum, actual_checksum, checksum_type=self.checksum.upper(), ) raise common.DataCorruption(response, msg)
def test__w_header_present(self, _LOGGER, template, checksum): checksums = {"md5": u"b2twdXNodGhpc2J1dHRvbg==", "crc32c": u"3q2+7w=="} header_value = template.format(checksums["crc32c"], checksums["md5"]) headers = {_helpers._HASH_HEADER: header_value} response = _mock_response(headers=headers) def _get_headers(response): return response.headers url = "https://example.com/" expected_checksum, checksum_obj = _helpers._get_expected_checksum( response, _get_headers, url, checksum_type=checksum) assert expected_checksum == checksums[checksum] checksum_types = { "md5": type(hashlib.md5()), "crc32c": type(_helpers._get_crc32c_object()), } assert isinstance(checksum_obj, checksum_types[checksum]) _LOGGER.info.assert_not_called()