Ejemplo n.º 1
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)
Ejemplo n.º 2
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: https://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)
Ejemplo n.º 3
0
 def test_additional(self, stubs):
     """Test with Content-Type header with additional informations."""
     reply = stubs.FakeNetworkReply(
         headers={'Content-Type': 'image/example; encoding=UTF-8'})
     mimetype, rest = http.parse_content_type(reply)
     assert mimetype == 'image/example'
     assert rest == ' encoding=UTF-8'
Ejemplo n.º 4
0
 def test_mimetype(self, stubs):
     """Test with simple Content-Type header."""
     reply = stubs.FakeNetworkReply(
         headers={'Content-Type': 'image/example'})
     mimetype, rest = http.parse_content_type(reply)
     assert mimetype == 'image/example'
     assert rest is None
Ejemplo n.º 5
0
def test_parse_content_type(stubs, content_type, expected_mimetype,
                            expected_rest):
    if content_type is None:
        reply = stubs.FakeNetworkReply()
    else:
        reply = stubs.FakeNetworkReply(headers={'Content-Type': content_type})
    mimetype, rest = http.parse_content_type(reply)
    assert mimetype == expected_mimetype
    assert rest == expected_rest
Ejemplo n.º 6
0
def test_parse_content_type(stubs, content_type, expected_mimetype,
                            expected_rest):
    if content_type is None:
        reply = stubs.FakeNetworkReply()
    else:
        reply = stubs.FakeNetworkReply(headers={'Content-Type': content_type})
    mimetype, rest = http.parse_content_type(reply)
    assert mimetype == expected_mimetype
    assert rest == expected_rest
Ejemplo n.º 7
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',
                                      scope='window',
                                      window=self._win_id)
        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 (mimetype in ['application/pdf', 'application/x-pdf']
              and config.val.content.pdfjs):
            # Use pdf.js to display the page
            self._show_pdfjs(reply)
        else:
            # Unknown mimetype, so download anyways.
            download_manager.fetch(reply,
                                   suggested_filename=suggested_filename)
Ejemplo n.º 8
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('download-manager', scope='window',
                                      window=self._win_id)
        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 (mimetype in ['application/pdf', 'application/x-pdf'] and
              config.get('content', 'enable-pdfjs')):
            # Use pdf.js to display the page
            self._show_pdfjs(reply)
        else:
            # Unknown mimetype, so download anyways.
            download_manager.fetch(reply,
                                   suggested_filename=suggested_filename)
Ejemplo n.º 9
0
def test_parse_content_type_hypothesis(stubs, s):
    reply = stubs.FakeNetworkReply(headers={'Content-Type': s})
    http.parse_content_type(reply)
Ejemplo n.º 10
0
def test_parse_content_type(stubs, s):
    reply = stubs.FakeNetworkReply(headers={'Content-Type': s})
    http.parse_content_type(reply)
Ejemplo n.º 11
0
 def test_empty(self, stubs):
     """Test with empty Content-Type header."""
     reply = stubs.FakeNetworkReply(headers={'Content-Type': ''})
     mimetype, rest = http.parse_content_type(reply)
     assert mimetype == ''
     assert rest is None
Ejemplo n.º 12
0
 def test_not_existing(self, stubs):
     """Test without any Content-Type header."""
     reply = stubs.FakeNetworkReply()
     mimetype, rest = http.parse_content_type(reply)
     assert mimetype is None
     assert rest is None