/*
 * 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
 */

#ifndef SMOLBOTE_URLREQUESTINTERCEPTOR_H
#define SMOLBOTE_URLREQUESTINTERCEPTOR_H

#include "urlfilter/filterrule.h"
#include <QByteArray>
#include <QVector>
#include <QWebEngineUrlRequestInterceptor>
#include <memory>

typedef std::pair<std::string, std::string> Header;

class Configuration;
class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
{
    Q_OBJECT
public:
    struct HostRule {
        bool isBlocking;
    };

    explicit UrlRequestInterceptor(const std::unique_ptr<Configuration> &config, QObject *parent = nullptr);
    ~UrlRequestInterceptor() override = default;

    void interceptRequest(QWebEngineUrlRequestInfo &info) override;

private:
    QHash<QString, HostRule> rules;
    std::vector<FilterRule> filters;
    std::vector<Header> m_headers;
};

QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename);
inline std::vector<FilterRule> parseAdBlockList(const QString &filename);

#endif // SMOLBOTE_URLREQUESTINTERCEPTOR_H