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
)
30 qDebug() << "--> HttpGet::HttpGet()";
31 outputToBuffer
= true;
33 connect(&http
, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
34 connect(&http
, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
35 connect(&http
, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
36 connect(&http
, SIGNAL(responseHeaderReceived(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
37 connect(&http
, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
38 connect(&http
, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
40 connect(&http
, SIGNAL(readyRead(const QHttpResponseHeader
&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader
&)));
45 QByteArray
HttpGet::readAll()
51 QHttp::Error
HttpGet::error()
57 void HttpGet::httpProgress(int read
, int total
)
59 emit
dataReadProgress(read
, total
);
63 void HttpGet::setProxy(const QUrl
&proxy
)
65 qDebug() << "HttpGet::setProxy" << proxy
.toString();
66 http
.setProxy(proxy
.host(), proxy
.port(), proxy
.userName(), proxy
.password());
70 void HttpGet::setFile(QFile
*file
)
73 outputToBuffer
= false;
74 qDebug() << "HttpGet::setFile" << outputFile
->fileName();
86 bool HttpGet::getFile(const QUrl
&url
)
89 qDebug() << "Error: Invalid URL" << endl
;
93 if (url
.scheme() != "http") {
94 qDebug() << "Error: URL must start with 'http:'" << endl
;
98 if (url
.path().isEmpty()) {
99 qDebug() << "Error: URL has no path" << endl
;
102 // if no output file was set write to buffer
103 if(!outputToBuffer
) {
104 if (!outputFile
->open(QIODevice::ReadWrite
)) {
105 qDebug() << "Error: Cannot open " << qPrintable(outputFile
->fileName())
106 << " for writing: " << qPrintable(outputFile
->errorString())
111 http
.setHost(url
.host(), url
.port(80));
112 // construct query (if any)
113 QList
<QPair
<QString
, QString
> > qitems
= url
.queryItems();
116 for(int i
= 0; i
< qitems
.size(); i
++)
117 query
+= qitems
.at(i
).first
+ "=" + qitems
.at(i
).second
+ "&";
122 qDebug() << "downloading to buffer:" << url
.toString();
123 getRequest
= http
.get(url
.path() + query
);
126 qDebug() << "downloading to file:" << url
.toString() << qPrintable(outputFile
->fileName());
127 getRequest
= http
.get(url
.path() + query
, outputFile
);
129 qDebug() << "request scheduled: GET" << getRequest
;
135 void HttpGet::httpDone(bool error
)
138 qDebug() << "Error: " << qPrintable(http
.errorString()) << httpResponse();
147 void HttpGet::httpFinished(int id
, bool error
)
149 qDebug() << "HttpGet::httpFinished(int, bool) =" << id
<< error
;
150 if(id
== getRequest
) dataBuffer
= http
.readAll();
151 qDebug() << "pending:" << http
.hasPendingRequests();
152 //if(!http.hasPendingRequests()) httpDone(error);
153 emit
requestFinished(id
, error
);
157 void HttpGet::httpStarted(int id
)
159 qDebug() << "HttpGet::httpStarted(int) =" << id
;
163 QString
HttpGet::errorString()
165 return http
.errorString();
169 void HttpGet::httpResponseHeader(const QHttpResponseHeader
&resp
)
171 // if there is a network error abort all scheduled requests for
173 response
= resp
.statusCode();
174 if(response
!= 200) {
175 qDebug() << "http response error:" << response
<< resp
.reasonPhrase();
178 // 301 -- moved permanently
181 // 307 -- moved temporarily
182 // in all cases, header: location has the correct address so we can follow.
183 if(response
== 301 || response
== 302 || response
== 303 || response
== 307) {
184 // start new request with new url
185 qDebug() << "http response" << response
<< "- following";
186 getFile(resp
.value("location") + query
);
191 int HttpGet::httpResponse()
197 void HttpGet::httpState(int state
)
199 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
200 "Reading", "Connected", "Closing"};
202 qDebug() << "HttpGet::httpState() = " << s
[state
];
203 else qDebug() << "HttpGet::httpState() = " << state
;