QAbstractHttpServer Class

API to subclass to implement an HTTP server. 更多...

頭: #include <QAbstractHttpServer>
CMake: find_package(Qt6 REQUIRED COMPONENTS HttpServer)
target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
qmake: QT += httpserver
Since: Qt 6.4
繼承: QObject
繼承者: QHttpServer

公共函數

QAbstractHttpServer (QObject * parent = nullptr)
void bind (QTcpServer * server = nullptr)
bool hasPendingWebSocketConnections () const
quint16 listen (const QHostAddress & address = QHostAddress::Any, quint16 port = 0)
std::unique_ptr<QWebSocket> nextPendingWebSocketConnection ()
QList<quint16> serverPorts ()
QList<QTcpServer *> servers () const
void sslSetup (const QSslCertificate & certificate , const QSslKey & privateKey , QSsl::SslProtocol protocol = QSsl::SecureProtocols)
void sslSetup (const QSslConfiguration & sslConfiguration )

信號

void newWebSocketConnection ()

保護函數

virtual bool handleRequest (const QHttpServerRequest & request , QHttpServerResponder & responder ) = 0
virtual void missingHandler (const QHttpServerRequest & request , QHttpServerResponder && responder ) = 0

詳細描述

Subclass this class and override handleRequest() to create an HTTP server. Use listen() or bind() to start listening to incoming connections.

成員函數文檔編製

[explicit] QAbstractHttpServer:: QAbstractHttpServer ( QObject * parent = nullptr)

Creates an instance of QAbstractHttpServer with the parent parent .

void QAbstractHttpServer:: bind ( QTcpServer * server = nullptr)

Bind the HTTP server to given TCP server over which the transmission happens. It is possible to call this function multiple times with different instances of TCP server to handle multiple connections and ports, for example both SSL and non-encrypted connections.

After calling this function, every _new_ connection will be handled and forwarded by the HTTP server.

It is the user's responsibility to call QTcpServer::listen () on the server .

server is null, then a new, default-constructed TCP server will be constructed, which will be listening on a random port and all interfaces.

The server will be parented to this HTTP server.

另請參閱 QTcpServer and QTcpServer::listen ().

[pure virtual protected] bool QAbstractHttpServer:: handleRequest (const QHttpServerRequest & request , QHttpServerResponder & responder )

Overload this function to handle each incoming request , by examining the request and sending the appropriate response back to responder . Return true request was handled successfully. If this method returns false , missingHandler() will be called afterwards.

This function must move out of responder before returning true .

bool QAbstractHttpServer:: hasPendingWebSocketConnections () const

返迴 true if the server has pending WebSocket connections; otherwise returns false .

另請參閱 newWebSocketConnection () 和 nextPendingWebSocketConnection ().

quint16 QAbstractHttpServer:: listen (const QHostAddress & address = QHostAddress::Any, quint16 port = 0)

Tries to bind a QTcpServer to address and port .

Returns the server port upon success, 0 otherwise.

[pure virtual protected] void QAbstractHttpServer:: missingHandler (const QHttpServerRequest & request , QHttpServerResponder && responder )

此函數被調用每當 handleRequest() 返迴 false request and responder parameters are the same as handleRequest() was called with.

[signal] void QAbstractHttpServer:: newWebSocketConnection ()

This signal is emitted every time a new WebSocket connection is available.

另請參閱 hasPendingWebSocketConnections () 和 nextPendingWebSocketConnection ().

std::unique_ptr < QWebSocket > QAbstractHttpServer:: nextPendingWebSocketConnection ()

返迴下一待決連接作為連接的 QWebSocket 對象。 nullptr 被返迴若在沒有待決連接時調用此函數。

注意: 返迴的 QWebSocket 對象不可以用於其它綫程。

另請參閱 newWebSocketConnection () 和 hasPendingWebSocketConnections ().

QList < quint16 > QAbstractHttpServer:: serverPorts ()

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.

另請參閱 servers ().

QList < QTcpServer *> QAbstractHttpServer:: servers () const

Returns list of child TCP servers of this HTTP server.

另請參閱 serverPorts ().

void QAbstractHttpServer:: sslSetup (const QSslCertificate & certificate , const QSslKey & privateKey , QSsl::SslProtocol protocol = QSsl::SecureProtocols)

Turns the server into an HTTPS server.

The next listen () call will use the given certificate , privateKey ,和 protocol .

void QAbstractHttpServer:: sslSetup (const QSslConfiguration & sslConfiguration )

Turns the server into an HTTPS server.

The next listen () call will use the given sslConfiguration .