aboutsummaryrefslogtreecommitdiff
path: root/lib/urlfilter/adblock/parser.cpp
blob: d65a2e4032b8a04cb26a32f7bac8dc981aaa0cc9 (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
/*
 * 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 "parser.h"

std::optional<std::pair<QString, QString>> parseComment(QString &line)
{
    const QLatin1String separator(": ");
    if(line.contains(separator)) {
        const QStringList comment = line.mid(1).split(QLatin1String(": "));
        return std::make_pair(comment.at(0).trimmed(), comment.at(1).trimmed());
    } else
        return std::nullopt;
}

std::optional<std::pair<QWebEngineUrlRequestInfo::ResourceType, bool>> parseResourceOption(const QString &option)
{
    const bool exception = !option.startsWith(QLatin1Literal("~"));

    if(option.endsWith(QLatin1Literal("script"))) {
        //  external scripts loaded via HTML script tag
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeScript, exception);

    } else if(option.endsWith(QLatin1Literal("image"))) {
        // regular images, typically loaded via HTML img tag
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeImage, exception);

    } else if(option.endsWith(QLatin1Literal("stylesheet"))) {
        // external CSS stylesheet files
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeStylesheet, exception);

    } else if(option.endsWith(QLatin1Literal("object"))) {
        // content handled by browser plugins, e.g. Flash or Java
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeObject, exception);

    } else if(option.endsWith(QLatin1Literal("xmlhttprequest"))) {
        //  requests started using the XMLHttpRequest object or fetch() API
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeXhr, exception);

    } else if(option.endsWith(QLatin1Literal("object-subrequest"))) {
        // requests started by plugins like Flash
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypePluginResource, exception);

    } else if(option.endsWith(QLatin1Literal("subdocument"))) {
        // embedded pages, usually included via HTML frames
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeSubFrame, exception);

    } else if(option.endsWith(QLatin1Literal("ping"))) {
        // requests started by <a ping> or navigator.sendBeacon()
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypePing, exception);

    } else if(option.endsWith(QLatin1Literal("websocket"))) {
        // requests initiated via WebSocket object
        qDebug("Resource type 'websocket' not available");

    } else if(option.endsWith(QLatin1Literal("webrtc"))) {
        // connections opened via RTCPeerConnection instances to ICE servers
        qDebug("Resource type 'webrtc' not available");

    } else if(option.endsWith(QLatin1Literal("document"))) {
        // the page itself
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeMainFrame, exception);

    } else if(option.endsWith(QLatin1Literal("other"))) {
        return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeUnknown, exception);
    }

    qDebug("TODO: %s", qUtf8Printable(option));
    return std::nullopt;
}