void DJDownloadManager::httpResponseHeaderReceived(const QHttpResponseHeader& response )
{
	int status	= response.statusCode();
	djDebug() << "httpResponseHeaderReceived" << status;
	
	QStringList	keys	= response.keys();
	for ( int i = 0; i < keys.size(); i++ ) {
		djDebug() << keys.at(i) << response.value(keys.at(i));
	}
	switch( status ) {
	case 200://normal
		m_file->open( QIODevice::WriteOnly );
		break;
	case 206: {//resume
		m_file->open( QIODevice::WriteOnly | QIODevice::Append );
        QString range	= response.value("content-range");
        //djDebug() << "rang =" << range << range.section("-",0,0);
        range.remove("bytes",Qt::CaseInsensitive).section("-",0,0);
        m_existedFileSize	= range.remove("bytes",Qt::CaseInsensitive).section("-",0,0).toUInt();
		break;
	}
	case 302: {//redirect
		QString url	= QString( response.value("location") );
		QString host,path;
		ExtractHostAndPath( url, host, path );
		//djDebug() << "redirect" << host << path;
		sendHttpRequest( m_file, path.section("/",-1), m_http, host, path );
		break;
	}
	case 416://outof rang,finished
		break;
	default:
		m_file->setFileName("");
		break;
	}
}
Exemple #2
0
void HttpComms::headerReceived(const QHttpResponseHeader &resp)
{
    m_statusCode = resp.statusCode();
    m_responseReason = resp.reasonPhrase();

    QString sidkey = "set-cookie";

    if (resp.hasKey(sidkey))
    {
        QRegExp rx("PHPSESSID=(.+);");
        rx.setMinimal(true);
        rx.setCaseSensitivity(Qt::CaseInsensitive);
        if (rx.indexIn(resp.value(sidkey)) >= 0)
        {
            m_cookie = "PHPSESSID=" + rx.cap(1);
            VERBOSE(VB_NETWORK, QString("HttpComms found cookie: %1").arg(m_cookie));
        }
    }


    VERBOSE(VB_NETWORK, QString("Got HTTP response: %1:%2")
                        .arg(m_statusCode)
                        .arg(m_responseReason));
    VERBOSE(VB_NETWORK, QString("Keys: %1")
                        .arg(resp.keys().join(",") ));


    if (resp.statusCode() >= 300 && resp.statusCode() <= 400)
    {
        // redirection
        QString uri = resp.value("LOCATION");
        VERBOSE(VB_NETWORK, QString("Redirection to: '%1'").arg(uri));

        m_redirectedURL = resp.value("LOCATION");
        m_authNeeded = false;
    }
    else if ((resp.statusCode() == 401))
    {
        // Toggle the state of our authentication pending flag
        // if we've gotten this after having tried to authenticate
        // once then we've failed authentication and turning the pending off will allow us to exit.
        m_authNeeded = !m_authNeeded;
        if (m_authNeeded)
        {
            QString authHeader(resp.value("www-authenticate"));

            if (authHeader.startsWith("Digest") )
            {
                if (!createDigestAuth(false, authHeader, &m_curRequest) )
                {
                    m_authNeeded = false;
                    return;
                }
            }
            else
            {
                QString sUser(m_webCredentials.user + ':' + m_webCredentials.pass);
                QByteArray auth = QCodecs::base64Encode(sUser.toLocal8Bit());
                m_curRequest.setValue( "Authorization", QString( "Basic " ).append( auth ) );
            }

            if (m_timer)
            {
                m_timer->stop();
                m_timer->setSingleShot(true);
                m_timer->start(m_timeoutInterval);
            }

            // Not sure if it's possible to receive a session ID or other cookie
            // before authenticating or not.
            if (!m_cookie.isEmpty())
            {
                m_curRequest.setValue("Cookie", m_cookie);
            }

            http->request(m_curRequest);
        }
    }
    else
    {
        m_authNeeded = false;
    }
}