Example #1
0
    def _process_reply(self, reply):
        try:
            request, handler, xml, refresh = self._active_requests.pop(reply)
        except KeyError:
            log.error("Request not found for %s" %
                      reply.request().url().toString(QUrl.RemoveUserInfo))
            return
        error = int(reply.error())
        if error:
            log.error(
                "Network request error for %s: %s (QT code %d, HTTP code %s)",
                reply.request().url().toString(QUrl.RemoveUserInfo),
                reply.errorString(), error,
                repr(
                    reply.attribute(
                        QtNetwork.QNetworkRequest.HttpStatusCodeAttribute)))
            if handler is not None:
                handler(str(reply.readAll()), reply, error)
        else:
            redirect = reply.attribute(
                QtNetwork.QNetworkRequest.RedirectionTargetAttribute)
            fromCache = reply.attribute(
                QtNetwork.QNetworkRequest.SourceIsFromCacheAttribute)
            cached = ' (CACHED)' if fromCache else ''
            log.debug(
                "Received reply for %s: HTTP %d (%s) %s",
                reply.request().url().toString(QUrl.RemoveUserInfo),
                reply.attribute(
                    QtNetwork.QNetworkRequest.HttpStatusCodeAttribute),
                reply.attribute(
                    QtNetwork.QNetworkRequest.HttpReasonPhraseAttribute),
                cached)
            if handler is not None:
                # Redirect if found and not infinite
                if redirect and not XmlWebService.urls_equivalent(
                        redirect,
                        reply.request().url()):
                    log.debug("Redirect to %s requested",
                              redirect.toString(QUrl.RemoveUserInfo))
                    redirect_host = str(redirect.host())
                    redirect_port = redirect.port(80)

                    url = request.url()
                    original_host = str(url.host())
                    original_port = url.port(80)

                    if ((original_host, original_port) in REQUEST_DELAY and
                        (redirect_host, redirect_port) not in REQUEST_DELAY):
                        log.debug(
                            "Setting rate limit for %s:%i to %i" %
                            (redirect_host, redirect_port,
                             REQUEST_DELAY[(original_host, original_port)]))
                        REQUEST_DELAY[(redirect_host, redirect_port)] =\
                            REQUEST_DELAY[(original_host, original_port)]

                    self.get(
                        redirect_host,
                        redirect_port,
                        # retain path, query string and anchors from redirect URL
                        redirect.toString(QUrl.RemoveAuthority
                                          | QUrl.RemoveScheme),
                        handler,
                        xml,
                        priority=True,
                        important=True,
                        refresh=refresh,
                        cacheloadcontrol=request.attribute(
                            QtNetwork.QNetworkRequest.CacheLoadControlAttribute
                        ))
                elif redirect:
                    log.error(
                        "Redirect loop: %s",
                        reply.request().url().toString(QUrl.RemoveUserInfo))
                    handler(str(reply.readAll()), reply, error)
                elif xml:
                    document = _read_xml(QXmlStreamReader(reply))
                    handler(document, reply, error)
                else:
                    handler(str(reply.readAll()), reply, error)
        reply.close()
        self.num_pending_web_requests -= 1
        self.tagger.tagger_stats_changed.emit()
Example #2
0
    def _handle_reply(self, reply, request, handler, xml, refresh):
        error = int(reply.error())
        if error:
            log.error(
                "Network request error for %s: %s (QT code %d, HTTP code %s)",
                reply.request().url().toString(QUrl.RemoveUserInfo),
                reply.errorString(), error,
                repr(
                    reply.attribute(
                        QtNetwork.QNetworkRequest.HttpStatusCodeAttribute)))
            if handler is not None:
                handler(str(reply.readAll()), reply, error)
        else:
            redirect = reply.attribute(
                QtNetwork.QNetworkRequest.RedirectionTargetAttribute)
            fromCache = reply.attribute(
                QtNetwork.QNetworkRequest.SourceIsFromCacheAttribute)
            cached = ' (CACHED)' if fromCache else ''
            log.debug(
                "Received reply for %s: HTTP %d (%s) %s",
                reply.request().url().toString(QUrl.RemoveUserInfo),
                reply.attribute(
                    QtNetwork.QNetworkRequest.HttpStatusCodeAttribute),
                reply.attribute(
                    QtNetwork.QNetworkRequest.HttpReasonPhraseAttribute),
                cached)
            if handler is not None:
                # Redirect if found and not infinite
                if redirect:
                    url = request.url()
                    # merge with base url (to cover the possibility of the URL being relative)
                    redirect = url.resolved(redirect)
                    if not XmlWebService.urls_equivalent(
                            redirect,
                            reply.request().url()):
                        log.debug("Redirect to %s requested",
                                  redirect.toString(QUrl.RemoveUserInfo))
                        redirect_host = str(redirect.host())
                        redirect_port = self.url_port(redirect)
                        redirect_query = dict(redirect.encodedQueryItems())
                        redirect_path = redirect.path()

                        original_host = str(url.host())
                        original_port = self.url_port(url)

                        if ((original_host, original_port) in REQUEST_DELAY
                                and (redirect_host, redirect_port)
                                not in REQUEST_DELAY):
                            log.debug(
                                "Setting rate limit for %s:%i to %i" %
                                (redirect_host, redirect_port, REQUEST_DELAY[
                                    (original_host, original_port)]))
                            REQUEST_DELAY[(redirect_host, redirect_port)] =\
                                REQUEST_DELAY[(original_host, original_port)]

                        self.get(redirect_host,
                                 redirect_port,
                                 redirect_path,
                                 handler,
                                 xml,
                                 priority=True,
                                 important=True,
                                 refresh=refresh,
                                 queryargs=redirect_query,
                                 cacheloadcontrol=request.attribute(
                                     QtNetwork.QNetworkRequest.
                                     CacheLoadControlAttribute))
                    else:
                        log.error(
                            "Redirect loop: %s",
                            reply.request().url().toString(
                                QUrl.RemoveUserInfo))
                        handler(str(reply.readAll()), reply, error)
                elif xml:
                    document = _read_xml(QXmlStreamReader(reply))
                    handler(document, reply, error)
                else:
                    handler(str(reply.readAll()), reply, error)
Example #3
0
 def __init__(self):
     self._xmlReader = QXmlStreamReader()