Skip to content

Commit 1ba6f44

Browse files
committed
Add serverAddress() and serverPort() to QDjangoHttpServer
This allows retrieval of the address and port the server is listening on. This closes #9.
1 parent 85020f1 commit 1ba6f44

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/http/QDjangoHttpServer.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,26 @@ bool QDjangoHttpServer::listen(const QHostAddress &address, quint16 port)
318318
return d->tcpServer->listen(address, port);
319319
}
320320

321+
/** Returns the server's address if the server is listening for connections;
322+
* otherwise returns QHostAddress::Null.
323+
*/
324+
QHostAddress QDjangoHttpServer::serverAddress() const
325+
{
326+
if (!d->tcpServer)
327+
return QHostAddress::Null;
328+
return d->tcpServer->serverAddress();
329+
}
330+
331+
/** Returns the server's port if the server is listening for connections;
332+
* otherwise returns 0.
333+
*/
334+
quint16 QDjangoHttpServer::serverPort() const
335+
{
336+
if (!d->tcpServer)
337+
return 0;
338+
return d->tcpServer->serverPort();
339+
}
340+
321341
/** Returns the root URL resolver for the server, which dispatches
322342
* requests to handlers.
323343
*/

src/http/QDjangoHttpServer.h

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class QDJANGO_EXPORT QDjangoHttpServer : public QObject
4949

5050
void close();
5151
bool listen(const QHostAddress &address, quint16 port);
52+
QHostAddress serverAddress() const;
53+
quint16 serverPort() const;
5254
QDjangoUrlResolver *urls() const;
5355

5456
signals:

tests/http/qdjangohttpserver/tst_qdjangohttpserver.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ void tst_QDjangoHttpServer::initTestCase()
6161
httpServer = new QDjangoHttpServer;
6262
httpServer->urls()->set(QRegExp(QLatin1String(QLatin1String("^$"))), this, "_q_index");
6363
httpServer->urls()->set(QRegExp(QLatin1String("^internal-server-error$")), this, "_q_error");
64+
QCOMPARE(httpServer->serverAddress(), QHostAddress(QHostAddress::Null));
65+
QCOMPARE(httpServer->serverPort(), quint16(0));
6466
QCOMPARE(httpServer->listen(QHostAddress::LocalHost, 8123), true);
67+
QCOMPARE(httpServer->serverAddress(), QHostAddress(QHostAddress::LocalHost));
68+
QCOMPARE(httpServer->serverPort(), quint16(8123));
6569
}
6670

6771
void tst_QDjangoHttpServer::testCloseConnection()

0 commit comments

Comments
 (0)