Example #1
0
    def __init__(self, win_id, parent=None):
        # It seems QNetworkAccessManager somehow calls processEvents which
        # causes some trouble, so we try to process outstanding events here
        # first.
        app = objreg.get('app')
        app.processEvents()
        log.init.debug("Initializing NetworkManager")
        with log.disable_qt_msghandler():
            # WORKAROUND for a hang when a message is printed - See:
            # http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
            super().__init__(parent)
        log.init.debug("NetworkManager init done")
        self._win_id = win_id
        self._requests = []
        self._scheme_handlers = {
            'qute': qutescheme.QuteSchemeHandler(win_id),
        }

        # We have a shared cookie jar and cache - we restore their parents so
        # we don't take ownership of them.
        app = QCoreApplication.instance()
        cookie_jar = objreg.get('cookie-jar')
        self.setCookieJar(cookie_jar)
        cookie_jar.setParent(app)
        cache = objreg.get('cache')
        self.setCache(cache)
        cache.setParent(app)

        if SSL_AVAILABLE:
            self.sslErrors.connect(self.on_ssl_errors)
        self.authenticationRequired.connect(self.on_authentication_required)
        self.proxyAuthenticationRequired.connect(
            self.on_proxy_authentication_required)
Example #2
0
 def __init__(self, *, win_id, tab_id, private, parent=None):
     log.init.debug("Initializing NetworkManager")
     with log.disable_qt_msghandler():
         # WORKAROUND for a hang when a message is printed - See:
         # http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
         super().__init__(parent)
     log.init.debug("NetworkManager init done")
     self.adopted_downloads = 0
     self._args = objreg.get('args')
     self._win_id = win_id
     self._tab_id = tab_id
     self._private = private
     self._scheme_handlers = {
         'qute': webkitqutescheme.handler,
         'file': filescheme.handler,
     }
     self._set_cookiejar()
     self._set_cache()
     self.sslErrors.connect(self.on_ssl_errors)
     self._rejected_ssl_errors = collections.defaultdict(list)
     self._accepted_ssl_errors = collections.defaultdict(list)
     self.authenticationRequired.connect(self.on_authentication_required)
     self.proxyAuthenticationRequired.connect(
         self.on_proxy_authentication_required)
     self.netrc_used = False
 def __init__(self, *, win_id, tab_id, private, parent=None):
     log.init.debug("Initializing NetworkManager")
     with log.disable_qt_msghandler():
         # WORKAROUND for a hang when a message is printed - See:
         # http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
         super().__init__(parent)
     log.init.debug("NetworkManager init done")
     self.adopted_downloads = 0
     self._args = objreg.get('args')
     self._win_id = win_id
     self._tab_id = tab_id
     self._private = private
     self._scheme_handlers = {
         'qute': webkitqutescheme.handler,
         'file': filescheme.handler,
     }
     self._set_cookiejar()
     self._set_cache()
     self.sslErrors.connect(self.on_ssl_errors)
     self._rejected_ssl_errors = collections.defaultdict(list)
     self._accepted_ssl_errors = collections.defaultdict(list)
     self.authenticationRequired.connect(self.on_authentication_required)
     self.proxyAuthenticationRequired.connect(
         self.on_proxy_authentication_required)
     self.netrc_used = False
 def __init__(self, parent=None):
     super().__init__(parent)
     with log.disable_qt_msghandler():
         # WORKAROUND for a hang when messages are printed, see our
         # NetworkAccessManager subclass for details.
         self._nam = QNetworkAccessManager(self)
     self._timers: MutableMapping[QNetworkReply, QTimer] = {}
Example #5
0
    def __init__(self, win_id, parent=None):
        log.init.debug("Initializing NetworkManager")
        with log.disable_qt_msghandler():
            # WORKAROUND for a hang when a message is printed - See:
            # http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
            super().__init__(parent)
        log.init.debug("NetworkManager init done")
        self._win_id = win_id
        self._requests = []
        self._scheme_handlers = {
            'qute': qutescheme.QuteSchemeHandler(win_id),
        }

        # We have a shared cookie jar and cache - we restore their parents so
        # we don't take ownership of them.
        app = QCoreApplication.instance()
        cookie_jar = objreg.get('cookie-jar')
        self.setCookieJar(cookie_jar)
        cookie_jar.setParent(app)
        cache = objreg.get('cache')
        self.setCache(cache)
        cache.setParent(app)

        if SSL_AVAILABLE:
            self.sslErrors.connect(self.on_ssl_errors)
        self.authenticationRequired.connect(self.on_authentication_required)
        self.proxyAuthenticationRequired.connect(
            self.on_proxy_authentication_required)
