| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "network-web/basenetworkaccessmanager.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | #include "miscellaneous/settings.h" |
| 7 | #include "network-web/webfactory.h" |
| 8 | |
| 9 | #include <QNetworkProxy> |
| 10 | #include <QNetworkReply> |
| 11 | #include <QNetworkRequest> |
| 12 | |
| 13 | #if defined(NO_LITE) |
| 14 | #include <QWebEngineProfile> |
| 15 | #include <QWebEngineSettings> |
| 16 | #endif |
| 17 | |
| 18 | BaseNetworkAccessManager::BaseNetworkAccessManager(QObject* parent) |
| 19 | : QNetworkAccessManager(parent), m_enableHttp2(false) { |
| 20 | connect(this, &BaseNetworkAccessManager::sslErrors, this, &BaseNetworkAccessManager::onSslErrors); |
| 21 | loadSettings(); |
| 22 | } |
| 23 | |
| 24 | void BaseNetworkAccessManager::setSpecificHtpp2Status(NetworkFactory::Http2Status status) { |
| 25 | switch (status) { |
| 26 | case NetworkFactory::Http2Status::DontSet: |
| 27 | m_enableHttp2 = qApp->settings()->value(GROUP(Network), SETTING(Network::EnableHttp2)).toBool(); |
| 28 | break; |
| 29 | |
| 30 | case NetworkFactory::Http2Status::Enabled: |
| 31 | m_enableHttp2 = true; |
| 32 | break; |
| 33 | |
| 34 | case NetworkFactory::Http2Status::Disabled: |
| 35 | m_enableHttp2 = false; |
| 36 | break; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void BaseNetworkAccessManager::loadSettings() { |
| 41 | const QNetworkProxy::ProxyType selected_proxy_type = |
| 42 | static_cast<QNetworkProxy::ProxyType>(qApp->settings()->value(GROUP(Proxy), SETTING(Proxy::Type)).toInt()); |
| 43 | |
| 44 | if (selected_proxy_type == QNetworkProxy::ProxyType::NoProxy) { |
| 45 | // No extra setting is needed, set new proxy and exit this method. |
| 46 | setProxy(QNetworkProxy::ProxyType::NoProxy); |
| 47 | } |
| 48 | else { |
| 49 | qWarningNN << LOGSEC_NETWORK << "Using application-wide proxy."; |
| 50 | |
| 51 | if (QNetworkProxy::applicationProxy().type() != QNetworkProxy::ProxyType::DefaultProxy && |
| 52 | QNetworkProxy::applicationProxy().type() != QNetworkProxy::ProxyType::NoProxy) { |
| 53 | qWarningNN << LOGSEC_NETWORK |
| 54 | << "Used proxy address:" << QUOTE_W_SPACE_COMMA(QNetworkProxy::applicationProxy().hostName()) |
| 55 | << " type:" << QUOTE_W_SPACE_DOT(QNetworkProxy::applicationProxy().type()); |
| 56 | } |
| 57 | |
| 58 | setProxy(QNetworkProxy::applicationProxy()); |
| 59 | } |
| 60 | |
| 61 | m_enableHttp2 = qApp->settings()->value(GROUP(Network), SETTING(Network::EnableHttp2)).toBool(); |
| 62 | |
| 63 | qDebugNN << LOGSEC_NETWORK << "Settings of BaseNetworkAccessManager loaded."; |
| 64 | } |
| 65 | |
| 66 | void BaseNetworkAccessManager::onSslErrors(QNetworkReply* reply, const QList<QSslError>& error) { |
| 67 | qWarningNN << LOGSEC_NETWORK << "Ignoring SSL errors for" << QUOTE_W_SPACE_DOT(reply->url().toString()); |
| 68 | reply->ignoreSslErrors(error); |
| 69 | } |
| 70 | |
| 71 | QNetworkReply* BaseNetworkAccessManager::createRequest(QNetworkAccessManager::Operation op, |
| 72 | const QNetworkRequest& request, |
| 73 | QIODevice* outgoingData) { |
| 74 | QNetworkRequest new_request = request; |
| 75 | |
| 76 | new_request.setAttribute(QNetworkRequest::Attribute::RedirectPolicyAttribute, |
| 77 | QNetworkRequest::RedirectPolicy::ManualRedirectPolicy); |
| 78 | |
| 79 | #if defined(Q_OS_WIN) |
| 80 | new_request.setAttribute(QNetworkRequest::Attribute::HttpPipeliningAllowedAttribute, true); |
| 81 | #endif |
| 82 | |
| 83 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 84 | new_request.setAttribute(QNetworkRequest::Attribute::Http2AllowedAttribute, m_enableHttp2); |
| 85 | |
| 86 | if (m_enableHttp2) { |
| 87 | qDebugNN << LOGSEC_NETWORK << "Enabling HTTP/2 for this network request."; |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | #if QT_VERSION >= 0x060300 // Qt >= 6.3.0 |
| 92 | new_request.setAttribute(QNetworkRequest::Attribute::Http2CleartextAllowedAttribute, m_enableHttp2); |
| 93 | #endif |
| 94 | |
| 95 | // new_request.setMaximumRedirectsAllowed(0); |
| 96 | |
| 97 | new_request.setRawHeader(HTTP_HEADERS_COOKIE, QSL("JSESSIONID= ").toLocal8Bit()); |
| 98 | |
| 99 | auto custom_ua = qApp->web()->customUserAgent(); |
| 100 | auto existing_ua = new_request.rawHeader(HTTP_HEADERS_USER_AGENT); |
| 101 | |
| 102 | if (existing_ua.isEmpty()) { |
| 103 | if (custom_ua.isEmpty()) { |
| 104 | new_request.setRawHeader(HTTP_HEADERS_USER_AGENT, HTTP_COMPLETE_USERAGENT); |
| 105 | } |
| 106 | else { |
| 107 | new_request.setRawHeader(HTTP_HEADERS_USER_AGENT, custom_ua.toLocal8Bit()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | auto reply = QNetworkAccessManager::createRequest(op, new_request, outgoingData); |
| 112 | auto ssl_conf = reply->sslConfiguration(); |
| 113 | |
| 114 | ssl_conf.setPeerVerifyMode(QSslSocket::PeerVerifyMode::VerifyNone); |
| 115 | ssl_conf.setSslOption(QSsl::SslOption::SslOptionDisableLegacyRenegotiation, false); |
| 116 | |
| 117 | reply->setSslConfiguration(ssl_conf); |
| 118 | |
| 119 | return reply; |
| 120 | } |