v2/blob: s/make_response/Response()

This commit is contained in:
Jimmy Zelinskie 2016-08-09 12:28:00 -04:00
parent 35579093ca
commit 16b451437f
2 changed files with 65 additions and 61 deletions

View file

@ -1,7 +1,7 @@
import logging import logging
import re import re
from flask import make_response, url_for, request, redirect, Response, abort as flask_abort from flask import url_for, request, redirect, Response, abort as flask_abort
import resumablehashlib import resumablehashlib
@ -57,9 +57,7 @@ def check_blob_exists(namespace_name, repo_name, digest):
headers['Accept-Ranges'] = 'bytes' headers['Accept-Ranges'] = 'bytes'
# Write the response to the Docker client. # Write the response to the Docker client.
response = make_response('') return Response(headers=headers)
response.headers.extend(headers)
return response
@v2_bp.route(BLOB_DIGEST_ROUTE, methods=['GET']) @v2_bp.route(BLOB_DIGEST_ROUTE, methods=['GET'])
@ -124,13 +122,15 @@ def start_blob_upload(namespace_name, repo_name):
digest = request.args.get('digest', None) digest = request.args.get('digest', None)
if digest is None: if digest is None:
# Short-circuit because the user will send the blob data in another request. # Short-circuit because the user will send the blob data in another request.
accepted = make_response('', 202) return Response(
accepted.headers['Location'] = url_for('v2.upload_chunk', status=202,
repository='%s/%s' % (namespace_name, repo_name), headers={
upload_uuid=new_upload_uuid) 'Docker-Upload-UUID': new_upload_uuid,
accepted.headers['Range'] = _render_range(0) 'Range': _render_range(0),
accepted.headers['Docker-Upload-UUID'] = new_upload_uuid 'Location': url_for('v2.upload_chunk', repository='%s/%s' % (namespace_name, repo_name),
return accepted upload_uuid=new_upload_uuid)
},
)
# The user plans to send us the entire body right now. # The user plans to send us the entire body right now.
# Find the upload. # Find the upload.
@ -151,12 +151,14 @@ def start_blob_upload(namespace_name, repo_name):
_finish_upload(namespace_name, repo_name, updated_blob_upload, digest) _finish_upload(namespace_name, repo_name, updated_blob_upload, digest)
# Write the response to the docker client. # Write the response to the docker client.
response = make_response('', 201) return Response(
response.headers['Docker-Content-Digest'] = digest status=201,
response.headers['Location'] = url_for('v2.download_blob', headers={
repository='%s/%s' % (namespace_name, repo_name), 'Docker-Content-Digest': digest,
digest=digest) 'Location': url_for('v2.download_blob', repository='%s/%s' % (namespace_name, repo_name),
return response digest=digest),
},
)
@v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['GET']) @v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['GET'])
@ -169,12 +171,13 @@ def fetch_existing_upload(namespace_name, repo_name, upload_uuid):
if blob_upload is None: if blob_upload is None:
raise BlobUploadUnknown() raise BlobUploadUnknown()
accepted = make_response('', 204) return Response(
accepted.headers.extend({ status=204,
'Docker-Upload-UUID': upload_uuid, headers={
'Range': _render_range(blob_upload.byte_count+1), # Docker byte ranges are exclusive 'Docker-Upload-UUID': upload_uuid,
}) 'Range': _render_range(blob_upload.byte_count+1), # Docker byte ranges are exclusive
return accepted },
)
@v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['PATCH']) @v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['PATCH'])
@ -198,13 +201,14 @@ def upload_chunk(namespace_name, repo_name, upload_uuid):
v2.update_blob_upload(updated_blob_upload) v2.update_blob_upload(updated_blob_upload)
# Write the response to the Docker client. # Write the response to the Docker client.
accepted = make_response('', 204) return Response(
accepted.headers.extend({ status=204,
'Location': _current_request_path(), headers={
'Range': _render_range(updated_blob_upload.byte_count, with_bytes_prefix=False), 'Location': _current_request_path(),
'Docker-Upload-UUID': upload_uuid, 'Range': _render_range(updated_blob_upload.byte_count, with_bytes_prefix=False),
}) 'Docker-Upload-UUID': upload_uuid,
return accepted },
)
@v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['PUT']) @v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['PUT'])
@ -233,13 +237,14 @@ def monolithic_upload_or_last_chunk(namespace_name, repo_name, upload_uuid):
_finish_upload(namespace_name, repo_name, updated_blob_upload, digest) _finish_upload(namespace_name, repo_name, updated_blob_upload, digest)
# Write the response to the Docker client. # Write the response to the Docker client.
response = make_response('', 201) return Response(
response.headers.extend({ status=201,
'Docker-Content-Digest': digest, headers={
'Location': url_for('v2.download_blob', repository='%s/%s' % (namespace_name, repo_name), 'Docker-Content-Digest': digest,
digest=digest) 'Location': url_for('v2.download_blob', repository='%s/%s' % (namespace_name, repo_name),
}) digest=digest),
return response }
)
@v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['DELETE']) @v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['DELETE'])
@ -257,7 +262,7 @@ def cancel_upload(namespace_name, repo_name, upload_uuid):
v2.delete_blob_upload(upload_uuid) v2.delete_blob_upload(upload_uuid)
storage.cancel_chunked_upload({upload.location_name}, upload.uuid, upload.storage_metadata) storage.cancel_chunked_upload({upload.location_name}, upload.uuid, upload.storage_metadata)
return make_response('', 204) return Response(status=204)
@v2_bp.route('/<repopath:repository>/blobs/<digest>', methods=['DELETE']) @v2_bp.route('/<repopath:repository>/blobs/<digest>', methods=['DELETE'])
@ -288,13 +293,9 @@ def _abort_range_not_satisfiable(valid_end, upload_uuid):
TODO(jzelinskie): Unify this with the V2RegistryException class. TODO(jzelinskie): Unify this with the V2RegistryException class.
""" """
invalid_range = make_response('', 416) flask_abort(Response(status=416, headers={'Location': _current_request_path(),
invalid_range.headers.extend({ 'Range': '0-{0}'.format(valid_end),
'Location': _current_request_path(), 'Docker-Upload-UUID': upload_uuid}))
'Range': '0-{0}'.format(valid_end),
'Docker-Upload-UUID': upload_uuid,
})
flask_abort(invalid_range)
def _parse_range_header(range_header_text): def _parse_range_header(range_header_text):

