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 |
继承者: | |
状态: | 技术预览 |
QAbstractHttpServer (QObject * parent = nullptr) | |
void | bind (QTcpServer * server = nullptr) |
bool | hasPendingWebSocketConnections () const |
quint16 | listen (const QHostAddress & address = QHostAddress::Any, quint16 port = 0) |
QList<QLocalServer *> | localServers () const |
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 .
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
.
返回
true
if the server has pending WebSocket connections; otherwise returns
false
.
另请参阅 newWebSocketConnection () 和 nextPendingWebSocketConnection ().
Tries to bind a
QTcpServer
to
address
and
port
.
Returns the server port upon success, 0 otherwise.
Returns list of child TCP servers of this HTTP server.
另请参阅 serverPorts ().
[pure virtual protected]
void
QAbstractHttpServer::
missingHandler
(const
QHttpServerRequest
&
request
,
QHttpServerResponder
&&
responder
)
This function is called whenever
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 ().
返回下一待决连接作为连接的
QWebSocket
对象。
nullptr
被返回若在没有待决连接时调用此函数。
注意: 返回的 QWebSocket 对象不可以用于其它线程。
另请参阅 newWebSocketConnection () 和 hasPendingWebSocketConnections ().
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 ().
Returns list of child TCP servers of this HTTP server.
另请参阅 serverPorts ().
Turns the server into an HTTPS server.
The next listen () call will use the given certificate , privateKey ,和 protocol .
Turns the server into an HTTPS server.
The next listen () call will use the given sslConfiguration .