summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/httpget.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/httpget.cpp')
-rw-r--r--rbutil/rbutilqt/httpget.cpp81
1 files changed, 76 insertions, 5 deletions
diff --git a/rbutil/rbutilqt/httpget.cpp b/rbutil/rbutilqt/httpget.cpp
index 60c8a58a6d..7680cb2d53 100644
--- a/rbutil/rbutilqt/httpget.cpp
+++ b/rbutil/rbutilqt/httpget.cpp
@@ -27,8 +27,10 @@
27HttpGet::HttpGet(QObject *parent) 27HttpGet::HttpGet(QObject *parent)
28 : QObject(parent) 28 : QObject(parent)
29{ 29{
30 m_usecache = false;
30 qDebug() << "--> HttpGet::HttpGet()"; 31 qDebug() << "--> HttpGet::HttpGet()";
31 outputToBuffer = true; 32 outputToBuffer = true;
33 cached = false;
32 getRequest = -1; 34 getRequest = -1;
33 connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool))); 35 connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
34 connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int))); 36 connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
@@ -42,6 +44,27 @@ HttpGet::HttpGet(QObject *parent)
42} 44}
43 45
44 46
47void HttpGet::setCache(QDir d)
48{
49 m_cachedir = d;
50 bool result = true;
51
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");
56 else result = false;
57 qDebug() << "HttpGet::setCache(QDir)" << result;
58 m_usecache = !result;
59}
60
61
62void HttpGet::setCache(bool c)
63{
64 m_usecache = c;
65}
66
67
45QByteArray HttpGet::readAll() 68QByteArray HttpGet::readAll()
46{ 69{
47 return dataBuffer; 70 return dataBuffer;
@@ -108,6 +131,40 @@ bool HttpGet::getFile(const QUrl &url)
108 return false; 131 return false;
109 } 132 }
110 } 133 }
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;
137 if(m_usecache) {
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;
142 getRequest = -1;
143 QFile c(cachefile);
144 if(!outputToBuffer) {
145 qDebug() << outputFile->fileName();
146 c.open(QIODevice::ReadOnly);
147 outputFile->open(QIODevice::ReadWrite);
148 outputFile->write(c.readAll());
149 outputFile->close();
150 c.close();
151 }
152 else {
153 c.open(QIODevice::ReadOnly);
154 dataBuffer = c.readAll();
155 c.close();
156 }
157 response = 200; // fake "200 OK" HTTP response
158 cached = true;
159 httpDone(false); // we're done now. This will emit the correct signal too.
160 return true;
161 }
162 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile;
163
164 }
165 else {
166 qDebug() << "[HTTP] cache DISABLED";
167 }
111 http.setHost(url.host(), url.port(80)); 168 http.setHost(url.host(), url.port(80));
112 // construct query (if any) 169 // construct query (if any)
113 QList<QPair<QString, QString> > qitems = url.queryItems(); 170 QList<QPair<QString, QString> > qitems = url.queryItems();
@@ -119,14 +176,14 @@ bool HttpGet::getFile(const QUrl &url)
119 } 176 }
120 177
121 if(outputToBuffer) { 178 if(outputToBuffer) {
122 qDebug() << "downloading to buffer:" << url.toString(); 179 qDebug() << "[HTTP] downloading to buffer:" << url.toString();
123 getRequest = http.get(url.path() + query); 180 getRequest = http.get(url.path() + query);
124 } 181 }
125 else { 182 else {
126 qDebug() << "downloading to file:" << url.toString() << qPrintable(outputFile->fileName()); 183 qDebug() << "[HTTP] downloading to file:" << url.toString() << qPrintable(outputFile->fileName());
127 getRequest = http.get(url.path() + query, outputFile); 184 getRequest = http.get(url.path() + query, outputFile);
128 } 185 }
129 qDebug() << "request scheduled: GET" << getRequest; 186 qDebug() << "[HTTP] request scheduled: GET" << getRequest;
130 187
131 return true; 188 return true;
132} 189}
@@ -135,11 +192,25 @@ bool HttpGet::getFile(const QUrl &url)
135void HttpGet::httpDone(bool error) 192void HttpGet::httpDone(bool error)
136{ 193{
137 if (error) { 194 if (error) {
138 qDebug() << "Error: " << qPrintable(http.errorString()) << httpResponse(); 195 qDebug() << "[HTTP] Error: " << qPrintable(http.errorString()) << httpResponse();
139 } 196 }
140 if(!outputToBuffer) 197 if(!outputToBuffer)
141 outputFile->close(); 198 outputFile->close();
142 199
200 if(m_usecache && !cached) {
201 qDebug() << "[HTTP] creating cache file" << cachefile;
202 QFile c(cachefile);
203 c.open(QIODevice::ReadWrite);
204 if(!outputToBuffer) {
205 outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate);
206 c.write(outputFile->readAll());
207 outputFile->close();
208 }
209 else
210 c.write(dataBuffer);
211
212 c.close();
213 }
143 emit done(error); 214 emit done(error);
144} 215}
145 216