Add setting the User-Agent for http requests to HttpGet class. Make rbutil set its own user agent string.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18366 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dominik Riebeling 2008-08-30 20:51:50 +00:00
parent 76a3959ac2
commit 90f92b216b
4 changed files with 29 additions and 12 deletions

View file

@ -26,6 +26,7 @@
QDir HttpGet::m_globalCache; //< global cach path value for new objects QDir HttpGet::m_globalCache; //< global cach path value for new objects
QUrl HttpGet::m_globalProxy; //< global proxy value for new objects QUrl HttpGet::m_globalProxy; //< global proxy value for new objects
bool HttpGet::m_globalDumbCache = false; //< globally set cache "dumb" mode bool HttpGet::m_globalDumbCache = false; //< globally set cache "dumb" mode
QString HttpGet::m_globalUserAgent; //< globally set user agent for requests
HttpGet::HttpGet(QObject *parent) HttpGet::HttpGet(QObject *parent)
: QObject(parent) : QObject(parent)
@ -199,13 +200,19 @@ bool HttpGet::getFile(const QUrl &url)
m_hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex(); m_hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
m_path = QString(QUrl::toPercentEncoding(url.path(), "/")); m_path = QString(QUrl::toPercentEncoding(url.path(), "/"));
// construct request header
m_header.setValue("Host", url.host());
m_header.setValue("User-Agent", m_globalUserAgent);
m_header.setValue("Connection", "Keep-Alive");
if(m_dumbCache || !m_usecache) { if(m_dumbCache || !m_usecache) {
getFileFinish(); getFileFinish();
} }
else { else {
// request HTTP header // schedule HTTP header request
connect(this, SIGNAL(headerFinished()), this, SLOT(getFileFinish())); connect(this, SIGNAL(headerFinished()), this, SLOT(getFileFinish()));
headRequest = http.head(m_path + m_query); m_header.setRequest("HEAD", m_path + m_query);
headRequest = http.request(m_header);
} }
return true; return true;
@ -262,15 +269,16 @@ void HttpGet::getFileFinish()
else { else {
qDebug() << "[HTTP] cache DISABLED"; qDebug() << "[HTTP] cache DISABLED";
} }
// schedule GET request
m_header.setRequest("GET", m_path + m_query);
if(outputToBuffer) { if(outputToBuffer) {
qDebug() << "[HTTP] downloading to buffer."; qDebug() << "[HTTP] downloading to buffer.";
getRequest = http.get(m_path + m_query); getRequest = http.request(m_header);
} }
else { else {
qDebug() << "[HTTP] downloading to file:" qDebug() << "[HTTP] downloading to file:"
<< qPrintable(outputFile->fileName()); << qPrintable(outputFile->fileName());
getRequest = http.get(m_path + m_query, outputFile); getRequest = http.request(m_header, 0, outputFile);
} }
qDebug() << "[HTTP] GET request scheduled, id:" << getRequest; qDebug() << "[HTTP] GET request scheduled, id:" << getRequest;

View file

@ -57,6 +57,8 @@ class HttpGet : public QObject
{ m_globalProxy = p; } { m_globalProxy = p; }
static void setGlobalDumbCache(bool b) //< set "dumb" (ignore server status) caching mode static void setGlobalDumbCache(bool b) //< set "dumb" (ignore server status) caching mode
{ m_globalDumbCache = b; } { m_globalDumbCache = b; }
static void setGlobalUserAgent(QString u) //< set global user agent string
{ m_globalUserAgent = u; }
public slots: public slots:
void abort(void); void abort(void);
@ -89,14 +91,17 @@ class HttpGet : public QObject
QString m_cachefile; // cached filename QString m_cachefile; // cached filename
bool m_cached; bool m_cached;
QUrl m_proxy; QUrl m_proxy;
static QDir m_globalCache; //< global cache path value
static QUrl m_globalProxy; //< global proxy value
static bool m_globalDumbCache; //< cache "dumb" mode global setting
QDateTime m_serverTimestamp; //< timestamp of file on server QDateTime m_serverTimestamp; //< timestamp of file on server
QString m_query; //< constructed query to pass http getter QString m_query; //< constructed query to pass http getter
QString m_path; //< constructed path to pass http getter QString m_path; //< constructed path to pass http getter
QString m_hash; //< caching hash QString m_hash; //< caching hash
bool m_dumbCache; //< true if caching should ignore the server header bool m_dumbCache; //< true if caching should ignore the server header
QHttpRequestHeader m_header;
static QDir m_globalCache; //< global cache path value
static QUrl m_globalProxy; //< global proxy value
static bool m_globalDumbCache; //< cache "dumb" mode global setting
static QString m_globalUserAgent; //< global user agent string
}; };
#endif #endif

View file

@ -57,7 +57,8 @@ RbUtilQt::RbUtilQt(QWidget *parent) : QMainWindow(parent)
settings = new RbSettings(); settings = new RbSettings();
settings->open(); settings->open();
HttpGet::setGlobalUserAgent("rbutil/"VERSION);
m_gotInfo = false; m_gotInfo = false;
// manual tab // manual tab
@ -241,7 +242,7 @@ void RbUtilQt::about()
QString rline = r.readAll(); QString rline = r.readAll();
about.browserCredits->insertPlainText(rline); about.browserCredits->insertPlainText(rline);
about.browserCredits->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor); about.browserCredits->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
QString title = QString("<b>The Rockbox Utility</b><br/>Version %1").arg(VERSION); QString title = QString("<b>The Rockbox Utility</b><br/>Version %1").arg(FULLVERSION);
about.labelTitle->setText(title); about.labelTitle->setText(title);
about.labelHomepage->setText("<a href='http://www.rockbox.org'>http://www.rockbox.org</a>"); about.labelHomepage->setText("<a href='http://www.rockbox.org'>http://www.rockbox.org</a>");

View file

@ -19,9 +19,12 @@
* *
****************************************************************************/ ****************************************************************************/
#define VERSION "SVN $Revision$ (m1.0.6), built "__DATE__" "__TIME__
// PUREVERSION is needed to be able to just compare versions. It does not // PUREVERSION is needed to be able to just compare versions. It does not
// contain a build timestamp because it needs to be the same in different // contain a build timestamp because it needs to be the same in different
// files // files
// VERSION is the plain version number, used for http User-Agent string.
#define VERSION "m1.0.6"
#define PUREVERSION "SVN $Revision$" #define PUREVERSION "SVN $Revision$"
#define FULLVERSION PUREVERSION" ("VERSION"), built "__DATE__" "__TIME__