| 1 |
//C- -*- C++ -*- |
| 2 |
//C- ------------------------------------------------------------------- |
| 3 |
//C- DjView4 |
| 4 |
//C- Copyright (c) 2006- Leon Bottou |
| 5 |
//C- |
| 6 |
//C- This software is subject to, and may be distributed under, the |
| 7 |
//C- GNU General Public License, either version 2 of the license, |
| 8 |
//C- or (at your option) any later version. The license should have |
| 9 |
//C- accompanied the software or you may obtain a copy of the license |
| 10 |
//C- from the Free Software Foundation at http://www.fsf.org . |
| 11 |
//C- |
| 12 |
//C- This program is distributed in the hope that it will be useful, |
| 13 |
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
//C- GNU General Public License for more details. |
| 16 |
//C- ------------------------------------------------------------------ |
| 17 |
|
| 18 |
#if AUTOCONF |
| 19 |
# include "config.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include "qdjvunet.h" |
| 23 |
#include "qdjviewprefs.h" |
| 24 |
#include <libdjvu/ddjvuapi.h> |
| 25 |
|
| 26 |
#include <QCoreApplication> |
| 27 |
#include <QDebug> |
| 28 |
#include <QList> |
| 29 |
#include <QMap> |
| 30 |
#include <QSet> |
| 31 |
#include <QPointer> |
| 32 |
|
| 33 |
#if QT_VERSION >= 0x40400 |
| 34 |
|
| 35 |
#include <QAuthenticator> |
| 36 |
#include <QNetworkAccessManager> |
| 37 |
#include <QNetworkProxy> |
| 38 |
#include <QNetworkReply> |
| 39 |
#include <QNetworkRequest> |
| 40 |
#include <QSslError> |
| 41 |
#include <QSslSocket> |
| 42 |
|
| 43 |
class QDjVuNetDocument::Private : public QObject |
| 44 |
{ |
| 45 |
Q_OBJECT |
| 46 |
|
| 47 |
public: |
| 48 |
Private(QDjVuNetDocument *q); |
| 49 |
~Private(); |
| 50 |
|
| 51 |
QDjVuNetDocument *q; |
| 52 |
QMap<QNetworkReply*, int> reqid; |
| 53 |
QMap<QNetworkReply*, bool> reqok; |
| 54 |
QDjVuContext *ctx; |
| 55 |
QUrl url; |
| 56 |
bool cache; |
| 57 |
|
| 58 |
public slots: |
| 59 |
void readyRead(); |
| 60 |
void finished(); |
| 61 |
void error(QNetworkReply::NetworkError code); |
| 62 |
void sslErrors(const QList<QSslError>&); |
| 63 |
void authenticationRequired (QNetworkReply *reply, QAuthenticator *auth); |
| 64 |
void proxyAuthenticationRequired (const QNetworkProxy &proxy, QAuthenticator *auth); |
| 65 |
|
| 66 |
private: |
| 67 |
bool doHeaders(QNetworkReply *reply, int streamid); |
| 68 |
bool doAuth(QString why, QAuthenticator *auth); |
| 69 |
}; |
| 70 |
|
| 71 |
QDjVuNetDocument::Private::Private(QDjVuNetDocument *q) |
| 72 |
: QObject(q), |
| 73 |
q(q) |
| 74 |
{ |
| 75 |
QNetworkAccessManager *mgr = manager(); |
| 76 |
connect(mgr, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), |
| 77 |
this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)) ); |
| 78 |
connect(mgr, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,QAuthenticator*)), |
| 79 |
this, SLOT(proxyAuthenticationRequired(const QNetworkProxy&,QAuthenticator*)) ); |
| 80 |
} |
| 81 |
|
| 82 |
QDjVuNetDocument::Private::~Private() |
| 83 |
{ |
| 84 |
while (! reqid.isEmpty()) |
| 85 |
{ |
| 86 |
QMap<QNetworkReply*,int>::iterator it = reqid.begin(); |
| 87 |
QNetworkReply *reply = it.key(); |
| 88 |
int streamid = it.value(); |
| 89 |
if (streamid >= 0) |
| 90 |
ddjvu_stream_close(*q, streamid, true); |
| 91 |
reply->abort(); |
| 92 |
reply->deleteLater(); |
| 93 |
reqid.remove(reply); |
| 94 |
} |
| 95 |
reqid.clear(); |
| 96 |
reqok.clear(); |
| 97 |
} |
| 98 |
|
| 99 |
bool |
| 100 |
QDjVuNetDocument::Private::doHeaders(QNetworkReply *reply, int streamid) |
| 101 |
{ |
| 102 |
if (streamid >= 0 && !reqok.value(reply, false)) |
| 103 |
{ |
| 104 |
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
| 105 |
QUrl location = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); |
| 106 |
QByteArray type = reply->header(QNetworkRequest::ContentTypeHeader).toByteArray(); |
| 107 |
// check redirection |
| 108 |
if (location.isValid()) |
| 109 |
{ |
| 110 |
reqid[reply] = -1; |
| 111 |
QUrl nurl = reply->url().resolved(location); |
| 112 |
if (streamid > 0 || status == 307) { |
| 113 |
q->newstream(streamid, QString(), nurl); |
| 114 |
} else { |
| 115 |
// Permanent redirect on main stream changes the base url. |
| 116 |
ddjvu_document_set_user_data(*q, 0); |
| 117 |
ddjvu_stream_close(*q, streamid, true); |
| 118 |
q->setUrl(ctx, nurl, cache); |
| 119 |
} |
| 120 |
return false; |
| 121 |
} |
| 122 |
// check status code |
| 123 |
if (status != 200 && status != 203 && status != 0) |
| 124 |
{ |
| 125 |
reqid[reply] = -1; |
| 126 |
ddjvu_stream_close(*q, streamid, false); |
| 127 |
QString msg = tr("Received http status %1 while retrieving %2.", |
| 128 |
"%1 is an http status code") |
| 129 |
.arg(status) |
| 130 |
.arg(reply->url().toString()); |
| 131 |
emit q->error(msg, __FILE__, __LINE__); |
| 132 |
return false; |
| 133 |
} |
| 134 |
// broadcast content type |
| 135 |
bool okay = !type.startsWith("text/"); |
| 136 |
if (! type.isEmpty()) |
| 137 |
emit q->gotContentType(type, okay); |
| 138 |
if (! okay) |
| 139 |
{ |
| 140 |
reqid[reply] = -1; |
| 141 |
ddjvu_stream_close(*q, streamid, false); |
| 142 |
QString msg = tr("Received <%1> data while retrieving %2.", |
| 143 |
"%1 is a mime type") |
| 144 |
.arg(QString::fromLatin1(type)) |
| 145 |
.arg(reply->url().toString()); |
| 146 |
emit q->error(msg, __FILE__, __LINE__); |
| 147 |
return false; |
| 148 |
} |
| 149 |
reqok[reply] = true; |
| 150 |
return true; |
| 151 |
} |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
QDjVuNetDocument::Private::readyRead() |
| 157 |
{ |
| 158 |
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); |
| 159 |
if (reply) |
| 160 |
{ |
| 161 |
int streamid = reqid.value(reply, -1); |
| 162 |
bool okay = reqok.value(reply, false); |
| 163 |
if (streamid >= 0 && !okay) |
| 164 |
okay = doHeaders(reply, streamid); |
| 165 |
if (streamid >= 0 && okay) |
| 166 |
{ |
| 167 |
QByteArray b = reply->readAll(); |
| 168 |
if (b.size() > 0) |
| 169 |
ddjvu_stream_write(*q, streamid, b.data(), b.size()); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void |
| 175 |
QDjVuNetDocument::Private::error(QNetworkReply::NetworkError code) |
| 176 |
{ |
| 177 |
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); |
| 178 |
if (reply) |
| 179 |
{ |
| 180 |
int streamid = reqid.value(reply, -1); |
| 181 |
if (streamid >= 0 && code != QNetworkReply::NoError) |
| 182 |
{ |
| 183 |
reqid[reply] = -1; |
| 184 |
ddjvu_stream_close(*q, streamid, false); |
| 185 |
QString msg = tr("%1 while retrieving '%2'.") |
| 186 |
.arg(reply->errorString()) |
| 187 |
.arg(reply->url().toString()); |
| 188 |
emit q->error(msg , __FILE__, __LINE__); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void |
| 194 |
QDjVuNetDocument::Private::finished() |
| 195 |
{ |
| 196 |
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); |
| 197 |
if (reply) |
| 198 |
{ |
| 199 |
int streamid = reqid.value(reply, -1); |
| 200 |
bool okay = reqok.value(reply, false); |
| 201 |
if (streamid >= 0 && !okay) |
| 202 |
okay = doHeaders(reply, streamid); |
| 203 |
if (streamid >= 0 && okay) |
| 204 |
{ |
| 205 |
ddjvu_stream_close(*q, streamid, false); |
| 206 |
reqid[reply] = -1; |
| 207 |
} |
| 208 |
} |
| 209 |
reqid.remove(reply); |
| 210 |
reqok.remove(reply); |
| 211 |
reply->deleteLater(); |
| 212 |
} |
| 213 |
|
| 214 |
void |
| 215 |
QDjVuNetDocument::Private::sslErrors(const QList<QSslError>&) |
| 216 |
{ |
| 217 |
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); |
| 218 |
if (reply) |
| 219 |
{ |
| 220 |
static QSet<QString> sslWhiteList; |
| 221 |
QString host = reply->url().host(); |
| 222 |
bool okay = sslWhiteList.contains(host); |
| 223 |
if (! okay) |
| 224 |
{ |
| 225 |
QString why = tr("Cannot validate the certificate for server %1.").arg(host); |
| 226 |
emit q->sslWhiteList(why, okay); |
| 227 |
if (okay) |
| 228 |
sslWhiteList += host; |
| 229 |
} |
| 230 |
if (okay) |
| 231 |
reply->ignoreSslErrors(); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
bool |
| 236 |
QDjVuNetDocument::Private::doAuth(QString why, QAuthenticator *auth) |
| 237 |
{ |
| 238 |
QString user = auth->user(); |
| 239 |
QString pass = QString(); |
| 240 |
q->emit authRequired(why, user, pass); |
| 241 |
if (pass.isNull()) |
| 242 |
return false; |
| 243 |
auth->setUser(user); |
| 244 |
auth->setPassword(pass); |
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
void |
| 249 |
QDjVuNetDocument::Private::authenticationRequired(QNetworkReply *reply, QAuthenticator *auth) |
| 250 |
{ |
| 251 |
QString host = reply->url().host(); |
| 252 |
QString why = tr("Authentication required for %1 (%2).").arg(auth->realm()).arg(host); |
| 253 |
if (! doAuth(why, auth)) |
| 254 |
reply->abort(); |
| 255 |
} |
| 256 |
|
| 257 |
void |
| 258 |
QDjVuNetDocument::Private::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth) |
| 259 |
{ |
| 260 |
QString why = tr("Authentication required for proxy %1.").arg(proxy.hostName()); |
| 261 |
doAuth(why, auth); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
/*! \class QDjVuNetDocument |
| 266 |
\brief Represents DjVu documents accessible via the network This class is |
| 267 |
derived from \a QDjVuDocument and reimplements method \a newstream to handle |
| 268 |
documents available throught network requests. */ |
| 269 |
|
| 270 |
|
| 271 |
QDjVuNetDocument::~QDjVuNetDocument() |
| 272 |
{ |
| 273 |
delete p; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
/*! Construct a \a QDjVuNetDocument object. |
| 278 |
See \a QDjVuDocument::QDjVuDocument for the other two arguments. */ |
| 279 |
|
| 280 |
QDjVuNetDocument::QDjVuNetDocument(bool autoDelete, QObject *parent) |
| 281 |
: QDjVuDocument(autoDelete, parent), |
| 282 |
p(new QDjVuNetDocument::Private(this)) |
| 283 |
{ |
| 284 |
} |
| 285 |
|
| 286 |
/*! \overload */ |
| 287 |
|
| 288 |
QDjVuNetDocument::QDjVuNetDocument(QObject *parent) |
| 289 |
: QDjVuDocument(parent), |
| 290 |
p(new QDjVuNetDocument::Private(this)) |
| 291 |
{ |
| 292 |
} |
| 293 |
|
| 294 |
/*! Returns the \a QNetworkAccessManager used to reach the network. */ |
| 295 |
|
| 296 |
QNetworkAccessManager* |
| 297 |
QDjVuNetDocument::manager() |
| 298 |
{ |
| 299 |
static QPointer<QNetworkAccessManager> mgr; |
| 300 |
QObject *app = QCoreApplication::instance(); |
| 301 |
if (! mgr) |
| 302 |
mgr = new QNetworkAccessManager(app); |
| 303 |
return mgr; |
| 304 |
} |
| 305 |
|
| 306 |
/*! Sets the application proxy using the host, port, user, and password |
| 307 |
specified by url \a proxyUrl. The proxy type depends on the url scheme. |
| 308 |
Recognized schemes are \a "http", \a "ftp", and \a "socks5". */ |
| 309 |
|
| 310 |
void |
| 311 |
QDjVuNetDocument::setProxy(QUrl proxyUrl) |
| 312 |
{ |
| 313 |
QNetworkProxy proxy; |
| 314 |
QString scheme = proxyUrl.scheme(); |
| 315 |
if (scheme == "http") |
| 316 |
proxy.setType(QNetworkProxy::HttpCachingProxy); |
| 317 |
if (scheme == "ftp") |
| 318 |
proxy.setType(QNetworkProxy::FtpCachingProxy); |
| 319 |
else if (scheme == "socks5") |
| 320 |
proxy.setType(QNetworkProxy::Socks5Proxy); |
| 321 |
proxy.setHostName(proxyUrl.host()); |
| 322 |
proxy.setPort(proxyUrl.port()); |
| 323 |
proxy.setUser(proxyUrl.userName()); |
| 324 |
proxy.setPassword(proxyUrl.password()); |
| 325 |
QNetworkProxy::setApplicationProxy(proxy); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
/*! Associates the \a QDjVuDocument object with |
| 330 |
with the \a QDjVuContext object \ctx in order |
| 331 |
to decode the DjVu data located at URL \a url. */ |
| 332 |
|
| 333 |
bool |
| 334 |
QDjVuNetDocument::setUrl(QDjVuContext *ctx, QUrl url, bool cache) |
| 335 |
{ |
| 336 |
if (url.isValid()) |
| 337 |
{ |
| 338 |
if (url.scheme() == "file" && url.host().isEmpty()) |
| 339 |
QDjVuDocument::setFileName(ctx, url.toLocalFile(), cache); |
| 340 |
else |
| 341 |
QDjVuDocument::setUrl(ctx, url, cache); |
| 342 |
} |
| 343 |
if (isValid()) |
| 344 |
{ |
| 345 |
p->url = url; |
| 346 |
p->ctx = ctx; |
| 347 |
p->cache = cache; |
| 348 |
return true; |
| 349 |
} |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
/* Perform a request. */ |
| 355 |
|
| 356 |
void |
| 357 |
QDjVuNetDocument::newstream(int streamid, QString, QUrl url) |
| 358 |
{ |
| 359 |
QString message = tr("Requesting '%1'").arg(url.toString()); |
| 360 |
QNetworkRequest request(url); |
| 361 |
QString agent = "Djview/" + QDjViewPrefs::versionString(); |
| 362 |
request.setRawHeader("User-Agent", agent.toLatin1()); |
| 363 |
QNetworkReply *reply = manager()->get(request); |
| 364 |
connect(reply, SIGNAL(readyRead()), |
| 365 |
p, SLOT(readyRead()) ); |
| 366 |
connect(reply, SIGNAL(finished()), |
| 367 |
p, SLOT(finished()) ); |
| 368 |
connect(reply, SIGNAL(sslErrors(const QList<QSslError>&)), |
| 369 |
p, SLOT(sslErrors(const QList<QSslError>&)) ); |
| 370 |
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), |
| 371 |
p, SLOT(error(QNetworkReply::NetworkError)) ); |
| 372 |
p->reqid[reply] = streamid; |
| 373 |
p->reqok[reply] = false; |
| 374 |
emit info(message); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
/*! \fn |
| 379 |
void QDjVuNetDocument::authRequired(QString why, QString &user, QString &pass) |
| 380 |
This signal is emitted when a username and password is required. |
| 381 |
String \a why contains a suitable description of the purpose of the username and password. |
| 382 |
Simply set \a user and \a pass and the credentials will be remembered. |
| 383 |
*/ |
| 384 |
|
| 385 |
|
| 386 |
/*! \fn QDjVuNetDocument::sslWhiteList(QString host, bool &okay) |
| 387 |
This signal is emitted when there are recoverable errors on a ssl connection |
| 388 |
such as an inability to validate the certificate. String \a why contains |
| 389 |
a description of the problem. Setting \a okay to true allows |
| 390 |
the connection to proceed for the current session. */ |
| 391 |
|
| 392 |
|
| 393 |
// ---------------------------------------- |
| 394 |
// MOC |
| 395 |
|
| 396 |
#include "qdjvunet.moc" |
| 397 |
|
| 398 |
|
| 399 |
#else // QT_VERSION < 0x40400 |
| 400 |
|
| 401 |
// ---------------------------------------- |
| 402 |
// STUBS QT < 4.4.0 |
| 403 |
|
| 404 |
|
| 405 |
class QDjVuNetDocument::Private : public QObject |
| 406 |
{ |
| 407 |
Q_OBJECT |
| 408 |
public: |
| 409 |
Private() : QObject() { } |
| 410 |
}; |
| 411 |
|
| 412 |
QDjVuNetDocument::~QDjVuNetDocument() |
| 413 |
{ } |
| 414 |
|
| 415 |
QDjVuNetDocument::QDjVuNetDocument(bool autoDelete, QObject *parent) |
| 416 |
: QDjVuDocument(autoDelete, parent) |
| 417 |
{ } |
| 418 |
|
| 419 |
QDjVuNetDocument::QDjVuNetDocument(QObject *parent) |
| 420 |
: QDjVuDocument(parent) |
| 421 |
{ } |
| 422 |
|
| 423 |
bool |
| 424 |
QDjVuNetDocument::setUrl(QDjVuContext *ctx, QUrl url, bool cache) |
| 425 |
{ |
| 426 |
if (url.isValid() && url.scheme() == "file") |
| 427 |
if (url.host().isEmpty() || url.host() == "localhost") |
| 428 |
return setFileName(ctx, url.toLocalFile(), cache); |
| 429 |
return false; |
| 430 |
} |
| 431 |
|
| 432 |
QNetworkAccessManager* |
| 433 |
QDjVuNetDocument::manager() |
| 434 |
{ return 0; } |
| 435 |
|
| 436 |
void |
| 437 |
QDjVuNetDocument::setProxy(QUrl) |
| 438 |
{ } |
| 439 |
|
| 440 |
void |
| 441 |
QDjVuNetDocument::newstream(int, QString, QUrl) |
| 442 |
{ } |
| 443 |
|
| 444 |
#endif |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
/* ------------------------------------------------------------- |
| 450 |
Local Variables: |
| 451 |
c++-font-lock-extra-types: ( "\\sw+_t" "Q[A-Z]\\sw*[a-z]\\sw*" ) |
| 452 |
End: |
| 453 |
------------------------------------------------------------- */ |