Simplify RFC6266 error handling.

This commit is contained in:
Florian Bruhin 2015-08-02 18:09:01 +02:00
parent cd7c3ec3a4
commit aaf90d0fe3
2 changed files with 15 additions and 17 deletions

View File

@ -46,11 +46,12 @@ def parse_content_disposition(reply):
# We use the unsafe variant of the filename as we sanitize it via
# os.path.basename later.
try:
content_disposition = rfc6266.parse_headers(
bytes(reply.rawHeader(content_disposition_header)))
value = bytes(reply.rawHeader(content_disposition_header))
log.rfc6266.debug("Parsing Content-Disposition: {}".format(value))
content_disposition = rfc6266.parse_headers(value)
filename = content_disposition.filename()
except UnicodeDecodeError:
log.rfc6266.exception("Error while decoding filename")
except (SyntaxError, UnicodeDecodeError, rfc6266.Error):
log.rfc6266.exception("Error while parsing filename")
else:
is_inline = content_disposition.is_inline()
# Then try to get filename from url

View File

@ -26,7 +26,7 @@ import re
import pypeg2 as peg
from qutebrowser.utils import log, utils
from qutebrowser.utils import utils
class UniqueNamespace(peg.Namespace):
@ -215,12 +215,17 @@ class ContentDispositionValue:
LangTagged = collections.namedtuple('LangTagged', ['string', 'langtag'])
class DuplicateParamError(Exception):
class Error(Exception):
"""Base class for RFC6266 errors."""
class DuplicateParamError(Error):
"""Exception raised when a parameter has been given twice."""
class InvalidISO8859Error(Exception):
class InvalidISO8859Error(Error):
"""Exception raised when a byte is invalid in ISO-8859-1."""
@ -302,8 +307,6 @@ def parse_headers(content_disposition):
# filename parameter. But it does mean we occasionally give
# less-than-certain values for some legacy senders.
content_disposition = content_disposition.decode('iso-8859-1')
log.rfc6266.debug("Parsing Content-Disposition: {}".format(
content_disposition))
# Our parsing is relaxed in these regards:
# - The grammar allows a final ';' in the header;
# - We do LWS-folding, and possibly normalise other broken
@ -311,14 +314,8 @@ def parse_headers(content_disposition):
# XXX Would prefer to accept only the quoted whitespace
# case, rather than normalising everything.
content_disposition = normalize_ws(content_disposition)
try:
parsed = peg.parse(content_disposition, ContentDispositionValue)
except (SyntaxError, DuplicateParamError, InvalidISO8859Error):
log.rfc6266.exception("Error while parsing Content-Disposition")
return ContentDisposition()
else:
return ContentDisposition(disposition=parsed.dtype,
assocs=parsed.params)
parsed = peg.parse(content_disposition, ContentDispositionValue)
return ContentDisposition(disposition=parsed.dtype, assocs=parsed.params)
def parse_ext_value(val):