Make QAbstractHttpServer::listen return 0 on fail

Which then makes it compatible with other listen() functions in Qt
in the sense that doing if (server.listen()) will behave
consistently.
Ports being listened to can then be gotten from the new serverPorts()
function, mirroring serverPort() from QTcpServer etc., except it's a
QVector.

This is a source incompatible change, but is allowed because this
module is still in tech preview.

Fixes: QTBUG-79411
Change-Id: I09764afbf8fd866af4f43ab90fcf9c2e9a373237
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@gmail.com>
This commit is contained in:
Mårten Nordheim 2019-11-11 21:40:27 +01:00
parent 4b87afbbe6
commit 93ff67a3fc
5 changed files with 34 additions and 9 deletions

View File

@ -113,9 +113,9 @@ int main(int argc, char *argv[])
});
const auto port = httpServer.listen(QHostAddress::Any);
if (port == -1) {
if (!port) {
qDebug() << QCoreApplication::translate(
"QHttpServerExample", "Could not run on http://127.0.0.1:%1/").arg(port);
"QHttpServerExample", "Server failed to listen on a port.");
return 0;
}

View File

@ -144,9 +144,9 @@ QAbstractHttpServer::QAbstractHttpServer(QAbstractHttpServerPrivate &dd, QObject
/*!
Tries to bind a \c QTcpServer to \a address and \a port.
Returns the server port upon success, -1 otherwise.
Returns the server port upon success, 0 otherwise.
*/
int QAbstractHttpServer::listen(const QHostAddress &address, quint16 port)
quint16 QAbstractHttpServer::listen(const QHostAddress &address, quint16 port)
{
#if QT_CONFIG(ssl)
Q_D(QAbstractHttpServer);
@ -165,7 +165,26 @@ int QAbstractHttpServer::listen(const QHostAddress &address, quint16 port)
}
delete tcpServer;
return -1;
return 0;
}
/*!
Returns the list of ports this instance of QAbstractHttpServer
is listening to.
This function has the same guarantee as QObject::children,
the latest server added is the last entry in the vector.
\sa servers()
*/
QVector<quint16> QAbstractHttpServer::serverPorts()
{
QVector<quint16> ports;
auto children = findChildren<QTcpServer *>();
ports.reserve(children.count());
std::transform(children.cbegin(), children.cend(), std::back_inserter(ports),
[](const QTcpServer *server) { return server->serverPort(); });
return ports;
}
/*!
@ -210,6 +229,8 @@ void QAbstractHttpServer::bind(QTcpServer *server)
/*!
Returns list of child TCP servers of this HTTP server.
\sa serverPorts()
*/
QVector<QTcpServer *> QAbstractHttpServer::servers() const
{

View File

@ -58,7 +58,8 @@ class Q_HTTPSERVER_EXPORT QAbstractHttpServer : public QObject
public:
QAbstractHttpServer(QObject *parent = nullptr);
int listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
quint16 listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
QVector<quint16> serverPorts();
void bind(QTcpServer *server = nullptr);
QVector<QTcpServer *> servers() const;

View File

@ -184,8 +184,11 @@ void tst_QAbstractHttpServer::servers()
tcpServer2->listen();
server.bind(tcpServer2);
QTRY_COMPARE(server.servers().count(), 2);
QTRY_COMPARE(server.serverPorts().count(), 2);
QTRY_COMPARE(server.servers().first(), tcpServer);
QTRY_COMPARE(server.serverPorts().first(), tcpServer->serverPort());
QTRY_COMPARE(server.servers().last(), tcpServer2);
QTRY_COMPARE(server.serverPorts().last(), tcpServer2->serverPort());
}
void tst_QAbstractHttpServer::fork()

View File

@ -289,8 +289,8 @@ void tst_QHttpServer::initTestCase()
return resp;
});
int port = httpserver.listen();
if (port < 0)
quint16 port = httpserver.listen();
if (!port)
qCritical() << "Http server listen failed";
urlBase = QStringLiteral("http://localhost:%1%2").arg(port);
@ -300,7 +300,7 @@ void tst_QHttpServer::initTestCase()
QSslKey(g_privateKey, QSsl::Rsa));
port = httpserver.listen();
if (port < 0)
if (!port)
qCritical() << "Http server listen failed";
sslUrlBase = QStringLiteral("https://localhost:%1%2").arg(port);