0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the demonstration applications of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "networkaccessmanager.h"
|
|
43 |
|
|
44 |
#include "browserapplication.h"
|
|
45 |
#include "browsermainwindow.h"
|
|
46 |
#include "ui_passworddialog.h"
|
|
47 |
#include "ui_proxy.h"
|
|
48 |
|
|
49 |
#include <QtCore/QSettings>
|
|
50 |
|
|
51 |
#include <QtGui/QDesktopServices>
|
|
52 |
#include <QtGui/QDialog>
|
|
53 |
#include <QtGui/QMessageBox>
|
|
54 |
#include <QtGui/QStyle>
|
|
55 |
#include <QtGui/QTextDocument>
|
|
56 |
|
|
57 |
#include <QtNetwork/QAuthenticator>
|
|
58 |
#include <QtNetwork/QNetworkDiskCache>
|
|
59 |
#include <QtNetwork/QNetworkProxy>
|
|
60 |
#include <QtNetwork/QNetworkRequest>
|
|
61 |
#include <QtNetwork/QNetworkReply>
|
|
62 |
#include <QtNetwork/QSslError>
|
|
63 |
|
|
64 |
NetworkAccessManager::NetworkAccessManager(QObject *parent)
|
|
65 |
: QNetworkAccessManager(parent),
|
|
66 |
requestFinishedCount(0), requestFinishedFromCacheCount(0), requestFinishedPipelinedCount(0),
|
|
67 |
requestFinishedSecureCount(0)
|
|
68 |
{
|
|
69 |
connect(this, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
|
|
70 |
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
|
|
71 |
connect(this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
|
|
72 |
SLOT(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
|
|
73 |
connect(this, SIGNAL(finished(QNetworkReply *)),
|
|
74 |
SLOT(requestFinished(QNetworkReply *)));
|
|
75 |
#ifndef QT_NO_OPENSSL
|
|
76 |
connect(this, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)),
|
|
77 |
SLOT(sslErrors(QNetworkReply*, const QList<QSslError>&)));
|
|
78 |
#endif
|
|
79 |
loadSettings();
|
|
80 |
|
|
81 |
QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
|
|
82 |
QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
|
|
83 |
diskCache->setCacheDirectory(location);
|
|
84 |
setCache(diskCache);
|
|
85 |
}
|
|
86 |
|
|
87 |
QNetworkReply* NetworkAccessManager::createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData)
|
|
88 |
{
|
|
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);
|
|
94 |
}
|
|
95 |
|
|
96 |
void NetworkAccessManager::requestFinished(QNetworkReply *reply)
|
|
97 |
{
|
|
98 |
requestFinishedCount++;
|
|
99 |
|
|
100 |
if (reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool() == true)
|
|
101 |
requestFinishedFromCacheCount++;
|
|
102 |
|
|
103 |
if (reply->attribute(QNetworkRequest::HttpPipeliningWasUsedAttribute).toBool() == true)
|
|
104 |
requestFinishedPipelinedCount++;
|
|
105 |
|
|
106 |
if (reply->attribute(QNetworkRequest::ConnectionEncryptedAttribute).toBool() == true)
|
|
107 |
requestFinishedSecureCount++;
|
|
108 |
|
|
109 |
if (requestFinishedCount % 10)
|
|
110 |
return;
|
|
111 |
|
|
112 |
double pctCached = (double(requestFinishedFromCacheCount) * 100.0/ double(requestFinishedCount));
|
|
113 |
double pctPipelined = (double(requestFinishedPipelinedCount) * 100.0/ double(requestFinishedCount));
|
|
114 |
double pctSecure = (double(requestFinishedSecureCount) * 100.0/ double(requestFinishedCount));
|
|
115 |
qDebug("STATS [%lli requests total] [%3.2f%% from cache] [%3.2f%% pipelined] [%3.2f%% SSL/TLS]", requestFinishedCount, pctCached, pctPipelined, pctSecure);
|
|
116 |
|
|
117 |
}
|
|
118 |
|
|
119 |
void NetworkAccessManager::loadSettings()
|
|
120 |
{
|
|
121 |
QSettings settings;
|
|
122 |
settings.beginGroup(QLatin1String("proxy"));
|
|
123 |
QNetworkProxy proxy;
|
|
124 |
if (settings.value(QLatin1String("enabled"), false).toBool()) {
|
|
125 |
if (settings.value(QLatin1String("type"), 0).toInt() == 0)
|
|
126 |
proxy = QNetworkProxy::Socks5Proxy;
|
|
127 |
else
|
|
128 |
proxy = QNetworkProxy::HttpProxy;
|
|
129 |
proxy.setHostName(settings.value(QLatin1String("hostName")).toString());
|
|
130 |
proxy.setPort(settings.value(QLatin1String("port"), 1080).toInt());
|
|
131 |
proxy.setUser(settings.value(QLatin1String("userName")).toString());
|
|
132 |
proxy.setPassword(settings.value(QLatin1String("password")).toString());
|
|
133 |
}
|
|
134 |
setProxy(proxy);
|
|
135 |
}
|
|
136 |
|
|
137 |
void NetworkAccessManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *auth)
|
|
138 |
{
|
|
139 |
BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
|
|
140 |
|
|
141 |
QDialog dialog(mainWindow);
|
|
142 |
dialog.setWindowFlags(Qt::Sheet);
|
|
143 |
|
|
144 |
Ui::PasswordDialog passwordDialog;
|
|
145 |
passwordDialog.setupUi(&dialog);
|
|
146 |
|
|
147 |
passwordDialog.iconLabel->setText(QString());
|
|
148 |
passwordDialog.iconLabel->setPixmap(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
|
|
149 |
|
|
150 |
QString introMessage = tr("<qt>Enter username and password for \"%1\" at %2</qt>");
|
|
151 |
introMessage = introMessage.arg(Qt::escape(reply->url().toString())).arg(Qt::escape(reply->url().toString()));
|
|
152 |
passwordDialog.introLabel->setText(introMessage);
|
|
153 |
passwordDialog.introLabel->setWordWrap(true);
|
|
154 |
|
|
155 |
if (dialog.exec() == QDialog::Accepted) {
|
|
156 |
auth->setUser(passwordDialog.userNameLineEdit->text());
|
|
157 |
auth->setPassword(passwordDialog.passwordLineEdit->text());
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
void NetworkAccessManager::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth)
|
|
162 |
{
|
|
163 |
BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
|
|
164 |
|
|
165 |
QDialog dialog(mainWindow);
|
|
166 |
dialog.setWindowFlags(Qt::Sheet);
|
|
167 |
|
|
168 |
Ui::ProxyDialog proxyDialog;
|
|
169 |
proxyDialog.setupUi(&dialog);
|
|
170 |
|
|
171 |
proxyDialog.iconLabel->setText(QString());
|
|
172 |
proxyDialog.iconLabel->setPixmap(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
|
|
173 |
|
|
174 |
QString introMessage = tr("<qt>Connect to proxy \"%1\" using:</qt>");
|
|
175 |
introMessage = introMessage.arg(Qt::escape(proxy.hostName()));
|
|
176 |
proxyDialog.introLabel->setText(introMessage);
|
|
177 |
proxyDialog.introLabel->setWordWrap(true);
|
|
178 |
|
|
179 |
if (dialog.exec() == QDialog::Accepted) {
|
|
180 |
auth->setUser(proxyDialog.userNameLineEdit->text());
|
|
181 |
auth->setPassword(proxyDialog.passwordLineEdit->text());
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
#ifndef QT_NO_OPENSSL
|
|
186 |
void NetworkAccessManager::sslErrors(QNetworkReply *reply, const QList<QSslError> &error)
|
|
187 |
{
|
|
188 |
// check if SSL certificate has been trusted already
|
|
189 |
QString replyHost = reply->url().host() + ":" + reply->url().port();
|
|
190 |
if(! sslTrustedHostList.contains(replyHost)) {
|
|
191 |
BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
|
|
192 |
|
|
193 |
QStringList errorStrings;
|
|
194 |
for (int i = 0; i < error.count(); ++i)
|
|
195 |
errorStrings += error.at(i).errorString();
|
|
196 |
QString errors = errorStrings.join(QLatin1String("\n"));
|
|
197 |
int ret = QMessageBox::warning(mainWindow, QCoreApplication::applicationName(),
|
|
198 |
tr("SSL Errors:\n\n%1\n\n%2\n\n"
|
|
199 |
"Do you want to ignore these errors for this host?").arg(reply->url().toString()).arg(errors),
|
|
200 |
QMessageBox::Yes | QMessageBox::No,
|
|
201 |
QMessageBox::No);
|
|
202 |
if (ret == QMessageBox::Yes) {
|
|
203 |
reply->ignoreSslErrors();
|
|
204 |
sslTrustedHostList.append(replyHost);
|
|
205 |
}
|
|
206 |
}
|
|
207 |
}
|
|
208 |
#endif
|