Example #6
0
    def createRequest(self, op, req, outgoing_data):
        """Return a new QNetworkReply object.

        Extend QNetworkAccessManager::createRequest to save requests in
        self._requests and handle custom schemes.

        Args:
             op: Operation op
             req: const QNetworkRequest & req
             outgoing_data: QIODevice * outgoingData

        Return:
            A QNetworkReply.
        """
        scheme = req.url().scheme()
        if scheme == 'https' and not SSL_AVAILABLE:
            return networkreply.ErrorNetworkReply(
                req, "SSL is not supported by the installed Qt library!",
                QNetworkReply.ProtocolUnknownError, self)
        elif scheme in self._scheme_handlers:
            return self._scheme_handlers[scheme].createRequest(
                op, req, outgoing_data)

        host_blocker = objreg.get('host-blocker')
        if (op == QNetworkAccessManager.GetOperation and
                req.url().host() in host_blocker.blocked_hosts and
                config.get('content', 'host-blocking-enabled')):
            log.webview.info("Request to {} blocked by host blocker.".format(
                req.url().host()))
            return networkreply.ErrorNetworkReply(
                req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
                self)

        if config.get('network', 'do-not-track'):
            dnt = '1'.encode('ascii')
        else:
            dnt = '0'.encode('ascii')
        req.setRawHeader('DNT'.encode('ascii'), dnt)
        req.setRawHeader('X-Do-Not-Track'.encode('ascii'), dnt)
        accept_language = config.get('network', 'accept-language')
        if accept_language is not None:
            req.setRawHeader('Accept-Language'.encode('ascii'),
                             accept_language.encode('ascii'))
        if PYQT_VERSION < 0x050301:
            # WORKAROUND (remove this when we bump the requirements to 5.3.1)
            #
            # If we don't disable our message handler, we get a freeze if a
            # warning is printed due to a PyQt bug, e.g. when clicking a
            # currency on http://ch.mouser.com/localsites/
            #
            # See http://www.riverbankcomputing.com/pipermail/pyqt/2014-June/034420.html
            with log.disable_qt_msghandler():
                reply = super().createRequest(op, req, outgoing_data)
        else:
            reply = super().createRequest(op, req, outgoing_data)
        self._requests.append(reply)
        reply.destroyed.connect(self._requests.remove)
        return reply
Example #7
0
    def createRequest(self, op, req, outgoing_data):
        """Return a new QNetworkReply object.

        Extend QNetworkAccessManager::createRequest to save requests in
        self._requests and handle custom schemes.

        Args:
             op: Operation op
             req: const QNetworkRequest & req
             outgoing_data: QIODevice * outgoingData

        Return:
            A QNetworkReply.
        """
        scheme = req.url().scheme()
        if scheme == 'https' and not SSL_AVAILABLE:
            return networkreply.ErrorNetworkReply(
                req, "SSL is not supported by the installed Qt library!",
                QNetworkReply.ProtocolUnknownError, self)
        elif scheme in self._scheme_handlers:
            return self._scheme_handlers[scheme].createRequest(
                op, req, outgoing_data)

        host_blocker = objreg.get('host-blocker')
        if (op == QNetworkAccessManager.GetOperation and
                req.url().host() in host_blocker.blocked_hosts and
                config.get('content', 'host-blocking-enabled')):
            log.webview.info("Request to {} blocked by host blocker.".format(
                req.url().host()))
            return networkreply.ErrorNetworkReply(
                req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
                self)

        if config.get('network', 'do-not-track'):
            dnt = '1'.encode('ascii')
        else:
            dnt = '0'.encode('ascii')
        req.setRawHeader('DNT'.encode('ascii'), dnt)
        req.setRawHeader('X-Do-Not-Track'.encode('ascii'), dnt)
        accept_language = config.get('network', 'accept-language')
        if accept_language is not None:
            req.setRawHeader('Accept-Language'.encode('ascii'),
                             accept_language.encode('ascii'))
        if PYQT_VERSION < 0x050301:
            # WORKAROUND (remove this when we bump the requirements to 5.3.1)
            #
            # If we don't disable our message handler, we get a freeze if a
            # warning is printed due to a PyQt bug, e.g. when clicking a
            # currency on http://ch.mouser.com/localsites/
            #
            # See http://www.riverbankcomputing.com/pipermail/pyqt/2014-June/034420.html
            with log.disable_qt_msghandler():
                reply = super().createRequest(op, req, outgoing_data)
        else:
            reply = super().createRequest(op, req, outgoing_data)
        self._requests.append(reply)
        reply.destroyed.connect(self._requests.remove)
        return reply
