ec2537cd4d0960118b91be0d0b767dbcd891e7b2
[contrib/qtwebsockets.git] / src / websockets / qwebsocketcorsauthenticator.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL21$
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.
16 **
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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** In addition, as a special exception, Digia gives you certain additional
27 ** rights. These rights are described in the Digia Qt LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** $QT_END_LICENSE$
31 **
32 ****************************************************************************/
33
34 /*!
35     \class QWebSocketCorsAuthenticator
36
37     \inmodule QtWebSockets
38     \brief The QWebSocketCorsAuthenticator class provides an authenticator object for
39     Cross Origin Requests (CORS).
40
41     The QWebSocketCorsAuthenticator class is used in the
42     \l{QWebSocketServer::}{originAuthenticationRequired()} signal.
43     The class provides a way to pass back the required information to the QWebSocketServer.
44     It provides applications with fine-grained control over which origin URLs are allowed
45     and which aren't.
46     By default, every origin is accepted.
47     To get fine-grained control, an application connects the
48     \l{QWebSocketServer::}{originAuthenticationRequired()} signal to a slot.
49     When the origin (QWebSocketCorsAuthenticator::origin()) is accepted,
50     it calls QWebSocketCorsAuthenticator::setAllowed(true)
51
52     \note Checking on the origin does not make much sense when the server is accessed
53     via a non-browser client, as that client can set whatever origin header it likes.
54     In case of a browser client, the server SHOULD check the validity of the origin.
55     \sa http://tools.ietf.org/html/rfc6455#section-10
56
57     \sa QWebSocketServer
58 */
59
60 #include "qwebsocketcorsauthenticator.h"
61 #include "qwebsocketcorsauthenticator_p.h"
62
63 QT_BEGIN_NAMESPACE
64
65 /*!
66   \internal
67  */
68 QWebSocketCorsAuthenticatorPrivate::QWebSocketCorsAuthenticatorPrivate(const QString &origin,
69                                                                        bool allowed) :
70     m_origin(origin),
71     m_isAllowed(allowed)
72 {}
73
74 /*!
75   Destroys the object.
76  */
77 QWebSocketCorsAuthenticatorPrivate::~QWebSocketCorsAuthenticatorPrivate()
78 {}
79
80 /*!
81   Constructs a new QCorsAuthencator object with the given \a origin.
82   \note By default, allowed() returns true. This means that per default every origin is accepted.
83  */
84 QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QString &origin) :
85     d_ptr(new QWebSocketCorsAuthenticatorPrivate(origin, true))
86 {
87 }
88
89 /*!
90   Destroys the object.
91  */
92 QWebSocketCorsAuthenticator::~QWebSocketCorsAuthenticator()
93 {
94 }
95
96 /*!
97   Constructs a copy of \a other.
98  */
99 QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QWebSocketCorsAuthenticator &other) :
100     d_ptr(new QWebSocketCorsAuthenticatorPrivate(other.d_ptr->m_origin, other.d_ptr->m_isAllowed))
101 {
102 }
103
104 /*!
105   Assigns \a other to this authenticator object.
106  */
107 QWebSocketCorsAuthenticator &
108 QWebSocketCorsAuthenticator::operator =(const QWebSocketCorsAuthenticator &other)
109 {
110     Q_D(QWebSocketCorsAuthenticator);
111     if (this != &other) {
112         d->m_origin = other.d_ptr->m_origin;
113         d->m_isAllowed = other.d_ptr->m_isAllowed;
114     }
115     return *this;
116 }
117
118 #ifdef Q_COMPILER_RVALUE_REFS
119 /*!
120   Move-constructs a QWebSocketCorsAuthenticator, making it point at the same
121   object \a other was pointing to.
122  */
123 QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(QWebSocketCorsAuthenticator &&other) :
124     d_ptr(other.d_ptr.take())
125 {}
126
127 /*!
128   Move-assigns \a other to this instance.
129  */
130 QWebSocketCorsAuthenticator &
131 QWebSocketCorsAuthenticator::operator =(QWebSocketCorsAuthenticator &&other)
132 {
133     qSwap(d_ptr, other.d_ptr);
134     return *this;
135 }
136
137 #endif
138
139 /*!
140   Swaps \a other with this authenticator.
141
142   This operation is very fast and never fails.
143  */
144 void QWebSocketCorsAuthenticator::swap(QWebSocketCorsAuthenticator &other)
145 {
146     if (&other != this)
147         qSwap(d_ptr, other.d_ptr);
148 }
149
150 /*!
151   Returns the origin this autenticator is handling about.
152  */
153 QString QWebSocketCorsAuthenticator::origin() const
154 {
155     Q_D(const QWebSocketCorsAuthenticator);
156     return d->m_origin;
157 }
158
159 /*!
160   Allows or disallows the origin. Setting \a allowed to true, will accept the connection request
161   for the given origin.
162
163   Setting \a allowed to false, will reject the connection request.
164
165   \note By default, all origins are accepted.
166  */
167 void QWebSocketCorsAuthenticator::setAllowed(bool allowed)
168 {
169     Q_D(QWebSocketCorsAuthenticator);
170     d->m_isAllowed = allowed;
171 }
172
173 /*!
174   Returns true if the origin is allowed, otherwise returns false.
175
176   \note By default, all origins are accepted.
177  */
178 bool QWebSocketCorsAuthenticator::allowed() const
179 {
180     Q_D(const QWebSocketCorsAuthenticator);
181     return d->m_isAllowed;
182 }
183
184 QT_END_NAMESPACE