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 connect(&http
, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
36 connect(&http
, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
37 connect(&http
, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
38 connect(&http
, SIGNAL(responseHeaderReceived(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
39 connect(&http
, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
40 connect(&http
, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
42 connect(&http
, SIGNAL(readyRead(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
47 void HttpGet::setCache(QDir d
)
52 QString p
= m_cachedir
.absolutePath() + "/rbutil-cache";
53 if(QFileInfo(m_cachedir
.absolutePath()).isDir())
55 if(!QFileInfo(p
).isDir())
56 result
= m_cachedir
.mkdir("rbutil-cache");
59 qDebug() << "HttpGet::setCache(QDir)" << result
;
64 void HttpGet::setCache(bool c
)
70 QByteArray
HttpGet::readAll()
76 QHttp::Error
HttpGet::error()
82 void HttpGet::httpProgress(int read
, int total
)
84 emit
dataReadProgress(read
, total
);
88 void HttpGet::setProxy(const QUrl
&proxy
)
90 qDebug() << "HttpGet::setProxy" << proxy
.toString();
91 http
.setProxy(proxy
.host(), proxy
.port(), proxy
.userName(), proxy
.password());
95 void HttpGet::setFile(QFile
*file
)
98 outputToBuffer
= false;
99 qDebug() << "HttpGet::setFile" << outputFile
->fileName();
103 void HttpGet::abort()
111 bool HttpGet::getFile(const QUrl
&url
)
113 if (!url
.isValid()) {
114 qDebug() << "Error: Invalid URL" << endl
;
118 if (url
.scheme() != "http") {
119 qDebug() << "Error: URL must start with 'http:'" << endl
;
123 if (url
.path().isEmpty()) {
124 qDebug() << "Error: URL has no path" << endl
;
127 // if no output file was set write to buffer
128 if(!outputToBuffer
) {
129 if (!outputFile
->open(QIODevice::ReadWrite
)) {
130 qDebug() << "Error: Cannot open " << qPrintable(outputFile
->fileName())
131 << " for writing: " << qPrintable(outputFile
->errorString())
136 // put hash generation here so it can get reused later
137 QString hash
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Md5
).toHex();
138 cachefile
= m_cachedir
.absolutePath() + "/rbutil-cache/" + hash
;
140 // check if the file is present in cache
141 qDebug() << "[HTTP] cache ENABLED for" << url
.toEncoded();
142 if(QFileInfo(cachefile
).isReadable() && QFileInfo(cachefile
).size() > 0) {
143 qDebug() << "[HTTP] cached file found!" << cachefile
;
146 if(!outputToBuffer
) {
147 qDebug() << outputFile
->fileName();
148 c
.open(QIODevice::ReadOnly
);
149 outputFile
->open(QIODevice::ReadWrite
);
150 outputFile
->write(c
.readAll());
155 c
.open(QIODevice::ReadOnly
);
156 dataBuffer
= c
.readAll();
159 response
= 200; // fake "200 OK" HTTP response
161 httpDone(false); // we're done now. This will emit the correct signal too.
164 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile
;
168 qDebug() << "[HTTP] cache DISABLED";
170 http
.setHost(url
.host(), url
.port(80));
171 // construct query (if any)
172 QList
<QPair
<QString
, QString
> > qitems
= url
.queryItems();
175 for(int i
= 0; i
< qitems
.size(); i
++)
176 query
+= qitems
.at(i
).first
+ "=" + qitems
.at(i
).second
+ "&";
181 qDebug() << "[HTTP] downloading to buffer:" << url
.toString();
182 getRequest
= http
.get(url
.path() + query
);
185 qDebug() << "[HTTP] downloading to file:" << url
.toString() << qPrintable(outputFile
->fileName());
186 getRequest
= http
.get(url
.path() + query
, outputFile
);
188 qDebug() << "[HTTP] request scheduled: GET" << getRequest
;
194 void HttpGet::httpDone(bool error
)
197 qDebug() << "[HTTP] Error: " << qPrintable(http
.errorString()) << httpResponse();
202 if(m_usecache
&& !cached
) {
203 qDebug() << "[HTTP] creating cache file" << cachefile
;
205 c
.open(QIODevice::ReadWrite
);
206 if(!outputToBuffer
) {
207 outputFile
->open(QIODevice::ReadOnly
| QIODevice::Truncate
);
208 c
.write(outputFile
->readAll());
220 void HttpGet::httpFinished(int id
, bool error
)
222 qDebug() << "HttpGet::httpFinished(int, bool) =" << id
<< error
;
223 if(id
== getRequest
) dataBuffer
= http
.readAll();
224 qDebug() << "pending:" << http
.hasPendingRequests();
225 //if(!http.hasPendingRequests()) httpDone(error);
226 emit
requestFinished(id
, error
);
230 void HttpGet::httpStarted(int id
)
232 qDebug() << "HttpGet::httpStarted(int) =" << id
;
236 QString
HttpGet::errorString()
238 return http
.errorString();
242 void HttpGet::httpResponseHeader(const QHttpResponseHeader
&resp
)
244 // if there is a network error abort all scheduled requests for
246 response
= resp
.statusCode();
247 if(response
!= 200) {
248 qDebug() << "http response error:" << response
<< resp
.reasonPhrase();
251 // 301 -- moved permanently
254 // 307 -- moved temporarily
255 // in all cases, header: location has the correct address so we can follow.
256 if(response
== 301 || response
== 302 || response
== 303 || response
== 307) {
257 // start new request with new url
258 qDebug() << "http response" << response
<< "- following";
259 getFile(resp
.value("location") + query
);
264 int HttpGet::httpResponse()
270 void HttpGet::httpState(int state
)
272 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
273 "Reading", "Connected", "Closing"};
275 qDebug() << "HttpGet::httpState() = " << s
[state
];
276 else qDebug() << "HttpGet::httpState() = " << state
;