| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef NETWORKFACTORY_H |
| 4 | #define NETWORKFACTORY_H |
| 5 | |
| 6 | #include "definitions/typedefs.h" |
| 7 | #include "network-web/httpresponse.h" |
| 8 | |
| 9 | #include <QCoreApplication> |
| 10 | #include <QHttpPart> |
| 11 | #include <QNetworkCookie> |
| 12 | #include <QNetworkProxy> |
| 13 | #include <QNetworkReply> |
| 14 | #include <QPair> |
| 15 | #include <QVariant> |
| 16 | |
| 17 | struct RSSGUARD_DLLSPEC NetworkResult { |
| 18 | QNetworkReply::NetworkError m_networkError; |
| 19 | int m_httpCode; |
| 20 | QString m_contentType; |
| 21 | QList<QNetworkCookie> m_cookies; |
| 22 | QMap<QString, QString> m_headers; |
| 23 | QUrl m_url; |
| 24 | |
| 25 | explicit NetworkResult(); |
| 26 | explicit NetworkResult(QNetworkReply::NetworkError err, |
| 27 | int http_code, |
| 28 | const QString& ct, |
| 29 | const QList<QNetworkCookie>& cook); |
| 30 | }; |
| 31 | |
| 32 | class RSSGUARD_DLLSPEC NetworkFactory { |
| 33 | Q_DECLARE_TR_FUNCTIONS(NetworkFactory) |
| 34 | |
| 35 | private: |
| 36 | explicit NetworkFactory() = default; |
| 37 | |
| 38 | public: |
| 39 | enum class NetworkAuthentication { |
| 40 | NoAuthentication = 0, |
| 41 | Basic = 1, |
| 42 | Token = 2 |
| 43 | }; |
| 44 | |
| 45 | enum class Http2Status { |
| 46 | DontSet = 0, // Use application-wide setting. |
| 47 | Enabled = 1, |
| 48 | Disabled = 2 |
| 49 | }; |
| 50 | |
| 51 | static QDateTime extractRetryAfter(const QString& retry_after_value); |
| 52 | |
| 53 | static QStringList extractFeedLinksFromHtmlPage(const QUrl& url, const QString& html); |
| 54 | static QPair<QByteArray, QByteArray> generateBasicAuthHeader(NetworkAuthentication protection, |
| 55 | const QString& username, |
| 56 | const QString& password); |
| 57 | |
| 58 | // Returns human readable text for given network error. |
| 59 | static QString networkErrorText(QNetworkReply::NetworkError error_code); |
| 60 | static QString sanitizeUrl(const QString& url); |
| 61 | |
| 62 | // Performs SYNCHRONOUS favicon download for the site, |
| 63 | // given URL belongs to. |
| 64 | static QNetworkReply::NetworkError downloadIcon(const QList<IconLocation>& urls, |
| 65 | int timeout, |
| 66 | QPixmap& output, |
| 67 | const QList<QPair<QByteArray, QByteArray>>& additional_headers, |
| 68 | const QNetworkProxy& custom_proxy = |
| 69 | QNetworkProxy::ProxyType::DefaultProxy, |
| 70 | Http2Status http2_status = Http2Status::DontSet); |
| 71 | static NetworkResult performNetworkOperation(const QString& url, |
| 72 | int timeout, |
| 73 | const QByteArray& input_data, |
| 74 | QByteArray& output, |
| 75 | QNetworkAccessManager::Operation operation, |
| 76 | const QList<QPair<QByteArray, QByteArray>>& additional_headers = |
| 77 | QList<QPair<QByteArray, QByteArray>>(), |
| 78 | bool protected_contents = false, |
| 79 | const QString& username = QString(), |
| 80 | const QString& password = QString(), |
| 81 | const QNetworkProxy& custom_proxy = |
| 82 | QNetworkProxy::ProxyType::DefaultProxy, |
| 83 | Http2Status http2_status = Http2Status::DontSet); |
| 84 | static NetworkResult performNetworkOperation(const QString& url, |
| 85 | int timeout, |
| 86 | QHttpMultiPart* input_data, |
| 87 | QList<HttpResponse>& output, |
| 88 | QNetworkAccessManager::Operation operation, |
| 89 | const QList<QPair<QByteArray, QByteArray>>& additional_headers = |
| 90 | QList<QPair<QByteArray, QByteArray>>(), |
| 91 | bool protected_contents = false, |
| 92 | const QString& username = QString(), |
| 93 | const QString& password = QString(), |
| 94 | const QNetworkProxy& custom_proxy = |
| 95 | QNetworkProxy::ProxyType::DefaultProxy, |
| 96 | Http2Status http2_status = Http2Status::DontSet); |
| 97 | }; |
| 98 | |
| 99 | Q_DECLARE_METATYPE(NetworkResult) |
| 100 | Q_DECLARE_METATYPE(NetworkFactory::NetworkAuthentication) |
| 101 | |
| 102 | #endif // NETWORKFACTORY_H |