1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Riebeling
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
26 QDir
HttpGet::m_globalCache
; //< global cach path value for new objects
27 QUrl
HttpGet::m_globalProxy
; //< global proxy value for new objects
28 bool HttpGet::m_globalDumbCache
= false; //< globally set cache "dumb" mode
30 HttpGet::HttpGet(QObject
*parent
)
33 outputToBuffer
= true;
35 m_dumbCache
= m_globalDumbCache
;
38 // if a request is cancelled before a reponse is available return some
39 // hint about this in the http response instead of nonsense.
42 // default to global proxy / cache if not empty.
43 // proxy is automatically enabled, disable it by setting an empty proxy
44 // cache is enabled to be in line, can get disabled with setCache(bool)
45 if(!m_globalProxy
.isEmpty())
46 setProxy(m_globalProxy
);
48 m_cachedir
= m_globalCache
;
50 m_serverTimestamp
= QDateTime();
52 connect(&http
, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
53 connect(&http
, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(dataReadProgress(int, int)));
54 connect(&http
, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
55 connect(&http
, SIGNAL(responseHeaderReceived(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
56 connect(&http
, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
57 connect(&http
, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
59 connect(&http
, SIGNAL(readyRead(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
64 //! @brief set cache path
65 // @param d new directory to use as cache path
66 void HttpGet::setCache(QDir d
)
70 result
= initializeCache(d
);
71 qDebug() << "[HTTP]"<< __func__
<< "(QDir)" << d
.absolutePath() << result
;
76 /** @brief enable / disable cache useage
77 * @param c set cache usage
79 void HttpGet::setCache(bool c
)
81 qDebug() << "[HTTP]" << __func__
<< "(bool) =" << c
;
83 // make sure cache is initialized
85 m_usecache
= initializeCache(m_cachedir
);
89 bool HttpGet::initializeCache(const QDir
& d
)
92 QString p
= d
.absolutePath() + "/rbutil-cache";
93 if(QFileInfo(d
.absolutePath()).isDir())
95 if(!QFileInfo(p
).isDir())
96 result
= d
.mkdir("rbutil-cache");
108 /** @brief read all downloaded data into a buffer
111 QByteArray
HttpGet::readAll()
117 /** @brief get http error
120 QHttp::Error
HttpGet::error()
126 void HttpGet::setProxy(const QUrl
&proxy
)
128 qDebug() << "[HTTP]" << __func__
<< "(QUrl)" << proxy
.toString();
130 http
.setProxy(m_proxy
.host(), m_proxy
.port(), m_proxy
.userName(), m_proxy
.password());
134 void HttpGet::setProxy(bool enable
)
136 qDebug() << "[HTTP]" << __func__
<< "(bool)" << enable
;
138 http
.setProxy(m_proxy
.host(), m_proxy
.port(), m_proxy
.userName(), m_proxy
.password());
140 http
.setProxy("", 0);
144 void HttpGet::setFile(QFile
*file
)
147 outputToBuffer
= false;
148 qDebug() << "[HTTP]" << __func__
<< "(QFile*)" << outputFile
->fileName();
152 void HttpGet::abort()
160 bool HttpGet::getFile(const QUrl
&url
)
162 if (!url
.isValid()) {
163 qDebug() << "[HTTP] Error: Invalid URL" << endl
;
167 if (url
.scheme() != "http") {
168 qDebug() << "[HTTP] Error: URL must start with 'http:'" << endl
;
172 if (url
.path().isEmpty()) {
173 qDebug() << "[HTTP] Error: URL has no path" << endl
;
176 m_serverTimestamp
= QDateTime();
177 // if no output file was set write to buffer
178 if(!outputToBuffer
) {
179 if (!outputFile
->open(QIODevice::ReadWrite
)) {
180 qDebug() << "[HTTP] Error: Cannot open " << qPrintable(outputFile
->fileName())
181 << " for writing: " << qPrintable(outputFile
->errorString())
186 qDebug() << "[HTTP] downloading" << url
.toEncoded();
188 http
.setHost(url
.host(), url
.port(80));
189 // construct query (if any)
190 QList
<QPair
<QString
, QString
> > qitems
= url
.queryItems();
193 for(int i
= 0; i
< qitems
.size(); i
++)
194 m_query
+= QUrl::toPercentEncoding(qitems
.at(i
).first
, "/") + "="
195 + QUrl::toPercentEncoding(qitems
.at(i
).second
, "/") + "&";
198 // create hash used for caching
199 m_hash
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Md5
).toHex();
200 m_path
= QString(QUrl::toPercentEncoding(url
.path(), "/"));
202 if(m_dumbCache
|| !m_usecache
) {
206 // request HTTP header
207 connect(this, SIGNAL(headerFinished()), this, SLOT(getFileFinish()));
208 headRequest
= http
.head(m_path
+ m_query
);
215 void HttpGet::getFileFinish()
217 m_cachefile
= m_cachedir
.absolutePath() + "/rbutil-cache/" + m_hash
;
219 // check if the file is present in cache
220 qDebug() << "[HTTP] cache ENABLED";
221 QFileInfo cachefile
= QFileInfo(m_cachefile
);
222 if(cachefile
.isReadable()
223 && cachefile
.size() > 0
224 && cachefile
.lastModified() > m_serverTimestamp
) {
226 qDebug() << "[HTTP] cached file found:" << m_cachefile
;
229 QFile
c(m_cachefile
);
230 if(!outputToBuffer
) {
231 qDebug() << "[HTTP] copying cache file to output" << outputFile
->fileName();
232 c
.open(QIODevice::ReadOnly
);
233 outputFile
->open(QIODevice::ReadWrite
);
234 outputFile
->write(c
.readAll());
239 qDebug() << "[HTTP] reading cache file into buffer";
240 c
.open(QIODevice::ReadOnly
);
241 dataBuffer
= c
.readAll();
244 m_response
= 200; // fake "200 OK" HTTP response
246 httpDone(false); // we're done now. Fake http "done" signal.
250 if(cachefile
.isReadable())
251 qDebug() << "[HTTP] file in cache timestamp:" << cachefile
.lastModified();
253 qDebug() << "[HTTP] file not in cache.";
254 qDebug() << "[HTTP] server file timestamp:" << m_serverTimestamp
;
255 qDebug() << "[HTTP] downloading file to" << m_cachefile
;
256 // unlink old cache file
257 if(cachefile
.isReadable())
258 QFile(m_cachefile
).remove();
263 qDebug() << "[HTTP] cache DISABLED";
267 qDebug() << "[HTTP] downloading to buffer.";
268 getRequest
= http
.get(m_path
+ m_query
);
271 qDebug() << "[HTTP] downloading to file:"
272 << qPrintable(outputFile
->fileName());
273 getRequest
= http
.get(m_path
+ m_query
, outputFile
);
275 qDebug() << "[HTTP] GET request scheduled, id:" << getRequest
;
281 void HttpGet::httpDone(bool error
)
284 qDebug() << "[HTTP] Error: " << qPrintable(http
.errorString()) << httpResponse();
289 if(m_usecache
&& !m_cached
&& !error
) {
290 qDebug() << "[HTTP] creating cache file" << m_cachefile
;
291 QFile
c(m_cachefile
);
292 c
.open(QIODevice::ReadWrite
);
293 if(!outputToBuffer
) {
294 outputFile
->open(QIODevice::ReadOnly
| QIODevice::Truncate
);
295 c
.write(outputFile
->readAll());
303 // if cached file found and cache enabled ignore http errors
304 if(m_usecache
&& m_cached
&& !http
.hasPendingRequests()) {
307 // take care of concurring requests. If there is still one running,
308 // don't emit done(). That request will call this slot again.
309 if(http
.currentId() == 0 && !http
.hasPendingRequests())
314 void HttpGet::httpFinished(int id
, bool error
)
316 qDebug() << "[HTTP]" << __func__
<< "(int, bool) =" << id
<< error
;
317 if(id
== getRequest
) {
318 dataBuffer
= http
.readAll();
320 emit
requestFinished(id
, error
);
322 qDebug() << "[HTTP] hasPendingRequests =" << http
.hasPendingRequests();
325 if(id
== headRequest
) {
326 QHttpResponseHeader h
= http
.lastResponse();
328 QString date
= h
.value("Last-Modified").simplified();
330 m_serverTimestamp
= QDateTime(); // no value = invalid
331 emit
headerFinished();
334 // to successfully parse the date strip weekday and timezone
335 date
.remove(0, date
.indexOf(" ") + 1);
336 if(date
.endsWith("GMT"))
337 date
.truncate(date
.indexOf(" GMT"));
338 // distinguish input formats (see RFC1945)
340 if(date
.contains("-"))
341 m_serverTimestamp
= QDateTime::fromString(date
, "dd-MMM-yy hh:mm:ss");
343 else if(date
.at(0).isLetter())
344 m_serverTimestamp
= QDateTime::fromString(date
, "MMM d hh:mm:ss yyyy");
347 m_serverTimestamp
= QDateTime::fromString(date
, "dd MMM yyyy hh:mm:ss");
348 qDebug() << "[HTTP] Header Request Date:" << date
<< ", parsed:" << m_serverTimestamp
;
349 emit
headerFinished();
353 emit
requestFinished(id
, error
);
356 void HttpGet::httpStarted(int id
)
358 qDebug() << "[HTTP]" << __func__
<< "(int) =" << id
;
359 qDebug() << "headRequest" << headRequest
<< "getRequest" << getRequest
;
363 QString
HttpGet::errorString()
365 return http
.errorString();
369 void HttpGet::httpResponseHeader(const QHttpResponseHeader
&resp
)
371 // if there is a network error abort all scheduled requests for
373 m_response
= resp
.statusCode();
374 if(m_response
!= 200) {
375 qDebug() << "[HTTP] response error =" << m_response
<< resp
.reasonPhrase();
378 // 301 -- moved permanently
381 // 307 -- moved temporarily
382 // in all cases, header: location has the correct address so we can follow.
383 if(m_response
== 301 || m_response
== 302 || m_response
== 303 || m_response
== 307) {
384 // start new request with new url
385 qDebug() << "[HTTP] response =" << m_response
<< "- following";
386 getFile(resp
.value("location") + m_query
);
391 int HttpGet::httpResponse()
397 void HttpGet::httpState(int state
)
399 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
400 "Reading", "Connected", "Closing"};
402 qDebug() << "[HTTP]" << __func__
<< "() = " << s
[state
];
403 else qDebug() << "[HTTP]" << __func__
<< "() = " << state
;