[tor-commits] [vidalia/alpha] Port BridgeDownloader from QHttp to QtNetwork

chiiph at torproject.org chiiph at torproject.org
Tue May 3 14:43:49 UTC 2011


commit cbfe5e908026729818d91d2fbb17a92469574abd
Author: Tomas Touceda <chiiph at gentoo.org>
Date:   Fri Apr 15 12:34:01 2011 -0300

    Port BridgeDownloader from QHttp to QtNetwork
    
    QHttp is deprecated in the latest versions.
    Also, added a check for the certificate sha1 digest.
---
 src/vidalia/config/BridgeDownloader.cpp            |   71 +++++++++-----------
 src/vidalia/config/BridgeDownloader.h              |   17 ++---
 .../config/BridgeDownloaderProgressDialog.cpp      |    2 +-
 .../config/BridgeDownloaderProgressDialog.h        |    2 +-
 src/vidalia/config/NetworkPage.cpp                 |    4 +-
 5 files changed, 44 insertions(+), 52 deletions(-)

diff --git a/src/vidalia/config/BridgeDownloader.cpp b/src/vidalia/config/BridgeDownloader.cpp
index d70034d..15d73cb 100644
--- a/src/vidalia/config/BridgeDownloader.cpp
+++ b/src/vidalia/config/BridgeDownloader.cpp
@@ -16,34 +16,26 @@
 #include "BridgeDownloader.h"
 #include "Vidalia.h"
 
-#include <QSslSocket>
-
 #define BRIDGEDB_HOST  "bridges.torproject.org"
 #define BRIDGEDB_PORT  443
-
+#define BRIDGEDB_SHA1  "a7e70f8a648fe04a9677f13eedf6f91b5f7f2e25"
 
 BridgeDownloader::BridgeDownloader(QObject *parent)
-  : QObject(parent),
-    _requestId(0)
+  : QObject(parent)
 {
-  _https = new QHttp(BRIDGEDB_HOST,  QHttp::ConnectionModeHttps,
-                     BRIDGEDB_PORT, this);
-
-  connect(_https, SIGNAL(stateChanged(int)),
-          this, SLOT(httpsStateChanged(int)));
-  connect(_https, SIGNAL(requestFinished(int, bool)),
-          this, SLOT(httpsRequestFinished(int, bool)));
-  connect(_https, SIGNAL(dataReadProgress(int, int)),
-          this, SIGNAL(downloadProgress(int, int)));
-  connect(_https, SIGNAL(sslErrors(QList<QSslError>)),
-          this, SLOT(sslErrors(QList<QSslError>)));  
+  _https = new QNetworkAccessManager();
+
+  connect(_https, SIGNAL(finished(QNetworkReply *)),
+          this, SLOT(httpsRequestFinished(QNetworkReply *)));
+  connect(_https, SIGNAL(sslErrors(QNetworkReply *, QList<QSslError>)),
+          this, SLOT(sslErrors(QNetworkReply *, QList<QSslError>)));  
 }
 
 void
 BridgeDownloader::setProxy(const QString &host, int port,
                            const QString &username, const QString &password)
 {
-  _https->setProxy(host, port, username, password);
+  _https->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, host, port, username, password));
 }
 
 bool
@@ -82,16 +74,18 @@ BridgeDownloader::startHttpsDownload()
   emit statusChanged(tr("Starting HTTPS bridge request..."));
   emit downloadProgress(0, 0);
 
-  _requestId = _https->get("/?format=plain");
-  vInfo("Sending an HTTPS bridge request to %1:%2 (id %3).").arg(BRIDGEDB_HOST)
-                                                            .arg(BRIDGEDB_PORT)
-                                                            .arg(_requestId);
+  _reply = _https->get(QNetworkRequest(QUrl("https://bridges.torproject.org/?format=plain")));
+  connect(_reply, SIGNAL(downloadProgress(qint64, qint64)),
+          this, SIGNAL(downloadProgress(qint64, qint64)));
+  vInfo("Sending an HTTPS bridge request to %1:%2.").arg(BRIDGEDB_HOST)
+                                                    .arg(BRIDGEDB_PORT);
 }
 
 void
 BridgeDownloader::cancelBridgeRequest()
 {
-  _https->abort();
+  _reply->close();
+  disconnect(_reply, 0, 0, 0);
 }
 
 void
@@ -117,20 +111,16 @@ BridgeDownloader::httpsStateChanged(int state)
 }
 
 void
