From aaf90d0fe32146e1b44d416cb7e160203965c885 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 2 Aug 2015 18:09:01 +0200 Subject: [PATCH] Simplify RFC6266 error handling. --- qutebrowser/browser/http.py | 9 +++++---- qutebrowser/browser/rfc6266.py | 23 ++++++++++------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/qutebrowser/browser/http.py b/qutebrowser/browser/http.py index e850ac877..5b21b6396 100644 --- a/qutebrowser/browser/http.py +++ b/qutebrowser/browser/http.py @@ -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 diff --git a/qutebrowser/browser/rfc6266.py b/qutebrowser/browser/rfc6266.py index 62d7b705b..2ceb8d22b 100644 --- a/qutebrowser/browser/rfc6266.py +++ b/qutebrowser/browser/rfc6266.py @@ -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):