Example #8
0
 def __init__(self, win_id, parent=None):
     log.init.debug("Initializing NetworkManager")
     with log.disable_qt_msghandler():
         # WORKAROUND for a hang when a message is printed - See:
         # http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
         super().__init__(parent)
     log.init.debug("NetworkManager init done")
     self._win_id = win_id
     self._requests = []
     self._scheme_handlers = {
         'qute': qutescheme.QuteSchemeHandler(win_id),
     }
     self._set_cookiejar()
     self._set_cache()
     if SSL_AVAILABLE:
         self.sslErrors.connect(self.on_ssl_errors)
     self.authenticationRequired.connect(self.on_authentication_required)
     self.proxyAuthenticationRequired.connect(
         self.on_proxy_authentication_required)
     objreg.get('config').changed.connect(self.on_config_changed)
Example #9
0
 def __init__(self, win_id, parent=None):
     log.init.debug("Initializing NetworkManager")
     with log.disable_qt_msghandler():
         # WORKAROUND for a hang when a message is printed - See:
         # http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
         super().__init__(parent)
     log.init.debug("NetworkManager init done")
     self._win_id = win_id
     self._requests = []
     self._scheme_handlers = {
         'qute': qutescheme.QuteSchemeHandler(win_id),
     }
     self._set_cookiejar()
     self._set_cache()
     if SSL_AVAILABLE:
         self.sslErrors.connect(self.on_ssl_errors)
     self.authenticationRequired.connect(self.on_authentication_required)
     self.proxyAuthenticationRequired.connect(
         self.on_proxy_authentication_required)
     objreg.get('config').changed.connect(self.on_config_changed)
Example #10
0
 def __init__(self, win_id, tab_id, parent=None):
     log.init.debug("Initializing NetworkManager")
     with log.disable_qt_msghandler():
         # WORKAROUND for a hang when a message is printed - See:
         # http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
         super().__init__(parent)
     log.init.debug("NetworkManager init done")
     self.adopted_downloads = 0
     self._win_id = win_id
     self._tab_id = tab_id
     self._requests = []
     self._scheme_handlers = {
         "qute": qutescheme.QuteSchemeHandler(win_id),
         "file": filescheme.FileSchemeHandler(win_id),
     }
     self._set_cookiejar(private=config.get("general", "private-browsing"))
     self._set_cache()
     self.sslErrors.connect(self.on_ssl_errors)
     self._rejected_ssl_errors = collections.defaultdict(list)
     self._accepted_ssl_errors = collections.defaultdict(list)
     self.authenticationRequired.connect(self.on_authentication_required)
     self.proxyAuthenticationRequired.connect(self.on_proxy_authentication_required)
     objreg.get("config").changed.connect(self.on_config_changed)
Example #11
0
    def __init__(self, url, parent=None):
        """Resolve a PAC proxy from URL.

        Args:
            url: QUrl of a PAC proxy.
        """
        super().__init__(parent)

        pac_prefix = "pac+"

        assert url.scheme().startswith(pac_prefix)
        url.setScheme(url.scheme()[len(pac_prefix):])

        self._pac_url = url
        with log.disable_qt_msghandler():
            # WORKAROUND for a hang when messages are printed, see our
            # NetworkAccessManager subclass for details.
            self._manager: Optional[
                QNetworkAccessManager] = QNetworkAccessManager()
        self._manager.setProxy(QNetworkProxy(QNetworkProxy.NoProxy))
        self._pac = None
        self._error_message = None
        self._reply = None
