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 QHttpHeaders;
35
36class Q_AUTOTEST_EXPORT QHstsCache
37{
38public:
39
40 void updateFromHeaders(const QHttpHeaders &headers,
41 const QUrl &url);
42 void updateFromPolicies(const QList<QHstsPolicy> &hosts);
43 void updateKnownHost(const QUrl &url, const QDateTime &expires,
44 bool includeSubDomains);
45 bool isKnownHost(const QUrl &url) const;
46 void clear();
47
48 QList<QHstsPolicy> policies() const;
49
50#if QT_CONFIG(settings)
51 void setStore(class QHstsStore *store);
52#endif // QT_CONFIG(settings)
53
54private:
55
56 void updateKnownHost(const QString &hostName, const QDateTime &expires,
57 bool includeSubDomains);
58
59 struct HostName
60 {
61 explicit HostName(const QString &n) : name(n) { }
62 explicit HostName(QStringView r) : fragment(r) { }
63
64 bool operator < (const HostName &rhs) const
65 {
66 if (fragment.size()) {
67 if (rhs.fragment.size())
68 return fragment < rhs.fragment;
69 return fragment < QStringView{rhs.name};
70 }
71
72 if (rhs.fragment.size())
73 return QStringView{name} < rhs.fragment;
74 return name < rhs.name;
75 }
76
77 // We use 'name' for a HostName object contained in our dictionary;
78 // we use 'fragment' only during lookup, when chopping the complete host
79 // name, removing subdomain names (such HostName object is 'transient', it
80 // must not outlive the original QString object.
81 QString name;
82 QStringView fragment;
83 };
84
85 mutable std::map<HostName, QHstsPolicy> knownHosts;
86#if QT_CONFIG(settings)
87 QHstsStore *hstsStore = nullptr;
88#endif // QT_CONFIG(settings)
89};
90
91class Q_AUTOTEST_EXPORT QHstsHeaderParser
92{
93public:
94
95 bool parse(const QHttpHeaders &headers);
96
97 QDateTime expirationDate() const { return expiry; }
98 bool includeSubDomains() const { return subDomainsFound; }
99
100private:
101
102 bool parseSTSHeader();
103 bool parseDirective();
104 bool processDirective(const QByteArray &name, const QByteArray &value);
105 bool nextToken();
106
107 QByteArray header;
108 QByteArray token;
109
110 QDateTime expiry;
111 int tokenPos = 0;
112 bool maxAgeFound = false;
113 qint64 maxAge = 0;
114 bool subDomainsFound = false;
115};
116
117QT_END_NAMESPACE
118
119#endif
120

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

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