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
)
33 outputToBuffer
= true;
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() << "HttpGet::setCache(QDir)" << d
.absolutePath() << result
;
71 /** @brief enable / disable cache useage
72 * @param c set cache usage
74 void HttpGet::setCache(bool c
)
76 qDebug() << "setCache(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 QByteArray
HttpGet::readAll()
109 /** @brief get http error
112 QHttp::Error
HttpGet::error()
118 void HttpGet::httpProgress(int read
, int total
)
120 emit
dataReadProgress(read
, total
);
124 void HttpGet::setProxy(const QUrl
&proxy
)
126 qDebug() << "HttpGet::setProxy(QUrl)" << proxy
.toString();
128 http
.setProxy(m_proxy
.host(), m_proxy
.port(), m_proxy
.userName(), m_proxy
.password());
132 void HttpGet::setProxy(bool enable
)
134 qDebug() << "HttpGet::setProxy(bool)" << enable
;
136 http
.setProxy(m_proxy
.host(), m_proxy
.port(), m_proxy
.userName(), m_proxy
.password());
138 http
.setProxy("", 0);
142 void HttpGet::setFile(QFile
*file
)
145 outputToBuffer
= false;
146 qDebug() << "HttpGet::setFile" << outputFile
->fileName();
150 void HttpGet::abort()
158 bool HttpGet::getFile(const QUrl
&url
)
160 if (!url
.isValid()) {
161 qDebug() << "Error: Invalid URL" << endl
;
165 if (url
.scheme() != "http") {
166 qDebug() << "Error: URL must start with 'http:'" << endl
;
170 if (url
.path().isEmpty()) {
171 qDebug() << "Error: URL has no path" << endl
;
174 // if no output file was set write to buffer
175 if(!outputToBuffer
) {
176 if (!outputFile
->open(QIODevice::ReadWrite
)) {
177 qDebug() << "Error: Cannot open " << qPrintable(outputFile
->fileName())
178 << " for writing: " << qPrintable(outputFile
->errorString())
183 // put hash generation here so it can get reused later
184 QString hash
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Md5
).toHex();
185 cachefile
= m_cachedir
.absolutePath() + "/rbutil-cache/" + hash
;
187 // check if the file is present in cache
188 qDebug() << "[HTTP] cache ENABLED for" << url
.toEncoded();
189 if(QFileInfo(cachefile
).isReadable() && QFileInfo(cachefile
).size() > 0) {
190 qDebug() << "[HTTP] cached file found!" << cachefile
;
193 if(!outputToBuffer
) {
194 qDebug() << outputFile
->fileName();
195 c
.open(QIODevice::ReadOnly
);
196 outputFile
->open(QIODevice::ReadWrite
);
197 outputFile
->write(c
.readAll());
202 c
.open(QIODevice::ReadOnly
);
203 dataBuffer
= c
.readAll();
206 response
= 200; // fake "200 OK" HTTP response
208 httpDone(false); // we're done now. This will emit the correct signal too.
211 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile
;
215 qDebug() << "[HTTP] cache DISABLED";
218 http
.setHost(url
.host(), url
.port(80));
219 // construct query (if any)
220 QList
<QPair
<QString
, QString
> > qitems
= url
.queryItems();
223 for(int i
= 0; i
< qitems
.size(); i
++)
224 query
+= qitems
.at(i
).first
+ "=" + qitems
.at(i
).second
+ "&";
229 path
= QString(QUrl::toPercentEncoding(url
.path(), "/"));
231 qDebug() << "[HTTP] downloading to buffer:" << url
.toString();
232 getRequest
= http
.get(path
+ query
);
235 qDebug() << "[HTTP] downloading to file:" << url
.toString() << qPrintable(outputFile
->fileName());
236 getRequest
= http
.get(path
+ query
, outputFile
);
238 qDebug() << "[HTTP] request scheduled: GET" << getRequest
;
244 void HttpGet::httpDone(bool error
)
247 qDebug() << "[HTTP] Error: " << qPrintable(http
.errorString()) << httpResponse();
252 if(m_usecache
&& !cached
) {
253 qDebug() << "[HTTP] creating cache file" << cachefile
;
255 c
.open(QIODevice::ReadWrite
);
256 if(!outputToBuffer
) {
257 outputFile
->open(QIODevice::ReadOnly
| QIODevice::Truncate
);
258 c
.write(outputFile
->readAll());
270 void HttpGet::httpFinished(int id
, bool error
)
272 qDebug() << "HttpGet::httpFinished(int, bool) =" << id
<< error
;
273 if(id
== getRequest
) dataBuffer
= http
.readAll();
274 qDebug() << "pending:" << http
.hasPendingRequests();
275 //if(!http.hasPendingRequests()) httpDone(error);
276 emit
requestFinished(id
, error
);
280 void HttpGet::httpStarted(int id
)
282 qDebug() << "HttpGet::httpStarted(int) =" << id
;
286 QString
HttpGet::errorString()
288 return http
.errorString();
292 void HttpGet::httpResponseHeader(const QHttpResponseHeader
&resp
)
294 // if there is a network error abort all scheduled requests for
296 response
= resp
.statusCode();
297 if(response
!= 200) {
298 qDebug() << "http response error:" << response
<< resp
.reasonPhrase();
301 // 301 -- moved permanently
304 // 307 -- moved temporarily
305 // in all cases, header: location has the correct address so we can follow.
306 if(response
== 301 || response
== 302 || response
== 303 || response
== 307) {
307 // start new request with new url
308 qDebug() << "http response" << response
<< "- following";
309 getFile(resp
.value("location") + query
);
314 int HttpGet::httpResponse()
320 void HttpGet::httpState(int state
)
322 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
323 "Reading", "Connected", "Closing"};
325 qDebug() << "HttpGet::httpState() = " << s
[state
];
326 else qDebug() << "HttpGet::httpState() = " << state
;