Example #12
0
 def __init__(self, win_id, tab_id, parent=None):
     log.init.debug("Initializing NetworkManager")
     with log.disable_qt_msghandler():
         # WORKAROUND for a hang when a message is printed - See:
         # http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
         super().__init__(parent)
     log.init.debug("NetworkManager init done")
     self._win_id = win_id
     self._tab_id = tab_id
     self._requests = []
     self._scheme_handlers = {
         'qute': webkitqutescheme.QuteSchemeHandler(win_id),
         'file': filescheme.FileSchemeHandler(win_id),
     }
     self._set_cookiejar(private=config.get('general', 'private-browsing'))
     self._set_cache()
     self.sslErrors.connect(self.on_ssl_errors)
     self._rejected_ssl_errors = collections.defaultdict(list)
     self._accepted_ssl_errors = collections.defaultdict(list)
     self.authenticationRequired.connect(self.on_authentication_required)
     self.proxyAuthenticationRequired.connect(
         self.on_proxy_authentication_required)
     objreg.get('config').changed.connect(self.on_config_changed)
Example #13
0
    def createRequest(self, op, req, outgoing_data):
        """Return a new QNetworkReply object.

        Args:
             op: Operation op
             req: const QNetworkRequest & req
             outgoing_data: QIODevice * outgoingData

        Return:
            A QNetworkReply.
        """
        proxy_factory = objreg.get('proxy-factory', None)
        if proxy_factory is not None:
            proxy_error = proxy_factory.get_error()
            if proxy_error is not None:
                return networkreply.ErrorNetworkReply(
                    req, proxy_error, QNetworkReply.UnknownProxyError, self)

        scheme = req.url().scheme()
        if scheme in self._scheme_handlers:
            result = self._scheme_handlers[scheme].createRequest(
                op, req, outgoing_data)
            if result is not None:
                return result

        for header, value in shared.custom_headers():
            req.setRawHeader(header, value)

        host_blocker = objreg.get('host-blocker')
        if host_blocker.is_blocked(req.url()):
            log.webview.info("Request to {} blocked by host blocker.".format(
                req.url().host()))
            return networkreply.ErrorNetworkReply(
                req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
                self)

        # There are some scenarios where we can't figure out current_url:
        # - There's a generic NetworkManager, e.g. for downloads
        # - The download was in a tab which is now closed.
        current_url = QUrl()

        if self._tab_id is not None:
            try:
                tab = objreg.get('tab',
                                 scope='tab',
                                 window=self._win_id,
                                 tab=self._tab_id)
                current_url = tab.url()
            except (KeyError, RuntimeError, TypeError):
                # https://github.com/qutebrowser/qutebrowser/issues/889
                # Catching RuntimeError and TypeError because we could be in
                # the middle of the webpage shutdown here.
                current_url = QUrl()

        self.set_referer(req, current_url)

        if PYQT_VERSION < 0x050301:
            # WORKAROUND (remove this when we bump the requirements to 5.3.1)
            #
            # If we don't disable our message handler, we get a freeze if a
            # warning is printed due to a PyQt bug, e.g. when clicking a
            # currency on http://ch.mouser.com/localsites/
            #
            # See http://www.riverbankcomputing.com/pipermail/pyqt/2014-June/034420.html
            with log.disable_qt_msghandler():
                reply = super().createRequest(op, req, outgoing_data)
        else:
            reply = super().createRequest(op, req, outgoing_data)
        return reply
