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())
54 if(!QFileInfo(p
).isDir())
55 result
= m_cachedir
.mkdir("rbutil-cache");
57 qDebug() << "HttpGet::setCache(QDir)" << result
;
62 void HttpGet::setCache(bool c
)
68 QByteArray
HttpGet::readAll()
74 QHttp::Error
HttpGet::error()
80 void HttpGet::httpProgress(int read
, int total
)
82 emit
dataReadProgress(read
, total
);
86 void HttpGet::setProxy(const QUrl
&proxy
)
88 qDebug() << "HttpGet::setProxy" << proxy
.toString();
89 http
.setProxy(proxy
.host(), proxy
.port(), proxy
.userName(), proxy
.password());
93 void HttpGet::setFile(QFile
*file
)
96 outputToBuffer
= false;
97 qDebug() << "HttpGet::setFile" << outputFile
->fileName();
101 void HttpGet::abort()
109 bool HttpGet::getFile(const QUrl
&url
)
111 if (!url
.isValid()) {
112 qDebug() << "Error: Invalid URL" << endl
;
116 if (url
.scheme() != "http") {
117 qDebug() << "Error: URL must start with 'http:'" << endl
;
121 if (url
.path().isEmpty()) {
122 qDebug() << "Error: URL has no path" << endl
;
125 // if no output file was set write to buffer
126 if(!outputToBuffer
) {
127 if (!outputFile
->open(QIODevice::ReadWrite
)) {
128 qDebug() << "Error: Cannot open " << qPrintable(outputFile
->fileName())
129 << " for writing: " << qPrintable(outputFile
->errorString())
134 // put hash generation here so it can get reused later
135 QString hash
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Md5
).toHex();
136 cachefile
= m_cachedir
.absolutePath() + "/rbutil-cache/" + hash
;
138 // check if the file is present in cache
139 qDebug() << "[HTTP] cache ENABLED for" << url
.toEncoded();
140 if(QFileInfo(cachefile
).isReadable() && QFileInfo(cachefile
).size() > 0) {
141 qDebug() << "[HTTP] cached file found!" << cachefile
;
144 if(!outputToBuffer
) {
145 qDebug() << outputFile
->fileName();
146 c
.open(QIODevice::ReadOnly
);
147 outputFile
->open(QIODevice::ReadWrite
);
148 outputFile
->write(c
.readAll());
153 c
.open(QIODevice::ReadOnly
);
154 dataBuffer
= c
.readAll();
157 response
= 200; // fake "200 OK" HTTP response
159 httpDone(false); // we're done now. This will emit the correct signal too.
162 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile
;
166 qDebug() << "[HTTP] cache DISABLED";
168 http
.setHost(url
.host(), url
.port(80));
169 // construct query (if any)
170 QList
<QPair
<QString
, QString
> > qitems
= url
.queryItems();
173 for(int i
= 0; i
< qitems
.size(); i
++)
174 query
+= qitems
.at(i
).first
+ "=" + qitems
.at(i
).second
+ "&";
179 qDebug() << "[HTTP] downloading to buffer:" << url
.toString();
180 getRequest
= http
.get(url
.path() + query
);
183 qDebug() << "[HTTP] downloading to file:" << url
.toString() << qPrintable(outputFile
->fileName());
184 getRequest
= http
.get(url
.path() + query
, outputFile
);
186 qDebug() << "[HTTP] request scheduled: GET" << getRequest
;
192 void HttpGet::httpDone(bool error
)
195 qDebug() << "[HTTP] Error: " << qPrintable(http
.errorString()) << httpResponse();
200 if(m_usecache
&& !cached
) {
201 qDebug() << "[HTTP] creating cache file" << cachefile
;
203 c
.open(QIODevice::ReadWrite
);
204 if(!outputToBuffer
) {
205 outputFile
->open(QIODevice::ReadOnly
| QIODevice::Truncate
);
206 c
.write(outputFile
->readAll());
218 void HttpGet::httpFinished(int id
, bool error
)
220 qDebug() << "HttpGet::httpFinished(int, bool) =" << id
<< error
;
221 if(id
== getRequest
) dataBuffer
= http
.readAll();
222 qDebug() << "pending:" << http
.hasPendingRequests();
223 //if(!http.hasPendingRequests()) httpDone(error);
224 emit
requestFinished(id
, error
);
228 void HttpGet::httpStarted(int id
)
230 qDebug() << "HttpGet::httpStarted(int) =" << id
;
234 QString
HttpGet::errorString()
236 return http
.errorString();
240 void HttpGet::httpResponseHeader(const QHttpResponseHeader
&resp
)
242 // if there is a network error abort all scheduled requests for
244 response
= resp
.statusCode();
245 if(response
!= 200) {
246 qDebug() << "http response error:" << response
<< resp
.reasonPhrase();
249 // 301 -- moved permanently
252 // 307 -- moved temporarily
253 // in all cases, header: location has the correct address so we can follow.
254 if(response
== 301 || response
== 302 || response
== 303 || response
== 307) {
255 // start new request with new url
256 qDebug() << "http response" << response
<< "- following";
257 getFile(resp
.value("location") + query
);
262 int HttpGet::httpResponse()
268 void HttpGet::httpState(int state
)
270 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
271 "Reading", "Connected", "Closing"};
273 qDebug() << "HttpGet::httpState() = " << s
[state
];
274 else qDebug() << "HttpGet::httpState() = " << state
;