f43447fc0aea6c66959112101e0ee6e923d59a49
[contrib/qtwebsockets.git] / src / websockets / qwebsocketcorsauthenticator.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
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: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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 /*!
43     \class QWebSocketCorsAuthenticator
44
45     \inmodule QtWebSockets
46     \brief The QWebSocketCorsAuthenticator class provides an authenticator object for Cross Origin Requests (CORS).
47
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)
54
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
59
60     \sa QWebSocketServer
61 */
62
63 #include "qwebsocketcorsauthenticator.h"
64 #include "qwebsocketcorsauthenticator_p.h"
65
66 QT_BEGIN_NAMESPACE
67
68 /*!
69   \internal
70  */
71 QWebSocketCorsAuthenticatorPrivate::QWebSocketCorsAuthenticatorPrivate(const QString &origin, bool allowed) :
72     m_origin(origin),
73     m_isAllowed(allowed)
74 {}
75
76 /*!
77   \internal
78  */
79 QWebSocketCorsAuthenticatorPrivate::~QWebSocketCorsAuthenticatorPrivate()
80 {}
81
82 /*!
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.
85  */
86 QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QString &origin) :
87     d_ptr(new QWebSocketCorsAuthenticatorPrivate(origin, true))  //all origins are per default allowed
88 {
89 }
90
91 /*!
92   Destructs the object
93  */
94 QWebSocketCorsAuthenticator::~QWebSocketCorsAuthenticator()
95 {
96 }
97
98 /*!
99   Constructs a coy of \a other
100  */
101 QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QWebSocketCorsAuthenticator &other) :
102     d_ptr(new QWebSocketCorsAuthenticatorPrivate(other.d_ptr->m_origin, other.d_ptr->m_isAllowed))
103 {
104 }
105
106 /*!
107   Assigns \a other to this authenticator object
108  */
109 QWebSocketCorsAuthenticator &QWebSocketCorsAuthenticator::operator =(const QWebSocketCorsAuthenticator &other)
110 {
111     Q_D(QWebSocketCorsAuthenticator);
112     if (this != &other)
113     {
114         d->m_origin = other.d_ptr->m_origin;
115         d->m_isAllowed = other.d_ptr->m_isAllowed;
116     }
117     return *this;
118 }
119
120 #ifdef Q_COMPILER_RVALUE_REFS
121 QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(QWebSocketCorsAuthenticator &&other) :
122     d_ptr(other.d_ptr.take())
123 {}
124
125 QWebSocketCorsAuthenticator &QWebSocketCorsAuthenticator::operator =(QWebSocketCorsAuthenticator &&other)
126 {
127     qSwap(d_ptr, other.d_ptr);
128     return *this;
129 }
130
131 #endif
132
133 void QWebSocketCorsAuthenticator::swap(QWebSocketCorsAuthenticator &other)
134 {
135     if (&other != this) {
136         qSwap(d_ptr, other.d_ptr);
137     }
138 }
139
140 /*!
141   Returns the origin this autenticator is handling about.
142  */
143 QString QWebSocketCorsAuthenticator::origin() const
144 {
145     Q_D(const QWebSocketCorsAuthenticator);
146     return d->m_origin;
147 }
148
149 /*!
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.
152
153   \note By default, all origins are accepted.
154  */
155 void QWebSocketCorsAuthenticator::setAllowed(bool allowed)
156 {
157     Q_D(QWebSocketCorsAuthenticator);
158     d->m_isAllowed = allowed;
159 }
160
161 /*!
162   Returns true if the origin is allowed, otherwise returns false.
163
164   \note By default, all origins are accepted.
165  */
166 bool QWebSocketCorsAuthenticator::allowed() const
167 {
168     Q_D(const QWebSocketCorsAuthenticator);
169     return d->m_isAllowed;
170 }
171
172 QT_END_NAMESPACE