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
;
27 QUrl
HttpGet::m_globalProxy
;
29 HttpGet::HttpGet(QObject
*parent
)
33 qDebug() << "--> HttpGet::HttpGet()";
34 outputToBuffer
= true;
37 // if a request is cancelled before a reponse is available return some
38 // hint about this in the http response instead of nonsense.
41 // default to global proxy / cache if not empty.
42 // proxy is automatically enabled, disable it by setting an empty proxy
43 // cache is enabled to be in line, can get disabled with setCache(bool)
44 qDebug() << "setting global proxy / cache";
45 if(!m_globalProxy
.isEmpty())
46 setProxy(m_globalProxy
);
48 m_cachedir
= m_globalCache
;
49 connect(&http
, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
50 connect(&http
, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
51 connect(&http
, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
52 connect(&http
, SIGNAL(responseHeaderReceived(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
53 connect(&http
, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
54 connect(&http
, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
56 connect(&http
, SIGNAL(readyRead(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
61 void HttpGet::setCache(QDir d
)
66 QString p
= m_cachedir
.absolutePath() + "/rbutil-cache";
67 if(QFileInfo(m_cachedir
.absolutePath()).isDir())
69 if(!QFileInfo(p
).isDir())
70 result
= m_cachedir
.mkdir("rbutil-cache");
73 qDebug() << "HttpGet::setCache(QDir)" << d
.absolutePath() << result
;
78 void HttpGet::setCache(bool c
)
80 qDebug() << "setCache(bool)" << c
;
85 QByteArray
HttpGet::readAll()
91 QHttp::Error
HttpGet::error()
97 void HttpGet::httpProgress(int read
, int total
)
99 emit
dataReadProgress(read
, total
);
103 void HttpGet::setProxy(const QUrl
&proxy
)
105 qDebug() << "HttpGet::setProxy(QUrl)" << proxy
.toString();
107 http
.setProxy(m_proxy
.host(), m_proxy
.port(), m_proxy
.userName(), m_proxy
.password());
111 void HttpGet::setProxy(bool enable
)
113 qDebug() << "HttpGet::setProxy(bool)" << enable
;
115 http
.setProxy(m_proxy
.host(), m_proxy
.port(), m_proxy
.userName(), m_proxy
.password());
117 http
.setProxy("", 0);
121 void HttpGet::setFile(QFile
*file
)
124 outputToBuffer
= false;
125 qDebug() << "HttpGet::setFile" << outputFile
->fileName();
129 void HttpGet::abort()
137 bool HttpGet::getFile(const QUrl
&url
)
139 if (!url
.isValid()) {
140 qDebug() << "Error: Invalid URL" << endl
;
144 if (url
.scheme() != "http") {
145 qDebug() << "Error: URL must start with 'http:'" << endl
;
149 if (url
.path().isEmpty()) {
150 qDebug() << "Error: URL has no path" << endl
;
153 // if no output file was set write to buffer
154 if(!outputToBuffer
) {
155 if (!outputFile
->open(QIODevice::ReadWrite
)) {
156 qDebug() << "Error: Cannot open " << qPrintable(outputFile
->fileName())
157 << " for writing: " << qPrintable(outputFile
->errorString())
162 // put hash generation here so it can get reused later
163 QString hash
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Md5
).toHex();
164 cachefile
= m_cachedir
.absolutePath() + "/rbutil-cache/" + hash
;
166 // check if the file is present in cache
167 qDebug() << "[HTTP] cache ENABLED for" << url
.toEncoded();
168 if(QFileInfo(cachefile
).isReadable() && QFileInfo(cachefile
).size() > 0) {
169 qDebug() << "[HTTP] cached file found!" << cachefile
;
172 if(!outputToBuffer
) {
173 qDebug() << outputFile
->fileName();
174 c
.open(QIODevice::ReadOnly
);
175 outputFile
->open(QIODevice::ReadWrite
);
176 outputFile
->write(c
.readAll());
181 c
.open(QIODevice::ReadOnly
);
182 dataBuffer
= c
.readAll();
185 response
= 200; // fake "200 OK" HTTP response
187 httpDone(false); // we're done now. This will emit the correct signal too.
190 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile
;
194 qDebug() << "[HTTP] cache DISABLED";
197 http
.setHost(url
.host(), url
.port(80));
198 // construct query (if any)
199 QList
<QPair
<QString
, QString
> > qitems
= url
.queryItems();
202 for(int i
= 0; i
< qitems
.size(); i
++)
203 query
+= qitems
.at(i
).first
+ "=" + qitems
.at(i
).second
+ "&";
208 qDebug() << "[HTTP] downloading to buffer:" << url
.toString();
209 getRequest
= http
.get(url
.path() + query
);
212 qDebug() << "[HTTP] downloading to file:" << url
.toString() << qPrintable(outputFile
->fileName());
213 getRequest
= http
.get(url
.path() + query
, outputFile
);
215 qDebug() << "[HTTP] request scheduled: GET" << getRequest
;
221 void HttpGet::httpDone(bool error
)
224 qDebug() << "[HTTP] Error: " << qPrintable(http
.errorString()) << httpResponse();
229 if(m_usecache
&& !cached
) {
230 qDebug() << "[HTTP] creating cache file" << cachefile
;
232 c
.open(QIODevice::ReadWrite
);
233 if(!outputToBuffer
) {
234 outputFile
->open(QIODevice::ReadOnly
| QIODevice::Truncate
);
235 c
.write(outputFile
->readAll());
247 void HttpGet::httpFinished(int id
, bool error
)
249 qDebug() << "HttpGet::httpFinished(int, bool) =" << id
<< error
;
250 if(id
== getRequest
) dataBuffer
= http
.readAll();
251 qDebug() << "pending:" << http
.hasPendingRequests();
252 //if(!http.hasPendingRequests()) httpDone(error);
253 emit
requestFinished(id
, error
);
257 void HttpGet::httpStarted(int id
)
259 qDebug() << "HttpGet::httpStarted(int) =" << id
;
263 QString
HttpGet::errorString()
265 return http
.errorString();
269 void HttpGet::httpResponseHeader(const QHttpResponseHeader
&resp
)
271 // if there is a network error abort all scheduled requests for
273 response
= resp
.statusCode();
274 if(response
!= 200) {
275 qDebug() << "http response error:" << response
<< resp
.reasonPhrase();
278 // 301 -- moved permanently
281 // 307 -- moved temporarily
282 // in all cases, header: location has the correct address so we can follow.
283 if(response
== 301 || response
== 302 || response
== 303 || response
== 307) {
284 // start new request with new url
285 qDebug() << "http response" << response
<< "- following";
286 getFile(resp
.value("location") + query
);
291 int HttpGet::httpResponse()
297 void HttpGet::httpState(int state
)
299 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
300 "Reading", "Connected", "Closing"};
302 qDebug() << "HttpGet::httpState() = " << s
[state
];
303 else qDebug() << "HttpGet::httpState() = " << state
;