void SslTlsSocket::handleStateChanged()
{
    /* Qt delivers the stateChanged() signal before the error() one.
    That's a problem because we really want to provide a nice error message
    to the user and QAbstractSocket::error() is not set yet by the time this
    function executes. That's why we have to delay the first disconnected() signal. */

    QAbstractSocket *sock = qobject_cast<QAbstractSocket *>(d);
    Q_ASSERT(sock);
    QString proxyMsg;
    switch (sock->proxy().type()) {
    case QNetworkProxy::NoProxy:
        break;
    case QNetworkProxy::HttpCachingProxy:
        Q_ASSERT_X(false, "proxy detection",
                   "Qt should have returned a proxy capable of tunneling, but we got back an HTTP proxy.");
        break;
    case QNetworkProxy::FtpCachingProxy:
        Q_ASSERT_X(false, "proxy detection",
                   "Qt should have returned a proxy capable of tunneling, but we got back an FTP proxy.");
        break;
    case QNetworkProxy::DefaultProxy:
        proxyMsg = tr(" (via proxy %1)").arg(sock->proxy().hostName());
        break;
    case QNetworkProxy::Socks5Proxy:
        proxyMsg = tr(" (via SOCKS5 proxy %1)").arg(sock->proxy().hostName());
        break;
    case QNetworkProxy::HttpProxy:
        proxyMsg = tr(" (via HTTP proxy %1)").arg(sock->proxy().hostName());
        break;
    }
    switch (sock->state()) {
    case QAbstractSocket::HostLookupState:
        emit stateChanged(Imap::CONN_STATE_HOST_LOOKUP, tr("Looking up %1%2...").arg(host,
                              sock->proxy().capabilities().testFlag(QNetworkProxy::HostNameLookupCapability) ?
                              proxyMsg : QString()));
        break;
    case QAbstractSocket::ConnectingState:
        emit stateChanged(Imap::CONN_STATE_CONNECTING, tr("Connecting to %1:%2%3%4...").arg(
                              host, QString::number(port), startEncrypted ? tr(" (SSL)") : QString(),
                              sock->proxy().capabilities().testFlag(QNetworkProxy::TunnelingCapability) ?
                              proxyMsg : QString()));
        break;
    case QAbstractSocket::BoundState:
    case QAbstractSocket::ListeningState:
        break;
    case QAbstractSocket::ConnectedState:
        if (! startEncrypted) {
            emit stateChanged(Imap::CONN_STATE_CONNECTED_PRETLS_PRECAPS, tr("Connected"));
        } else {
            emit stateChanged(Imap::CONN_STATE_SSL_HANDSHAKE, tr("Negotiating encryption..."));
        }
        break;
    case QAbstractSocket::UnconnectedState:
    case QAbstractSocket::ClosingState:
        disconnectedMessage = tr("Socket is disconnected: %1").arg(sock->errorString());
        delayedDisconnect->start();
        break;
    }
}
Exemple #2
0
QXmlStreamReader::TokenType Parser::Private::blockingReadNext()
{
    QXmlStreamReader::TokenType token = QXmlStreamReader::Invalid;

    forever {
        token = reader.readNext();

        if (reader.error() == QXmlStreamReader::PrematureEndOfDocumentError) {
            if (reader.device()->waitForReadyRead(1000)) {
                // let's try again
                continue;
            } else {
                // device error, e.g. remote host closed connection, or timeout
                // ### we have no way to know if waitForReadyRead() timed out or failed with a real
                //     error, and sensible heuristics based on QIODevice fail.
                //     - error strings are translated and in any case not guaranteed to stay the same,
                //       so we can't use them.
                //     - errorString().isEmpty() does not work because errorString() is
                //       "network timeout error" if the waitFor... timed out.
                //     - isSequential() [for socket] && isOpen() doesn't work because isOpen()
                //       returns true if the remote host closed the connection.
                // ...so we fall back to knowing it might be a QAbstractSocket.

                QIODevice *dev = reader.device();
                QAbstractSocket *sock = qobject_cast<QAbstractSocket *>(dev);

                if (!sock || sock->state() != QAbstractSocket::ConnectedState) {
                    throw ParserException(dev->errorString());
                }
            }
        } else if (reader.hasError()) {
            throw ParserException(reader.errorString()); //TODO add line, column?
            break;
        } else {
            // read a valid next token
            break;
        }
    }

    return token;
}
Exemple #3
0
void XmlRpcServer::incomingConnection( int socketDescriptor )
{
#ifdef DEBUG_XMLRPC
    qDebug() << this << "new incoming connection";
#endif

    QAbstractSocket * s = NULL;
#ifndef QT_NO_OPENSSL
    if ( sslParams != NULL && !sslParams->certificate.isNull() )
        {
            s = new QSslSocket( this );
            s->setSocketDescriptor( socketDescriptor );

            ((QSslSocket *)s)->setLocalCertificate( sslParams->certificate );
            ((QSslSocket *)s)->setPrivateKey( sslParams->privateKey );
            ((QSslSocket *)s)->setCaCertificates( sslParams->ca );
        }
    else
        {
            s = new QTcpSocket( this );
            s->setSocketDescriptor( socketDescriptor );
        }
#else
      s = new QTcpSocket( this );
      s->setSocketDescriptor( socketDescriptor );
#endif

      Q_ASSERT( s->state() == QAbstractSocket::ConnectedState );
      connect( s, SIGNAL(disconnected()), this, SLOT(slotSocketDisconnected()) );

      HttpServer * p = new HttpServer( s );
      connect( p, SIGNAL(protocolTimeout(Protocol*)),
               this, SLOT(slotProtocolTimeout(Protocol*)) );
      connect( p, SIGNAL(parseError(HttpServer*)),
               this, SLOT(slotParseError(HttpServer*)) );
      connect( p, SIGNAL(requestReceived(HttpServer*, const QHttpRequestHeader&, const QByteArray&)),
               this, SLOT(slotRequestReceived(HttpServer*, const QHttpRequestHeader&, const QByteArray&)) );
      connect( p, SIGNAL(replySent(HttpServer*)),
               this, SLOT(slotReplySent(HttpServer*)) );
}
bool SslTlsSocket::isDead()
{
    QAbstractSocket *sock = qobject_cast<QAbstractSocket *>(d);
    Q_ASSERT(sock);
    return sock->state() != QAbstractSocket::ConnectedState;
}