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 ****************************************************************************/
27 HttpGet::HttpGet(QObject
*parent
)
31 qDebug() << "--> HttpGet::HttpGet()";
32 outputToBuffer
= true;
35 // if a request is cancelled before a reponse is available return some
36 // hint about this in the http response instead of nonsense.
39 connect(&http
, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
40 connect(&http
, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
41 connect(&http
, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
42 connect(&http
, SIGNAL(responseHeaderReceived(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
43 connect(&http
, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
44 connect(&http
, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
46 connect(&http
, SIGNAL(readyRead(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
51 void HttpGet::setCache(QDir d
)
56 QString p
= m_cachedir
.absolutePath() + "/rbutil-cache";
57 if(QFileInfo(m_cachedir
.absolutePath()).isDir())
59 if(!QFileInfo(p
).isDir())
60 result
= m_cachedir
.mkdir("rbutil-cache");
63 qDebug() << "HttpGet::setCache(QDir)" << result
;
68 void HttpGet::setCache(bool c
)
74 QByteArray
HttpGet::readAll()
80 QHttp::Error
HttpGet::error()
86 void HttpGet::httpProgress(int read
, int total
)
88 emit
dataReadProgress(read
, total
);
92 void HttpGet::setProxy(const QUrl
&proxy
)
94 qDebug() << "HttpGet::setProxy" << proxy
.toString();
95 http
.setProxy(proxy
.host(), proxy
.port(), proxy
.userName(), proxy
.password());
99 void HttpGet::setFile(QFile
*file
)
102 outputToBuffer
= false;
103 qDebug() << "HttpGet::setFile" << outputFile
->fileName();
107 void HttpGet::abort()
115 bool HttpGet::getFile(const QUrl
&url
)
117 if (!url
.isValid()) {
118 qDebug() << "Error: Invalid URL" << endl
;
122 if (url
.scheme() != "http") {
123 qDebug() << "Error: URL must start with 'http:'" << endl
;
127 if (url
.path().isEmpty()) {
128 qDebug() << "Error: URL has no path" << endl
;
131 // if no output file was set write to buffer
132 if(!outputToBuffer
) {
133 if (!outputFile
->open(QIODevice::ReadWrite
)) {
134 qDebug() << "Error: Cannot open " << qPrintable(outputFile
->fileName())
135 << " for writing: " << qPrintable(outputFile
->errorString())
140 // put hash generation here so it can get reused later
141 QString hash
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Md5
).toHex();
142 cachefile
= m_cachedir
.absolutePath() + "/rbutil-cache/" + hash
;
144 // check if the file is present in cache
145 qDebug() << "[HTTP] cache ENABLED for" << url
.toEncoded();
146 if(QFileInfo(cachefile
).isReadable() && QFileInfo(cachefile
).size() > 0) {
147 qDebug() << "[HTTP] cached file found!" << cachefile
;
150 if(!outputToBuffer
) {
151 qDebug() << outputFile
->fileName();
152 c
.open(QIODevice::ReadOnly
);
153 outputFile
->open(QIODevice::ReadWrite
);
154 outputFile
->write(c
.readAll());
159 c
.open(QIODevice::ReadOnly
);
160 dataBuffer
= c
.readAll();
163 response
= 200; // fake "200 OK" HTTP response
165 httpDone(false); // we're done now. This will emit the correct signal too.
168 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile
;
172 qDebug() << "[HTTP] cache DISABLED";
174 http
.setHost(url
.host(), url
.port(80));
175 // construct query (if any)
176 QList
<QPair
<QString
, QString
> > qitems
= url
.queryItems();
179 for(int i
= 0; i
< qitems
.size(); i
++)
180 query
+= qitems
.at(i
).first
+ "=" + qitems
.at(i
).second
+ "&";
185 qDebug() << "[HTTP] downloading to buffer:" << url
.toString();
186 getRequest
= http
.get(url
.path() + query
);
189 qDebug() << "[HTTP] downloading to file:" << url
.toString() << qPrintable(outputFile
->fileName());
190 getRequest
= http
.get(url
.path() + query
, outputFile
);
192 qDebug() << "[HTTP] request scheduled: GET" << getRequest
;
198 void HttpGet::httpDone(bool error
)
201 qDebug() << "[HTTP] Error: " << qPrintable(http
.errorString()) << httpResponse();
206 if(m_usecache
&& !cached
) {
207 qDebug() << "[HTTP] creating cache file" << cachefile
;
209 c
.open(QIODevice::ReadWrite
);
210 if(!outputToBuffer
) {
211 outputFile
->open(QIODevice::ReadOnly
| QIODevice::Truncate
);
212 c
.write(outputFile
->readAll());
224 void HttpGet::httpFinished(int id
, bool error
)
226 qDebug() << "HttpGet::httpFinished(int, bool) =" << id
<< error
;
227 if(id
== getRequest
) dataBuffer
= http
.readAll();
228 qDebug() << "pending:" << http
.hasPendingRequests();
229 //if(!http.hasPendingRequests()) httpDone(error);
230 emit
requestFinished(id
, error
);
234 void HttpGet::httpStarted(int id
)
236 qDebug() << "HttpGet::httpStarted(int) =" << id
;
240 QString
HttpGet::errorString()
242 return http
.errorString();
246 void HttpGet::httpResponseHeader(const QHttpResponseHeader
&resp
)
248 // if there is a network error abort all scheduled requests for
250 response
= resp
.statusCode();
251 if(response
!= 200) {
252 qDebug() << "http response error:" << response
<< resp
.reasonPhrase();
255 // 301 -- moved permanently
258 // 307 -- moved temporarily
259 // in all cases, header: location has the correct address so we can follow.
260 if(response
== 301 || response
== 302 || response
== 303 || response
== 307) {
261 // start new request with new url
262 qDebug() << "http response" << response
<< "- following";
263 getFile(resp
.value("location") + query
);
268 int HttpGet::httpResponse()
274 void HttpGet::httpState(int state
)
276 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
277 "Reading", "Connected", "Closing"};
279 qDebug() << "HttpGet::httpState() = " << s
[state
];
280 else qDebug() << "HttpGet::httpState() = " << state
;