예제 #1
0
 def urls_equivalent(leftUrl, rightUrl):
     """
         Lazy method to determine whether two QUrls are equivalent. At the moment it assumes that if ports are unset
         that they are port 80 - in absence of a URL normalization function in QUrl or ability to use qHash
         from QT 4.7
     """
     return leftUrl.port(80) == rightUrl.port(80) and \
         leftUrl.toString(QUrl.FormattingOption(QUrl.RemovePort)) == rightUrl.toString(QUrl.FormattingOption(QUrl.RemovePort))
예제 #2
0
    def _process_reply(self, reply):
        try:
            request, handler, xml = self._active_requests.pop(reply)
        except KeyError:
            self.log.error("Error: Request not found for %s" %
                           str(reply.request().url().toString()))
            return
        error = int(reply.error())
        redirect = reply.attribute(
            QtNetwork.QNetworkRequest.RedirectionTargetAttribute).toUrl()
        self.log.debug(
            "Received reply for %s: HTTP %d (%s)",
            reply.request().url().toString(),
            reply.attribute(
                QtNetwork.QNetworkRequest.HttpStatusCodeAttribute).toInt()[0],
            reply.attribute(QtNetwork.QNetworkRequest.HttpReasonPhraseAttribute
                            ).toString())
        if handler is not None:
            if error:
                self.log.error(
                    "Network request error for %s: %s (QT code %d, HTTP code %d)",
                    reply.request().url().toString(), reply.errorString(),
                    error,
                    reply.attribute(QtNetwork.QNetworkRequest.
                                    HttpStatusCodeAttribute).toInt()[0])

            # Redirect if found and not infinite
            if not redirect.isEmpty() and not XmlWebService.urls_equivalent(
                    redirect,
                    reply.request().url()):
                self.log.debug("Redirect to %s requested", redirect.toString())
                self.get(
                    str(redirect.host()),
                    redirect.port(80),
                    # retain path, query string and anchors from redirect URL
                    redirect.toString(
                        QUrl.FormattingOption(QUrl.RemoveAuthority
                                              | QUrl.RemoveScheme)),
                    handler,
                    xml,
                    priority=True,
                    important=True)
            elif xml:
                xml_handler = XmlHandler()
                xml_handler.init()
                xml_reader = QtXml.QXmlSimpleReader()
                xml_reader.setContentHandler(xml_handler)
                xml_input = QtXml.QXmlInputSource(reply)
                xml_reader.parse(xml_input)
                handler(xml_handler.document, reply, error)
            else:
                handler(str(reply.readAll()), reply, error)
        reply.close()