networkjobs.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
  3. * Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #ifndef NETWORKJOBS_H
  16. #define NETWORKJOBS_H
  17. #include "abstractnetworkjob.h"
  18. #include "common/result.h"
  19. #include <QBuffer>
  20. #include <QUrlQuery>
  21. #include <QJsonDocument>
  22. #include <functional>
  23. class QUrl;
  24. class QJsonObject;
  25. namespace OCC {
  26. struct HttpError
  27. {
  28. int code; // HTTP error code
  29. QString message;
  30. };
  31. template <typename T>
  32. using HttpResult = Result<T, HttpError>;
  33. /**
  34. * @brief The EntityExistsJob class
  35. * @ingroup libsync
  36. */
  37. class OWNCLOUDSYNC_EXPORT EntityExistsJob : public AbstractNetworkJob
  38. {
  39. Q_OBJECT
  40. public:
  41. explicit EntityExistsJob(AccountPtr account, const QString &path, QObject *parent = nullptr);
  42. void start() override;
  43. signals:
  44. void exists(QNetworkReply *);
  45. private slots:
  46. bool finished() override;
  47. };
  48. /**
  49. * @brief A basic file manipulation job
  50. * @ingroup libsync
  51. */
  52. class OWNCLOUDSYNC_EXPORT SimpleFileJob : public AbstractNetworkJob
  53. {
  54. Q_OBJECT
  55. public:
  56. explicit SimpleFileJob(AccountPtr account, const QString &filePath, QObject *parent = nullptr);
  57. QNetworkReply *startRequest(
  58. const QByteArray &verb, const QNetworkRequest req = QNetworkRequest(), QIODevice *requestBody = nullptr);
  59. QNetworkReply *startRequest(const QByteArray &verb, const QUrl &url, const QNetworkRequest req = QNetworkRequest(),
  60. QIODevice *requestBody = nullptr);
  61. signals:
  62. void finishedSignal(QNetworkReply *reply);
  63. protected slots:
  64. bool finished() override;
  65. private:
  66. QByteArray _verb;
  67. };
  68. /**
  69. * @brief sends a DELETE http request to a url.
  70. *
  71. * See Nextcloud API usage for the possible DELETE requests.
  72. *
  73. * This does *not* delete files, it does a http request.
  74. */
  75. class OWNCLOUDSYNC_EXPORT DeleteApiJob : public SimpleFileJob
  76. {
  77. Q_OBJECT
  78. public:
  79. explicit DeleteApiJob(AccountPtr account, const QString &path, QObject *parent = nullptr);
  80. void start() override;
  81. signals:
  82. void result(int httpCode);
  83. private slots:
  84. bool finished() override;
  85. };
  86. struct ExtraFolderInfo {
  87. QByteArray fileId;
  88. qint64 size = -1;
  89. };
  90. /**
  91. * @brief The LsColJob class
  92. * @ingroup libsync
  93. */
  94. class OWNCLOUDSYNC_EXPORT LsColXMLParser : public QObject
  95. {
  96. Q_OBJECT
  97. public:
  98. explicit LsColXMLParser();
  99. bool parse(const QByteArray &xml,
  100. QHash<QString, ExtraFolderInfo> *sizes,
  101. const QString &expectedPath);
  102. signals:
  103. void directoryListingSubfolders(const QStringList &items);
  104. void directoryListingIterated(const QString &name, const QMap<QString, QString> &properties);
  105. void finishedWithError(QNetworkReply *reply);
  106. void finishedWithoutError();
  107. };
  108. class OWNCLOUDSYNC_EXPORT LsColJob : public AbstractNetworkJob
  109. {
  110. Q_OBJECT
  111. public:
  112. explicit LsColJob(AccountPtr account, const QString &path, QObject *parent = nullptr);
  113. explicit LsColJob(AccountPtr account, const QUrl &url, QObject *parent = nullptr);
  114. void start() override;
  115. QHash<QString, ExtraFolderInfo> _folderInfos;
  116. /**
  117. * Used to specify which properties shall be retrieved.
  118. *
  119. * The properties can
  120. * - contain no colon: they refer to a property in the DAV: namespace
  121. * - contain a colon: and thus specify an explicit namespace,
  122. * e.g. "ns:with:colons:bar", which is "bar" in the "ns:with:colons" namespace
  123. */
  124. void setProperties(QList<QByteArray> properties);
  125. [[nodiscard]] QList<QByteArray> properties() const;
  126. signals:
  127. void directoryListingSubfolders(const QStringList &items);
  128. void directoryListingIterated(const QString &name, const QMap<QString, QString> &properties);
  129. void finishedWithError(QNetworkReply *reply);
  130. void finishedWithoutError();
  131. private slots:
  132. bool finished() override;
  133. private:
  134. QList<QByteArray> _properties;
  135. QUrl _url; // Used instead of path() if the url is specified in the constructor
  136. };
  137. /**
  138. * @brief The PropfindJob class
  139. *
  140. * Setting the desired properties with setProperties() is mandatory.
  141. *
  142. * Note that this job is only for querying one item.
  143. * There is also the LsColJob which can be used to list collections
  144. *
  145. * @ingroup libsync
  146. */
  147. class OWNCLOUDSYNC_EXPORT PropfindJob : public AbstractNetworkJob
  148. {
  149. Q_OBJECT
  150. public:
  151. explicit PropfindJob(AccountPtr account, const QString &path, QObject *parent = nullptr);
  152. void start() override;
  153. /**
  154. * Used to specify which properties shall be retrieved.
  155. *
  156. * The properties can
  157. * - contain no colon: they refer to a property in the DAV: namespace
  158. * - contain a colon: and thus specify an explicit namespace,
  159. * e.g. "ns:with:colons:bar", which is "bar" in the "ns:with:colons" namespace
  160. */
  161. void setProperties(QList<QByteArray> properties);
  162. [[nodiscard]] QList<QByteArray> properties() const;
  163. signals:
  164. void result(const QVariantMap &values);
  165. void finishedWithError(QNetworkReply *reply = nullptr);
  166. private slots:
  167. bool finished() override;
  168. private:
  169. QList<QByteArray> _properties;
  170. };
  171. #ifndef TOKEN_AUTH_ONLY
  172. /**
  173. * @brief Retrieves the account users avatar from the server using a GET request.
  174. *
  175. * If the server does not have the avatar, the result Pixmap is empty.
  176. *
  177. * @ingroup libsync
  178. */
  179. class OWNCLOUDSYNC_EXPORT AvatarJob : public AbstractNetworkJob
  180. {
  181. Q_OBJECT
  182. public:
  183. /**
  184. * @param userId The user for which to obtain the avatar
  185. * @param size The size of the avatar (square so size*size)
  186. */
  187. explicit AvatarJob(AccountPtr account, const QString &userId, int size, QObject *parent = nullptr);
  188. void start() override;
  189. /** The retrieved avatar images don't have the circle shape by default */
  190. static QImage makeCircularAvatar(const QImage &baseAvatar);
  191. signals:
  192. /**
  193. * @brief avatarPixmap - returns either a valid pixmap or not.
  194. */
  195. void avatarPixmap(const QImage &);
  196. private slots:
  197. bool finished() override;
  198. private:
  199. QUrl _avatarUrl;
  200. };
  201. #endif
  202. /**
  203. * @brief Send a Proppatch request
  204. *
  205. * Setting the desired properties with setProperties() is mandatory.
  206. *
  207. * WARNING: Untested!
  208. *
  209. * @ingroup libsync
  210. */
  211. class OWNCLOUDSYNC_EXPORT ProppatchJob : public AbstractNetworkJob
  212. {
  213. Q_OBJECT
  214. public:
  215. explicit ProppatchJob(AccountPtr account, const QString &path, QObject *parent = nullptr);
  216. void start() override;
  217. /**
  218. * Used to specify which properties shall be set.
  219. *
  220. * The property keys can
  221. * - contain no colon: they refer to a property in the DAV: namespace
  222. * - contain a colon: and thus specify an explicit namespace,
  223. * e.g. "ns:with:colons:bar", which is "bar" in the "ns:with:colons" namespace
  224. */
  225. void setProperties(QMap<QByteArray, QByteArray> properties);
  226. [[nodiscard]] QMap<QByteArray, QByteArray> properties() const;
  227. signals:
  228. void success();
  229. void finishedWithError();
  230. private slots:
  231. bool finished() override;
  232. private:
  233. QMap<QByteArray, QByteArray> _properties;
  234. };
  235. /**
  236. * @brief The MkColJob class
  237. * @ingroup libsync
  238. */
  239. class OWNCLOUDSYNC_EXPORT MkColJob : public AbstractNetworkJob
  240. {
  241. Q_OBJECT
  242. QUrl _url; // Only used if the constructor taking a url is taken.
  243. QMap<QByteArray, QByteArray> _extraHeaders;
  244. public:
  245. explicit MkColJob(AccountPtr account, const QString &path, QObject *parent = nullptr);
  246. explicit MkColJob(AccountPtr account, const QString &path, const QMap<QByteArray, QByteArray> &extraHeaders, QObject *parent = nullptr);
  247. explicit MkColJob(AccountPtr account, const QUrl &url,
  248. const QMap<QByteArray, QByteArray> &extraHeaders, QObject *parent = nullptr);
  249. void start() override;
  250. signals:
  251. void finishedWithError(QNetworkReply *reply);
  252. void finishedWithoutError();
  253. private:
  254. bool finished() override;
  255. };
  256. /**
  257. * @brief The CheckServerJob class
  258. * @ingroup libsync
  259. */
  260. class OWNCLOUDSYNC_EXPORT CheckServerJob : public AbstractNetworkJob
  261. {
  262. Q_OBJECT
  263. public:
  264. explicit CheckServerJob(AccountPtr account, QObject *parent = nullptr);
  265. void start() override;
  266. static QString version(const QJsonObject &info);
  267. static QString versionString(const QJsonObject &info);
  268. static bool installed(const QJsonObject &info);
  269. signals:
  270. /** Emitted when a status.php was successfully read.
  271. *
  272. * \a url see _serverStatusUrl (does not include "/status.php")
  273. * \a info The status.php reply information
  274. */
  275. void instanceFound(const QUrl &url, const QJsonObject &info);
  276. /** Emitted on invalid status.php reply.
  277. *
  278. * \a reply is never null
  279. */
  280. void instanceNotFound(QNetworkReply *reply);
  281. /** A timeout occurred.
  282. *
  283. * \a url The specific url where the timeout happened.
  284. */
  285. void timeout(const QUrl &url);
  286. private:
  287. bool finished() override;
  288. void onTimedOut() override;
  289. private slots:
  290. virtual void metaDataChangedSlot();
  291. virtual void encryptedSlot();
  292. void slotRedirected(QNetworkReply *reply, const QUrl &targetUrl, int redirectCount);
  293. private:
  294. bool _subdirFallback;
  295. /** The permanent-redirect adjusted account url.
  296. *
  297. * Note that temporary redirects or a permanent redirect behind a temporary
  298. * one do not affect this url.
  299. */
  300. QUrl _serverUrl;
  301. /** Keep track of how many permanent redirect were applied. */
  302. int _permanentRedirects;
  303. };
  304. /**
  305. * @brief The RequestEtagJob class
  306. */
  307. class OWNCLOUDSYNC_EXPORT RequestEtagJob : public AbstractNetworkJob
  308. {
  309. Q_OBJECT
  310. public:
  311. explicit RequestEtagJob(AccountPtr account, const QString &path, QObject *parent = nullptr);
  312. void start() override;
  313. signals:
  314. void etagRetrieved(const QByteArray &etag, const QDateTime &time);
  315. void finishedWithResult(const HttpResult<QByteArray> &etag);
  316. private slots:
  317. bool finished() override;
  318. };
  319. class OWNCLOUDSYNC_EXPORT SimpleApiJob : public AbstractNetworkJob
  320. {
  321. Q_OBJECT
  322. public:
  323. enum class Verb {
  324. Get,
  325. Post,
  326. Put,
  327. Delete,
  328. };
  329. explicit SimpleApiJob(const AccountPtr &account, const QString &path, QObject *parent = nullptr);
  330. void setBody(const QByteArray &body);
  331. void setVerb(Verb value);
  332. /**
  333. * @brief addQueryParams - add more parameters to the ocs call
  334. * @param params: list pairs of strings containing the parameter name and the value.
  335. *
  336. * All parameters from the passed list are appended to the query. Note
  337. * that the format=json parameter is added automatically and does not
  338. * need to be set this way.
  339. *
  340. * This function needs to be called before start() obviously.
  341. */
  342. void addQueryParams(const QUrlQuery &params);
  343. void addRawHeader(const QByteArray &headerName, const QByteArray &value);
  344. public slots:
  345. void start() override;
  346. Q_SIGNALS:
  347. void resultReceived(int statusCode);
  348. protected:
  349. bool finished() override;
  350. [[nodiscard]] QNetworkRequest& request();
  351. [[nodiscard]] QByteArray& body();
  352. [[nodiscard]] QUrlQuery& additionalParams();
  353. [[nodiscard]] QByteArray verbToString() const;
  354. private:
  355. QByteArray _body;
  356. QUrlQuery _additionalParams;
  357. QNetworkRequest _request;
  358. Verb _verb = Verb::Get;
  359. };
  360. /**
  361. * @brief Job to check an API that return JSON
  362. *
  363. * Note! you need to be in the connected state before calling this because of a server bug:
  364. * https://github.com/owncloud/core/issues/12930
  365. *
  366. * To be used like this:
  367. * \code
  368. * _job = new JsonApiJob(account, QLatin1String("ocs/v1.php/foo/bar"), this);
  369. * connect(job, SIGNAL(jsonReceived(QJsonDocument)), ...)
  370. * The received QVariantMap is null in case of error
  371. * \encode
  372. *
  373. * @ingroup libsync
  374. */
  375. class OWNCLOUDSYNC_EXPORT JsonApiJob : public SimpleApiJob
  376. {
  377. Q_OBJECT
  378. public:
  379. explicit JsonApiJob(const AccountPtr &account, const QString &path, QObject *parent = nullptr);
  380. void setBody(const QJsonDocument &body);
  381. public slots:
  382. void start() override;
  383. protected:
  384. bool finished() override;
  385. signals:
  386. /**
  387. * @brief jsonReceived - signal to report the json answer from ocs
  388. * @param json - the parsed json document
  389. * @param statusCode - the OCS status code: 100 (!) for success
  390. */
  391. void jsonReceived(const QJsonDocument &json, int statusCode);
  392. /**
  393. * @brief etagResponseHeaderReceived - signal to report the ETag response header value
  394. * from ocs api v2
  395. * @param value - the ETag response header value
  396. * @param statusCode - the OCS status code: 100 (!) for success
  397. */
  398. void etagResponseHeaderReceived(const QByteArray &value, int statusCode);
  399. };
  400. /**
  401. * @brief Checks with auth type to use for a server
  402. * @ingroup libsync
  403. */
  404. class OWNCLOUDSYNC_EXPORT DetermineAuthTypeJob : public QObject
  405. {
  406. Q_OBJECT
  407. public:
  408. enum AuthType {
  409. NoAuthType, // used only before we got a chance to probe the server
  410. #ifdef WITH_WEBENGINE
  411. WebViewFlow,
  412. #endif // WITH_WEBENGINE
  413. Basic, // also the catch-all fallback for backwards compatibility reasons
  414. OAuth,
  415. LoginFlowV2
  416. };
  417. Q_ENUM(AuthType)
  418. explicit DetermineAuthTypeJob(AccountPtr account, QObject *parent = nullptr);
  419. void start();
  420. signals:
  421. void authType(OCC::DetermineAuthTypeJob::AuthType);
  422. private:
  423. void checkAllDone();
  424. AccountPtr _account;
  425. AuthType _resultGet = NoAuthType;
  426. AuthType _resultPropfind = NoAuthType;
  427. AuthType _resultOldFlow = NoAuthType;
  428. bool _getDone = false;
  429. bool _propfindDone = false;
  430. bool _oldFlowDone = false;
  431. };
  432. /**
  433. * @brief A basic job around a network request without extra funtionality
  434. * @ingroup libsync
  435. *
  436. * Primarily adds timeout and redirection handling.
  437. */
  438. class OWNCLOUDSYNC_EXPORT SimpleNetworkJob : public AbstractNetworkJob
  439. {
  440. Q_OBJECT
  441. public:
  442. explicit SimpleNetworkJob(AccountPtr account, QObject *parent = nullptr);
  443. QNetworkReply *startRequest(const QByteArray &verb, const QUrl &url,
  444. QNetworkRequest req = QNetworkRequest(),
  445. QIODevice *requestBody = nullptr);
  446. signals:
  447. void finishedSignal(QNetworkReply *reply);
  448. private slots:
  449. bool finished() override;
  450. };
  451. /**
  452. * @brief Runs a PROPFIND to figure out the private link url
  453. *
  454. * The numericFileId is used only to build the deprecatedPrivateLinkUrl
  455. * locally as a fallback. If it's empty and the PROPFIND fails, targetFun
  456. * will be called with an empty string.
  457. *
  458. * The job and signal connections are parented to the target QObject.
  459. *
  460. * Note: targetFun is guaranteed to be called only through the event
  461. * loop and never directly.
  462. */
  463. void OWNCLOUDSYNC_EXPORT fetchPrivateLinkUrl(
  464. AccountPtr account, const QString &remotePath,
  465. const QByteArray &numericFileId, QObject *target,
  466. std::function<void(const QString &url)> targetFun);
  467. } // namespace OCC
  468. #endif // NETWORKJOBS_H