1/*
2 SPDX-FileCopyrightText: 2000-2003 Waldo Bastian <bastian@kde.org>
3 SPDX-FileCopyrightText: 2000-2002 George Staikos <staikos@kde.org>
4 SPDX-FileCopyrightText: 2000-2002 Dawit Alemayehu <adawit@kde.org>
5 SPDX-FileCopyrightText: 2001, 2002 Hamish Rodda <rodda@kde.org>
6 SPDX-FileCopyrightText: 2007 Nick Shaforostoff <shafff@ukr.net>
7 SPDX-FileCopyrightText: 2007-2018 Daniel Nicoletti <dantti12@gmail.com>
8 SPDX-FileCopyrightText: 2008, 2009 Andreas Hartmetz <ahartmetz@gmail.com>
9 SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
10 SPDX-FileCopyrightText: 2023 Nicolas Fella <nicolas.fella@gmx.de>
11
12 SPDX-License-Identifier: LGPL-2.0-or-later
13*/
14
15#ifndef HTTP_H
16#define HTTP_H
17
18#include <KIO/WorkerBase>
19
20#include <QNetworkReply>
21#include <QSslError>
22
23#include "httpmethod_p.h"
24
25class QDomNodeList;
26
27class HTTPProtocol : public QObject, public KIO::WorkerBase
28{
29 Q_OBJECT
30public:
31 HTTPProtocol(const QByteArray &protocol, const QByteArray &pool, const QByteArray &app);
32 ~HTTPProtocol() override;
33
34 KIO::WorkerResult get(const QUrl &url) override;
35 KIO::WorkerResult put(const QUrl &url, int _mode, KIO::JobFlags flags) override;
36 KIO::WorkerResult mimetype(const QUrl &url) override;
37 KIO::WorkerResult special(const QByteArray &data) override;
38 KIO::WorkerResult stat(const QUrl &url) override;
39 KIO::WorkerResult listDir(const QUrl &url) override;
40 KIO::WorkerResult mkdir(const QUrl &url, int _permissions) override;
41 KIO::WorkerResult rename(const QUrl &src, const QUrl &dest, KIO::JobFlags flags) override;
42 KIO::WorkerResult copy(const QUrl &src, const QUrl &dest, int, KIO::JobFlags flags) override;
43 KIO::WorkerResult del(const QUrl &url, bool _isfile) override;
44 KIO::WorkerResult fileSystemFreeSpace(const QUrl &url) override;
45
46Q_SIGNALS:
47 void errorOut(KIO::Error error);
48
49private:
50 enum DataMode {
51 // emit data() as it is received
52 Emit,
53 // turn the data in the response
54 Return,
55 // discard any response data
56 Discard,
57 };
58
59 struct Response {
60 int httpCode;
61 QByteArray data;
62 int kioCode = 0;
63 };
64
65 /**
66 * Handles file -> webdav put requests.
67 */
68 [[nodiscard]] KIO::WorkerResult copyPut(const QUrl &src, const QUrl &dest, KIO::JobFlags flags);
69
70 void handleSslErrors(QNetworkReply *reply, const QList<QSslError> errors);
71 void handleRedirection(KIO::HTTP_METHOD method, const QUrl &originalUrl, QNetworkReply *reply);
72
73 [[nodiscard]] KIO::WorkerResult davStatList(const QUrl &url, bool stat);
74 void davParsePropstats(const QDomNodeList &propstats, KIO::UDSEntry &entry);
75 QDateTime parseDateTime(const QString &input, const QString &type);
76 void davParseActiveLocks(const QDomNodeList &activeLocks, uint &lockCount);
77 int codeFromResponse(const QString &response);
78 bool davDestinationExists(const QUrl &url);
79 QByteArray getData();
80 QString getContentType();
81
82 void setSslMetaData();
83
84 [[nodiscard]] KIO::WorkerResult post(const QUrl &url, qint64 size);
85 [[nodiscard]] Response
86 makeRequest(const QUrl &url, KIO::HTTP_METHOD method, QIODevice *inputData, DataMode dataMode, const QMap<QByteArray, QByteArray> &extraHeaders = {});
87
88 [[nodiscard]] Response
89 makeDavRequest(const QUrl &url, KIO::HTTP_METHOD, QByteArray &inputData, DataMode dataMode, const QMap<QByteArray, QByteArray> &extraHeaders = {});
90 [[nodiscard]] Response
91 makeRequest(const QUrl &url, KIO::HTTP_METHOD, QByteArray &inputData, DataMode dataMode, const QMap<QByteArray, QByteArray> &extraHeaders = {});
92
93 [[nodiscard]] KIO::WorkerResult davError(KIO::HTTP_METHOD method, const QUrl &url, const Response &response);
94 [[nodiscard]] KIO::WorkerResult davError(QString &errorMsg, KIO::HTTP_METHOD method, int code, const QUrl &_url, const QByteArray &responseData);
95 [[nodiscard]] KIO::WorkerResult sendHttpError(const QUrl &url, KIO::HTTP_METHOD method, const Response &response);
96 [[nodiscard]] KIO::WorkerResult davGeneric(const QUrl &url, KIO::HTTP_METHOD method, qint64 size = -1);
97 QString davProcessLocks();
98 static QByteArray methodToString(KIO::HTTP_METHOD method);
99
100 KIO::MetaData sslMetaData;
101 KIO::Error lastError = (KIO::Error)KJob::NoError;
102 QString m_hostName;
103};
104
105#endif
106

source code of kio/src/kioworkers/http/http.h