|
|
|
@ -30,8 +30,10 @@
@@ -30,8 +30,10 @@
|
|
|
|
|
|
|
|
|
|
#include "server.h" |
|
|
|
|
|
|
|
|
|
#include <QMutableListIterator> |
|
|
|
|
#include <QNetworkProxy> |
|
|
|
|
#include <QStringList> |
|
|
|
|
#include <QTimer> |
|
|
|
|
|
|
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
|
#include <QSslSocket> |
|
|
|
@ -41,6 +43,10 @@
@@ -41,6 +43,10 @@
|
|
|
|
|
|
|
|
|
|
#include "connection.h" |
|
|
|
|
|
|
|
|
|
static const int KEEP_ALIVE_DURATION = 7; // seconds
|
|
|
|
|
static const int CONNECTIONS_LIMIT = 500; |
|
|
|
|
static const int CONNECTIONS_SCAN_INTERVAL = 2; // seconds
|
|
|
|
|
|
|
|
|
|
using namespace Http; |
|
|
|
|
|
|
|
|
|
Server::Server(IRequestHandler *requestHandler, QObject *parent) |
|
|
|
@ -54,6 +60,10 @@ Server::Server(IRequestHandler *requestHandler, QObject *parent)
@@ -54,6 +60,10 @@ Server::Server(IRequestHandler *requestHandler, QObject *parent)
|
|
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
|
QSslSocket::setDefaultCiphers(safeCipherList()); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
QTimer *dropConnectionTimer = new QTimer(this); |
|
|
|
|
connect(dropConnectionTimer, &QTimer::timeout, this, &Server::dropTimedOutConnection); |
|
|
|
|
dropConnectionTimer->start(CONNECTIONS_SCAN_INTERVAL * 1000); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Server::~Server() |
|
|
|
@ -62,6 +72,8 @@ Server::~Server()
@@ -62,6 +72,8 @@ Server::~Server()
|
|
|
|
|
|
|
|
|
|
void Server::incomingConnection(qintptr socketDescriptor) |
|
|
|
|
{ |
|
|
|
|
if (m_connections.size() >= CONNECTIONS_LIMIT) return; |
|
|
|
|
|
|
|
|
|
QTcpSocket *serverSocket; |
|
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
|
if (m_https) |
|
|
|
@ -70,7 +82,11 @@ void Server::incomingConnection(qintptr socketDescriptor)
@@ -70,7 +82,11 @@ void Server::incomingConnection(qintptr socketDescriptor)
|
|
|
|
|
#endif |
|
|
|
|
serverSocket = new QTcpSocket(this); |
|
|
|
|
|
|
|
|
|
if (serverSocket->setSocketDescriptor(socketDescriptor)) { |
|
|
|
|
if (!serverSocket->setSocketDescriptor(socketDescriptor)) { |
|
|
|
|
delete serverSocket; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
|
if (m_https) { |
|
|
|
|
static_cast<QSslSocket *>(serverSocket)->setProtocol(QSsl::SecureProtocols); |
|
|
|
@ -80,10 +96,20 @@ void Server::incomingConnection(qintptr socketDescriptor)
@@ -80,10 +96,20 @@ void Server::incomingConnection(qintptr socketDescriptor)
|
|
|
|
|
static_cast<QSslSocket *>(serverSocket)->startServerEncryption(); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
new Connection(serverSocket, m_requestHandler, this); |
|
|
|
|
|
|
|
|
|
Connection *c = new Connection(serverSocket, m_requestHandler, this); |
|
|
|
|
m_connections.append(c); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Server::dropTimedOutConnection() |
|
|
|
|
{ |
|
|
|
|
QMutableListIterator<Connection *> i(m_connections); |
|
|
|
|
while (i.hasNext()) { |
|
|
|
|
auto connection = i.next(); |
|
|
|
|
if (connection->isClosed() || connection->hasExpired(KEEP_ALIVE_DURATION)) { |
|
|
|
|
delete connection; |
|
|
|
|
i.remove(); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
serverSocket->deleteLater(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|