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
29 HttpGet::HttpGet(QObject
*parent
)
32 outputToBuffer
= true;
34 m_noHeaderCheck
= false;
36 // if a request is cancelled before a reponse is available return some
37 // hint about this in the http response instead of nonsense.
40 // default to global proxy / cache if not empty.
41 // proxy is automatically enabled, disable it by setting an empty proxy
42 // cache is enabled to be in line, can get disabled with setCache(bool)
43 if(!m_globalProxy
.isEmpty())
44 setProxy(m_globalProxy
);
46 m_cachedir
= m_globalCache
;
47 connect(&http
, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
48 connect(&http
, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
49 connect(&http
, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
50 connect(&http
, SIGNAL(responseHeaderReceived(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
51 connect(&http
, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
52 connect(&http
, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
54 connect(&http
, SIGNAL(readyRead(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
59 //! @brief set cache path
60 // @param d new directory to use as cache path
61 void HttpGet::setCache(QDir d
)
65 result
= initializeCache(d
);
66 qDebug() << "[HTTP]"<< __func__
<< "(QDir)" << d
.absolutePath() << result
;
71 /** @brief enable / disable cache useage
72 * @param c set cache usage
74 void HttpGet::setCache(bool c
)
76 qDebug() << "[HTTP]" << __func__
<< "(bool) =" << c
;
78 // make sure cache is initialized
80 m_usecache
= initializeCache(m_cachedir
);
84 bool HttpGet::initializeCache(const QDir
& d
)
87 QString p
= d
.absolutePath() + "/rbutil-cache";
88 if(QFileInfo(d
.absolutePath()).isDir())
90 if(!QFileInfo(p
).isDir())
91 result
= d
.mkdir("rbutil-cache");
103 /** @brief read all downloaded data into a buffer
106 QByteArray
HttpGet::readAll()
112 /** @brief get http error
115 QHttp::Error
HttpGet::error()
121 void HttpGet::httpProgress(int read
, int total
)
123 emit
dataReadProgress(read
, total
);
127 void HttpGet::setProxy(const QUrl
&proxy
)
129 qDebug() << "[HTTP]" << __func__
<< "(QUrl)" << proxy
.toString();
131 http
.setProxy(m_proxy
.host(), m_proxy
.port(), m_proxy
.userName(), m_proxy
.password());
135 void HttpGet::setProxy(bool enable
)
137 qDebug() << "[HTTP]" << __func__
<< "(bool)" << enable
;
139 http
.setProxy(m_proxy
.host(), m_proxy
.port(), m_proxy
.userName(), m_proxy
.password());
141 http
.setProxy("", 0);
145 void HttpGet::setFile(QFile
*file
)
148 outputToBuffer
= false;
149 qDebug() << "[HTTP]" << __func__
<< "(QFile*)" << outputFile
->fileName();
153 void HttpGet::abort()
161 bool HttpGet::getFile(const QUrl
&url
)
163 if (!url
.isValid()) {
164 qDebug() << "[HTTP] Error: Invalid URL" << endl
;
168 if (url
.scheme() != "http") {
169 qDebug() << "[HTTP] Error: URL must start with 'http:'" << endl
;
173 if (url
.path().isEmpty()) {
174 qDebug() << "[HTTP] Error: URL has no path" << endl
;
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_noHeaderCheck
|| !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
) {
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 m_serverTimestamp
= QDateTime();
304 // take care of concurring requests. If there is still one running,
305 // don't emit done(). That request will call this slot again.
306 if(http
.currentId() == 0 && !http
.hasPendingRequests())
311 void HttpGet::httpFinished(int id
, bool error
)
313 qDebug() << "[HTTP]" << __func__
<< "(int, bool) =" << id
<< error
;
314 if(id
== getRequest
) {
315 dataBuffer
= http
.readAll();
317 emit
requestFinished(id
, error
);
319 qDebug() << "[HTTP] hasPendingRequests =" << http
.hasPendingRequests();
322 if(id
== headRequest
) {
323 QHttpResponseHeader h
= http
.lastResponse();
325 QString date
= h
.value("Last-Modified").simplified();
327 m_serverTimestamp
= QDateTime(); // no value = invalid
328 emit
headerFinished();
331 // to successfully parse the date strip weekday and timezone
332 date
.remove(0, date
.indexOf(" ") + 1);
333 if(date
.endsWith("GMT"))
334 date
.truncate(date
.indexOf(" GMT"));
335 // distinguish input formats (see RFC1945)
337 if(date
.contains("-"))
338 m_serverTimestamp
= QDateTime::fromString(date
, "dd-MMM-yy hh:mm:ss");
340 else if(date
.at(0).isLetter())
341 m_serverTimestamp
= QDateTime::fromString(date
, "MMM d hh:mm:ss yyyy");
344 m_serverTimestamp
= QDateTime::fromString(date
, "dd MMM yyyy hh:mm:ss");
345 qDebug() << "[HTTP] Header Request Date:" << date
<< ", parsed:" << m_serverTimestamp
;
346 emit
headerFinished();
350 emit
requestFinished(id
, error
);
353 void HttpGet::httpStarted(int id
)
355 qDebug() << "[HTTP]" << __func__
<< "(int) =" << id
;
356 qDebug() << "headRequest" << headRequest
<< "getRequest" << getRequest
;
360 QString
HttpGet::errorString()
362 return http
.errorString();
366 void HttpGet::httpResponseHeader(const QHttpResponseHeader
&resp
)
368 // if there is a network error abort all scheduled requests for
370 m_response
= resp
.statusCode();
371 if(m_response
!= 200) {
372 qDebug() << "[HTTP] response error =" << m_response
<< resp
.reasonPhrase();
375 // 301 -- moved permanently
378 // 307 -- moved temporarily
379 // in all cases, header: location has the correct address so we can follow.
380 if(m_response
== 301 || m_response
== 302 || m_response
== 303 || m_response
== 307) {
381 // start new request with new url
382 qDebug() << "[HTTP] response =" << m_response
<< "- following";
383 getFile(resp
.value("location") + m_query
);
388 int HttpGet::httpResponse()
394 void HttpGet::httpState(int state
)
396 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
397 "Reading", "Connected", "Closing"};
399 qDebug() << "[HTTP]" << __func__
<< "() = " << s
[state
];
400 else qDebug() << "[HTTP]" << __func__
<< "() = " << state
;