Use a constant.

This commit is contained in:
Itamar Turner-Trauring 2022-03-14 11:18:53 -04:00
parent 722f8e9598
commit 106cc708a0
3 changed files with 13 additions and 10 deletions

View File

@ -19,7 +19,7 @@ from twisted.internet.defer import inlineCallbacks, returnValue, fail, Deferred
from hyperlink import DecodedURL
import treq
from .http_common import swissnum_auth_header, Secrets, get_content_type
from .http_common import swissnum_auth_header, Secrets, get_content_type, CBOR_MIME_TYPE
from .common import si_b2a
@ -40,7 +40,7 @@ def _decode_cbor(response):
"""Given HTTP response, return decoded CBOR body."""
if response.code > 199 and response.code < 300:
content_type = get_content_type(response.headers)
if content_type == "application/cbor":
if content_type == CBOR_MIME_TYPE:
# TODO limit memory usage
# https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3872
return treq.content(response).addCallback(loads)
@ -186,7 +186,7 @@ class StorageClientImmutables(object):
lease_cancel_secret=lease_cancel_secret,
upload_secret=upload_secret,
data=message,
headers=Headers({"content-type": ["application/cbor"]}),
headers=Headers({"content-type": [CBOR_MIME_TYPE]}),
)
decoded_response = yield _decode_cbor(response)
returnValue(
@ -362,7 +362,7 @@ class StorageClientImmutables(object):
"POST",
url,
data=message,
headers=Headers({"content-type": ["application/cbor"]}),
headers=Headers({"content-type": [CBOR_MIME_TYPE]}),
)
if response.code == http.OK:
return

View File

@ -9,6 +9,8 @@ from typing import Optional
from werkzeug.http import parse_options_header
from twisted.web.http_headers import Headers
CBOR_MIME_TYPE = "application/cbor"
def get_content_type(headers: Headers) -> Optional[str]:
"""

View File

@ -23,7 +23,7 @@ from werkzeug.datastructures import ContentRange
from cbor2 import dumps, loads
from .server import StorageServer
from .http_common import swissnum_auth_header, Secrets, get_content_type
from .http_common import swissnum_auth_header, Secrets, get_content_type, CBOR_MIME_TYPE
from .common import si_a2b
from .immutable import BucketWriter, ConflictingWriteError
from ..util.hashutil import timing_safe_compare
@ -254,11 +254,12 @@ class HTTPServer(object):
Also sets the appropriate ``Content-Type`` header on the response.
"""
cbor_mime = "application/cbor"
accept_headers = request.requestHeaders.getRawHeaders("accept") or [cbor_mime]
accept_headers = request.requestHeaders.getRawHeaders("accept") or [
CBOR_MIME_TYPE
]
accept = parse_accept_header(accept_headers[0])
if accept.best == cbor_mime:
request.setHeader("Content-Type", cbor_mime)
if accept.best == CBOR_MIME_TYPE:
request.setHeader("Content-Type", CBOR_MIME_TYPE)
# TODO if data is big, maybe want to use a temporary file eventually...
# https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3872
return dumps(data)
@ -272,7 +273,7 @@ class HTTPServer(object):
Read encoded request body data, decoding it with CBOR by default.
"""
content_type = get_content_type(request.requestHeaders)
if content_type == "application/cbor":
if content_type == CBOR_MIME_TYPE:
# TODO limit memory usage, client could send arbitrarily large data...
# https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3872
return loads(request.content.read())