Example #1
0
    def acceptNavigationRequest(self,
                                _frame: QWebFrame,
                                request: QNetworkRequest,
                                typ: QWebPage.NavigationType):
        """Override acceptNavigationRequest to handle clicked links.

        Setting linkDelegationPolicy to DelegateAllLinks and using a slot bound
        to linkClicked won't work correctly, because when in a frameset, we
        have no idea in which frame the link should be opened.

        Checks if it should open it in a tab (middle-click or control) or not,
        and then conditionally opens the URL. Opening it in a new tab/window
        is handled in the slot connected to link_clicked.
        """
        url = request.url()
        target = self._tabdata.combined_target()
        log.webview.debug("navigation request: url {}, type {}, "
                          "target {}".format(
                              url.toDisplayString(),
                              debug.qenum_key(QWebPage, typ),
                              target))

        if typ == QWebPage.NavigationTypeReload:
            self.reloading.emit(url)
            return True
        elif typ != QWebPage.NavigationTypeLinkClicked:
            return True

        self.link_clicked.emit(url)

        return url.isValid() and target == usertypes.ClickTarget.normal
Example #2
0
    def acceptNavigationRequest(self, _frame: QWebFrame,
                                request: QNetworkRequest,
                                typ: QWebPage.NavigationType):
        """Override acceptNavigationRequest to handle clicked links.

        Setting linkDelegationPolicy to DelegateAllLinks and using a slot bound
        to linkClicked won't work correctly, because when in a frameset, we
        have no idea in which frame the link should be opened.

        Checks if it should open it in a tab (middle-click or control) or not,
        and then conditionally opens the URL. Opening it in a new tab/window
        is handled in the slot connected to link_clicked.
        """
        url = request.url()
        target = self._tabdata.combined_target()
        log.webview.debug("navigation request: url {}, type {}, "
                          "target {}".format(url.toDisplayString(),
                                             debug.qenum_key(QWebPage, typ),
                                             target))

        if typ == QWebPage.NavigationTypeReload:
            self.reloading.emit(url)
            return True
        elif typ != QWebPage.NavigationTypeLinkClicked:
            return True

        self.link_clicked.emit(url)

        return url.isValid() and target == usertypes.ClickTarget.normal
Example #3
0
 def doRequest(request: QNetworkRequest,
               body: Optional[bytes] = None,
               *_):
     key = method.upper(), request.url().toString()
     if body:
         self.request_bodies[key] = body
     return self.replies[key]
Example #4
0
    def requestHook(self, request: QNetworkRequest):
        super().requestHook(request)
        token = self._account.accessToken
        if not self._account.isLoggedIn or token is None:
            Logger.debug(
                "User is not logged in for Cloud API request to {url}".format(
                    url=request.url().toDisplayString()))
            return

        header_dict = {"Authorization": "Bearer {}".format(token)}
        self.addHeaders(request, header_dict)
Example #5
0
 def download(self, requestOrUrl, requestFileName=False, mainWindow=None):
     """
     Public method to download a file.
     
     @param requestOrUrl reference to a request object (QNetworkRequest)
         or a URL to be downloaded (QUrl)
     @keyparam requestFileName flag indicating to ask for the
         download file name (boolean)
     @keyparam mainWindow reference to the main window (HelpWindow)
     """
     request = QNetworkRequest(requestOrUrl)
     if request.url().isEmpty():
         return
     self.handleUnsupportedContent(
         self.__manager.get(request), requestFileName=requestFileName, download=True, mainWindow=mainWindow
     )
