aboutsummaryrefslogtreecommitdiff
path: root/lib/web/urlfilter/filterrule.cpp
blob: 73b357b11804b264fd5ab1a2b39e459bfd1286dd (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "filterrule.h"
#include <QWebEngineUrlRequestInfo>
#include <QJsonArray>

void parseJson(std::unique_ptr<FilterRule> &rule, const QJsonObject &filter)
{
    // set action
    {
        if(!filter.value("whitelist").isUndefined()) {
            rule->setActionType(FilterRule::Whitelist);

        } else if(!filter.value("blacklist").isUndefined()) {
            rule->setActionType(FilterRule::Blacklist);

        } else if(!filter.value("redirect").isUndefined()) {
            rule->setActionType(FilterRule::Redirect);
            rule->setRedirectUrl(QUrl::fromUserInput(filter.value("redirect").toString()));

        } else if(!filter.value("setheader").isUndefined()) {
            rule->setActionType(FilterRule::SetHeader);
            for(const QJsonValue v : filter.value("setheader").toArray()) {
                QStringList h = v.toString().split(':');
                rule->addHeaderRule(h.at(0).toLatin1(), h.at(1).toLatin1());
            }
        }

    }

    // set match type
    {
        const QJsonValue regexpValue = filter.value("regexp");
        const QJsonValue endswithValue = filter.value("endswith");
        const QJsonValue containsValue = filter.value("contains");

        if(!regexpValue.isUndefined()) {
            rule->setMatchType(FilterRule::RegExpMatchRule, regexpValue.toString());

        } else if(!endswithValue.isUndefined()) {
            rule->setMatchType(FilterRule::StringEndsMatchRule, endswithValue.toString());

        } else if(!containsValue.isUndefined()) {
            rule->setMatchType(FilterRule::StringContainsMatchRule, containsValue.toString());

        } else
            rule->setMatchType(FilterRule::MatchAllUrlsRule);
    }

}

FilterRule::FilterRule(const QJsonObject &filter)
{
    m_matcher.setCaseSensitivity(Qt::CaseInsensitive);
}

void FilterRule::setActionType(ActionType type)
{
    m_actionType = type;
}

void FilterRule::setMatchType(MatchType type, const QString &pattern)
{
    m_matchType = type;
    switch (type) {
    case RegExpMatchRule:
        m_regexp.setPattern(pattern);
        break;
    case StringEndsMatchRule:
        m_pattern = pattern;
        break;
    case StringContainsMatchRule:
        m_matcher.setPattern(pattern);
    default:
        break;
    }
}

void FilterRule::setRedirectUrl(const QUrl &url)
{
    m_redirectUrl = url;
}

void FilterRule::addHeaderRule(const QByteArray &header, const QByteArray &value)
{
    m_headers.insert(header, value);
}

bool FilterRule::isValid() const
{
    return (m_matchType != MatchType::InvalidMatch) && (m_actionType != ActionType::InvalidAction);
}

bool FilterRule::process(QWebEngineUrlRequestInfo &info) const
{
    Q_ASSERT(m_actionType != ActionType::InvalidAction);

    if(matchRequestUrl(info.requestUrl().toString(), info.resourceType())) {
        switch (m_actionType) {
        case ActionType::Whitelist:
            info.block(false);
            return true;
        case ActionType::Blacklist:
            info.block(true);
            return true;
        case ActionType::Redirect:
            info.redirect(m_redirectUrl);
            return true;
        case ActionType::SetHeader:
            for(auto it = m_headers.constBegin(); it != m_headers.constEnd(); ++it) {
                info.setHttpHeader(it.key(), it.value());
            }
            return true;
        case ActionType::InvalidAction:
            break;
        }
    }

    return false;
}

bool FilterRule::matchRequestUrl(const QString &requestUrl, const QWebEngineUrlRequestInfo::ResourceType type) const
{
    Q_ASSERT(m_matchType != MatchType::InvalidMatch);

    if(!m_resourceTypeOptions.isEmpty() && !m_resourceTypeOptions.contains(type))
        return false;

    switch (m_matchType) {
    case MatchType::RegExpMatchRule:
        return m_regexp.match(requestUrl).hasMatch();
    case MatchType::StringEndsMatchRule:
        return requestUrl.endsWith(m_pattern);
    case MatchType::StringContainsMatchRule:
        return m_matcher.indexIn(requestUrl) != -1;
    case MatchType::MatchAllUrlsRule:
        return true;
    default:
        return false;
    }
}