simplify setting of proxy value.
[Rockbox.git] / rbutil / rbutilqt / httpget.cpp
blob60c8a58a6d36d28f4f1dc4bab71a3a7a5fcb7b9a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Riebeling
10 * $Id$
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 ****************************************************************************/
20 #include <QtCore>
21 #include <QtNetwork>
22 #include <QtDebug>
24 #include "httpget.h"
27 HttpGet::HttpGet(QObject *parent)
28 : QObject(parent)
30 qDebug() << "--> HttpGet::HttpGet()";
31 outputToBuffer = true;
32 getRequest = -1;
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()
47 return dataBuffer;
51 QHttp::Error HttpGet::error()
53 return http.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)
72 outputFile = file;
73 outputToBuffer = false;
74 qDebug() << "HttpGet::setFile" << outputFile->fileName();
78 void HttpGet::abort()
80 http.abort();
81 if(!outputToBuffer)
82 outputFile->close();
86 bool HttpGet::getFile(const QUrl &url)
88 if (!url.isValid()) {
89 qDebug() << "Error: Invalid URL" << endl;
90 return false;
93 if (url.scheme() != "http") {
94 qDebug() << "Error: URL must start with 'http:'" << endl;
95 return false;
98 if (url.path().isEmpty()) {
99 qDebug() << "Error: URL has no path" << endl;
100 return false;
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())
107 << endl;
108 return false;
111 http.setHost(url.host(), url.port(80));
112 // construct query (if any)
113 QList<QPair<QString, QString> > qitems = url.queryItems();
114 if(url.hasQuery()) {
115 query = "?";
116 for(int i = 0; i < qitems.size(); i++)
117 query += qitems.at(i).first + "=" + qitems.at(i).second + "&";
118 qDebug() << query;
121 if(outputToBuffer) {
122 qDebug() << "downloading to buffer:" << url.toString();
123 getRequest = http.get(url.path() + query);
125 else {
126 qDebug() << "downloading to file:" << url.toString() << qPrintable(outputFile->fileName());
127 getRequest = http.get(url.path() + query, outputFile);
129 qDebug() << "request scheduled: GET" << getRequest;
131 return true;
135 void HttpGet::httpDone(bool error)
137 if (error) {
138 qDebug() << "Error: " << qPrintable(http.errorString()) << httpResponse();
140 if(!outputToBuffer)
141 outputFile->close();
143 emit done(error);
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
172 // this download
173 response = resp.statusCode();
174 if(response != 200) {
175 qDebug() << "http response error:" << response << resp.reasonPhrase();
176 http.abort();
178 // 301 -- moved permanently
179 // 302 -- found
180 // 303 -- see other
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()
193 return response;
197 void HttpGet::httpState(int state)
199 QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
200 "Reading", "Connected", "Closing"};
201 if(state <= 6)
202 qDebug() << "HttpGet::httpState() = " << s[state];
203 else qDebug() << "HttpGet::httpState() = " << state;