Example #6
0
    def acceptNavigationRequest(self,
                                _frame: QWebFrame,
                                request: QNetworkRequest,
                                typ: QWebPage.NavigationType):
        """Override acceptNavigationRequest to handle clicked links.

        Setting linkDelegationPolicy to DelegateAllLinks and using a slot bound
        to linkClicked won't work correctly, because when in a frameset, we
        have no idea in which frame the link should be opened.

        Checks if it should open it in a tab (middle-click or control) or not,
        and then conditionally opens the URL here or in another tab/window.
        """
        url = request.url()
        log.webview.debug("navigation request: url {}, type {}, "
                          "target {} override {}".format(
                              url.toDisplayString(),
                              debug.qenum_key(QWebPage, typ),
                              self.open_target,
                              self._tabdata.override_target))

        if self._tabdata.override_target is not None:
            target = self._tabdata.override_target
            self._tabdata.override_target = None
        else:
            target = self.open_target

        if typ == QWebPage.NavigationTypeReload:
            self.reloading.emit(url)
            return True
        elif typ != QWebPage.NavigationTypeLinkClicked:
            return True

        if not url.isValid():
            msg = urlutils.get_errstring(url, "Invalid link clicked")
            message.error(msg)
            self.open_target = usertypes.ClickTarget.normal
            return False

        if target == usertypes.ClickTarget.normal:
            return True

        tab = shared.get_tab(self._win_id, target)
        tab.openurl(url)
        self.open_target = usertypes.ClickTarget.normal
        return False
Example #7
0
    def acceptNavigationRequest(self,
                                _frame: QWebFrame,
                                request: QNetworkRequest,
                                typ: QWebPage.NavigationType):
        """Override acceptNavigationRequest to handle clicked links.

        Setting linkDelegationPolicy to DelegateAllLinks and using a slot bound
        to linkClicked won't work correctly, because when in a frameset, we
        have no idea in which frame the link should be opened.

        Checks if it should open it in a tab (middle-click or control) or not,
        and then conditionally opens the URL here or in another tab/window.
        """
        url = request.url()
        log.webview.debug("navigation request: url {}, type {}, "
                          "target {} override {}".format(
                              url.toDisplayString(),
                              debug.qenum_key(QWebPage, typ),
                              self.open_target,
                              self._tabdata.override_target))

        if self._tabdata.override_target is not None:
            target = self._tabdata.override_target
            self._tabdata.override_target = None
        else:
            target = self.open_target

        if typ == QWebPage.NavigationTypeReload:
            self.reloading.emit(url)
            return True
        elif typ != QWebPage.NavigationTypeLinkClicked:
            return True

        if not url.isValid():
            msg = urlutils.get_errstring(url, "Invalid link clicked")
            message.error(msg)
            self.open_target = usertypes.ClickTarget.normal
            return False

        if target == usertypes.ClickTarget.normal:
            return True

        tab = shared.get_tab(self._win_id, target)
        tab.openurl(url)
        self.open_target = usertypes.ClickTarget.normal
        return False
Example #8
0
 def download(self, requestOrUrl, requestFileName=False, mainWindow=None):
     """
     Public method to download a file.
     
     @param requestOrUrl reference to a request object (QNetworkRequest)
         or a URL to be downloaded (QUrl)
     @keyparam requestFileName flag indicating to ask for the
         download file name (boolean)
     @keyparam mainWindow reference to the main window (HelpWindow)
     """
     request = QNetworkRequest(requestOrUrl)
     if request.url().isEmpty():
         return
     self.handleUnsupportedContent(self.__manager.get(request),
                                   requestFileName=requestFileName,
                                   download=True,
                                   mainWindow=mainWindow)
Example #9
0
    def acceptNavigationRequest(self,
                                frame: QWebFrame,
                                request: QNetworkRequest,
                                typ: QWebPage.NavigationType) -> bool:
        """Override acceptNavigationRequest to handle clicked links.

        Setting linkDelegationPolicy to DelegateAllLinks and using a slot bound
        to linkClicked won't work correctly, because when in a frameset, we
        have no idea in which frame the link should be opened.

        Checks if it should open it in a tab (middle-click or control) or not,
        and then conditionally opens the URL here or in another tab/window.
        """
        type_map = {
            QWebPage.NavigationTypeLinkClicked:
                usertypes.NavigationRequest.Type.link_clicked,
            QWebPage.NavigationTypeFormSubmitted:
                usertypes.NavigationRequest.Type.form_submitted,
            QWebPage.NavigationTypeFormResubmitted:
                usertypes.NavigationRequest.Type.form_resubmitted,
            QWebPage.NavigationTypeBackOrForward:
                usertypes.NavigationRequest.Type.back_forward,
            QWebPage.NavigationTypeReload:
                usertypes.NavigationRequest.Type.reloaded,
            QWebPage.NavigationTypeOther:
                usertypes.NavigationRequest.Type.other,
        }
        is_main_frame = frame is self.mainFrame()
        navigation = usertypes.NavigationRequest(url=request.url(),
                                                 navigation_type=type_map[typ],
                                                 is_main_frame=is_main_frame)

        if navigation.navigation_type == navigation.Type.reloaded:
            self.reloading.emit(navigation.url)

        self.navigation_request.emit(navigation)
        return navigation.accepted
