Exemple #1
0
def parse_content_disposition(reply):
    """Parse a content_disposition header.

    Args:
        reply: The QNetworkReply to get a filename for.

    Return:
        A (is_inline, filename) tuple.
    """
    is_inline = True
    filename = None
    content_disposition_header = 'Content-Disposition'.encode('iso-8859-1')
    # First check if the Content-Disposition header has a filename
    # attribute.
    if reply.hasRawHeader(content_disposition_header):
        # We use the unsafe variant of the filename as we sanitize it via
        # os.path.basename later.
        try:
            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 (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
    if not filename:
        path = reply.url().path()
        if path is not None:
            filename = path.rstrip('/')
    # If that fails as well, use a fallback
    if not filename:
        filename = 'qutebrowser-download'
    return is_inline, os.path.basename(filename)
def test_content_disposition_directly(s):
    """Test rfc6266.parse_headers directly with binary data."""
    try:
        cd = rfc6266.parse_headers(s)
        cd.filename()
    except (SyntaxError, UnicodeDecodeError, rfc6266.Error):
        pass
def test_content_disposition_directly(s):
    """Test rfc6266.parse_headers directly with binary data."""
    try:
        cd = rfc6266.parse_headers(s)
        cd.filename()
    except (SyntaxError, UnicodeDecodeError, rfc6266.Error):
        pass
Exemple #4
0
def parse_content_disposition(reply):
    """Parse a content_disposition header.

    Args:
        reply: The QNetworkReply to get a filename for.

    Return:
        A (is_inline, filename) tuple.
    """
    is_inline = True
    filename = None
    content_disposition_header = 'Content-Disposition'.encode('iso-8859-1')
    # First check if the Content-Disposition header has a filename
    # attribute.
    if reply.hasRawHeader(content_disposition_header):
        # We use the unsafe variant of the filename as we sanitize it via
        # os.path.basename later.
        try:
            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 (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
    if not filename:
        path = reply.url().path()
        if path is not None:
            filename = path.rstrip('/')
    # If that fails as well, use a fallback
    if not filename:
        filename = 'qutebrowser-download'
    return is_inline, os.path.basename(filename)
Exemple #5
0
def parse_content_disposition(reply):
    """Parse a content_disposition header.

    Args:
        reply: The QNetworkReply to get a filename for.

    Return:
        A (is_inline, filename) tuple.
    """
    is_inline = True
    filename = None
    # First check if the Content-Disposition header has a filename
    # attribute.
    if reply.hasRawHeader('Content-Disposition'):
        # 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')))
            filename = content_disposition.filename()
        except UnicodeDecodeError:
            log.misc.exception("Error while decoding filename")
        else:
            is_inline = content_disposition.is_inline()
    # Then try to get filename from url
    if not filename:
        filename = reply.url().path()
    # If that fails as well, use a fallback
    if not filename:
        filename = 'qutebrowser-download'
    return is_inline, os.path.basename(filename)