def check_unnamed(self, header):
     """Check if the passed header results in an unnamed attachment."""
     reply = self.stubs.FakeNetworkReply(
         headers={'Content-Disposition': header})
     cd_inline, cd_filename = http.parse_content_disposition(reply)
     assert cd_filename == DEFAULT_NAME
     assert not cd_inline
 def check_filename(self, header, filename, expected_inline=False):
     """Check if the passed header has the given filename."""
     reply = self.stubs.FakeNetworkReply(
         headers={'Content-Disposition': header})
     cd_inline, cd_filename = http.parse_content_disposition(reply)
     assert cd_filename is not None
     assert cd_filename == filename
     assert cd_inline == expected_inline
 def check_ignored(self, header):
     """Check if the passed header is ignored."""
     reply = self.stubs.FakeNetworkReply(
         headers={'Content-Disposition': header})
     with self.caplog.at_level(logging.ERROR, 'rfc6266'):
         # with self.assertLogs(log.rfc6266, logging.ERROR):
         cd_inline, cd_filename = http.parse_content_disposition(reply)
     assert cd_filename == DEFAULT_NAME
     assert cd_inline
    def test_attonly(self, stubs):
        """'attachment' only.

        UA should offer to download the resource.
        """
        reply = stubs.FakeNetworkReply(
            headers={'Content-Disposition': 'attachment'})
        cd_inline, cd_filename = http.parse_content_disposition(reply)
        assert not cd_inline
        assert cd_filename == DEFAULT_NAME
    def fetch(self,
              reply,
              *,
              target=None,
              auto_remove=False,
              suggested_filename=None,
              prompt_download_directory=None):
        """Download a QNetworkReply to disk.

        Args:
            reply: The QNetworkReply to download.
            target: Where to save the download as downloads.DownloadTarget.
            auto_remove: Whether to remove the download even if
                         downloads.remove_finished is set to -1.

        Return:
            The created DownloadItem.
        """
        if not suggested_filename:
            try:
                suggested_filename = target.suggested_filename()
            except downloads.NoFilenameError:
                _, suggested_filename = http.parse_content_disposition(reply)
        log.downloads.debug("fetch: {} -> {}".format(reply.url(),
                                                     suggested_filename))
        download = DownloadItem(reply, manager=self)
        self._init_item(download, auto_remove, suggested_filename)

        if target is not None:
            download.set_target(target)
            return download

        # Neither filename nor fileobj were given

        filename = downloads.immediate_download_path(prompt_download_directory)
        if filename is not None:
            # User doesn't want to be asked, so just use the download_dir
            target = downloads.FileDownloadTarget(filename)
            download.set_target(target)
            return download

        # Ask the user for a filename
        question = downloads.get_filename_question(
            suggested_filename=suggested_filename,
            url=reply.url(),
            parent=self)
        self._init_filename_question(question, download)
        message.global_bridge.ask(question, blocking=False)

        return download
Example #6
0
    def on_unsupported_content(self, reply):
        """Handle an unsupportedContent signal.

        Most likely this will mean we need to download the reply, but we
        correct for some common errors the server do.

        At some point we might want to implement the MIME Sniffing standard
        here: http://mimesniff.spec.whatwg.org/
        """
        inline, suggested_filename = http.parse_content_disposition(reply)
        download_manager = objreg.get('qtnetwork-download-manager')
        if not inline:
            # Content-Disposition: attachment -> force download
            download_manager.fetch(reply,
                                   suggested_filename=suggested_filename)
            return
        mimetype, _rest = http.parse_content_type(reply)
        if mimetype == 'image/jpg':
            # Some servers (e.g. the LinkedIn CDN) send a non-standard
            # image/jpg (instead of image/jpeg, defined in RFC 1341 section
            # 7.5). If this is the case, we force displaying with a corrected
            # mimetype.
            if reply.isFinished():
                self.display_content(reply, 'image/jpeg')
            else:
                reply.finished.connect(
                    functools.partial(self.display_content, reply,
                                      'image/jpeg'))
        elif pdfjs.should_use_pdfjs(mimetype, reply.url()):
            download_manager.fetch(reply,
                                   target=downloads.PDFJSDownloadTarget(),
                                   auto_remove=True)
        else:
            # Unknown mimetype, so download anyways.
            download_manager.fetch(reply,
                                   suggested_filename=suggested_filename)
Example #7
0
def test_no_content_disposition(stubs, url, expected):
    reply = stubs.FakeNetworkReply(url=QUrl(url))
    inline, filename = http.parse_content_disposition(reply)
    assert inline
    assert filename == expected
def test_parse_content_disposition(caplog, template, stubs, s):
    """Test parsing headers based on templates which hypothesis completes."""
    header = template.format(s)
    reply = stubs.FakeNetworkReply(headers={'Content-Disposition': header})
    with caplog.at_level(logging.ERROR, 'rfc6266'):
        http.parse_content_disposition(reply)