QWebSocketCorsAuthenticator Class
The QWebSocketCorsAuthenticator class decides whether a WebSocket client's origin is accepted. More…
| Header | #include <QWebSocketCorsAuthenticator> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::WebSockets) |
| Module | Nt WebSockets C++ Classes |
| Since | 5.3 |
List of all members, including inherited members
Public Functions
| QWebSocketCorsAuthenticator(const QString &origin) | |
| QWebSocketCorsAuthenticator(const QWebSocketCorsAuthenticator &other) | |
| QWebSocketCorsAuthenticator(QWebSocketCorsAuthenticator &&other) | |
| QWebSocketCorsAuthenticator & | operator=(const QWebSocketCorsAuthenticator &other) |
| QWebSocketCorsAuthenticator & | operator=(QWebSocketCorsAuthenticator &&other) |
| ~QWebSocketCorsAuthenticator() | |
| void | swap(QWebSocketCorsAuthenticator &other) |
| QString | origin() const |
| void | setAllowed(bool allowed) |
| bool | allowed() const |
Detailed Description
QWebSocketServer::originAuthenticationRequired() passes one to its slots during each opening handshake. Browsers put the origin of the page that opens the connection in the Origin header, so a server that only serves its own pages can refuse the others:
// Browsers send the origin of the page; refuse pages from other sites.
QObject::connect(&server, &QWebSocketServer::originAuthenticationRequired,
[](QWebSocketCorsAuthenticator *authenticator) {
const QString origin = authenticator->origin();
authenticator->setAllowed(origin.isEmpty() || origin.startsWith("http://127.0.0.1") ||
origin.startsWith("http://localhost"));
});
See also QWebSocketServer::originAuthenticationRequired().
Differences from Qt
None.
Member Function Documentation
QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QString &origin)
Constructs an authenticator for origin. The origin is allowed until setAllowed(false) is called.
QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QWebSocketCorsAuthenticator &other)
Constructs a copy of other.
QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(QWebSocketCorsAuthenticator &&other)
Move-constructs an authenticator from other.
QWebSocketCorsAuthenticator::~QWebSocketCorsAuthenticator()
Destroys the authenticator.
bool QWebSocketCorsAuthenticator::allowed() const
Returns true if the origin is accepted; otherwise returns false.
See also setAllowed().
QWebSocketCorsAuthenticator & QWebSocketCorsAuthenticator::operator=(const QWebSocketCorsAuthenticator &other)
Assigns other to this authenticator and returns a reference to it.
QWebSocketCorsAuthenticator & QWebSocketCorsAuthenticator::operator=(QWebSocketCorsAuthenticator &&other)
This is an overloaded function.
Move-assigns other to this authenticator and returns a reference to it.
QString QWebSocketCorsAuthenticator::origin() const
Returns the origin the client sent, or an empty string if it sent none.
void QWebSocketCorsAuthenticator::setAllowed(bool allowed)
Sets whether the origin is accepted to allowed.
See also allowed().
void QWebSocketCorsAuthenticator::swap(QWebSocketCorsAuthenticator &other)
Swaps this authenticator with other.