2019-07-12 16:54:26 +02:00
|
|
|
|
2016-06-12 22:27:24 +02:00
|
|
|
#include "QtHttpServer.h"
|
|
|
|
#include "QtHttpRequest.h"
|
|
|
|
#include "QtHttpReply.h"
|
|
|
|
#include "QtHttpClientWrapper.h"
|
|
|
|
|
2017-04-03 05:19:05 +02:00
|
|
|
#include <QUrlQuery>
|
2016-06-12 22:27:24 +02:00
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
#include <utils/NetOrigin.h>
|
|
|
|
|
2016-06-12 22:27:24 +02:00
|
|
|
const QString & QtHttpServer::HTTP_VERSION = QStringLiteral ("HTTP/1.1");
|
|
|
|
|
2017-04-03 05:19:05 +02:00
|
|
|
QtHttpServerWrapper::QtHttpServerWrapper (QObject * parent)
|
2019-07-12 16:54:26 +02:00
|
|
|
: QTcpServer (parent)
|
|
|
|
, m_useSsl (false)
|
|
|
|
{ }
|
2017-04-03 05:19:05 +02:00
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
QtHttpServerWrapper::~QtHttpServerWrapper (void) { }
|
2017-04-03 05:19:05 +02:00
|
|
|
|
|
|
|
void QtHttpServerWrapper::setUseSecure (const bool ssl) {
|
2019-07-12 16:54:26 +02:00
|
|
|
m_useSsl = ssl;
|
2017-04-03 05:19:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServerWrapper::incomingConnection (qintptr handle) {
|
|
|
|
QTcpSocket * sock = (m_useSsl
|
|
|
|
? new QSslSocket (this)
|
|
|
|
: new QTcpSocket (this));
|
|
|
|
if (sock->setSocketDescriptor (handle)) {
|
|
|
|
addPendingConnection (sock);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete sock;
|
|
|
|
}
|
2017-04-03 05:19:05 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 22:27:24 +02:00
|
|
|
QtHttpServer::QtHttpServer (QObject * parent)
|
2019-07-12 16:54:26 +02:00
|
|
|
: QObject (parent)
|
|
|
|
, m_useSsl (false)
|
|
|
|
, m_serverName (QStringLiteral ("The Qt5 HTTP Server"))
|
|
|
|
, m_netOrigin (NetOrigin::getInstance())
|
2016-06-12 22:27:24 +02:00
|
|
|
{
|
2019-07-12 16:54:26 +02:00
|
|
|
m_sockServer = new QtHttpServerWrapper (this);
|
|
|
|
connect (m_sockServer, &QtHttpServerWrapper::newConnection, this, &QtHttpServer::onClientConnected);
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
const QString & QtHttpServer::getServerName (void) const {
|
|
|
|
return m_serverName;
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
quint16 QtHttpServer::getServerPort (void) const {
|
|
|
|
return m_sockServer->serverPort ();
|
2017-01-14 19:04:58 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
QString QtHttpServer::getErrorString (void) const {
|
|
|
|
return m_sockServer->errorString ();
|
2017-01-14 19:04:58 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::start (quint16 port) {
|
2018-12-27 23:11:32 +01:00
|
|
|
if(!m_sockServer->isListening())
|
|
|
|
{
|
2019-07-12 16:54:26 +02:00
|
|
|
if (m_sockServer->listen (QHostAddress::Any, port)) {
|
|
|
|
emit started (m_sockServer->serverPort ());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
emit error (m_sockServer->errorString ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 15:48:29 +01:00
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::stop (void) {
|
|
|
|
if (m_sockServer->isListening ()) {
|
|
|
|
m_sockServer->close ();
|
2018-12-31 15:48:29 +01:00
|
|
|
// disconnect clients
|
|
|
|
const QList<QTcpSocket*> socks = m_socksClientsHash.keys();
|
|
|
|
for(auto sock : socks)
|
|
|
|
{
|
|
|
|
sock->close();
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
2019-07-12 16:54:26 +02:00
|
|
|
emit stopped ();
|
|
|
|
}
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::setServerName (const QString & serverName) {
|
|
|
|
m_serverName = serverName;
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::setUseSecure (const bool ssl) {
|
|
|
|
m_useSsl = ssl;
|
|
|
|
m_sockServer->setUseSecure (m_useSsl);
|
2017-04-03 05:19:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::setPrivateKey (const QSslKey & key) {
|
|
|
|
m_sslKey = key;
|
2017-04-03 05:19:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::setCertificates (const QList<QSslCertificate> & certs) {
|
|
|
|
m_sslCerts = certs;
|
2017-04-03 05:19:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::onClientConnected (void) {
|
|
|
|
while (m_sockServer->hasPendingConnections ()) {
|
|
|
|
if (QTcpSocket * sock = m_sockServer->nextPendingConnection ()) {
|
|
|
|
if(m_netOrigin->accessAllowed(sock->peerAddress(), sock->localAddress()))
|
2018-12-31 15:48:29 +01:00
|
|
|
{
|
2019-07-12 16:54:26 +02:00
|
|
|
connect (sock, &QTcpSocket::disconnected, this, &QtHttpServer::onClientDisconnected);
|
|
|
|
if (m_useSsl) {
|
|
|
|
if (QSslSocket * ssl = qobject_cast<QSslSocket *> (sock)) {
|
|
|
|
connect (ssl, SslErrorSignal (&QSslSocket::sslErrors), this, &QtHttpServer::onClientSslErrors);
|
|
|
|
connect (ssl, &QSslSocket::encrypted, this, &QtHttpServer::onClientSslEncrypted);
|
|
|
|
connect (ssl, &QSslSocket::peerVerifyError, this, &QtHttpServer::onClientSslPeerVerifyError);
|
|
|
|
connect (ssl, &QSslSocket::modeChanged, this, &QtHttpServer::onClientSslModeChanged);
|
|
|
|
ssl->setLocalCertificateChain (m_sslCerts);
|
|
|
|
ssl->setPrivateKey (m_sslKey);
|
|
|
|
ssl->setPeerVerifyMode (QSslSocket::AutoVerifyPeer);
|
|
|
|
ssl->startServerEncryption ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QtHttpClientWrapper * wrapper = new QtHttpClientWrapper (sock, m_netOrigin->isLocalAddress(sock->peerAddress(), sock->localAddress()), this);
|
|
|
|
m_socksClientsHash.insert (sock, wrapper);
|
|
|
|
emit clientConnected (wrapper->getGuid ());
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
2019-07-12 16:54:26 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sock->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::onClientSslEncrypted (void) { }
|
2017-04-03 05:19:05 +02:00
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::onClientSslPeerVerifyError (const QSslError & err) {
|
|
|
|
Q_UNUSED (err)
|
2017-04-03 05:19:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::onClientSslErrors (const QList<QSslError> & errors) {
|
|
|
|
Q_UNUSED (errors)
|
2017-04-03 05:19:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::onClientSslModeChanged (QSslSocket::SslMode mode) {
|
|
|
|
Q_UNUSED (mode)
|
2017-04-03 05:19:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
void QtHttpServer::onClientDisconnected (void) {
|
|
|
|
if (QTcpSocket * sockClient = qobject_cast<QTcpSocket *> (sender ())) {
|
|
|
|
if (QtHttpClientWrapper * wrapper = m_socksClientsHash.value (sockClient, Q_NULLPTR)) {
|
|
|
|
emit clientDisconnected (wrapper->getGuid ());
|
|
|
|
wrapper->deleteLater ();
|
|
|
|
m_socksClientsHash.remove (sockClient);
|
|
|
|
}
|
|
|
|
}
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|