Commit 671854ac authored by Hilal Ozdemir's avatar Hilal Ozdemir

Add checks for response status code and contents

parent 4667f5c2
......@@ -89,7 +89,10 @@ async def async_write_file_stream(url: str, save_path: str, queue=None, # pylint
async with async_timeout.timeout(timeout):
async with session.request(method, url, json=json,
data=data, headers=headers) as resp:
if resp.status >= 400:
raise Exception("Failed to write stream to file. code: %d"%resp.status)
if not resp.content:
raise Exception("Failed to write stream to file. Response does not have a body.")
with open(save_path, 'wb')as file:
async for chunk in input_reader(resp.content):
file.write(chunk)
......
......@@ -601,23 +601,29 @@ class DockerUtil: # pylint: disable=too-many-instance-attributes
resp, _ = await async_req(url=url, return_json=False, timeout=self.TIMEOUT,
retry=self.RETRY, method='HEAD')
if resp.status == 401 or resp.status == 200:
if resp.status == 401:
requirements = await self.get_auth_requirements(resp.headers, **kwargs)
if resp.status == 401:
requirements = await self.get_auth_requirements(resp.headers, **kwargs)
# HEAD request for get size
resp, _ = await async_req(url=url, return_json=False, timeout=self.TIMEOUT,
retry=self.RETRY, method='HEAD', Authorization=requirements)
layer_size = int(resp.headers.get('content-length'))
retry=self.RETRY, method='HEAD', Authorization=requirements)
if resp.status >= 400:
raise DockerUtil.LayerDownloadFailed("Failed to download layer from %s. code: %d"%(url, resp.status))
if not resp.content:
raise DockerUtil.LayerDownloadFailed("Failed to download layer from %s. Response does not have a body."%url)
if not resp.headers.get('content-length'):
raise DockerUtil.LayerDownloadFailed("Failed to download layer from %s. Response headers do not have content-length."%url)
self.queues[jobid][digest]['size'] = layer_size
self.queues[jobid][digest]['status'] = self.DL_GZ_DOWNLOADING
layer_size = int(resp.headers.get('content-length'))
resp = await async_write_file_stream(url, save_path, timeout=self.TIMEOUT_DL_LAYER + \
self.get_additional_time_downlaod(layer_size),
retry=self.RETRY,
queue=self.queues[jobid][digest]['queue'],
Authorization=requirements)
self.queues[jobid][digest]['size'] = layer_size
self.queues[jobid][digest]['status'] = self.DL_GZ_DOWNLOADING
resp = await async_write_file_stream(url, save_path, timeout=self.TIMEOUT_DL_LAYER + \
self.get_additional_time_downlaod(layer_size),
retry=self.RETRY,
queue=self.queues[jobid][digest]['queue'],
Authorization=requirements)
if resp.status != 200:
raise DockerUtil.LayerDownloadFailed("Failed to download layer. code: %d" % resp.status)
......@@ -640,6 +646,13 @@ class DockerUtil: # pylint: disable=too-many-instance-attributes
# HEAD request to get size
resp, _ = await async_req(url=url, return_json=False, timeout=self.TIMEOUT,
retry=self.RETRY, method='HEAD')
if resp.status >= 400:
raise DockerUtil.LayerDownloadFailed("Failed to download layer from %s. code: %d"%(url, resp.status))
if not resp.content:
raise DockerUtil.LayerDownloadFailed("Failed to download layer from %s. Response does not have a body."%url)
if not resp.headers.get('content-length'):
raise DockerUtil.LayerDownloadFailed("Failed to download layer from %s. Response headers do not have content-length."%url)
layer_size = int(resp.headers.get('content-length'))
self.queues[jobid][digest]['size'] = layer_size
......@@ -649,6 +662,8 @@ class DockerUtil: # pylint: disable=too-many-instance-attributes
self.get_additional_time_downlaod(layer_size),
retry=self.RETRY,
queue=self.queues[jobid][digest]['queue'])
if resp.status != 200:
raise DockerUtil.LayerDownloadFailed("Failed to download layer. code: %d" % resp.status)
self.logger.debug("downloaded layer %s to %s", digest, save_path)
self.queues[jobid][digest]['status'] = self.DL_FINISH
......@@ -936,8 +951,12 @@ class DockerUtil: # pylint: disable=too-many-instance-attributes
# get manifest
manifest = await self.fetch_docker_image_manifest(ref['domain'], ref['repo'],
try:
manifest = await self.fetch_docker_image_manifest(ref['domain'], ref['repo'],
manifest_digest)
except DockerUtil.FetchManifestFailed as e:
raise DockerUtil.ManifestError(str(e))
schema_v = manifest['schemaVersion']
if schema_v == 1:
......@@ -1054,8 +1073,11 @@ class DockerUtil: # pylint: disable=too-many-instance-attributes
ref = normalize_ref(tag, index=True)
# get manifest
manifest = await self.fetch_docker_image_manifest(
ref['domain'], ref['repo'], ref['suffix'])
try:
manifest = await self.fetch_docker_image_manifest(
ref['domain'], ref['repo'], ref['suffix'])
except DockerUtil.FetchManifestFailed as e:
raise DockerUtil.ManifestError(str(e))
schema_v = manifest['schemaVersion']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment