Remove string comparisons for illegal ACL

We should use try/except syntax to try to get rid of
unnecessary string comparisons.

Change-Id: I8dcd902beb27911ea52f8721355a336ccb1b2206
This commit is contained in:
Kota Tsuyuzaki
2014-08-13 18:43:51 -07:00
parent a2a812a792
commit ea0d0a4640
2 changed files with 15 additions and 11 deletions

View File

@@ -16,6 +16,7 @@
from swift.common.http import HTTP_OK from swift.common.http import HTTP_OK
from swift.common.middleware.acl import parse_acl, referrer_allowed from swift.common.middleware.acl import parse_acl, referrer_allowed
from swift3.exception import ACLError
from swift3.controllers.base import Controller from swift3.controllers.base import Controller
from swift3.response import HTTPOk, S3NotImplemented, MalformedACLError, \ from swift3.response import HTTPOk, S3NotImplemented, MalformedACLError, \
InvalidArgument InvalidArgument
@@ -114,9 +115,9 @@ def swift_acl_translate(acl, group='', user='', xml=False):
acl = 'unsupported' acl = 'unsupported'
if acl == 'authenticated-read': if acl == 'authenticated-read':
return "NotImplemented" raise S3NotImplemented()
elif acl not in swift_acl: elif acl not in swift_acl:
return "InvalidArgument" raise ACLError()
return swift_acl[acl] return swift_acl[acl]
@@ -133,10 +134,9 @@ def handle_acl_header(req):
if req.query_string: if req.query_string:
req.query_string = '' req.query_string = ''
translated_acl = swift_acl_translate(amz_acl) try:
if translated_acl == 'NotImplemented': translated_acl = swift_acl_translate(amz_acl)
raise S3NotImplemented() except ACLError:
elif translated_acl == 'InvalidArgument':
raise InvalidArgument('x-amz-acl', amz_acl) raise InvalidArgument('x-amz-acl', amz_acl)
for header, acl in translated_acl: for header, acl in translated_acl:
@@ -175,12 +175,12 @@ class AclController(Controller):
handle_acl_header(req) handle_acl_header(req)
# We very likely have an XML-based ACL request. # We very likely have an XML-based ACL request.
translated_acl = swift_acl_translate(req.xml(MAX_ACL_BODY_SIZE), try:
xml=True) translated_acl = swift_acl_translate(
if translated_acl == 'NotImplemented': req.xml(MAX_ACL_BODY_SIZE), xml=True)
raise S3NotImplemented() except ACLError:
elif translated_acl == 'InvalidArgument':
raise MalformedACLError() raise MalformedACLError()
for header, acl in translated_acl: for header, acl in translated_acl:
req.headers[header] = acl req.headers[header] = acl

View File

@@ -24,3 +24,7 @@ class NotS3Request(S3Exception):
class BadSwiftRequest(S3Exception): class BadSwiftRequest(S3Exception):
pass pass
class ACLError(S3Exception):
pass