aboutsummaryrefslogtreecommitdiff
path: root/src/protocolhandler.hpp
blob: 8e2c1e8b758b9bf95106c68244a1bda29feb8823 (plain) (blame)
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
#ifndef GENERICPROTOCOLCLIENT_HPP
#define GENERICPROTOCOLCLIENT_HPP

#include "cryptoidentity.hpp"

#include <QObject>
#include <QAbstractSocket>

enum class RequestState : int;

class ProtocolHandler : public QObject
{
    Q_OBJECT
public:
    enum NetworkError {
        UnknownError, //!< There was an unhandled network error
        ProtocolViolation, //!< The server responded with something unexpected and violated the protocol
        HostNotFound, //!< The host was not found by the client
        ConnectionRefused, //!< The host refused connection on that port
        ResourceNotFound, //!< The requested resource was not found on the server
        BadRequest, //!< Our client misbehaved and did a request the server cannot understand
        ProxyRequest, //!< We requested a proxy operation, but the server does not allow that
        InternalServerError,
        InvalidClientCertificate,
        UntrustedHost, //!< We don't know the host, and we don't trust it
        MistrustedHost, //!< We know the host and it's not the server identity we've seen before
        Unauthorized, //!< The requested resource could not be accessed.
        TlsFailure, //!< Unspecified TLS failure
        Timeout, //!< The network connection timed out.
    };
    enum RequestOptions {
        Default = 0,
        IgnoreTlsErrors = 1,
    };
public:
    explicit ProtocolHandler(QObject *parent = nullptr);

    virtual bool supportsScheme(QString const & scheme) const = 0;

    virtual bool startRequest(QUrl const & url, RequestOptions options) = 0;

    virtual bool isInProgress() const = 0;

    virtual bool cancelRequest() = 0;

    virtual bool enableClientCertificate(CryptoIdentity const & ident);
    virtual void disableClientCertificate();
signals:
    //! We successfully transferred some bytes from the server
    void requestProgress(qint64 transferred);

    //! The request completed with the given data and mime type
    void requestComplete(QByteArray const & data, QString const & mime);

    //! The state of the request has changed
    void requestStateChange(RequestState state);

    //! Server redirected us to another URL
    void redirected(QUrl const & uri, bool is_permanent);

    //! The server needs some information from the user to process this query.
    void inputRequired(QString const & user_query, bool is_sensitive);

    //! There was an error while processing the request
    void networkError(NetworkError error, QString const & reason);

    //! The server wants us to use a client certificate
    void certificateRequired(QString const & info);

    //! The server uses TLS and has a certificate.
    void hostCertificateLoaded(QSslCertificate const & cert);
protected:
    void emitNetworkError(QAbstractSocket::SocketError error_code, QString const & textual_description);
};

#endif // GENERICPROTOCOLCLIENT_HPP