From 25cc03b57812211f5d2cc373a0286a5f39c6cbd6 Mon Sep 17 00:00:00 2001 From: MORITA Kazutaka Date: Tue, 12 Aug 2014 00:19:14 +0900 Subject: [PATCH] Move x-amz-acl header handling into acl.py This also adds support for the x-amz-acl header in ?acl requests. Change-Id: I6e2dd6671bce3913a61f8f59f6d214be7eae0b86 --- swift3/controllers/acl.py | 27 ++++++++++++++++++++++++++- swift3/controllers/bucket.py | 19 ++----------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/swift3/controllers/acl.py b/swift3/controllers/acl.py index 0940ce95..e3b519bd 100644 --- a/swift3/controllers/acl.py +++ b/swift3/controllers/acl.py @@ -17,7 +17,8 @@ from swift.common.http import HTTP_OK from swift.common.middleware.acl import parse_acl, referrer_allowed from swift3.controllers.base import Controller -from swift3.response import HTTPOk, S3NotImplemented, MalformedACLError +from swift3.response import HTTPOk, S3NotImplemented, MalformedACLError, \ + InvalidArgument from swift3.etree import Element, SubElement, fromstring, tostring, \ XMLSyntaxError, DocumentInvalid @@ -120,6 +121,28 @@ def swift_acl_translate(acl, group='', user='', xml=False): return swift_acl[acl] +def handle_acl_header(req): + """ + Handle the x-amz-acl header. + """ + amz_acl = req.environ['HTTP_X_AMZ_ACL'] + # Translate the Amazon ACL to something that can be + # implemented in Swift, 501 otherwise. Swift uses POST + # for ACLs, whereas S3 uses PUT. + del req.environ['HTTP_X_AMZ_ACL'] + if req.query_string: + req.query_string = '' + + translated_acl = swift_acl_translate(amz_acl) + if translated_acl == 'NotImplemented': + raise S3NotImplemented() + elif translated_acl == 'InvalidArgument': + raise InvalidArgument('x-amz-acl', amz_acl) + + for header, acl in translated_acl: + req.headers[header] = acl + + class AclController(Controller): """ Handles the following APIs: @@ -148,6 +171,8 @@ class AclController(Controller): raise S3NotImplemented() else: # Handle Bucket ACL + if 'HTTP_X_AMZ_ACL' in req.environ: + handle_acl_header(req) # We very likely have an XML-based ACL request. translated_acl = swift_acl_translate(req.xml(MAX_ACL_BODY_SIZE), diff --git a/swift3/controllers/bucket.py b/swift3/controllers/bucket.py index 5d0f44be..bfe08034 100644 --- a/swift3/controllers/bucket.py +++ b/swift3/controllers/bucket.py @@ -18,7 +18,7 @@ from simplejson import loads from swift.common.http import HTTP_OK from swift3.controllers.base import Controller -from swift3.controllers.acl import swift_acl_translate +from swift3.controllers.acl import handle_acl_header from swift3.etree import Element, SubElement, tostring, fromstring, \ XMLSyntaxError, DocumentInvalid from swift3.response import HTTPOk, S3NotImplemented, InvalidArgument, \ @@ -106,22 +106,7 @@ class BucketController(Controller): Handle PUT Bucket request """ if 'HTTP_X_AMZ_ACL' in req.environ: - amz_acl = req.environ['HTTP_X_AMZ_ACL'] - # Translate the Amazon ACL to something that can be - # implemented in Swift, 501 otherwise. Swift uses POST - # for ACLs, whereas S3 uses PUT. - del req.environ['HTTP_X_AMZ_ACL'] - if req.query_string: - req.query_string = '' - - translated_acl = swift_acl_translate(amz_acl) - if translated_acl == 'NotImplemented': - raise S3NotImplemented() - elif translated_acl == 'InvalidArgument': - raise InvalidArgument('x-amz-acl', amz_acl) - - for header, acl in translated_acl: - req.headers[header] = acl + handle_acl_header(req) xml = req.xml(MAX_PUT_BUCKET_BODY_SIZE) if xml: