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) | |
| virtual | ~QAbstractHttpServer () override |
(从 6.8 起)
void
|
addWebSocketUpgradeVerifier (const typename QtPrivate::ContextTypeForFunctor<Handler>::ContextType * context , Handler && func ) |
| bool | bind (QLocalServer * server ) |
| bool | bind (QTcpServer * server ) |
| bool | hasPendingWebSocketConnections () const |
(从 6.8 起)
QHttp2Configuration
|
http2Configuration () const |
| QList<QLocalServer *> | localServers () const |
| std::unique_ptr<QWebSocket> | nextPendingWebSocketConnection () |
| QList<quint16> | serverPorts () const |
| QList<QTcpServer *> | servers () const |
(从 6.8 起)
void
|
setHttp2Configuration (const QHttp2Configuration & configuration ) |
| 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 () 和 missingHandler () to create an HTTP server. Use bind () to start listening to all the incoming connections to a server.
This is a low level API, see QHttpServer for a highler level API to implement an HTTP server.
[explicit]
QAbstractHttpServer::
QAbstractHttpServer
(
QObject
*
parent
= nullptr)
Creates an instance of QAbstractHttpServer with the parent parent .
[override virtual noexcept]
QAbstractHttpServer::
~QAbstractHttpServer
()
Destroys an instance of QAbstractHttpServer .
[since 6.8]
template <typename Handler, QAbstractHttpServer::if_compatible_callable<Handler> = true>
void
QAbstractHttpServer::
addWebSocketUpgradeVerifier
(const
typename
QtPrivate::ContextTypeForFunctor
<
Handler
>
::ContextType
*
context
,
Handler
&&
func
)
Adds a callback function
func
that verifies incoming WebSocket upgrades using the context object
context
. Upgrade attempts succeed if at least one of the registered callback functions returns
Accept
and a handler returning
Deny
has not been executed before it. If no handlers are registered or all return
PassToNext
,
missingHandler
() is called. The callback functions are executed in the order they are registered. The callbacks cannot call addWebSocketUpgradeVerifier().
注意: The WebSocket upgrades fail if no callbacks has been registered.
注意:
This overload participates in overload resolution only if the callback function takes a
const
QHttpServerRequest
& as an argument and returns a
QHttpServerWebSocketUpgradeResponse
.
server.addWebSocketUpgradeVerifier( &server, [](const QHttpServerRequest &request) { if (request.url().path() == "/allowed"_L1) return QHttpServerWebSocketUpgradeResponse::accept(); else return QHttpServerWebSocketUpgradeResponse::passToNext(); });
该函数在 Qt 6.8 引入。
另请参阅 QHttpServerRequest , QHttpServerWebSocketUpgradeResponse , hasPendingWebSocketConnections (), nextPendingWebSocketConnection (), newWebSocketConnection (),和 missingHandler ().
Bind the HTTP server to given QLocalServer server over which the transmission happens. It is possible to call this function multiple times with different instances of server to handle multiple 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
QLocalServer::listen
() on the
server
before calling this function. If
server
is not listening, nothing will happen and
false
将被返回。
若 server is nullptr false is returned.
If successful the
server
will be parented to this HTTP server and
true
被返回。
另请参阅 QLocalServer and QLocalServer::listen ().
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
before calling this function. If
server
is not listening, nothing will happen and
false
将被返回。
If successful the
server
will be parented to this HTTP server and
true
被返回。
To allow usage of HTTP 2, bind to a
QSslServer
where
QSslConfiguration::setAllowedNextProtocols
() has been called with the arguments
{ QSslConfiguration::ALPNProtocolHTTP2 }
.
另请参阅 QTcpServer , QTcpServer::listen (),和 QSslConfiguration::setAllowedNextProtocols ().
[pure virtual protected]
bool
QAbstractHttpServer::
handleRequest
(const
QHttpServerRequest
&
request
,
QHttpServerResponder
&
responder
)
Override 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 (),和 addWebSocketUpgradeVerifier ().
[since 6.8]
QHttp2Configuration
QAbstractHttpServer::
http2Configuration
() const
Returns server's HTTP/2 configuration parameters.
该函数在 Qt 6.8 引入。
另请参阅 setHttp2Configuration ().
Returns the local servers of this HTTP server.
另请参阅 serverPorts ().
[pure virtual protected]
void
QAbstractHttpServer::
missingHandler
(const
QHttpServerRequest
&
request
,
QHttpServerResponder
&
responder
)
Override this function to handle each incoming
request
that was not handled by
handleRequest
(). This function is called whenever
handleRequest
() 返回
false
, or if there is a WebSocket upgrade attempt and either there are no connections to
newWebSocketConnection
() or there are no matching WebSocket verifiers. The
request
and
responder
parameters are the same as
handleRequest
() was called with.
另请参阅 handleRequest () 和 addWebSocketUpgradeVerifier ().
[signal]
void
QAbstractHttpServer::
newWebSocketConnection
()
This signal is emitted every time a new WebSocket connection is available.
另请参阅 hasPendingWebSocketConnections (), nextPendingWebSocketConnection (),和 addWebSocketUpgradeVerifier ().
返回下一待决连接作为连接的
QWebSocket
对象。
nullptr
被返回若在没有待决连接时调用此函数。
注意: 返回的 QWebSocket 对象不可以用于其它线程。
另请参阅 newWebSocketConnection (), hasPendingWebSocketConnections (),和 addWebSocketUpgradeVerifier ().
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 the TCP servers of this HTTP server.
另请参阅 serverPorts ().
[since 6.8]
void
QAbstractHttpServer::
setHttp2Configuration
(const
QHttp2Configuration
&
configuration
)
Sets server's HTTP/2 configuration parameters.
The next HTTP/2 connection will use the given configuration .
该函数在 Qt 6.8 引入。
另请参阅 http2Configuration ().