| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef DOWNLOADER_H |
| 4 | #define DOWNLOADER_H |
| 5 | |
| 6 | #include "definitions/definitions.h" |
| 7 | #include "network-web/gemini/geminiclient.h" |
| 8 | #include "network-web/gemini/geminiparser.h" |
| 9 | #include "network-web/httpresponse.h" |
| 10 | #include "network-web/networkfactory.h" |
| 11 | |
| 12 | #include <QHttpMultiPart> |
| 13 | #include <QNetworkProxy> |
| 14 | #include <QNetworkReply> |
| 15 | #include <QObject> |
| 16 | #include <QSslError> |
| 17 | |
| 18 | class SilentNetworkAccessManager; |
| 19 | class QTimer; |
| 20 | |
| 21 | class Downloader : public QObject { |
| 22 | Q_OBJECT |
| 23 | |
| 24 | public: |
| 25 | explicit Downloader(QObject* parent = nullptr); |
| 26 | virtual ~Downloader(); |
| 27 | |
| 28 | // Access to last received full output data/error/content-type. |
| 29 | QByteArray lastOutputData() const; |
| 30 | QNetworkReply::NetworkError lastOutputError() const; |
| 31 | QList<HttpResponse> lastOutputMultipartData() const; |
| 32 | QString lastContentType() const; |
| 33 | QList<QNetworkCookie> lastCookies() const; |
| 34 | int lastHttpStatusCode() const; |
| 35 | QMap<QString, QString> lastHeaders() const; |
| 36 | QUrl lastUrl() const; |
| 37 | |
| 38 | void setHttp2Status(NetworkFactory::Http2Status status); |
| 39 | void setProxy(const QNetworkProxy& proxy); |
| 40 | |
| 41 | public slots: |
| 42 | void cancel(); |
| 43 | |
| 44 | void appendRawHeaders(const QList<QPair<QByteArray, QByteArray>>& headers); |
| 45 | void appendRawHeader(const QByteArray& name, const QByteArray& value); |
| 46 | |
| 47 | // Performs asynchronous download of given file. Redirections are handled. |
| 48 | void downloadFile(const QString& url, |
| 49 | int timeout = DOWNLOAD_TIMEOUT, |
| 50 | bool protected_contents = false, |
| 51 | const QString& username = QString(), |
| 52 | const QString& password = QString()); |
| 53 | |
| 54 | void uploadFile(const QString& url, |
| 55 | const QByteArray& data, |
| 56 | int timeout = DOWNLOAD_TIMEOUT, |
| 57 | bool protected_contents = false, |
| 58 | const QString& username = QString(), |
| 59 | const QString& password = QString()); |
| 60 | |
| 61 | void manipulateData(const QString& url, |
| 62 | QNetworkAccessManager::Operation operation, |
| 63 | QHttpMultiPart* multipart_data, |
| 64 | int timeout = DOWNLOAD_TIMEOUT, |
| 65 | bool protected_contents = false, |
| 66 | const QString& username = QString(), |
| 67 | const QString& password = QString()); |
| 68 | |
| 69 | void manipulateData(const QString& url, |
| 70 | QNetworkAccessManager::Operation operation, |
| 71 | const QByteArray& data = QByteArray(), |
| 72 | int timeout = DOWNLOAD_TIMEOUT, |
| 73 | bool protected_contents = false, |
| 74 | const QString& username = QString(), |
| 75 | const QString& password = QString()); |
| 76 | |
| 77 | signals: |
| 78 | void progress(qint64 bytes_received, qint64 bytes_total); |
| 79 | void completed(const QUrl& url, QNetworkReply::NetworkError status, int http_code, QByteArray contents = {}); |
| 80 | |
| 81 | private slots: |
| 82 | void geminiRedirect(const QUrl& uri, bool is_permanent); |
| 83 | void geminiFinished(const QByteArray& data, const QString& mime); |
| 84 | void geminiError(GeminiClient::NetworkError error, const QString& reason); |
| 85 | |
| 86 | // Called when current reply is processed. |
| 87 | void finished(); |
| 88 | |
| 89 | // Called when progress of downloaded file changes. |
| 90 | void progressInternal(qint64 bytes_received, qint64 bytes_total); |
| 91 | |
| 92 | private: |
| 93 | void setCustomPropsToReply(QNetworkReply* reply); |
| 94 | QList<HttpResponse> decodeMultipartAnswer(QNetworkReply* reply); |
| 95 | void manipulateData(const QString& url, |
| 96 | QNetworkAccessManager::Operation operation, |
| 97 | const QByteArray& data, |
| 98 | QHttpMultiPart* multipart_data, |
| 99 | int timeout = DOWNLOAD_TIMEOUT, |
| 100 | bool protected_contents = false, |
| 101 | const QString& username = QString(), |
| 102 | const QString& password = QString()); |
| 103 | void runDeleteRequest(const QNetworkRequest& request); |
| 104 | void runPutRequest(const QNetworkRequest& request, const QByteArray& data); |
| 105 | void runPostRequest(const QNetworkRequest& request, QHttpMultiPart* multipart_data); |
| 106 | void runPostRequest(const QNetworkRequest& request, const QByteArray& data); |
| 107 | void runGetRequest(const QNetworkRequest& request); |
| 108 | void runGeminiRequest(const QUrl& url); |
| 109 | |
| 110 | private: |
| 111 | GeminiClient* m_geminiClient; |
| 112 | GeminiParser m_geminiParser; |
| 113 | QNetworkReply* m_activeReply; |
| 114 | QScopedPointer<SilentNetworkAccessManager> m_downloadManager; |
| 115 | QTimer* m_timer; |
| 116 | QHash<QByteArray, QByteArray> m_customHeaders; |
| 117 | QByteArray m_inputData; |
| 118 | QHttpMultiPart* m_inputMultipartData; |
| 119 | bool m_targetProtected; |
| 120 | QString m_targetUsername; |
| 121 | QString m_targetPassword; |
| 122 | |
| 123 | // Response data. |
| 124 | QByteArray m_lastOutputData; |
| 125 | QList<HttpResponse> m_lastOutputMultipartData; |
| 126 | QNetworkReply::NetworkError m_lastOutputError; |
| 127 | int m_lastHttpStatusCode; |
| 128 | QString m_lastContentType; |
| 129 | QUrl m_lastUrl; |
| 130 | QList<QNetworkCookie> m_lastCookies; |
| 131 | QMap<QString, QString> m_lastHeaders; |
| 132 | }; |
| 133 | |
| 134 | #endif // DOWNLOADER_H |