Ejemplo n.º 1
0
void FeedFetcher::readData(const QHttpResponseHeader& response_header) {
    if(!impl_->current_feed_.get())
        return;
    int statusCode =  response_header.statusCode();
    qDebug() << statusCode;
    if (statusCode == 200) {
        //TODO in order to insert CDATA to invalid XML atom,
        //we need capture data of full page
        bytes_.append(impl_->http_->readAll());
//         qDebug() << bytes_.size() << " bytes received.";
        // insert CDATA FLAG if not exists
    } else if ((statusCode >300 || statusCode <300)
        && (response_header.hasKey("location") || response_header.hasKey("Location"))) {
        qDebug()<<response_header.statusCode();
        QUrl location = QUrl(response_header.value("location"));
        if (location.isEmpty()) {
            location = QUrl(response_header.value("Location"));
        }
        qDebug() << location;
        impl_->http_.get()->setHost(location.host(),80);
        impl_->connection_id_ = impl_->http_.get()->get(location.toString());
    } else if (statusCode == 404) {
       //NOTE: Do Nothing
       qDebug() << "404 !!";
    } else {
        qDebug() << "Received non-200 response code: "
                 << statusCode << response_header.toString();
    }
}
Ejemplo n.º 2
0
void HttpEngine::handleDownloadHeaders(QHttpResponseHeader header)
{
	switch(header.statusCode())
	{
	case 200:
	{
		if(m_file.pos() && !header.hasKey("content-range"))
		{
			if(m_nSegmentStart < 0)
			{
				// resume not supported
				m_file.resize(0);
				m_file.seek(0);
				
				emit statusMessage(tr("Resume not supported"));
			}
			else
				throw tr("Segmentation not supported by server");
		}
	}
	case 206: // data will follow
	{
		bool bChunked = false;
		if(header.value("transfer-encoding").compare("chunked",Qt::CaseInsensitive) == 0)
			bChunked = true;
		else
		{
			m_nToTransfer = header.value("content-length").toLongLong();
			emit receivedSize(m_nToTransfer+m_nResume);
		}
		
		if(header.hasKey("content-disposition"))
		{
			QString disp = header.value("content-disposition");
			int pos = disp.indexOf("filename=");
			
			if(pos != -1)
			{
				QString name = disp.mid(pos+9);
				
				if(name.startsWith('"') && name.endsWith('"'))
					name = name.mid(1, name.size()-2);
				emit renamed(name);
			}
		}
		
		dataCycle(bChunked);
		emit finished(false);
		
		break;
	}
	case 301 ... 399: // redirect
		emit redirected(header.value("location"));
		break;
	case 416: // Requested Range Not Satisfiable
		m_strError = tr("Requested Range Not Satisfiable - the file has probably already been downloaded");
		emit finished(false);
		break;
	default: // error
		throw header.reasonPhrase();
	}
}
Ejemplo n.º 3
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;
    }
}