| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qhstspolicy.h" |
| 5 | |
| 6 | #include <QtCore/qdatetime.h> |
| 7 | #include <QtCore/qstring.h> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | /*! |
| 12 | \class QHstsPolicy |
| 13 | \brief The QHstsPolicy class specifies that a host supports HTTP Strict Transport |
| 14 | Security policy (HSTS). |
| 15 | \since 5.9 |
| 16 | \ingroup network |
| 17 | \inmodule QtNetwork |
| 18 | |
| 19 | HSTS policy defines a period of time during which QNetworkAccessManager |
| 20 | should only access a host in a secure fashion. HSTS policy is defined by |
| 21 | RFC6797. |
| 22 | |
| 23 | You can set expiry time and host name for this policy, and control whether it |
| 24 | applies to subdomains, either in the constructor or by calling \l setExpiry(), |
| 25 | \l setHost() and \l setIncludesSubDomains(). |
| 26 | |
| 27 | \sa QNetworkAccessManager::setStrictTransportSecurityEnabled() |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | \enum QHstsPolicy::PolicyFlag |
| 32 | |
| 33 | Specifies attributes that a policy can have. |
| 34 | |
| 35 | \value IncludeSubDomains HSTS policy also applies to subdomains. |
| 36 | */ |
| 37 | |
| 38 | class QHstsPolicyPrivate : public QSharedData |
| 39 | { |
| 40 | public: |
| 41 | QUrl url; |
| 42 | QDateTime expiry; |
| 43 | bool includeSubDomains = false; |
| 44 | |
| 45 | bool operator == (const QHstsPolicyPrivate &other) const |
| 46 | { |
| 47 | return url.host() == other.url.host() && expiry == other.expiry |
| 48 | && includeSubDomains == other.includeSubDomains; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | /*! |
| 53 | \fn bool QHstsPolicy::operator==(const QHstsPolicy &lhs, const QHstsPolicy &rhs) |
| 54 | |
| 55 | Returns \c true if the two policies \a lhs and \a rhs have the same host and |
| 56 | expiration date while agreeing on whether to include or exclude subdomains. |
| 57 | */ |
| 58 | |
| 59 | /*! |
| 60 | \fn bool QHstsPolicy::operator!=(const QHstsPolicy &lhs, const QHstsPolicy &rhs) |
| 61 | |
| 62 | Returns \c true if the two policies \a lhs and \a rhs do not have the same host |
| 63 | or expiration date, or do not agree on whether to include or exclude subdomains. |
| 64 | */ |
| 65 | |
| 66 | /*! |
| 67 | \internal |
| 68 | */ |
| 69 | bool QHstsPolicy::isEqual(const QHstsPolicy &other) const |
| 70 | { |
| 71 | return *d == *other.d; |
| 72 | } |
| 73 | |
| 74 | /*! |
| 75 | Constructs an invalid (expired) policy with empty host name and subdomains |
| 76 | not included. |
| 77 | */ |
| 78 | QHstsPolicy::QHstsPolicy() : d(new QHstsPolicyPrivate) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | /*! |
| 83 | \enum QHstsPolicy::PolicyFlag |
| 84 | |
| 85 | \value IncludeSubDomains Indicates whether a policy must include subdomains |
| 86 | */ |
| 87 | |
| 88 | /*! |
| 89 | Constructs QHstsPolicy with \a expiry (in UTC); \a flags is a value indicating |
| 90 | whether this policy must also include subdomains, \a host data is interpreted |
| 91 | according to \a mode. |
| 92 | |
| 93 | \sa QUrl::setHost(), QUrl::ParsingMode, QHstsPolicy::PolicyFlag |
| 94 | */ |
| 95 | QHstsPolicy::QHstsPolicy(const QDateTime &expiry, PolicyFlags flags, |
| 96 | const QString &host, QUrl::ParsingMode mode) |
| 97 | : d(new QHstsPolicyPrivate) |
| 98 | { |
| 99 | d->url.setHost(host, mode); |
| 100 | d->expiry = expiry; |
| 101 | d->includeSubDomains = flags.testFlag(flag: IncludeSubDomains); |
| 102 | } |
| 103 | |
| 104 | /*! |
| 105 | Creates a copy of \a other object. |
| 106 | */ |
| 107 | QHstsPolicy::QHstsPolicy(const QHstsPolicy &other) |
| 108 | : d(new QHstsPolicyPrivate(*other.d)) |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | /*! |
| 113 | Destructor. |
| 114 | */ |
| 115 | QHstsPolicy::~QHstsPolicy() |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | /*! |
| 120 | Copy-assignment operator, makes a copy of \a other. |
| 121 | */ |
| 122 | QHstsPolicy &QHstsPolicy::operator=(const QHstsPolicy &other) |
| 123 | { |
| 124 | d = other.d; |
| 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | /*! |
| 129 | Sets a host, \a host data is interpreted according to \a mode parameter. |
| 130 | |
| 131 | \sa host(), QUrl::setHost(), QUrl::ParsingMode |
| 132 | */ |
| 133 | void QHstsPolicy::setHost(const QString &host, QUrl::ParsingMode mode) |
| 134 | { |
| 135 | d->url.setHost(host, mode); |
| 136 | } |
| 137 | |
| 138 | /*! |
| 139 | Returns a host for a given policy, formatted according to \a options. |
| 140 | |
| 141 | \sa setHost(), QUrl::host(), QUrl::ComponentFormattingOptions |
| 142 | */ |
| 143 | QString QHstsPolicy::host(QUrl::ComponentFormattingOptions options) const |
| 144 | { |
| 145 | return d->url.host(options); |
| 146 | } |
| 147 | |
| 148 | /*! |
| 149 | Sets the expiration date for the policy (in UTC) to \a expiry. |
| 150 | |
| 151 | \sa expiry() |
| 152 | */ |
| 153 | void QHstsPolicy::setExpiry(const QDateTime &expiry) |
| 154 | { |
| 155 | d->expiry = expiry; |
| 156 | } |
| 157 | |
| 158 | /*! |
| 159 | Returns the expiration date for the policy (in UTC). |
| 160 | |
| 161 | \sa setExpiry() |
| 162 | */ |
| 163 | QDateTime QHstsPolicy::expiry() const |
| 164 | { |
| 165 | return d->expiry; |
| 166 | } |
| 167 | |
| 168 | /*! |
| 169 | Sets whether subdomains are included for this policy to \a include. |
| 170 | |
| 171 | \sa includesSubDomains() |
| 172 | */ |
| 173 | void QHstsPolicy::setIncludesSubDomains(bool include) |
| 174 | { |
| 175 | d->includeSubDomains = include; |
| 176 | } |
| 177 | |
| 178 | /*! |
| 179 | Returns \c true if this policy also includes subdomains. |
| 180 | |
| 181 | \sa setIncludesSubDomains() |
| 182 | */ |
| 183 | bool QHstsPolicy::includesSubDomains() const |
| 184 | { |
| 185 | return d->includeSubDomains; |
| 186 | } |
| 187 | |
| 188 | /*! |
| 189 | Return \c true if this policy has a valid expiration date and this date |
| 190 | is greater than QDateTime::currentGetDateTimeUtc(). |
| 191 | |
| 192 | \sa setExpiry(), expiry() |
| 193 | */ |
| 194 | bool QHstsPolicy::isExpired() const |
| 195 | { |
| 196 | return !d->expiry.isValid() || d->expiry <= QDateTime::currentDateTimeUtc(); |
| 197 | } |
| 198 | |
| 199 | /*! |
| 200 | \fn void QHstsPolicy::swap(QHstsPolicy &other) |
| 201 | \memberswap{policy} |
| 202 | */ |
| 203 | |
| 204 | QT_END_NAMESPACE |
| 205 | |