1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QHTTPSERVERREQUESTFILTER_P_H
6#define QHTTPSERVERREQUESTFILTER_P_H
7
8#include <QtHttpServer/qthttpserverglobal.h>
9#include <QtHttpServer/qhttpserverconfiguration.h>
10
11#include <QtCore/qhash.h>
12#include <QtNetwork/qhostaddress.h>
13
14//
15// W A R N I N G
16// -------------
17//
18// This file is not part of the Qt API. It exists for the convenience
19// of QHttpServer. This header file may change from version to
20// version without notice, or even be removed.
21//
22// We mean it.
23
24QT_BEGIN_NAMESPACE
25
26namespace QHttpServerRequestFilterPrivate {
27extern const Q_HTTPSERVER_EXPORT int cPeriodDurationMSec;
28}
29
30class QHttpServerRequestFilter
31{
32public:
33 // Rule Of Zero applies!
34
35 Q_HTTPSERVER_EXPORT void setConfiguration(const QHttpServerConfiguration &config);
36
37 Q_HTTPSERVER_EXPORT bool isRequestAllowed(const QHostAddress &peerAddress) const;
38
39 Q_HTTPSERVER_EXPORT bool isRequestWithinRate(const QHostAddress &peerAddress);
40 Q_HTTPSERVER_EXPORT bool isRequestWithinRate(const QHostAddress &peerAddress, qint64 currTimeMSec);
41
42private:
43 struct IpInfo
44 {
45 IpInfo() : m_thisPeriodEnd(0) {}
46 IpInfo(qint64 thisPeriodEnd) : m_thisPeriodEnd(thisPeriodEnd) {}
47
48 bool isGarbage(qint64 currTime) const;
49
50 qint64 m_thisPeriodEnd = 0;
51 unsigned m_nRequests = 0;
52 };
53
54 void cleanIpInfoGarbage(QHash<QHostAddress, IpInfo>::iterator it, qint64 currTime);
55
56 unsigned maxRequestPerPeriod() const;
57
58 QHttpServerConfiguration m_config;
59 QHash<QHostAddress, IpInfo> ipInfo;
60};
61
62QT_END_NAMESPACE
63
64#endif // QHTTPSERVERREQUESTFILTER_P_H
65

source code of qthttpserver/src/httpserver/qhttpserverrequestfilter_p.h