Example #10
0
    def acceptNavigationRequest(self,
                                frame: QWebFrame,
                                request: QNetworkRequest,
                                typ: QWebPage.NavigationType) -> bool:
        """Override acceptNavigationRequest to handle clicked links.

        Setting linkDelegationPolicy to DelegateAllLinks and using a slot bound
        to linkClicked won't work correctly, because when in a frameset, we
        have no idea in which frame the link should be opened.

        Checks if it should open it in a tab (middle-click or control) or not,
        and then conditionally opens the URL here or in another tab/window.
        """
        type_map = {
            QWebPage.NavigationTypeLinkClicked:
                usertypes.NavigationRequest.Type.link_clicked,
            QWebPage.NavigationTypeFormSubmitted:
                usertypes.NavigationRequest.Type.form_submitted,
            QWebPage.NavigationTypeFormResubmitted:
                usertypes.NavigationRequest.Type.form_resubmitted,
            QWebPage.NavigationTypeBackOrForward:
                usertypes.NavigationRequest.Type.back_forward,
            QWebPage.NavigationTypeReload:
                usertypes.NavigationRequest.Type.reloaded,
            QWebPage.NavigationTypeOther:
                usertypes.NavigationRequest.Type.other,
        }
        is_main_frame = frame is self.mainFrame()
        navigation = usertypes.NavigationRequest(url=request.url(),
                                                 navigation_type=type_map[typ],
                                                 is_main_frame=is_main_frame)

        if navigation.navigation_type == navigation.Type.reloaded:
            self.reloading.emit(navigation.url)

        self.navigation_request.emit(navigation)
        return navigation.accepted
    def request(self,
                url,
                method="GET",
                body=None,
                headers=None,
                redirections=DEFAULT_MAX_REDIRECTS,
                connection_type=None,
                blocking=True):
        """
        Make a network request by calling QgsNetworkAccessManager.
        redirections argument is ignored and is here only for httplib2 compatibility.
        """
        self.http_call_result.url = url
        self.msg_log(u'http_call request: {0}'.format(url))

        self.blocking_mode = blocking
        req = QNetworkRequest()
        # Avoid double quoting form QUrl
        url = urllib.parse.unquote(url)
        req.setUrl(QUrl(url))
        if headers is not None:
            # This fixes a weird error with compressed content not being correctly
            # inflated.
            # If you set the header on the QNetworkRequest you are basically telling
            # QNetworkAccessManager "I know what I'm doing, please don't do any content
            # encoding processing".
            # See: https://bugs.webkit.org/show_bug.cgi?id=63696#c1
            try:
                del headers['Accept-Encoding']
            except KeyError:
                pass
            for k, v in list(headers.items()):
                self.msg_log("Setting header %s to %s" % (k, v))
                req.setRawHeader(k, v)
        if self.authid:
            self.msg_log("Update request w/ authid: {0}".format(self.authid))
            QgsAuthManager.instance().updateNetworkRequest(req, self.authid)
        if self.reply is not None and self.reply.isRunning():
            self.reply.close()
        if method.lower() == 'delete':
            func = getattr(QgsNetworkAccessManager.instance(),
                           'deleteResource')
        else:
            func = getattr(QgsNetworkAccessManager.instance(), method.lower())
        # Calling the server ...
        # Let's log the whole call for debugging purposes:
        self.msg_log("Sending %s request to %s" %
                     (method.upper(), req.url().toString()))
        self.on_abort = False
        headers = {str(h): str(req.rawHeader(h)) for h in req.rawHeaderList()}
        for k, v in list(headers.items()):
            self.msg_log("%s: %s" % (k, v))
        if method.lower() in ['post', 'put']:
            if isinstance(body, file):
                body = body.read()
            self.reply = func(req, body)
        else:
            self.reply = func(req)
        if self.authid:
            self.msg_log("Update reply w/ authid: {0}".format(self.authid))
            QgsAuthManager.instance().updateNetworkReply(
                self.reply, self.authid)

        # necessary to trap local timeout manage by QgsNetworkAccessManager
        # calling QgsNetworkAccessManager::abortRequest
        QgsNetworkAccessManager.instance().requestTimedOut.connect(
            self.requestTimedOut)

        self.reply.sslErrors.connect(self.sslErrors)
        self.reply.finished.connect(self.replyFinished)
        self.reply.downloadProgress.connect(self.downloadProgress)

        # block if blocking mode otherwise return immediately
        # it's up to the caller to manage listeners in case of no blocking mode
        if not self.blocking_mode:
            return None, None

        # Call and block
        self.el = QEventLoop()
        self.reply.finished.connect(self.el.quit)

        # Catch all exceptions (and clean up requests)
        try:
            self.el.exec_(QEventLoop.ExcludeUserInputEvents)
        except Exception as e:
            raise e

        if self.reply:
            self.reply.finished.disconnect(self.el.quit)

        # emit exception in case of error
        if not self.http_call_result.ok:
            if self.http_call_result.exception and not self.exception_class:
                raise self.http_call_result.exception
            elif self.exception_class:
                raise self.exception_class(self.http_call_result.reason)
            else:
                raise RequestsException('Unknown reason')

        return self.http_call_result, self.http_call_result.content
