Vidalia  0.3.1
BridgeDownloader.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file BridgeDownloader.cpp
13 ** \brief Downloads a list of new bridge addresses via HTTPS
14 */
15 
16 #include "BridgeDownloader.h"
17 #include "Vidalia.h"
18 
19 #define BRIDGEDB_HOST "bridges.torproject.org"
20 #define BRIDGEDB_PORT 443
21 
23  : QObject(parent)
24 {
25  _https = new QNetworkAccessManager();
26 
27  connect(_https, SIGNAL(finished(QNetworkReply *)),
28  this, SLOT(httpsRequestFinished(QNetworkReply *)));
29  connect(_https, SIGNAL(sslErrors(QNetworkReply *, QList<QSslError>)),
30  this, SLOT(sslErrors(QNetworkReply *, QList<QSslError>)));
31 }
32 
33 void
34 BridgeDownloader::setProxy(const QString &host, int port,
35  const QString &username, const QString &password)
36 {
37  _https->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, host, port, username, password));
38 }
39 
40 bool
42 {
43  if (! isMethodSupported(method))
44  return false;
45 
46  switch (method) {
49  break;
50 
51  default:
52  break;
53  }
54  return true;
55 }
56 
57 bool
59 {
60  switch (method) {
62  return QSslSocket::supportsSsl();
63 
64  default:
65  break;
66  }
67  return false;
68 }
69 
70 void
72 {
73  emit statusChanged(tr("Starting HTTPS bridge request..."));
74  emit downloadProgress(0, 0);
75 
76  _reply = _https->get(QNetworkRequest(QUrl("https://bridges.torproject.org/?format=plain")));
77  connect(_reply, SIGNAL(downloadProgress(qint64, qint64)),
78  this, SIGNAL(downloadProgress(qint64, qint64)));
79  vInfo("Sending an HTTPS bridge request to %1:%2.").arg(BRIDGEDB_HOST)
80  .arg(BRIDGEDB_PORT);
81 }
82 
83 void
85 {
86  _reply->close();
87  disconnect(_reply, 0, 0, 0);
88 }
89 
90 void
92 {
93  switch (state) {
94  case QHttp::Connecting:
95  emit statusChanged(tr("Connecting to %1:%2...").arg(BRIDGEDB_HOST)
96  .arg(BRIDGEDB_PORT));
97  break;
98 
99  case QHttp::Sending:
100  emit statusChanged(tr("Sending an HTTPS request for bridges..."));
101  break;
102 
103  case QHttp::Reading:
104  emit statusChanged(tr("Downloading a list of bridges..."));
105  break;
106 
107  default:
108  break;
109  }
110 }
111 
112 void
114 {
115  if (reply->error() != QNetworkReply::NoError) {
116  QString errorString = reply->errorString();
117  vWarn("Bridge request failed: %2").arg(errorString);
118 
119  emit bridgeRequestFailed(errorString);
120  } else {
121  QByteArray response = reply->readAll();
122  vInfo("Bridge request complete: received %2 bytes.").arg(response.size());
123 
124  QStringList bridges, lines = QString(response).split("\n");
125  foreach (QString line, lines) {
126  line = line.trimmed();
127  if (line.startsWith("bridge ", Qt::CaseInsensitive))
128  bridges << line;
129  }
130  emit bridgeRequestFinished(bridges);
131  }
132  _reply->close();
133  disconnect(_reply,0,0,0);
134 }
135 
136 void
137 BridgeDownloader::sslErrors(QNetworkReply *reply, const QList<QSslError> &sslErrors)
138 {
139  QString errorString;
140  QStringList errorStrings;
141 
142  vWarn("%1 SSL error(s) when requesting bridge information:")
143  .arg(sslErrors.size());
144  foreach (QSslError sslError, sslErrors) {
145  errorString = sslError.errorString();
146  errorStrings << errorString;
147  vWarn(" SSL Error: %1").arg(errorString);
148  }
149 }
void statusChanged(const QString &status)
void bridgeRequestFailed(const QString &error)
BridgeDownloader(QObject *parent=0)
QNetworkAccessManager * _https
#define vInfo(fmt)
Definition: Vidalia.h:40
QNetworkReply * _reply
void bridgeRequestFinished(const QStringList &bridges)
void downloadProgress(qint64 done, qint64 total)
void httpsRequestFinished(QNetworkReply *reply)
stop errmsg connect(const QHostAddress &address, quint16 port)
#define BRIDGEDB_HOST
#define vWarn(fmt)
Definition: Vidalia.h:42
static bool isMethodSupported(BridgeDownloadMethod method)
stop errmsg disconnect()
void sslErrors(QNetworkReply *, const QList< QSslError > &sslErrors)
#define BRIDGEDB_PORT
void setProxy(const QString &host, int port, const QString &username=QString(), const QString &password=QString())
void httpsStateChanged(int state)
bool downloadBridges(BridgeDownloadMethod method)