forked from len0rd/rockbox
Avoid unnecessary HEAD request on uncached file.
If a file is not available in the cache immediately sent a GET request. Using a HEAD request to retrieve the file timestamp on the server is not necessary and only creates an unnecessary network request. Change-Id: I358507dcc0c6b837ff47e5fd710b5262d03cb7b0
This commit is contained in:
parent
a2ab22efbf
commit
554ee364b0
1 changed files with 27 additions and 27 deletions
|
@ -199,6 +199,7 @@ bool HttpGet::getFile(const QUrl &url)
|
||||||
|
|
||||||
// create hash used for caching
|
// create hash used for caching
|
||||||
m_hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
|
m_hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
|
||||||
|
m_cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + m_hash;
|
||||||
// RFC2616: the absoluteURI form must get used when the request is being
|
// RFC2616: the absoluteURI form must get used when the request is being
|
||||||
// sent to a proxy.
|
// sent to a proxy.
|
||||||
m_path.clear();
|
m_path.clear();
|
||||||
|
@ -211,7 +212,7 @@ bool HttpGet::getFile(const QUrl &url)
|
||||||
m_header.setValue("User-Agent", m_globalUserAgent);
|
m_header.setValue("User-Agent", m_globalUserAgent);
|
||||||
m_header.setValue("Connection", "Keep-Alive");
|
m_header.setValue("Connection", "Keep-Alive");
|
||||||
|
|
||||||
if(!m_usecache) {
|
if(!m_usecache || !QFileInfo(m_cachefile).exists()) {
|
||||||
getFileFinish();
|
getFileFinish();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -227,7 +228,6 @@ bool HttpGet::getFile(const QUrl &url)
|
||||||
|
|
||||||
void HttpGet::getFileFinish()
|
void HttpGet::getFileFinish()
|
||||||
{
|
{
|
||||||
m_cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + m_hash;
|
|
||||||
QString indexFile = m_cachedir.absolutePath() + "/rbutil-cache/cache.txt";
|
QString indexFile = m_cachedir.absolutePath() + "/rbutil-cache/cache.txt";
|
||||||
if(m_usecache) {
|
if(m_usecache) {
|
||||||
// check if the file is present in cache
|
// check if the file is present in cache
|
||||||
|
@ -347,35 +347,35 @@ void HttpGet::httpFinished(int id, bool error)
|
||||||
emit requestFinished(id, error);
|
emit requestFinished(id, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(id == headRequest) {
|
QHttpResponseHeader h = http.lastResponse();
|
||||||
QHttpResponseHeader h = http.lastResponse();
|
QString date = h.value("Last-Modified").simplified();
|
||||||
|
if(date.isEmpty()) {
|
||||||
QString date = h.value("Last-Modified").simplified();
|
m_serverTimestamp = QDateTime(); // no value = invalid
|
||||||
if(date.isEmpty()) {
|
if(id == headRequest)
|
||||||
m_serverTimestamp = QDateTime(); // no value = invalid
|
|
||||||
emit headerFinished();
|
emit headerFinished();
|
||||||
return;
|
|
||||||
}
|
|
||||||
// to successfully parse the date strip weekday and timezone
|
|
||||||
date.remove(0, date.indexOf(" ") + 1);
|
|
||||||
if(date.endsWith("GMT"))
|
|
||||||
date.truncate(date.indexOf(" GMT"));
|
|
||||||
// distinguish input formats (see RFC1945)
|
|
||||||
// RFC 850
|
|
||||||
if(date.contains("-"))
|
|
||||||
m_serverTimestamp = QLocale::c().toDateTime(date, "dd-MMM-yy hh:mm:ss");
|
|
||||||
// asctime format
|
|
||||||
else if(date.at(0).isLetter())
|
|
||||||
m_serverTimestamp = QLocale::c().toDateTime(date, "MMM d hh:mm:ss yyyy");
|
|
||||||
// RFC 822
|
|
||||||
else
|
else
|
||||||
m_serverTimestamp = QLocale::c().toDateTime(date, "dd MMM yyyy hh:mm:ss");
|
emit requestFinished(id, error);
|
||||||
qDebug() << "[HTTP] HEAD finished, server date:" << date << ", parsed:" << m_serverTimestamp;
|
|
||||||
emit headerFinished();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(id == getRequest)
|
// to successfully parse the date strip weekday and timezone
|
||||||
|
date.remove(0, date.indexOf(" ") + 1);
|
||||||
|
if(date.endsWith("GMT")) date.truncate(date.indexOf(" GMT"));
|
||||||
|
// distinguish input formats (see RFC1945)
|
||||||
|
if(date.contains("-")) // RFC 850
|
||||||
|
m_serverTimestamp = QLocale::c().toDateTime(date, "dd-MMM-yy hh:mm:ss");
|
||||||
|
else if(date.at(0).isLetter()) // asctime format
|
||||||
|
m_serverTimestamp = QLocale::c().toDateTime(date, "MMM d hh:mm:ss yyyy");
|
||||||
|
else // RFC 822
|
||||||
|
m_serverTimestamp = QLocale::c().toDateTime(date, "dd MMM yyyy hh:mm:ss");
|
||||||
|
|
||||||
|
qDebug() << "[HTTP] file server date:" << date
|
||||||
|
<< "parsed:" << m_serverTimestamp;
|
||||||
|
|
||||||
|
if(id == headRequest)
|
||||||
|
emit headerFinished();
|
||||||
|
else
|
||||||
emit requestFinished(id, error);
|
emit requestFinished(id, error);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpGet::httpStarted(int id)
|
void HttpGet::httpStarted(int id)
|
||||||
|
@ -396,7 +396,7 @@ void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
|
||||||
// if there is a network error abort all scheduled requests for
|
// if there is a network error abort all scheduled requests for
|
||||||
// this download
|
// this download
|
||||||
m_response = resp.statusCode();
|
m_response = resp.statusCode();
|
||||||
|
|
||||||
// 301 -- moved permanently
|
// 301 -- moved permanently
|
||||||
// 302 -- found
|
// 302 -- found
|
||||||
// 303 -- see other
|
// 303 -- see other
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue