diff --git a/picard/webservice.py b/picard/webservice.py index aae18cdf7..4d72c410d 100644 --- a/picard/webservice.py +++ b/picard/webservice.py @@ -230,52 +230,60 @@ class XmlWebService(QtCore.QObject): log.error("Request not found for %s" % reply.request().url().toString(QUrl.RemoveUserInfo)) return error = int(reply.error()) - 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: - if error: - log.error("Network request error for %s: %s (QT code %d, HTTP code %d)", - reply.request().url().toString(QUrl.RemoveUserInfo), - reply.errorString(), - error, - reply.attribute(QtNetwork.QNetworkRequest.HttpStatusCodeAttribute)) - - # 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 xml: - document = _read_xml(QXmlStreamReader(reply)) - handler(document, reply, error) - else: + 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()