View file

@ -2,7 +2,7 @@ import logging
from functools import wraps from functools import wraps
from flask import make_response, request, url_for from flask import request, url_for, Response
import features import features
@ -51,10 +51,11 @@ def fetch_manifest_by_tagname(namespace_name, repo_name, tag_name):
track_and_log('pull_repo', repo, analytics_name='pull_repo_100x', analytics_sample=0.01) track_and_log('pull_repo', repo, analytics_name='pull_repo_100x', analytics_sample=0.01)
metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2']) metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2'])
response = make_response(manifest.bytes, 200) return Response(
response.headers['Content-Type'] = manifest.content_type manifest.bytes,
response.headers['Docker-Content-Digest'] = manifest.digest status=200,
return response headers={'Content-Type': manifest.content_type, 'Docker-Content-Digest': manifest.digest},
)
@v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['GET']) @v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['GET'])
@ -73,10 +74,8 @@ def fetch_manifest_by_digest(namespace_name, repo_name, manifest_ref):
track_and_log('pull_repo', repo) track_and_log('pull_repo', repo)
metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2']) metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2'])
response = make_response(manifest.json, 200) return Response(manifest.json, status=200, headers={'Content-Type': manifest.content_type,
response.headers['Content-Type'] = manifest.content_type 'Docker-Content-Digest': manifest.digest})
response.headers['Docker-Content-Digest'] = manifest.digest
return response
def _reject_manifest2_schema2(func): def _reject_manifest2_schema2(func):
@ -190,12 +189,16 @@ def _write_manifest(namespace_name, repo_name, manifest):
spawn_notification(repo, 'repo_push', {'updated_tags': [manifest.tag]}) spawn_notification(repo, 'repo_push', {'updated_tags': [manifest.tag]})
metric_queue.repository_push.Inc(labelvalues=[namespace_name, repo_name, 'v2']) metric_queue.repository_push.Inc(labelvalues=[namespace_name, repo_name, 'v2'])
response = make_response('OK', 202) return Response(
response.headers['Docker-Content-Digest'] = manifest.digest 'OK',
response.headers['Location'] = url_for('v2.fetch_manifest_by_digest', status=202,
repository='%s/%s' % (namespace_name, repo_name), headers={
manifest_ref=manifest.digest) 'Docker-Content-Digest': manifest.digest,
return response 'Location': url_for('v2.fetch_manifest_by_digest',
repository='%s/%s' % (namespace_name, repo_name),
manifest_ref=manifest.digest),
},
)
@v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['DELETE']) @v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['DELETE'])
@ -223,7 +226,7 @@ def delete_manifest_by_digest(namespace_name, repo_name, digest):
track_and_log('delete_tag', tag.repository, tag=tag.name, digest=digest) track_and_log('delete_tag', tag.repository, tag=tag.name, digest=digest)
return make_response('', 202) return Response(status=202)
def _generate_and_store_manifest(namespace_name, repo_name, tag_name): def _generate_and_store_manifest(namespace_name, repo_name, tag_name):