Example #12
0
 def createRequest(self, op, request, outgoingData=None):
     """
     Public method to create a request.
     
     @param op the operation to be performed
         (QNetworkAccessManager.Operation)
     @param request reference to the request object (QNetworkRequest)
     @param outgoingData reference to an IODevice containing data to be sent
         (QIODevice)
     @return reference to the created reply object (QNetworkReply)
     """
     scheme = request.url().scheme()
     if scheme == "https" and \
             (not SSL_AVAILABLE or not QSslSocket.supportsSsl()):
         from .NetworkProtocolUnknownErrorReply import \
             NetworkProtocolUnknownErrorReply
         return NetworkProtocolUnknownErrorReply(scheme, self)
     
     import Helpviewer.HelpWindow
     
     if op == QNetworkAccessManager.PostOperation and \
             outgoingData is not None:
         outgoingDataByteArray = outgoingData.peek(1024 * 1024)
         Helpviewer.HelpWindow.HelpWindow.passwordManager().post(
             request, outgoingDataByteArray)
     
     reply = None
     if scheme in self.__schemeHandlers:
         reply = self.__schemeHandlers[scheme]\
                     .createRequest(op, request, outgoingData)
     if reply is not None:
         return reply
     
     # give GreaseMonkey the chance to create a request
     reply = Helpviewer.HelpWindow.HelpWindow.greaseMonkeyManager()\
         .createRequest(op, request, outgoingData)
     if reply is not None:
         return reply
     
     req = QNetworkRequest(request)
     if req.rawHeader(b"X-Eric6-UserLoadAction") == QByteArray(b"1"):
         req.setRawHeader(b"X-Eric6-UserLoadAction", QByteArray())
         req.setAttribute(QNetworkRequest.User + 200, "")
     else:
         req.setAttribute(
             QNetworkRequest.User + 200, req.rawHeader(b"Referer"))
     
     if hasattr(QNetworkRequest, 'HttpPipeliningAllowedAttribute'):
         req.setAttribute(
             QNetworkRequest.HttpPipeliningAllowedAttribute, True)
     if not self.__acceptLanguage.isEmpty():
         req.setRawHeader(b"Accept-Language", self.__acceptLanguage)
     
     # AdBlock code
     if op == QNetworkAccessManager.GetOperation:
         if self.__adblockNetwork is None:
             self.__adblockNetwork = \
                 Helpviewer.HelpWindow.HelpWindow.adBlockManager().network()
         reply = self.__adblockNetwork.block(req)
         if reply is not None:
             reply.setParent(self)
             return reply
     
     # set cache policy
     if op == QNetworkAccessManager.GetOperation:
         urlHost = req.url().host()
         for host in Preferences.getHelp("NoCacheHosts"):
             if host in urlHost:
                 req.setAttribute(
                     QNetworkRequest.CacheLoadControlAttribute,
                     QNetworkRequest.AlwaysNetwork)
                 break
         else:
             req.setAttribute(
                 QNetworkRequest.CacheLoadControlAttribute,
                 Preferences.getHelp("CachePolicy"))
     else:
         req.setAttribute(
             QNetworkRequest.CacheLoadControlAttribute,
             QNetworkRequest.AlwaysNetwork)
     
     # Do Not Track feature
     if self.__doNotTrack:
         req.setRawHeader(b"DNT", b"1")
         req.setRawHeader(b"X-Do-Not-Track", b"1")
     
     # Send referer header?
     if not self.__sendReferer and \
        req.url().host() not in Preferences.getHelp("SendRefererWhitelist"):
         req.setRawHeader(b"Referer", b"")
     
     reply = QNetworkAccessManager.createRequest(
         self, op, req, outgoingData)
     self.requestCreated.emit(op, req, reply)
     
     return reply