-BridgeDownloader::httpsRequestFinished(int id, bool error)
+BridgeDownloader::httpsRequestFinished(QNetworkReply *reply)
 {
-  if (id != _requestId)
-    return;
-
-  if (error) {
-    QString errorString = _https->errorString();
-    vWarn("Bridge request failed (id %1): %2").arg(id).arg(errorString);
+  if (reply->error() != QNetworkReply::NoError) {
+    QString errorString = reply->errorString();
+    vWarn("Bridge request failed: %2").arg(errorString);
   
     emit bridgeRequestFailed(errorString);
   } else {
-    QByteArray response = _https->readAll();
-    vInfo("Bridge request complete (id %1): received %2 bytes.").arg(id)
-                                                   .arg(response.size());
+    QByteArray response = reply->readAll();
+    vInfo("Bridge request complete: received %2 bytes.").arg(response.size());
 
     QStringList bridges, lines = QString(response).split("\n");
     foreach (QString line, lines) {
@@ -140,22 +130,27 @@ BridgeDownloader::httpsRequestFinished(int id, bool error)
     }
     emit bridgeRequestFinished(bridges);
   }
-  _https->close();
+  _reply->close();
+  disconnect(_reply,0,0,0);
 }
 
 void
-BridgeDownloader::sslErrors(const QList<QSslError> &sslErrors)
+BridgeDownloader::sslErrors(QNetworkReply *reply, const QList<QSslError> &sslErrors)
 {
   QString errorString;
   QStringList errorStrings;
 
-  vWarn("%1 SSL error(s) when requesting bridge information (id %2):")
-                                                      .arg(sslErrors.size())
-                                                      .arg(_requestId);
+  vWarn("%1 SSL error(s) when requesting bridge information:")
+                                                      .arg(sslErrors.size());
   foreach (QSslError sslError, sslErrors) {
     errorString = sslError.errorString();
     errorStrings << errorString;
     vWarn("  SSL Error: %1").arg(errorString);
   }
-}
 
+  // Check if the certificate is the one we expect
+  if(_reply->sslConfiguration().peerCertificate().digest(QCryptographicHash::Sha1)==QByteArray::fromHex(BRIDGEDB_SHA1)) {
+    _reply->ignoreSslErrors();
+  } else
+    vWarn("There's been a problem with the certificate.");
+}
diff --git a/src/vidalia/config/BridgeDownloader.h b/src/vidalia/config/BridgeDownloader.h
index 0cb87f4..cf30979 100644
--- a/src/vidalia/config/BridgeDownloader.h
+++ b/src/vidalia/config/BridgeDownloader.h
@@ -16,10 +16,7 @@
 #ifndef _BRIDGEDOWNLOADER_H
 #define _BRIDGEDOWNLOADER_H
 
-#include <QHttp>
-#include <QSslError>
-#include <QStringList>
-
+#include <QtNetwork>
 
 class BridgeDownloader : public QObject
 {
@@ -65,7 +62,7 @@ signals:
    * have been read so far. Note that <b>total</b> may be 0 if the expected
    * total size of the response is not known.
    */
-  void downloadProgress(int done, int total);
+  void downloadProgress(qint64 done, qint64 total);
 
   /** Emitted when the status of the bridge request changes. <b>status</b>
    * describes the new current state of the request.
@@ -98,14 +95,14 @@ private slots:
    * signal is ignored since it is the result of a close() or abort()
    * request.
    */
-  void httpsRequestFinished(int id, bool error);
+  void httpsRequestFinished(QNetworkReply *reply);
 
   /** Called when the HTTPS connection encounters one or more
    * <b>sslErrors</b>. Currently the errors are just logged and
    * bridgeRequestFailed() is <i>not</i> emitted, since QHttp will also
    * emit 
    */
-  void sslErrors(const QList<QSslError> &sslErrors);
+  void sslErrors(QNetworkReply *, const QList<QSslError> &sslErrors);
 
 private:
   /** Initiates an HTTPS connection to bridges.torproject.org to start
@@ -115,10 +112,10 @@ private:
 
   /** Used to connect to the bridge database, send an HTTPS request for
    * new bridge addresses and then read the response. */
-  QHttp* _https;
+  QNetworkAccessManager* _https;
   
-  /** Unique numeric identifier of the current bridge request. */
-  int _requestId;
+  /** Identifier of the current bridge request */
+  QNetworkReply *_reply;
 };
 
 #endif
diff --git a/src/vidalia/config/BridgeDownloaderProgressDialog.cpp b/src/vidalia/config/BridgeDownloaderProgressDialog.cpp
index b4d8e28..f16daa8 100644
--- a/src/vidalia/config/BridgeDownloaderProgressDialog.cpp
+++ b/src/vidalia/config/BridgeDownloaderProgressDialog.cpp
@@ -46,7 +46,7 @@ BridgeDownloaderProgressDialog::setStatus(const QString &status)
 }
 
 void
-BridgeDownloaderProgressDialog::setDownloadProgress(int done, int total)
+BridgeDownloaderProgressDialog::setDownloadProgress(qint64 done, qint64 total)
 {
   ui.progressBar->setRange(0, total);
   ui.progressBar->setValue(done);
diff --git a/src/vidalia/config/BridgeDownloaderProgressDialog.h b/src/vidalia/config/BridgeDownloaderProgressDialog.h
index 86fb0d3..d9d505f 100644
--- a/src/vidalia/config/BridgeDownloaderProgressDialog.h
+++ b/src/vidalia/config/BridgeDownloaderProgressDialog.h
@@ -39,7 +39,7 @@ public slots:
    * <b>maximum</b> steps. If <b>value</b> and <b>maximum</b> are both 0,
    * then a "busy" progress bar is displayed.
    */
-  void setDownloadProgress(int value, int maximum);
+  void setDownloadProgress(qint64 value, qint64 maximum);
 
   /** Called when the bridge download completes successfully and discards 
    * the progress dialog with an Accept result code. <b>bridges</b>
diff --git a/src/vidalia/config/NetworkPage.cpp b/src/vidalia/config/NetworkPage.cpp
index f5bbc72..7154334 100644
--- a/src/vidalia/config/NetworkPage.cpp
+++ b/src/vidalia/config/NetworkPage.cpp
@@ -389,8 +389,8 @@ NetworkPage::findBridges()
 
   connect(_bridgeDownloader, SIGNAL(statusChanged(QString)),
           dlg, SLOT(setStatus(QString)));
-  connect(_bridgeDownloader, SIGNAL(downloadProgress(int, int)),
-          dlg, SLOT(setDownloadProgress(int, int)));
+  connect(_bridgeDownloader, SIGNAL(downloadProgress(qint64, qint64)),
+          dlg, SLOT(setDownloadProgress(qint64, qint64)));
   connect(_bridgeDownloader, SIGNAL(bridgeRequestFailed(QString)),
           dlg, SLOT(bridgeRequestFailed(QString)));
   connect(_bridgeDownloader, SIGNAL(bridgeRequestFinished(QStringList)),





More information about the tor-commits mailing list