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#ifndef QHSTS_P_H
5#define QHSTS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of the Network Access API. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtNetwork/private/qtnetworkglobal_p.h>
19
20#include <QtNetwork/qhstspolicy.h>
21
22#include <QtCore/qbytearray.h>
23#include <QtCore/qdatetime.h>
24#include <QtCore/qstring.h>
25#include <QtCore/qglobal.h>
26#include <QtCore/qpair.h>
27#include <QtCore/qurl.h>
28#include <QtCore/qcontainerfwd.h>
29
30#include <map>
31
32QT_BEGIN_NAMESPACE
33
34class Q_AUTOTEST_EXPORT QHstsCache
35{
36public:
37
38 void updateFromHeaders(const QList<QPair<QByteArray, QByteArray>> &headers,
39 const QUrl &url);
40 void updateFromPolicies(const QList<QHstsPolicy> &hosts);
41 void updateKnownHost(const QUrl &url, const QDateTime &expires,
42 bool includeSubDomains);
43 bool isKnownHost(const QUrl &url) const;
44 void clear();
45
46 QList<QHstsPolicy> policies() const;
47
48#if QT_CONFIG(settings)
49 void setStore(class QHstsStore *store);
50#endif // QT_CONFIG(settings)
51
52private:
53
54 void updateKnownHost(const QString &hostName, const QDateTime &expires,
55 bool includeSubDomains);
56
57 struct HostName
58 {
59 explicit HostName(const QString &n) : name(n) { }
60 explicit HostName(QStringView r) : fragment(r) { }
61
62 bool operator < (const HostName &rhs) const
63 {
64 if (fragment.size()) {
65 if (rhs.fragment.size())
66 return fragment < rhs.fragment;
67 return fragment < QStringView{rhs.name};
68 }
69
70 if (rhs.fragment.size())
71 return QStringView{name} < rhs.fragment;
72 return name < rhs.name;
73 }
74
75 // We use 'name' for a HostName object contained in our dictionary;
76 // we use 'fragment' only during lookup, when chopping the complete host
77 // name, removing subdomain names (such HostName object is 'transient', it
78 // must not outlive the original QString object.
79 QString name;
80 QStringView fragment;
81 };
82
83 mutable std::map<HostName, QHstsPolicy> knownHosts;
84#if QT_CONFIG(settings)
85 QHstsStore *hstsStore = nullptr;
86#endif // QT_CONFIG(settings)
87};
88
89class Q_AUTOTEST_EXPORT QHstsHeaderParser
90{
91public:
92
93 bool parse(const QList<QPair<QByteArray, QByteArray>> &headers);
94
95 QDateTime expirationDate() const { return expiry; }
96 bool includeSubDomains() const { return subDomainsFound; }
97
98private:
99
100 bool parseSTSHeader();
101 bool parseDirective();
102 bool processDirective(const QByteArray &name, const QByteArray &value);
103 bool nextToken();
104
105 QByteArray header;
106 QByteArray token;
107
108 QDateTime expiry;
109 int tokenPos = 0;
110 bool maxAgeFound = false;
111 qint64 maxAge = 0;
112 bool subDomainsFound = false;
113};
114
115QT_END_NAMESPACE
116
117#endif
118

source code of qtbase/src/network/access/qhsts_p.h