Example #14
0
    def createRequest(self, op, req, outgoing_data):
        """Return a new QNetworkReply object.

        Extend QNetworkAccessManager::createRequest to save requests in
        self._requests and handle custom schemes.

        Args:
             op: Operation op
             req: const QNetworkRequest & req
             outgoing_data: QIODevice * outgoingData

        Return:
            A QNetworkReply.
        """
        scheme = req.url().scheme()
        if scheme in self._scheme_handlers:
            result = self._scheme_handlers[scheme].createRequest(
                op, req, outgoing_data)
            if result is not None:
                return result

        host_blocker = objreg.get('host-blocker')
        if (op == QNetworkAccessManager.GetOperation
                and host_blocker.is_blocked(req.url())):
            log.webview.info("Request to {} blocked by host blocker.".format(
                req.url().host()))
            return networkreply.ErrorNetworkReply(
                req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
                self)

        if config.get('network', 'do-not-track'):
            dnt = '1'.encode('ascii')
        else:
            dnt = '0'.encode('ascii')
        req.setRawHeader('DNT'.encode('ascii'), dnt)
        req.setRawHeader('X-Do-Not-Track'.encode('ascii'), dnt)

        # Load custom headers
        custom_headers = config.get('network', 'custom-headers')

        if custom_headers is not None:
            for header, value in custom_headers.items():
                req.setRawHeader(header.encode('ascii'), value.encode('ascii'))

        # There are some scenarios where we can't figure out current_url:
        # - There's a generic NetworkManager, e.g. for downloads
        # - The download was in a tab which is now closed.
        current_url = QUrl()

        if self._tab_id is not None:
            try:
                tab = objreg.get('tab',
                                 scope='tab',
                                 window=self._win_id,
                                 tab=self._tab_id)
                current_url = tab.url()
            except (KeyError, RuntimeError, TypeError):
                # https://github.com/The-Compiler/qutebrowser/issues/889
                # Catching RuntimeError and TypeError because we could be in
                # the middle of the webpage shutdown here.
                current_url = QUrl()

        self.set_referer(req, current_url)

        accept_language = config.get('network', 'accept-language')
        if accept_language is not None:
            req.setRawHeader('Accept-Language'.encode('ascii'),
                             accept_language.encode('ascii'))
        if PYQT_VERSION < 0x050301:
            # WORKAROUND (remove this when we bump the requirements to 5.3.1)
            #
            # If we don't disable our message handler, we get a freeze if a
            # warning is printed due to a PyQt bug, e.g. when clicking a
            # currency on http://ch.mouser.com/localsites/
            #
            # See http://www.riverbankcomputing.com/pipermail/pyqt/2014-June/034420.html
            with log.disable_qt_msghandler():
                reply = super().createRequest(op, req, outgoing_data)
        else:
            reply = super().createRequest(op, req, outgoing_data)
        self._requests.append(reply)
        reply.destroyed.connect(self._requests.remove)
        return reply
Example #15
0
    def createRequest(self, op, req, outgoing_data):
        """Return a new QNetworkReply object.

        Extend QNetworkAccessManager::createRequest to save requests in
        self._requests and handle custom schemes.

        Args:
             op: Operation op
             req: const QNetworkRequest & req
             outgoing_data: QIODevice * outgoingData

        Return:
            A QNetworkReply.
        """
        scheme = req.url().scheme()
        if scheme in self._scheme_handlers:
            result = self._scheme_handlers[scheme].createRequest(
                op, req, outgoing_data)
            if result is not None:
                return result

        host_blocker = objreg.get('host-blocker')
        if (op == QNetworkAccessManager.GetOperation and
                host_blocker.is_blocked(req.url())):
            log.webview.info("Request to {} blocked by host blocker.".format(
                req.url().host()))
            return networkreply.ErrorNetworkReply(
                req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
                self)

        if config.get('network', 'do-not-track'):
            dnt = '1'.encode('ascii')
        else:
            dnt = '0'.encode('ascii')
        req.setRawHeader('DNT'.encode('ascii'), dnt)
        req.setRawHeader('X-Do-Not-Track'.encode('ascii'), dnt)

        if self._tab_id is None:
            current_url = QUrl()  # generic NetworkManager, e.g. for downloads
        else:
            webview = objreg.get('webview', scope='tab', window=self._win_id,
                                 tab=self._tab_id)
            current_url = webview.url()

        self.set_referer(req, current_url)

        accept_language = config.get('network', 'accept-language')
        if accept_language is not None:
            req.setRawHeader('Accept-Language'.encode('ascii'),
                             accept_language.encode('ascii'))
        if PYQT_VERSION < 0x050301:
            # WORKAROUND (remove this when we bump the requirements to 5.3.1)
            #
            # If we don't disable our message handler, we get a freeze if a
            # warning is printed due to a PyQt bug, e.g. when clicking a
            # currency on http://ch.mouser.com/localsites/
            #
            # See http://www.riverbankcomputing.com/pipermail/pyqt/2014-June/034420.html
            with log.disable_qt_msghandler():
                reply = super().createRequest(op, req, outgoing_data)
        else:
            reply = super().createRequest(op, req, outgoing_data)
        self._requests.append(reply)
        reply.destroyed.connect(self._requests.remove)
        return reply
