1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "networkaccessmanager.h"
44 #include "browserapplication.h"
45 #include "browsermainwindow.h"
46 #include "ui_passworddialog.h"
49 #include <QtCore/QSettings>
51 #include <QtGui/QDesktopServices>
52 #include <QtGui/QDialog>
53 #include <QtGui/QMessageBox>
54 #include <QtGui/QStyle>
55 #include <QtGui/QTextDocument>
57 #include <QtNetwork/QAuthenticator>
58 #include <QtNetwork/QNetworkDiskCache>
59 #include <QtNetwork/QNetworkProxy>
60 #include <QtNetwork/QNetworkRequest>
61 #include <QtNetwork/QNetworkReply>
62 #include <QtNetwork/QSslError>
64 NetworkAccessManager::NetworkAccessManager(QObject *parent)
65 : QNetworkAccessManager(parent),
66 requestFinishedCount(0), requestFinishedFromCacheCount(0), requestFinishedPipelinedCount(0),
67 requestFinishedSecureCount(0), requestFinishedDownloadBufferCount(0)
69 connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
70 SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
71 connect(this, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
72 SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
73 connect(this, SIGNAL(finished(QNetworkReply*)),
74 SLOT(requestFinished(QNetworkReply*)));
76 connect(this, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
77 SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
81 QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
82 QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
83 diskCache->setCacheDirectory(location);
87 QNetworkReply* NetworkAccessManager::createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData)
89 QNetworkRequest request = req; // copy so we can modify
90 // this is a temporary hack until we properly use the pipelining flags from QtWebkit
91 // pipeline everything! :)
92 request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
93 return QNetworkAccessManager::createRequest(op, request, outgoingData);
96 void NetworkAccessManager::requestFinished(QNetworkReply *reply)
98 requestFinishedCount++;
100 if (reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool() == true)
101 requestFinishedFromCacheCount++;
103 if (reply->attribute(QNetworkRequest::HttpPipeliningWasUsedAttribute).toBool() == true)
104 requestFinishedPipelinedCount++;
106 if (reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute).toBool() == true)
107 requestFinishedSecureCount++;
109 if (reply->attribute(QNetworkRequest::DownloadBufferAttribute).isValid() == true)
110 requestFinishedDownloadBufferCount++;
112 if (requestFinishedCount % 10)
115 double pctCached = (double(requestFinishedFromCacheCount) * 100.0/ double(requestFinishedCount));
116 double pctPipelined = (double(requestFinishedPipelinedCount) * 100.0/ double(requestFinishedCount));
117 double pctSecure = (double(requestFinishedSecureCount) * 100.0/ double(requestFinishedCount));
118 double pctDownloadBuffer = (double(requestFinishedDownloadBufferCount) * 100.0/ double(requestFinishedCount));
121 qDebug("STATS [%lli requests total] [%3.2f%% from cache] [%3.2f%% pipelined] [%3.2f%% SSL/TLS] [%3.2f%% Zerocopy]", requestFinishedCount, pctCached, pctPipelined, pctSecure, pctDownloadBuffer);
125 void NetworkAccessManager::loadSettings()
128 settings.beginGroup(QLatin1String("proxy"));
130 if (settings.value(QLatin1String("enabled"), false).toBool()) {
131 if (settings.value(QLatin1String("type"), 0).toInt() == 0)
132 proxy = QNetworkProxy::Socks5Proxy;
134 proxy = QNetworkProxy::HttpProxy;
135 proxy.setHostName(settings.value(QLatin1String("hostName")).toString());
136 proxy.setPort(settings.value(QLatin1String("port"), 1080).toInt());
137 proxy.setUser(settings.value(QLatin1String("userName")).toString());
138 proxy.setPassword(settings.value(QLatin1String("password")).toString());
143 void NetworkAccessManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *auth)
145 BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
147 QDialog dialog(mainWindow);
148 dialog.setWindowFlags(Qt::Sheet);
150 Ui::PasswordDialog passwordDialog;
151 passwordDialog.setupUi(&dialog);
153 passwordDialog.iconLabel->setText(QString());
154 passwordDialog.iconLabel->setPixmap(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
156 QString introMessage = tr("<qt>Enter username and password for \"%1\" at %2</qt>");
157 introMessage = introMessage.arg(Qt::escape(reply->url().toString())).arg(Qt::escape(reply->url().toString()));
158 passwordDialog.introLabel->setText(introMessage);
159 passwordDialog.introLabel->setWordWrap(true);
161 if (dialog.exec() == QDialog::Accepted) {
162 auth->setUser(passwordDialog.userNameLineEdit->text());
163 auth->setPassword(passwordDialog.passwordLineEdit->text());
167 void NetworkAccessManager::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth)
169 BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
171 QDialog dialog(mainWindow);
172 dialog.setWindowFlags(Qt::Sheet);
174 Ui::ProxyDialog proxyDialog;
175 proxyDialog.setupUi(&dialog);
177 proxyDialog.iconLabel->setText(QString());
178 proxyDialog.iconLabel->setPixmap(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
180 QString introMessage = tr("<qt>Connect to proxy \"%1\" using:</qt>");
181 introMessage = introMessage.arg(Qt::escape(proxy.hostName()));
182 proxyDialog.introLabel->setText(introMessage);
183 proxyDialog.introLabel->setWordWrap(true);
185 if (dialog.exec() == QDialog::Accepted) {
186 auth->setUser(proxyDialog.userNameLineEdit->text());
187 auth->setPassword(proxyDialog.passwordLineEdit->text());
191 #ifndef QT_NO_OPENSSL
192 void NetworkAccessManager::sslErrors(QNetworkReply *reply, const QList<QSslError> &error)
194 // check if SSL certificate has been trusted already
195 QString replyHost = reply->url().host() + QString(":%1").arg(reply->url().port());
196 if(! sslTrustedHostList.contains(replyHost)) {
197 BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
199 QStringList errorStrings;
200 for (int i = 0; i < error.count(); ++i)
201 errorStrings += error.at(i).errorString();
202 QString errors = errorStrings.join(QLatin1String("\n"));
203 int ret = QMessageBox::warning(mainWindow, QCoreApplication::applicationName(),
204 tr("SSL Errors:\n\n%1\n\n%2\n\n"
205 "Do you want to ignore these errors for this host?").arg(reply->url().toString()).arg(errors),
206 QMessageBox::Yes | QMessageBox::No,
208 if (ret == QMessageBox::Yes) {
209 reply->ignoreSslErrors();
210 sslTrustedHostList.append(replyHost);