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 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
@ -57,9 +57,7 @@ def check_blob_exists(namespace_name, repo_name, digest):
headers['Accept-Ranges'] = 'bytes'
# Write the response to the Docker client.
response = make_response('')
response.headers.extend(headers)
return response
return Response(headers=headers)
@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)
if digest is None:
# Short-circuit because the user will send the blob data in another request.
accepted = make_response('', 202)
accepted.headers['Location'] = url_for('v2.upload_chunk',
repository='%s/%s' % (namespace_name, repo_name),
upload_uuid=new_upload_uuid)
accepted.headers['Range'] = _render_range(0)
accepted.headers['Docker-Upload-UUID'] = new_upload_uuid
return accepted
return Response(
status=202,
headers={
'Docker-Upload-UUID': new_upload_uuid,
'Range': _render_range(0),
'Location': url_for('v2.upload_chunk', repository='%s/%s' % (namespace_name, repo_name),
upload_uuid=new_upload_uuid)
},
)
# The user plans to send us the entire body right now.
# 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)
# Write the response to the docker client.
response = make_response('', 201)
response.headers['Docker-Content-Digest'] = digest
response.headers['Location'] = url_for('v2.download_blob',
repository='%s/%s' % (namespace_name, repo_name),
digest=digest)
return response
return Response(
status=201,
headers={
'Docker-Content-Digest': digest,
'Location': url_for('v2.download_blob', repository='%s/%s' % (namespace_name, repo_name),
digest=digest),
},
)
@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:
raise BlobUploadUnknown()
accepted = make_response('', 204)
accepted.headers.extend({
'Docker-Upload-UUID': upload_uuid,
'Range': _render_range(blob_upload.byte_count+1), # Docker byte ranges are exclusive
})
return accepted
return Response(
status=204,
headers={
'Docker-Upload-UUID': upload_uuid,
'Range': _render_range(blob_upload.byte_count+1), # Docker byte ranges are exclusive
},
)
@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)
# Write the response to the Docker client.
accepted = make_response('', 204)
accepted.headers.extend({
'Location': _current_request_path(),
'Range': _render_range(updated_blob_upload.byte_count, with_bytes_prefix=False),
'Docker-Upload-UUID': upload_uuid,
})
return accepted
return Response(
status=204,
headers={
'Location': _current_request_path(),
'Range': _render_range(updated_blob_upload.byte_count, with_bytes_prefix=False),
'Docker-Upload-UUID': upload_uuid,
},
)
@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)
# Write the response to the Docker client.
response = make_response('', 201)
response.headers.extend({
'Docker-Content-Digest': digest,
'Location': url_for('v2.download_blob', repository='%s/%s' % (namespace_name, repo_name),
digest=digest)
})
return response
return Response(
status=201,
headers={
'Docker-Content-Digest': digest,
'Location': url_for('v2.download_blob', repository='%s/%s' % (namespace_name, repo_name),
digest=digest),
}
)
@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)
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'])
@ -288,13 +293,9 @@ def _abort_range_not_satisfiable(valid_end, upload_uuid):
TODO(jzelinskie): Unify this with the V2RegistryException class.
"""
invalid_range = make_response('', 416)
invalid_range.headers.extend({
'Location': _current_request_path(),
'Range': '0-{0}'.format(valid_end),
'Docker-Upload-UUID': upload_uuid,
})
flask_abort(invalid_range)
flask_abort(Response(status=416, headers={'Location': _current_request_path(),
'Range': '0-{0}'.format(valid_end),
'Docker-Upload-UUID': upload_uuid}))
def _parse_range_header(range_header_text):

View file

@ -2,7 +2,7 @@ import logging
from functools import wraps
from flask import make_response, request, url_for
from flask import request, url_for, Response
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)
metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2'])
response = make_response(manifest.bytes, 200)
response.headers['Content-Type'] = manifest.content_type
response.headers['Docker-Content-Digest'] = manifest.digest
return response
return Response(
manifest.bytes,
status=200,
headers={'Content-Type': manifest.content_type, 'Docker-Content-Digest': manifest.digest},
)
@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)
metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2'])
response = make_response(manifest.json, 200)
response.headers['Content-Type'] = manifest.content_type
response.headers['Docker-Content-Digest'] = manifest.digest
return response
return Response(manifest.json, status=200, headers={'Content-Type': manifest.content_type,
'Docker-Content-Digest': manifest.digest})
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]})
metric_queue.repository_push.Inc(labelvalues=[namespace_name, repo_name, 'v2'])
response = make_response('OK', 202)
response.headers['Docker-Content-Digest'] = manifest.digest
response.headers['Location'] = url_for('v2.fetch_manifest_by_digest',
repository='%s/%s' % (namespace_name, repo_name),
manifest_ref=manifest.digest)
return response
return Response(
'OK',
status=202,
headers={
'Docker-Content-Digest': manifest.digest,
'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'])
@ -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)
return make_response('', 202)
return Response(status=202)
def _generate_and_store_manifest(namespace_name, repo_name, tag_name):