aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/urlinterceptor.cpp
blob: 490dea665b7c14ffa357f9bab8292104093f078a (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
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "urlinterceptor.h"
#include "webprofile.h"
#include "urlfilter.h"

// test DNT on https://browserleaks.com/donottrack

UrlRequestInterceptor::UrlRequestInterceptor(WebProfile* profile, QObject* parent)
    : QWebEngineUrlRequestInterceptor(parent)
{
    Q_CHECK_PTR(profile);
    m_profile = profile;
}

void UrlRequestInterceptor::addHttpHeader(const QByteArray &key, const QByteArray &value)
{
    headers.append(qMakePair(key, value));
}

void UrlRequestInterceptor::addFilter(UrlFilter *filter)
{
    if(filter != nullptr)
        filters.append(filter);
}
void UrlRequestInterceptor::removeFilter(UrlFilter *filter)
{
    if(filter != nullptr)
        filters.removeOne(filter);
}

void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
    for(const auto *filter : filters) {
        const auto match = filter->match(info.firstPartyUrl(), info.requestUrl(), info.resourceType());

        // skip if no match
        if(match.first == UrlFilter::NotMatched)
            continue;

        else {
            if(match.first == UrlFilter::Allow)
                info.block(false);
            else if(match.first == UrlFilter::Block)
                info.block(true);
            else if(match.first == UrlFilter::Redirect)
                info.redirect(QUrl::fromUserInput(match.second));
            // we found a match, skip the rest
            break;
        }
    }

    // set headers
    for(const auto &header : headers) {
        info.setHttpHeader(header.first, header.second);
    }
    for(auto i = m_profile->headers().constBegin(); i != m_profile->headers().constEnd(); ++i) {
        info.setHttpHeader(i.key(), i.value());
    }
}