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
29 QString
HttpGet::m_globalUserAgent
; //< globally set user agent for requests
31 HttpGet::HttpGet(QObject
*parent
)
34 outputToBuffer
= true;
36 m_dumbCache
= m_globalDumbCache
;
39 // if a request is cancelled before a reponse is available return some
40 // hint about this in the http response instead of nonsense.
44 // default to global proxy / cache if not empty.
45 // proxy is automatically enabled, disable it by setting an empty proxy
46 // cache is enabled to be in line, can get disabled with setCache(bool)
47 if(!m_globalProxy
.isEmpty())
48 setProxy(m_globalProxy
);
50 m_cachedir
= m_globalCache
;
52 m_serverTimestamp
= QDateTime();
54 connect(&http
, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
55 connect(&http
, SIGNAL(dataReadProgress(int, int)),
56 this, SIGNAL(dataReadProgress(int, int)));
57 connect(&http
, SIGNAL(requestFinished(int, bool)),
58 this, SLOT(httpFinished(int, bool)));
59 connect(&http
, SIGNAL(responseHeaderReceived(const QHttpResponseHeader
&)),
60 this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
61 // connect(&http, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
62 connect(&http
, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
64 connect(&http
, SIGNAL(readyRead(const QHttpResponseHeader
&)),
65 this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
66 connect(this, SIGNAL(headerFinished()), this, SLOT(getFileFinish()));
71 //! @brief set cache path
72 // @param d new directory to use as cache path
73 void HttpGet::setCache(const QDir
& d
)
77 result
= initializeCache(d
);
82 /** @brief enable / disable cache useage
83 * @param c set cache usage
85 void HttpGet::setCache(bool c
)
88 // make sure cache is initialized
90 m_usecache
= initializeCache(m_cachedir
);
94 bool HttpGet::initializeCache(const QDir
& d
)
97 QString p
= d
.absolutePath() + "/rbutil-cache";
98 if(QFileInfo(d
.absolutePath()).isDir())
100 if(!QFileInfo(p
).isDir())
101 result
= d
.mkdir("rbutil-cache");
113 /** @brief read all downloaded data into a buffer
116 QByteArray
HttpGet::readAll()
122 /** @brief get http error
125 QHttp::Error
HttpGet::error()
131 void HttpGet::setProxy(const QUrl
&proxy
)
135 http
.setProxy(m_proxy
.host(), m_proxy
.port(),
136 m_proxy
.userName(), m_proxy
.password());
140 void HttpGet::setProxy(bool enable
)
144 http
.setProxy(m_proxy
.host(), m_proxy
.port(),
145 m_proxy
.userName(), m_proxy
.password());
149 http
.setProxy("", 0);
154 void HttpGet::setFile(QFile
*file
)
157 outputToBuffer
= false;
161 void HttpGet::abort()
163 qDebug() << "[HTTP] Aborting requests, pending:" << http
.hasPendingRequests();
170 bool HttpGet::getFile(const QUrl
&url
)
172 if (!url
.isValid()) {
173 qDebug() << "[HTTP] Error: Invalid URL" << endl
;
177 if (url
.scheme() != "http") {
178 qDebug() << "[HTTP] Error: URL must start with 'http:'" << endl
;
182 if (url
.path().isEmpty()) {
183 qDebug() << "[HTTP] Error: URL has no path" << endl
;
186 m_serverTimestamp
= QDateTime();
187 // if no output file was set write to buffer
188 if(!outputToBuffer
) {
189 if (!outputFile
->open(QIODevice::ReadWrite
)) {
190 qDebug() << "[HTTP] Error: Cannot open " << qPrintable(outputFile
->fileName())
191 << " for writing: " << qPrintable(outputFile
->errorString());
196 // output to buffer. Make sure buffer is empty so no old data gets
197 // returned in case the object is reused.
200 qDebug() << "[HTTP] GET URI" << url
.toEncoded();
202 http
.setHost(url
.host(), url
.port(80));
203 // construct query (if any)
204 QList
<QPair
<QString
, QString
> > qitems
= url
.queryItems();
207 for(int i
= 0; i
< qitems
.size(); i
++)
208 m_query
+= QUrl::toPercentEncoding(qitems
.at(i
).first
, "/") + "="
209 + QUrl::toPercentEncoding(qitems
.at(i
).second
, "/") + "&";
212 // create hash used for caching
213 m_hash
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Md5
).toHex();
214 // RFC2616: the absoluteURI form must get used when the request is being
218 m_path
= url
.scheme() + "://" + url
.host();
219 m_path
+= QString(QUrl::toPercentEncoding(url
.path(), "/"));
221 // construct request header
222 m_header
.setValue("Host", url
.host());
223 m_header
.setValue("User-Agent", m_globalUserAgent
);
224 m_header
.setValue("Connection", "Keep-Alive");
226 if(m_dumbCache
|| !m_usecache
) {
230 // schedule HTTP header request
231 m_header
.setRequest("HEAD", m_path
+ m_query
);
232 headRequest
= http
.request(m_header
);
233 qDebug() << "[HTTP] HEAD scheduled: " << headRequest
;
240 void HttpGet::getFileFinish()
242 m_cachefile
= m_cachedir
.absolutePath() + "/rbutil-cache/" + m_hash
;
244 // check if the file is present in cache
245 QFileInfo cachefile
= QFileInfo(m_cachefile
);
246 if(cachefile
.isReadable()
247 && cachefile
.size() > 0
248 && cachefile
.lastModified() > m_serverTimestamp
) {
250 qDebug() << "[HTTP] Cache: up-to-date file found:" << m_cachefile
;
253 QFile
c(m_cachefile
);
254 if(!outputToBuffer
) {
255 qDebug() << "[HTTP] Cache: copying file to output"
256 << outputFile
->fileName();
257 c
.open(QIODevice::ReadOnly
);
258 outputFile
->open(QIODevice::ReadWrite
);
259 outputFile
->write(c
.readAll());
264 qDebug() << "[HTTP] Cache: reading file into buffer";
265 c
.open(QIODevice::ReadOnly
);
266 dataBuffer
= c
.readAll();
269 m_response
= 200; // fake "200 OK" HTTP response
271 httpDone(false); // we're done now. Handle http "done" signal.
275 // unlink old cache file
276 if(cachefile
.isReadable()) {
277 QFile(m_cachefile
).remove();
278 qDebug() << "[HTTP] Cache: outdated, timestamp:"
279 << cachefile
.lastModified();
281 qDebug() << "[HTTP] Cache: caching as" << m_cachefile
;
286 qDebug() << "[HTTP] cache DISABLED";
288 // schedule GET request
290 m_header
.setRequest("GET", m_path
+ m_query
);
292 qDebug() << "[HTTP] downloading to buffer.";
293 getRequest
= http
.request(m_header
);
296 qDebug() << "[HTTP] downloading to file:"
297 << qPrintable(outputFile
->fileName());
298 getRequest
= http
.request(m_header
, 0, outputFile
);
300 qDebug() << "[HTTP] GET scheduled: " << getRequest
;
306 void HttpGet::httpDone(bool error
)
309 qDebug() << "[HTTP] Error:" << qPrintable(http
.errorString()) << httpResponse();
314 if(m_usecache
&& !m_cached
&& !error
) {
315 qDebug() << "[HTTP] creating cache file" << m_cachefile
;
316 QFile
c(m_cachefile
);
317 c
.open(QIODevice::ReadWrite
);
318 if(!outputToBuffer
) {
319 outputFile
->open(QIODevice::ReadOnly
| QIODevice::Truncate
);
320 c
.write(outputFile
->readAll());
328 // take care of concurring requests. If there is still one running,
329 // don't emit done(). That request will call this slot again.
330 if(http
.currentId() == 0 && !http
.hasPendingRequests())
335 void HttpGet::httpFinished(int id
, bool error
)
337 qDebug() << "[HTTP] Request finished:" << id
<< "Error:" << error
338 << "pending requests:" << http
.hasPendingRequests();
339 if(id
== getRequest
) {
340 dataBuffer
= http
.readAll();
341 emit
requestFinished(id
, error
);
344 if(id
== headRequest
) {
345 QHttpResponseHeader h
= http
.lastResponse();
347 QString date
= h
.value("Last-Modified").simplified();
349 m_serverTimestamp
= QDateTime(); // no value = invalid
350 emit
headerFinished();
353 // to successfully parse the date strip weekday and timezone
354 date
.remove(0, date
.indexOf(" ") + 1);
355 if(date
.endsWith("GMT"))
356 date
.truncate(date
.indexOf(" GMT"));
357 // distinguish input formats (see RFC1945)
359 if(date
.contains("-"))
360 m_serverTimestamp
= QDateTime::fromString(date
, "dd-MMM-yy hh:mm:ss");
362 else if(date
.at(0).isLetter())
363 m_serverTimestamp
= QDateTime::fromString(date
, "MMM d hh:mm:ss yyyy");
366 m_serverTimestamp
= QDateTime::fromString(date
, "dd MMM yyyy hh:mm:ss");
367 qDebug() << "[HTTP] HEAD finished, server date:" << date
<< ", parsed:" << m_serverTimestamp
;
368 emit
headerFinished();
372 emit
requestFinished(id
, error
);
375 void HttpGet::httpStarted(int id
)
377 qDebug() << "[HTTP] Request started: " << id
<< "Header req:"
378 << headRequest
<< "Get req:" << getRequest
;
382 QString
HttpGet::errorString()
384 return http
.errorString();
388 void HttpGet::httpResponseHeader(const QHttpResponseHeader
&resp
)
390 // if there is a network error abort all scheduled requests for
392 m_response
= resp
.statusCode();
393 if(m_response
!= 200) {
394 // abort old request first.
397 // 301 -- moved permanently
400 // 307 -- moved temporarily
401 // in all cases, header: location has the correct address so we can follow.
402 if(m_response
== 301 || m_response
== 302 || m_response
== 303 || m_response
== 307) {
403 // start new request with new url
404 qDebug() << "[HTTP] response =" << m_response
<< "- following";
405 getFile(resp
.value("location") + m_query
);
407 else if(m_response
!= 200) {
408 // all other errors are fatal.
409 qDebug() << "[HTTP] Response error:" << m_response
<< resp
.reasonPhrase();
415 int HttpGet::httpResponse()
421 void HttpGet::httpState(int state
)
423 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
424 "Reading", "Connected", "Closing"};
426 qDebug() << "[HTTP] State:" << s
[state
];
427 else qDebug() << "[HTTP] State:" << state
;