本文整理汇总了C++中qnetworkcachemetadata::RawHeaderList类的典型用法代码示例。如果您正苦于以下问题:C++ RawHeaderList类的具体用法?C++ RawHeaderList怎么用?C++ RawHeaderList使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RawHeaderList类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: injectFakeData
// This function simulates a partially or fully occupied disk cache
// like a normal user of a cache might encounter is real-life browsing.
// The point of this is to trigger degradation in file-system and media performance
// that occur due to the quantity and layout of data.
void tst_qnetworkdiskcache::injectFakeData()
{
QNetworkCacheMetaData::RawHeaderList headers;
headers.append(qMakePair(QByteArray("X-TestHeader"),QByteArray("HeaderValue")));
//Prep cache dir with fake data using QNetworkDiskCache APIs
for (quint32 i = 0; i < NumFakeCacheObjects; i++) {
//prepare metata for url
QNetworkCacheMetaData meta;
QString fakeURL;
QTextStream stream(&fakeURL);
stream << fakeURLbase << i;
QUrl url(fakeURL);
meta.setUrl(url);
meta.setRawHeaders(headers);
meta.setSaveToDisk(true);
//commit payload and metadata to disk
QIODevice *device = cache->prepare(meta);
device->write(payload);
cache->insert(device);
}
}
示例2: prepare
QIODevice *TabCache::prepare(const QNetworkCacheMetaData &metaData) {
QLOG_DEBUG() << "TabCache::prepare";
QNetworkCacheMetaData local{metaData};
//Default policy based on received HTTP headers is to not save to disk.
//Override here, and set proper expiration so items are put in cache
//(setSaveToDisk) and valid when retrieved using metaData call -
//(setExpirationDate)
local.setSaveToDisk(true);
// Need to set some reasonable length of time in which our cache entries
// will expire. It's possible we'll want to allow users to customize this.
local.setExpirationDate(QDateTime().currentDateTime().addDays(kCacheExpireInDays));
QNetworkCacheMetaData::RawHeaderList headers;
for (auto &header: local.rawHeaders()) {
// Modify Cache-Control headers - basically need to drop 'no-store'
// as we want to store to cache and 'must-revalidate' as we don't have
// ETag or last modified headers available to attempt re-validation. To
// be on the safe side though just drop Cache-Control and Pragma headers.
if (header.first == "Cache-Control") continue;
if (header.first == "Pragma") continue;
headers.push_back(header);
}
local.setRawHeaders(headers);
return QNetworkDiskCache::prepare(local);
}
示例3: GetLastModified
/** \fn MythDownloadManager::GetLastModified(const QString &url)
* \brief Gets the Last Modified timestamp for a URI
* \param url URI to test.
* \return Timestamp the URI was last modified or now if an error occurred
*/
QDateTime MythDownloadManager::GetLastModified(const QString &url)
{
static const char dateFormat[] = "ddd, dd MMM yyyy hh:mm:ss 'GMT'";
VERBOSE(VB_FILE+VB_EXTRA, LOC + QString("GetLastModified('%1')").arg(url));
QDateTime result;
QDateTime now = QDateTime::currentDateTime();
QNetworkCacheMetaData urlData = m_manager->cache()->metaData(QUrl(url));
if (urlData.isValid())
{
if (urlData.lastModified().secsTo(now) <= 60)
{
result = urlData.lastModified();
}
else
{
// If the last modification date is older than 60 seconds, and
// we loaded the page over 60 seconds ago, then redownload the
// page to re-verify it's last modified date.
QNetworkCacheMetaData::RawHeaderList headers =
urlData.rawHeaders();
bool found = false;
QNetworkCacheMetaData::RawHeaderList::iterator it
= headers.begin();
for (; !found && it != headers.end(); ++it)
{
if ((*it).first == "Date")
{
found = true;
QDateTime loadDate =
QDateTime::fromString((*it).second, dateFormat);
loadDate.setTimeSpec(Qt::UTC);
if (loadDate.secsTo(now) <= 60)
result = urlData.lastModified();
}
}
}
}
if (!result.isValid())
{
MythDownloadInfo *dlInfo = new MythDownloadInfo;
dlInfo->m_url = url;
dlInfo->m_syncMode = true;
if (downloadNow(dlInfo, false) && dlInfo->m_reply)
{
QVariant lastMod =
dlInfo->m_reply->header(QNetworkRequest::LastModifiedHeader);
if (lastMod.isValid())
result = lastMod.toDateTime();
}
delete dlInfo;
}
return result;
}
示例4: sendCacheContents
bool QNetworkAccessCacheBackend::sendCacheContents()
{
setCachingEnabled(false);
QAbstractNetworkCache *nc = networkCache();
if (!nc)
return false;
QNetworkCacheMetaData item = nc->metaData(url());
if (!item.isValid())
return false;
QNetworkCacheMetaData::AttributesMap attributes = item.attributes();
setAttribute(QNetworkRequest::HttpStatusCodeAttribute, attributes.value(QNetworkRequest::HttpStatusCodeAttribute));
setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, attributes.value(QNetworkRequest::HttpReasonPhraseAttribute));
// set the raw headers
QNetworkCacheMetaData::RawHeaderList rawHeaders = item.rawHeaders();
QNetworkCacheMetaData::RawHeaderList::ConstIterator it = rawHeaders.constBegin(),
end = rawHeaders.constEnd();
for ( ; it != end; ++it) {
if (it->first.toLower() == "cache-control" &&
it->second.toLower().contains("must-revalidate")) {
return false;
}
setRawHeader(it->first, it->second);
}
// handle a possible redirect
QVariant redirectionTarget = attributes.value(QNetworkRequest::RedirectionTargetAttribute);
if (redirectionTarget.isValid()) {
setAttribute(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget);
redirectionRequested(redirectionTarget.toUrl());
}
// signal we're open
metaDataChanged();
if (operation() == QNetworkAccessManager::GetOperation) {
QIODevice *contents = nc->data(url());
if (!contents)
return false;
contents->setParent(this);
writeDownstreamData(contents);
}
#if defined(QNETWORKACCESSCACHEBACKEND_DEBUG)
qDebug() << "Successfully sent cache:" << url();
#endif
return true;
}
示例5: downloadQNetworkRequest
/** \fn MythDownloadManager::downloadQNetworkRequest(MythDownloadInfo *dlInfo)
* \brief Downloads a QNetworkRequest via the QNetworkAccessManager
* \param dlInfo MythDownloadInfo information for download
*/
void MythDownloadManager::downloadQNetworkRequest(MythDownloadInfo *dlInfo)
{
static const char dateFormat[] = "ddd, dd MMM yyyy hh:mm:ss 'GMT'";
QUrl qurl(dlInfo->m_url);
QNetworkRequest request;
if (dlInfo->m_request)
{
request = *dlInfo->m_request;
delete dlInfo->m_request;
dlInfo->m_request = NULL;
}
else
request.setUrl(qurl);
if (!dlInfo->m_reload)
{
// Prefer the in-cache item if one exists and it is less than 60
// seconds old and has not expired in the last 10 seconds.
QDateTime now = QDateTime::currentDateTime();
QNetworkCacheMetaData urlData = m_manager->cache()->metaData(qurl);
if ((urlData.isValid()) &&
((!urlData.expirationDate().isValid()) ||
(urlData.expirationDate().secsTo(now) < 10)))
{
QNetworkCacheMetaData::RawHeaderList headers =
urlData.rawHeaders();
bool found = false;
QNetworkCacheMetaData::RawHeaderList::iterator it
= headers.begin();
for (; !found && it != headers.end(); ++it)
{
if ((*it).first == "Date")
{
found = true;
QDateTime loadDate =
QDateTime::fromString((*it).second, dateFormat);
loadDate.setTimeSpec(Qt::UTC);
if (loadDate.secsTo(now) < 60)
dlInfo->m_preferCache = true;
}
}
}
}
if (dlInfo->m_preferCache)
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
QNetworkRequest::PreferCache);
request.setRawHeader("User-Agent",
"MythDownloadManager v" MYTH_BINARY_VERSION);
if (dlInfo->m_post)
dlInfo->m_reply = m_manager->post(request, *dlInfo->m_data);
else
dlInfo->m_reply = m_manager->get(request);
m_downloadReplies[dlInfo->m_reply] = dlInfo;
connect(dlInfo->m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
SLOT(downloadError(QNetworkReply::NetworkError)));
connect(dlInfo->m_reply, SIGNAL(downloadProgress(qint64, qint64)),
this, SLOT(downloadProgress(qint64, qint64)));
}
示例6: replyFinished
void QgsWfsRequest::replyFinished()
{
if ( !mIsAborted && mReply )
{
if ( mReply->error() == QNetworkReply::NoError )
{
QgsDebugMsgLevel( QStringLiteral( "reply OK" ), 4 );
QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
if ( !redirect.isNull() )
{
QgsDebugMsgLevel( QStringLiteral( "Request redirected." ), 4 );
const QUrl &toUrl = redirect.toUrl();
mReply->request();
if ( toUrl == mReply->url() )
{
mErrorMessage = tr( "Redirect loop detected: %1" ).arg( toUrl.toString() );
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
mResponse.clear();
}
else
{
QNetworkRequest request( toUrl );
QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsWfsRequest" ) );
if ( !mUri.auth().setAuthorization( request ) )
{
mResponse.clear();
mErrorMessage = errorMessageFailedAuth();
mErrorCode = QgsWfsRequest::NetworkError;
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
emit downloadFinished();
return;
}
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, mForceRefresh ? QNetworkRequest::AlwaysNetwork : QNetworkRequest::PreferCache );
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
mReply->deleteLater();
mReply = nullptr;
QgsDebugMsgLevel( QStringLiteral( "redirected: %1 forceRefresh=%2" ).arg( redirect.toString() ).arg( mForceRefresh ), 4 );
mReply = QgsNetworkAccessManager::instance()->get( request );
mReply->setReadBufferSize( READ_BUFFER_SIZE_HINT );
if ( !mUri.auth().setAuthorizationReply( mReply ) )
{
mResponse.clear();
mErrorMessage = errorMessageFailedAuth();
mErrorCode = QgsWfsRequest::NetworkError;
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
emit downloadFinished();
return;
}
connect( mReply, &QNetworkReply::finished, this, &QgsWfsRequest::replyFinished, Qt::DirectConnection );
connect( mReply, &QNetworkReply::downloadProgress, this, &QgsWfsRequest::replyProgress, Qt::DirectConnection );
return;
}
}
else
{
const QgsNetworkAccessManager *nam = QgsNetworkAccessManager::instance();
if ( nam->cache() )
{
QNetworkCacheMetaData cmd = nam->cache()->metaData( mReply->request().url() );
QNetworkCacheMetaData::RawHeaderList hl;
const auto constRawHeaders = cmd.rawHeaders();
for ( const QNetworkCacheMetaData::RawHeader &h : constRawHeaders )
{
if ( h.first != "Cache-Control" )
hl.append( h );
}
cmd.setRawHeaders( hl );
QgsDebugMsgLevel( QStringLiteral( "expirationDate:%1" ).arg( cmd.expirationDate().toString() ), 4 );
if ( cmd.expirationDate().isNull() )
{
cmd.setExpirationDate( QDateTime::currentDateTime().addSecs( defaultExpirationInSec() ) );
}
nam->cache()->updateMetaData( cmd );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "No cache!" ), 4 );
}
#ifdef QGISDEBUG
bool fromCache = mReply->attribute( QNetworkRequest::SourceIsFromCacheAttribute ).toBool();
QgsDebugMsgLevel( QStringLiteral( "Reply was cached: %1" ).arg( fromCache ), 4 );
#endif
mResponse = mReply->readAll();
if ( mResponse.isEmpty() && !mGotNonEmptyResponse )
{
mErrorMessage = tr( "empty response: %1" ).arg( mReply->errorString() );
mErrorCode = QgsWfsRequest::ServerExceptionError;
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
}
}
//.........这里部分代码省略.........