1 /****************************************************************************
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
43 \class QWebSocketCorsAuthenticator
45 \inmodule QtWebSockets
46 \brief The QWebSocketCorsAuthenticator class provides an authenticator object for Cross Origin Requests (CORS).
48 The QWebSocketCorsAuthenticator class is used in the \l{QWebSocketServer::}{originAuthenticationRequired()} signal.
49 The class provides a way to pass back the required information to the QWebSocketServer.
50 It provides applications with fine-grained control over which origin URLs are allowed and which aren't.
51 By default, every origin is accepted.
52 To get fine grained control, an application connects the \l{QWebSocketServer::}{originAuthenticationRequired()} signal to
53 a slot. When the origin (QWebSocketCorsAuthenticator::origin()) is accepted, it calls QWebSocketCorsAuthenticator::setAllowed(true)
55 \note Checking on the origin does not make much sense when the server is accessed
56 via a non-browser client, as that client can set whatever origin header it likes.
57 In case of a browser client, the server SHOULD check the validity of the origin.
58 \sa http://tools.ietf.org/html/rfc6455#section-10
63 #include "qwebsocketcorsauthenticator.h"
64 #include "qwebsocketcorsauthenticator_p.h"
71 QWebSocketCorsAuthenticatorPrivate::QWebSocketCorsAuthenticatorPrivate(const QString &origin, bool allowed) :
79 QWebSocketCorsAuthenticatorPrivate::~QWebSocketCorsAuthenticatorPrivate()
83 Constructs a new QCorsAuthencator object with the given \a origin.
84 \note By default, allowed() returns true. This means that per default every origin is accepted.
86 QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QString &origin) :
87 d_ptr(new QWebSocketCorsAuthenticatorPrivate(origin, true)) //all origins are per default allowed
94 QWebSocketCorsAuthenticator::~QWebSocketCorsAuthenticator()
99 Constructs a coy of \a other
101 QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QWebSocketCorsAuthenticator &other) :
102 d_ptr(new QWebSocketCorsAuthenticatorPrivate(other.d_ptr->m_origin, other.d_ptr->m_isAllowed))
107 Assigns \a other to this authenticator object
109 QWebSocketCorsAuthenticator &QWebSocketCorsAuthenticator::operator =(const QWebSocketCorsAuthenticator &other)
111 Q_D(QWebSocketCorsAuthenticator);
114 d->m_origin = other.d_ptr->m_origin;
115 d->m_isAllowed = other.d_ptr->m_isAllowed;
120 #ifdef Q_COMPILER_RVALUE_REFS
121 QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(QWebSocketCorsAuthenticator &&other) :
122 d_ptr(other.d_ptr.take())
125 QWebSocketCorsAuthenticator &QWebSocketCorsAuthenticator::operator =(QWebSocketCorsAuthenticator &&other)
127 qSwap(d_ptr, other.d_ptr);
133 void QWebSocketCorsAuthenticator::swap(QWebSocketCorsAuthenticator &other)
135 if (&other != this) {
136 qSwap(d_ptr, other.d_ptr);
141 Returns the origin this autenticator is handling about.
143 QString QWebSocketCorsAuthenticator::origin() const
145 Q_D(const QWebSocketCorsAuthenticator);
150 Allows or disallows the origin. Setting \a allowed to true, will accept the connection request for the given origin.
151 Setting \a allowed to false, will reject the connection request.
153 \note By default, all origins are accepted.
155 void QWebSocketCorsAuthenticator::setAllowed(bool allowed)
157 Q_D(QWebSocketCorsAuthenticator);
158 d->m_isAllowed = allowed;
162 Returns true if the origin is allowed, otherwise returns false.
164 \note By default, all origins are accepted.
166 bool QWebSocketCorsAuthenticator::allowed() const
168 Q_D(const QWebSocketCorsAuthenticator);
169 return d->m_isAllowed;