Example #16
0
    def createRequest(self, op, req, outgoing_data):
        """Return a new QNetworkReply object.

        Extend QNetworkAccessManager::createRequest to save requests in
        self._requests and handle custom schemes.

        Args:
             op: Operation op
             req: const QNetworkRequest & req
             outgoing_data: QIODevice * outgoingData

        Return:
            A QNetworkReply.
        """
        scheme = req.url().scheme()
        if scheme in self._scheme_handlers:
            return self._scheme_handlers[scheme].createRequest(
                op, req, outgoing_data)

        host_blocker = objreg.get('host-blocker')
        if (op == QNetworkAccessManager.GetOperation and
                req.url().host() in host_blocker.blocked_hosts and
                config.get('content', 'host-blocking-enabled')):
            log.webview.info("Request to {} blocked by host blocker.".format(
                req.url().host()))
            return networkreply.ErrorNetworkReply(
                req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
                self)

        if config.get('network', 'do-not-track'):
            dnt = '1'.encode('ascii')
        else:
            dnt = '0'.encode('ascii')
        req.setRawHeader('DNT'.encode('ascii'), dnt)
        req.setRawHeader('X-Do-Not-Track'.encode('ascii'), dnt)

        if self._tab_id is None:
            current_url = QUrl()  # generic NetworkManager, e.g. for downloads
        else:
            webview = objreg.get('webview', scope='tab', window=self._win_id,
                                 tab=self._tab_id)
            current_url = webview.url()
        referer_header_conf = config.get('network', 'referer-header')

        try:
            if referer_header_conf == 'never':
                # Note: using ''.encode('ascii') sends a header with no value,
                # instead of no header at all
                req.setRawHeader('Referer'.encode('ascii'), QByteArray())
            elif (referer_header_conf == 'same-domain' and
                    not urlutils.same_domain(req.url(), current_url)):
                req.setRawHeader('Referer'.encode('ascii'), QByteArray())
            # If refer_header_conf is set to 'always', we leave the header
            # alone as QtWebKit did set it.
        except urlutils.InvalidUrlError:
            # req.url() or current_url can be invalid - this happens on
            # https://www.playstation.com/ for example.
            pass

        accept_language = config.get('network', 'accept-language')
        if accept_language is not None:
            req.setRawHeader('Accept-Language'.encode('ascii'),
                             accept_language.encode('ascii'))
        if PYQT_VERSION < 0x050301:
            # WORKAROUND (remove this when we bump the requirements to 5.3.1)
            #
            # If we don't disable our message handler, we get a freeze if a
            # warning is printed due to a PyQt bug, e.g. when clicking a
            # currency on http://ch.mouser.com/localsites/
            #
            # See http://www.riverbankcomputing.com/pipermail/pyqt/2014-June/034420.html
            with log.disable_qt_msghandler():
                reply = super().createRequest(op, req, outgoing_data)
        else:
            reply = super().createRequest(op, req, outgoing_data)
        self._requests.append(reply)
        reply.destroyed.connect(self._requests.remove)
        return reply
