Browse Source

Refactor: move the validation of certificates & key functions under Server class

Rename method
Add log messages
adaptive-webui-19844
Chocobo1 8 years ago
parent
commit
7f346b49a7
  1. 22
      src/base/http/server.cpp
  2. 2
      src/base/http/server.h
  3. 17
      src/webui/webui.cpp

22
src/base/http/server.cpp

@ -61,11 +61,27 @@ Server::~Server()
} }
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void Server::enableHttps(const QList<QSslCertificate> &certificates, const QSslKey &key) bool Server::setupHttps(const QByteArray &certificates, const QByteArray &key)
{ {
m_certificates = certificates; QSslKey sslKey(key, QSsl::Rsa);
m_key = key; if (sslKey.isNull())
sslKey = QSslKey(key, QSsl::Ec);
const QList<QSslCertificate> certs = QSslCertificate::fromData(certificates);
const bool areCertsValid = !certs.empty() && std::all_of(certs.begin(), certs.end(), [](const QSslCertificate &c) { return !c.isNull(); });
if (!sslKey.isNull() && areCertsValid)
{
m_key = sslKey;
m_certificates = certs;
m_https = true; m_https = true;
return true;
}
else
{
disableHttps();
return false;
}
} }
void Server::disableHttps() void Server::disableHttps()

2
src/base/http/server.h

@ -55,7 +55,7 @@ namespace Http
~Server(); ~Server();
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void enableHttps(const QList<QSslCertificate> &certificates, const QSslKey &key); bool setupHttps(const QByteArray &certificates, const QByteArray &key);
void disableHttps(); void disableHttps();
#endif #endif

17
src/webui/webui.cpp

@ -75,18 +75,13 @@ void WebUI::init()
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
if (pref->isWebUiHttpsEnabled()) { if (pref->isWebUiHttpsEnabled()) {
const QByteArray keyRaw = pref->getWebUiHttpsKey(); const QByteArray certs = pref->getWebUiHttpsCertificate();
QSslKey key(keyRaw, QSsl::Rsa); const QByteArray key = pref->getWebUiHttpsKey();
if (key.isNull()) bool success = m_httpServer->setupHttps(certs, key);
key = QSslKey(keyRaw, QSsl::Ec); if (success)
logger->addMessage(tr("Web UI: https setup successful"));
const QList<QSslCertificate> certs = QSslCertificate::fromData(pref->getWebUiHttpsCertificate());
const bool areCertsValid = !certs.empty() && std::all_of(certs.begin(), certs.end(), [](QSslCertificate c) { return !c.isNull(); });
if (!key.isNull() && areCertsValid)
m_httpServer->enableHttps(certs, key);
else else
m_httpServer->disableHttps(); logger->addMessage(tr("Web UI: https setup failed, fallback to http"), Log::CRITICAL);
} }
else { else {
m_httpServer->disableHttps(); m_httpServer->disableHttps();

Loading…
Cancel
Save