summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/base/httpget.cpp
diff options
context:
space:
mode:
authorDominik Riebeling <Dominik.Riebeling@gmail.com>2013-01-22 20:34:33 +0100
committerDominik Riebeling <Dominik.Riebeling@gmail.com>2013-01-22 20:39:40 +0100
commit554ee364b0ab37fec2c2a83a93a466cb10025230 (patch)
tree7fa927a2ff9a8ad31723f35d171c26b516079824 /rbutil/rbutilqt/base/httpget.cpp
parenta2ab22efbf93981f9a86b6b06dc6d3c2f1167728 (diff)
downloadrockbox-554ee364b0ab37fec2c2a83a93a466cb10025230.tar.gz
rockbox-554ee364b0ab37fec2c2a83a93a466cb10025230.zip
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
Diffstat (limited to 'rbutil/rbutilqt/base/httpget.cpp')
-rw-r--r--rbutil/rbutilqt/base/httpget.cpp54
1 files changed, 27 insertions, 27 deletions
diff --git a/rbutil/rbutilqt/base/httpget.cpp b/rbutil/rbutilqt/base/httpget.cpp
index 66a7076391..58f6bd5e09 100644
--- a/rbutil/rbutilqt/base/httpget.cpp
+++ b/rbutil/rbutilqt/base/httpget.cpp
@@ -199,6 +199,7 @@ bool HttpGet::getFile(const QUrl &url)
199 199
200 // create hash used for caching 200 // create hash used for caching
201 m_hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex(); 201 m_hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
202 m_cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + m_hash;
202 // RFC2616: the absoluteURI form must get used when the request is being 203 // RFC2616: the absoluteURI form must get used when the request is being
203 // sent to a proxy. 204 // sent to a proxy.
204 m_path.clear(); 205 m_path.clear();
@@ -211,7 +212,7 @@ bool HttpGet::getFile(const QUrl &url)
211 m_header.setValue("User-Agent", m_globalUserAgent); 212 m_header.setValue("User-Agent", m_globalUserAgent);
212 m_header.setValue("Connection", "Keep-Alive"); 213 m_header.setValue("Connection", "Keep-Alive");
213 214
214 if(!m_usecache) { 215 if(!m_usecache || !QFileInfo(m_cachefile).exists()) {
215 getFileFinish(); 216 getFileFinish();
216 } 217 }
217 else { 218 else {
@@ -227,7 +228,6 @@ bool HttpGet::getFile(const QUrl &url)
227 228
228void HttpGet::getFileFinish() 229void HttpGet::getFileFinish()
229{ 230{
230 m_cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + m_hash;
231 QString indexFile = m_cachedir.absolutePath() + "/rbutil-cache/cache.txt"; 231 QString indexFile = m_cachedir.absolutePath() + "/rbutil-cache/cache.txt";
232 if(m_usecache) { 232 if(m_usecache) {
233 // check if the file is present in cache 233 // check if the file is present in cache
@@ -347,35 +347,35 @@ void HttpGet::httpFinished(int id, bool error)
347 emit requestFinished(id, error); 347 emit requestFinished(id, error);
348 } 348 }
349 349
350 if(id == headRequest) { 350 QHttpResponseHeader h = http.lastResponse();
351 QHttpResponseHeader h = http.lastResponse(); 351 QString date = h.value("Last-Modified").simplified();
352 352 if(date.isEmpty()) {
353 QString date = h.value("Last-Modified").simplified(); 353 m_serverTimestamp = QDateTime(); // no value = invalid
354 if(date.isEmpty()) { 354 if(id == headRequest)
355 m_serverTimestamp = QDateTime(); // no value = invalid
356 emit headerFinished(); 355 emit headerFinished();
357 return;
358 }
359 // to successfully parse the date strip weekday and timezone
360 date.remove(0, date.indexOf(" ") + 1);
361 if(date.endsWith("GMT"))
362 date.truncate(date.indexOf(" GMT"));
363 // distinguish input formats (see RFC1945)
364 // RFC 850
365 if(date.contains("-"))
366 m_serverTimestamp = QLocale::c().toDateTime(date, "dd-MMM-yy hh:mm:ss");
367 // asctime format
368 else if(date.at(0).isLetter())
369 m_serverTimestamp = QLocale::c().toDateTime(date, "MMM d hh:mm:ss yyyy");
370 // RFC 822
371 else 356 else
372 m_serverTimestamp = QLocale::c().toDateTime(date, "dd MMM yyyy hh:mm:ss"); 357 emit requestFinished(id, error);
373 qDebug() << "[HTTP] HEAD finished, server date:" << date << ", parsed:" << m_serverTimestamp;
374 emit headerFinished();
375 return; 358 return;
376 } 359 }
377 if(id == getRequest) 360 // to successfully parse the date strip weekday and timezone
361 date.remove(0, date.indexOf(" ") + 1);
362 if(date.endsWith("GMT")) date.truncate(date.indexOf(" GMT"));
363 // distinguish input formats (see RFC1945)
364 if(date.contains("-")) // RFC 850
365 m_serverTimestamp = QLocale::c().toDateTime(date, "dd-MMM-yy hh:mm:ss");
366 else if(date.at(0).isLetter()) // asctime format
367 m_serverTimestamp = QLocale::c().toDateTime(date, "MMM d hh:mm:ss yyyy");
368 else // RFC 822
369 m_serverTimestamp = QLocale::c().toDateTime(date, "dd MMM yyyy hh:mm:ss");
370
371 qDebug() << "[HTTP] file server date:" << date
372 << "parsed:" << m_serverTimestamp;
373
374 if(id == headRequest)
375 emit headerFinished();
376 else
378 emit requestFinished(id, error); 377 emit requestFinished(id, error);
378 return;
379} 379}
380 380
381void HttpGet::httpStarted(int id) 381void HttpGet::httpStarted(int id)
@@ -396,7 +396,7 @@ void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
396 // if there is a network error abort all scheduled requests for 396 // if there is a network error abort all scheduled requests for
397 // this download 397 // this download
398 m_response = resp.statusCode(); 398 m_response = resp.statusCode();
399 399
400 // 301 -- moved permanently 400 // 301 -- moved permanently
401 // 302 -- found 401 // 302 -- found
402 // 303 -- see other 402 // 303 -- see other