Example #13
0
 def post(self, request: QNetworkRequest, data=None, *__args):
     self.data['data'] = data
     print('request', request.url())
     q_data = bytearray(JsonParser.dump(self.data), encoding='utf-8')
     return super().post(request, q_data, *__args)
Example #14
0
 def doRequest(request: QNetworkRequest, body: Optional[bytes] = None, *_):
     key = method.upper(), request.url().toString()
     if body:
         self.request_bodies[key] = body
     return self.replies[key]
Example #15
0
 def _setAuth(self, request: QNetworkRequest):
     current_url = request.url().toString()
     operator = "&" if current_url.find("?") > 0 else "?"
     new_url = QUrl("{}{}key={}".format(current_url, operator, Settings.MYMINIFACTORY_API_TOKEN))
     request.setUrl(new_url)
Example #16
0
    def execute_request(self, url, **kwargs):
        """
        Uses QgsNetworkAccessManager and QgsAuthManager.
        """
        method = kwargs.get('http_method', 'get')

        headers = kwargs.get('headers', {})
        # This fixes a weird error with compressed content not being correctly
        # inflated.
        # If you set the header on the QNetworkRequest you are basically telling
        # QNetworkAccessManager "I know what I'm doing, please don't do any content
        # encoding processing".
        # See: https://bugs.webkit.org/show_bug.cgi?id=63696#c1
        try:
            del headers[b'Accept-Encoding']
        except KeyError as ke:
            # only debugging here as after 1st remove it isn't there anymore
            self.util.msg_log_debug(
                u'unexpected error deleting request header: {}'.format(ke))
            pass

        # Avoid double quoting form QUrl
        url = unquote(url)

        self.util.msg_log_debug(u'http_call request: {} {}'.format(
            method, url))

        class Response:
            status_code = 200
            status_message = 'OK'
            text = ''
            ok = True
            headers = {}
            reason = ''
            exception = None

            def iter_content(self, _):
                return [self.text]

        self.http_call_result = Response()
        url = self.util.remove_newline(url)

        req = QNetworkRequest()
        req.setUrl(QUrl(url))
        req.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True)

        for k, v in headers.items():
            self.util.msg_log_debug("%s: %s" % (k, v))
            try:
                req.setRawHeader(k, v)
            except:
                self.util.msg_log_error(
                    u'FAILED to set header: {} => {}'.format(k, v))
                self.util.msg_log_last_exception()
        if self.settings.authcfg:
            self.util.msg_log_debug(u'before updateNetworkRequest')
            QgsApplication.authManager().updateNetworkRequest(
                req, self.settings.authcfg)
            self.util.msg_log_debug(u'before updateNetworkRequest')

        if self.reply is not None and self.reply.isRunning():
            self.reply.close()

        self.util.msg_log_debug(u'getting QgsNetworkAccessManager.instance()')
        #func = getattr(QgsNetworkAccessManager.instance(), method)
        #func = QgsNetworkAccessManager().get(req)

        #manager = QNetworkAccessManager()
        #event = QEventLoop()
        #response = manager.get(QNetworkRequest(QUrl(url)))
        #response.downloadProgress.connect(self.download_progress)
        #response.finished.connect(event.quit)
        #event.exec()
        #response_msg = response.readAll()
        ##response_msg = str(response_msg)
        #response_msg = str(response_msg, encoding='utf-8')
        ##response_msg = response_msg.decode('utf-8')
        #response.deleteLater()
        #self.util.msg_log_debug(u'response message:\n{} ...'.format(response_msg[:255]))
        #self.http_call_result.text = response_msg  # in Python3 all strings are unicode, so QString is not defined
        #return self.http_call_result

        # Calling the server ...
        self.util.msg_log_debug('before self.reply = func(req)')
        #self.reply = func(req)
        #self.reply = QgsNetworkAccessManager.instance().get(req)
        method_call = getattr(QgsNetworkAccessManager.instance(), method)
        self.reply = method_call(req)
        #self.reply.setReadBufferSize(1024*1024*1024)
        #self.reply.setReadBufferSize(1024 * 1024 * 1024 * 1024)
        self.reply.setReadBufferSize(0)
        self.util.msg_log_debug('after self.reply = func(req)')

        # Let's log the whole call for debugging purposes:
        if self.settings.debug:
            self.util.msg_log_debug("\nSending %s request to %s" %
                                    (method.upper(), req.url().toString()))
            headers = {
                str(h): str(req.rawHeader(h))
                for h in req.rawHeaderList()
            }
            for k, v in headers.items():
                try:
                    self.util.msg_log_debug("%s: %s" % (k, v))
                except:
                    self.util.msg_log_debug('error logging headers')

        if self.settings.authcfg:
            self.util.msg_log_debug("update reply w/ authcfg: {0}".format(
                self.settings.authcfg))
            QgsApplication.authManager().updateNetworkReply(
                self.reply, self.settings.authcfg)

        self.util.msg_log_debug('before connecting to events')

        # connect downloadProgress event
        try:
            self.reply.downloadProgress.connect(self.download_progress)
            #pass
        except:
            self.util.msg_log_error(
                'error connecting "downloadProgress" event')
            self.util.msg_log_last_exception()

        # connect reply finished event
        try:
            self.reply.finished.connect(self.reply_finished)
            #pass
        except:
            self.util.msg_log_error(
                'error connecting reply "finished" progress event')
            self.util.msg_log_last_exception()
        self.util.msg_log_debug('after connecting to events')

        # Call and block
        self.event_loop = QEventLoop()
        try:
            self.reply.finished.connect(self.event_loop.quit)
        except:
            self.util.msg_log_error(
                'error connecting reply "finished" progress event to event loop quit'
            )
            self.util.msg_log_last_exception()

        self.mb_downloaded = 0
        # Catch all exceptions (and clean up requests)
        self.event_loop.exec()

        # Let's log the whole response for debugging purposes:
        if self.settings.debug:
            self.util.msg_log_debug(
                u'\nGot response [{}/{}] ({} bytes) from:\n{}\nexception:{}'.
                format(self.http_call_result.status_code,
                       self.http_call_result.status_message,
                       len(self.http_call_result.text),
                       self.reply.url().toString(),
                       self.http_call_result.exception))
            headers = {
                str(h): str(self.reply.rawHeader(h))
                for h in self.reply.rawHeaderList()
            }
            for k, v in headers.items():
                self.util.msg_log_debug("%s: %s" % (k, v))
            self.util.msg_log_debug("Payload :\n%s ......" %
                                    self.http_call_result.text[:255])

        self.reply.close()
        self.util.msg_log_debug("Deleting reply ...")
        try:
            self.reply.deleteLater()
        except:
            self.util.msg_log_error('unexpected error deleting QNetworkReply')
            self.util.msg_log_last_exception()

        self.reply = None

        if self.http_call_result.exception is not None:
            self.util.msg_log_error('http_call_result.exception is not None')
            self.http_call_result.ok = False
            # raise self.http_call_result.exception
        return self.http_call_result
    def createRequest(self, op, request, outgoingData=None):
        """
        Public method to create a request.
        
        @param op the operation to be performed
            (QNetworkAccessManager.Operation)
        @param request reference to the request object (QNetworkRequest)
        @param outgoingData reference to an IODevice containing data to be sent
            (QIODevice)
        @return reference to the created reply object (QNetworkReply)
        """
        scheme = request.url().scheme()
        if scheme == "https" and \
                (not SSL_AVAILABLE or not QSslSocket.supportsSsl()):
            from .NetworkProtocolUnknownErrorReply import \
                NetworkProtocolUnknownErrorReply
            return NetworkProtocolUnknownErrorReply(scheme, self)

        import Helpviewer.HelpWindow

        if op == QNetworkAccessManager.PostOperation and \
                outgoingData is not None:
            outgoingDataByteArray = outgoingData.peek(1024 * 1024)
            Helpviewer.HelpWindow.HelpWindow.passwordManager().post(
                request, outgoingDataByteArray)

        reply = None
        if scheme in self.__schemeHandlers:
            reply = self.__schemeHandlers[scheme]\
                        .createRequest(op, request, outgoingData)
        if reply is not None:
            return reply

        # give GreaseMonkey the chance to create a request
        reply = Helpviewer.HelpWindow.HelpWindow.greaseMonkeyManager()\
            .createRequest(op, request, outgoingData)
        if reply is not None:
            return reply

        req = QNetworkRequest(request)
        if req.rawHeader(b"X-Eric6-UserLoadAction") == QByteArray(b"1"):
            req.setRawHeader(b"X-Eric6-UserLoadAction", QByteArray())
            req.setAttribute(QNetworkRequest.User + 200, "")
        else:
            req.setAttribute(QNetworkRequest.User + 200,
                             req.rawHeader(b"Referer"))

        if hasattr(QNetworkRequest, 'HttpPipeliningAllowedAttribute'):
            req.setAttribute(QNetworkRequest.HttpPipeliningAllowedAttribute,
                             True)
        if not self.__acceptLanguage.isEmpty():
            req.setRawHeader(b"Accept-Language", self.__acceptLanguage)

        # AdBlock code
        if op == QNetworkAccessManager.GetOperation:
            if self.__adblockNetwork is None:
                self.__adblockNetwork = \
                    Helpviewer.HelpWindow.HelpWindow.adBlockManager().network()
            reply = self.__adblockNetwork.block(req)
            if reply is not None:
                reply.setParent(self)
                return reply

        # set cache policy
        if op == QNetworkAccessManager.GetOperation:
            urlHost = req.url().host()
            for host in Preferences.getHelp("NoCacheHosts"):
                if host in urlHost:
                    req.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
                                     QNetworkRequest.AlwaysNetwork)
                    break
            else:
                req.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
                                 Preferences.getHelp("CachePolicy"))
        else:
            req.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
                             QNetworkRequest.AlwaysNetwork)

        # Do Not Track feature
        if self.__doNotTrack:
            req.setRawHeader(b"DNT", b"1")
            req.setRawHeader(b"X-Do-Not-Track", b"1")

        # Send referer header?
        if not self.__sendReferer and \
           req.url().host() not in Preferences.getHelp("SendRefererWhitelist"):
            req.setRawHeader(b"Referer", b"")

        reply = QNetworkAccessManager.createRequest(self, op, req,
                                                    outgoingData)
        self.requestCreated.emit(op, req, reply)

        return reply
Example #18
0
 def _injectApiToken(request: QNetworkRequest) -> None:
     current_url = request.url().toString()
     operator = "&" if current_url.find("?") > 0 else "?"
     new_url = QUrl("{}{}key={}".format(current_url, operator,
                                        Settings.MYMINIFACTORY_API_TOKEN))
     request.setUrl(new_url)