Example #17
0
    def createRequest(self, op, req, outgoing_data):
        """Return a new QNetworkReply object.

        Extend QNetworkAccessManager::createRequest to save requests in
        self._requests and handle custom schemes.

        Args:
             op: Operation op
             req: const QNetworkRequest & req
             outgoing_data: QIODevice * outgoingData

        Return:
            A QNetworkReply.
        """
        scheme = req.url().scheme()
        if scheme in self._scheme_handlers:
            result = self._scheme_handlers[scheme].createRequest(
                op, req, outgoing_data)
            if result is not None:
                return result

        for header, value in shared.custom_headers():
            req.setRawHeader(header, value)

        host_blocker = objreg.get('host-blocker')
        if (op == QNetworkAccessManager.GetOperation and
                host_blocker.is_blocked(req.url())):
            log.webview.info("Request to {} blocked by host blocker.".format(
                req.url().host()))
            return networkreply.ErrorNetworkReply(
                req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
                self)

        # There are some scenarios where we can't figure out current_url:
        # - There's a generic NetworkManager, e.g. for downloads
        # - The download was in a tab which is now closed.
        current_url = QUrl()

        if self._tab_id is not None:
            try:
                tab = objreg.get('tab', scope='tab', window=self._win_id,
                                 tab=self._tab_id)
                current_url = tab.url()
            except (KeyError, RuntimeError, TypeError):
                # https://github.com/The-Compiler/qutebrowser/issues/889
                # Catching RuntimeError and TypeError because we could be in
                # the middle of the webpage shutdown here.
                current_url = QUrl()

        self.set_referer(req, current_url)

        if PYQT_VERSION < 0x050301:
            # WORKAROUND (remove this when we bump the requirements to 5.3.1)
            #
            # If we don't disable our message handler, we get a freeze if a
            # warning is printed due to a PyQt bug, e.g. when clicking a
            # currency on http://ch.mouser.com/localsites/
            #
            # See http://www.riverbankcomputing.com/pipermail/pyqt/2014-June/034420.html
            with log.disable_qt_msghandler():
                reply = super().createRequest(op, req, outgoing_data)
        else:
            reply = super().createRequest(op, req, outgoing_data)
        self._requests.append(reply)
        reply.destroyed.connect(self._requests.remove)
        return reply
Example #18
0
    def createRequest(self, op, req, outgoing_data):
        """Return a new QNetworkReply object.

        Extend QNetworkAccessManager::createRequest to save requests in
        self._requests and handle custom schemes.

        Args:
             op: Operation op
             req: const QNetworkRequest & req
             outgoing_data: QIODevice * outgoingData

        Return:
            A QNetworkReply.
        """
        scheme = req.url().scheme()
        if scheme in self._scheme_handlers:
            result = self._scheme_handlers[scheme].createRequest(
                op, req, outgoing_data)
            if result is not None:
                return result

        host_blocker = objreg.get('host-blocker')
        if (op == QNetworkAccessManager.GetOperation
                and host_blocker.is_blocked(req.url())):
            log.webview.info("Request to {} blocked by host blocker.".format(
                req.url().host()))
            return networkreply.ErrorNetworkReply(
                req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
                self)

        if config.get('network', 'do-not-track'):
            dnt = '1'.encode('ascii')
        else:
            dnt = '0'.encode('ascii')
        req.setRawHeader('DNT'.encode('ascii'), dnt)
        req.setRawHeader('X-Do-Not-Track'.encode('ascii'), dnt)

        if self._tab_id is None:
            current_url = QUrl()  # generic NetworkManager, e.g. for downloads
        else:
            webview = objreg.get('webview',
                                 scope='tab',
                                 window=self._win_id,
                                 tab=self._tab_id)
            current_url = webview.url()

        self.set_referer(req, current_url)

        accept_language = config.get('network', 'accept-language')
        if accept_language is not None:
            req.setRawHeader('Accept-Language'.encode('ascii'),
                             accept_language.encode('ascii'))
        if PYQT_VERSION < 0x050301:
            # WORKAROUND (remove this when we bump the requirements to 5.3.1)
            #
            # If we don't disable our message handler, we get a freeze if a
            # warning is printed due to a PyQt bug, e.g. when clicking a
            # currency on http://ch.mouser.com/localsites/
            #
            # See http://www.riverbankcomputing.com/pipermail/pyqt/2014-June/034420.html
            with log.disable_qt_msghandler():
                reply = super().createRequest(op, req, outgoing_data)
        else:
            reply = super().createRequest(op, req, outgoing_data)
        self._requests.append(reply)
        reply.destroyed.connect(self._requests.remove)
        return reply