| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | // |
| 3 | // This file is heavily inspired by https://github.com/ikskuh/kristall. |
| 4 | |
| 5 | #ifndef GEMINICLIENT_H |
| 6 | #define GEMINICLIENT_H |
| 7 | |
| 8 | #include <QMimeType> |
| 9 | #include <QObject> |
| 10 | #include <QSslCertificate> |
| 11 | #include <QSslKey> |
| 12 | #include <QSslSocket> |
| 13 | #include <QUrl> |
| 14 | |
| 15 | #define GEMINI_MIME_TYPE "text/gemini" |
| 16 | |
| 17 | //! Cryptographic user identitiy consisting |
| 18 | //! of a key-certificate pair and some user information. |
| 19 | struct CryptoIdentity { |
| 20 | //! The certificate that is used for cryptography |
| 21 | QSslCertificate certificate; |
| 22 | |
| 23 | //! The actual private key that is used for cryptography |
| 24 | QSslKey private_key; |
| 25 | |
| 26 | //! The title with which the identity is presented to the user. |
| 27 | QString display_name; |
| 28 | |
| 29 | //! Notes that the user can have per identity for improved identity management |
| 30 | QString user_notes; |
| 31 | |
| 32 | //! True for long-lived identities |
| 33 | bool is_persistent = false; |
| 34 | |
| 35 | //! If not empty, Kristall will check |
| 36 | QString host_filter = ""; |
| 37 | |
| 38 | //! When this is set to true and the host_filter is not empty, |
| 39 | //! the certificate will be automatically enabled for hosts matching the filter. |
| 40 | bool auto_enable = false; |
| 41 | |
| 42 | bool isValid() const; |
| 43 | |
| 44 | //! returns true if a host does not match the filter criterion |
| 45 | bool isHostFiltered(const QUrl& url) const; |
| 46 | |
| 47 | //! returns true when the identity should be enabled on url |
| 48 | bool isAutomaticallyEnabledOn(const QUrl& url) const; |
| 49 | }; |
| 50 | |
| 51 | class GeminiClient : public QObject { |
| 52 | Q_OBJECT |
| 53 | |
| 54 | public: |
| 55 | enum class RequestState { |
| 56 | None = 0, |
| 57 | Started = 1, |
| 58 | HostFound = 2, |
| 59 | Connected = 3, |
| 60 | |
| 61 | StartedWeb = 255, |
| 62 | }; |
| 63 | |
| 64 | enum NetworkError { |
| 65 | UnknownError, //!< There was an unhandled network error |
| 66 | ProtocolViolation, //!< The server responded with something unexpected and violated the protocol |
| 67 | HostNotFound, //!< The host was not found by the client |
| 68 | ConnectionRefused, //!< The host refused connection on that port |
| 69 | ResourceNotFound, //!< The requested resource was not found on the server |
| 70 | BadRequest, //!< Our client misbehaved and did a request the server cannot understand |
| 71 | ProxyRequest, //!< We requested a proxy operation, but the server does not allow that |
| 72 | InternalServerError, |
| 73 | InvalidClientCertificate, |
| 74 | UntrustedHost, //!< We don't know the host, and we don't trust it |
| 75 | MistrustedHost, //!< We know the host and it's not the server identity we've seen before |
| 76 | Unauthorized, //!< The requested resource could not be accessed. |
| 77 | TlsFailure, //!< Unspecified TLS failure |
| 78 | Timeout, //!< The network connection timed out. |
| 79 | }; |
| 80 | |
| 81 | enum RequestOptions { |
| 82 | Default = 0, |
| 83 | IgnoreTlsErrors = 1, |
| 84 | }; |
| 85 | |
| 86 | explicit GeminiClient(QObject* parent = nullptr); |
| 87 | virtual ~GeminiClient(); |
| 88 | |
| 89 | bool supportsUrl(const QString& url) const; |
| 90 | bool supportsUrl(const QUrl& url) const; |
| 91 | |
| 92 | bool startRequest(const QUrl& url, RequestOptions options); |
| 93 | bool inProgress() const; |
| 94 | bool cancelRequest(); |
| 95 | |
| 96 | bool enableClientCertificate(const CryptoIdentity& ident); |
| 97 | void disableClientCertificate(); |
| 98 | |
| 99 | QUrl targetUrl() const; |
| 100 | |
| 101 | signals: |
| 102 | //! We successfully transferred some bytes from the server |
| 103 | void requestProgress(qint64 transferred); |
| 104 | |
| 105 | //! The request completed with the given data and mime type |
| 106 | void requestComplete(const QByteArray& data, const QString& mime); |
| 107 | |
| 108 | //! The state of the request has changed |
| 109 | void requestStateChange(RequestState state); |
| 110 | |
| 111 | //! Server redirected us to another URL |
| 112 | void redirected(const QUrl& uri, bool is_permanent); |
| 113 | |
| 114 | //! The server needs some information from the user to process this query. |
| 115 | void inputRequired(const QString& user_query, bool is_sensitive); |
| 116 | |
| 117 | //! There was an error while processing the request |
| 118 | void networkError(NetworkError error, const QString& reason); |
| 119 | |
| 120 | //! The server wants us to use a client certificate |
| 121 | void certificateRequired(const QString& info); |
| 122 | |
| 123 | //! The server uses TLS and has a certificate. |
| 124 | void hostCertificateLoaded(const QSslCertificate& cert); |
| 125 | |
| 126 | protected: |
| 127 | void emitNetworkError(QAbstractSocket::SocketError error_code, const QString& textual_description); |
| 128 | |
| 129 | private slots: |
| 130 | void socketEncrypted(); |
| 131 | void socketReadyRead(); |
| 132 | void socketDisconnected(); |
| 133 | void sslErrors(const QList<QSslError>& errors); |
| 134 | void socketError(QAbstractSocket::SocketError socketError); |
| 135 | |
| 136 | private: |
| 137 | bool m_isReceivingBody; |
| 138 | bool m_suppressSocketTlsErrors; |
| 139 | bool m_inErrorState; |
| 140 | |
| 141 | QUrl m_targetUrl; |
| 142 | QSslSocket m_socket; |
| 143 | QByteArray m_buffer; |
| 144 | QByteArray m_body; |
| 145 | QString m_mimeType; |
| 146 | RequestOptions m_options; |
| 147 | }; |
| 148 | |
| 149 | #endif // GEMINICLIENT_H |