1 #ifndef GENERICPROTOCOLCLIENT_HPP
2 #define GENERICPROTOCOLCLIENT_HPP
3 
4 #include "cryptoidentity.hpp"
5 
6 #include <QObject>
7 #include <QAbstractSocket>
8 
9 enum class RequestState : int;
10 
11 class ProtocolHandler : public QObject
12 {
13     Q_OBJECT
14 public:
15     enum NetworkError {
16         UnknownError, //!< There was an unhandled network error
17         ProtocolViolation, //!< The server responded with something unexpected and violated the protocol
18         HostNotFound, //!< The host was not found by the client
19         ConnectionRefused, //!< The host refused connection on that port
20         ResourceNotFound, //!< The requested resource was not found on the server
21         BadRequest, //!< Our client misbehaved and did a request the server cannot understand
22         ProxyRequest, //!< We requested a proxy operation, but the server does not allow that
23         InternalServerError,
24         InvalidClientCertificate,
25         UntrustedHost, //!< We don't know the host, and we don't trust it
26         MistrustedHost, //!< We know the host and it's not the server identity we've seen before
27         Unauthorized, //!< The requested resource could not be accessed.
28         TlsFailure, //!< Unspecified TLS failure
29         Timeout, //!< The network connection timed out.
30     };
31     enum RequestOptions {
32         Default = 0,
33         IgnoreTlsErrors = 1,
34     };
35 public:
36     explicit ProtocolHandler(QObject *parent = nullptr);
37 
38     virtual bool supportsScheme(QString const & scheme) const = 0;
39 
40     virtual bool startRequest(QUrl const & url, RequestOptions options) = 0;
41 
42     virtual bool isInProgress() const = 0;
43 
44     virtual bool cancelRequest() = 0;
45 
46     virtual bool enableClientCertificate(CryptoIdentity const & ident);
47     virtual void disableClientCertificate();
48 signals:
49     //! We successfully transferred some bytes from the server
50     void requestProgress(qint64 transferred);
51 
52     //! The request completed with the given data and mime type
53     void requestComplete(QByteArray const & data, QString const & mime);
54 
55     //! The state of the request has changed
56     void requestStateChange(RequestState state);
57 
58     //! Server redirected us to another URL
59     void redirected(QUrl const & uri, bool is_permanent);
60 
61     //! The server needs some information from the user to process this query.
62     void inputRequired(QString const & user_query, bool is_sensitive);
63 
64     //! There was an error while processing the request
65     void networkError(NetworkError error, QString const & reason);
66 
67     //! The server wants us to use a client certificate
68     void certificateRequired(QString const & info);
69 
70     //! The server uses TLS and has a certificate.
71     void hostCertificateLoaded(QSslCertificate const & cert);
72 protected:
73     void emitNetworkError(QAbstractSocket::SocketError error_code, QString const & textual_description);
74 };
75 
76 #endif // GENERICPROTOCOLCLIENT_HPP
77