aboutsummaryrefslogtreecommitdiff
path: root/lib/web/urlfilter/filterrule.h
blob: f20ab15be311f9876c93bccc479abb2f570406bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef SMOLBOTE_FILTERRULE_H
#define SMOLBOTE_FILTERRULE_H

#include <QObject>
#include <QRegularExpression>
#include <QStringList>
#include <QStringMatcher>
#include <QJsonObject>
#include <QUrl>
#include <QWebEngineUrlRequestInfo>
#include <memory>

class QUrl;
class FilterRule
{
public:
    enum ActionType {
        Whitelist,
        Blacklist,
        Redirect,
        SetHeader,
        InvalidAction
    };

    enum MatchType {
//        CssRule = 0,                    //
//        DomainMatchRule = 1,            //
        RegExpMatchRule = 2,            // match request url with regexp
        StringEndsMatchRule = 3,        // request url ends with string
        StringContainsMatchRule = 4,    // request url contains string
        MatchAllUrlsRule = 5,           //
        InvalidMatch = 6,
    };

    FilterRule(const QJsonObject &filter);
    ~FilterRule() = default;

    void setActionType(ActionType type);
    void setMatchType(MatchType type, const QString &pattern = QString());
    void setRedirectUrl(const QUrl &url);
    void addHeaderRule(const QByteArray &header, const QByteArray &value);

    bool isValid() const;
    bool process(QWebEngineUrlRequestInfo &info) const;
    bool matchRequestUrl(const QString &requestUrl, const QWebEngineUrlRequestInfo::ResourceType type) const;

private:
    Q_DISABLE_COPY(FilterRule)


    ActionType m_actionType = ActionType::InvalidAction;
    MatchType m_matchType = MatchType::InvalidMatch;

    QHash<QWebEngineUrlRequestInfo::ResourceType, bool> m_resourceTypeOptions;
    QHash<QByteArray, QByteArray> m_headers;

    // Parsed rule for string matching (CSS Selector for CSS rules)
    QString m_matchString;
    // Case sensitivity for string matching
    Qt::CaseSensitivity m_caseSensitivity = Qt::CaseInsensitive;

    QUrl m_redirectUrl;
    QRegularExpression m_regexp;
    QStringMatcher m_matcher;
    QString m_pattern;
};

void parseJson(std::unique_ptr<FilterRule> &rule, const QJsonObject &filter);

#endif // SMOLBOTE_FILTERRULE_H