2022-02-16 08:49:13 +02:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
2014-11-13 01:04:59 +02:00
|
|
|
*/
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
#include "kio_curl.h"
|
2022-02-16 08:49:13 +02:00
|
|
|
#include "kcomponentdata.h"
|
2024-03-16 23:08:00 +02:00
|
|
|
#include "kmimetype.h"
|
2024-03-17 03:02:42 +02:00
|
|
|
#include "kremoteencoding.h"
|
2024-03-17 22:06:04 +02:00
|
|
|
#include "kconfiggroup.h"
|
2024-03-21 07:42:42 +02:00
|
|
|
#include "kstandarddirs.h"
|
|
|
|
#include "kmessagebox.h"
|
2024-03-16 23:08:00 +02:00
|
|
|
#include "kdebug.h"
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-05-13 22:12:38 +03:00
|
|
|
#include <QApplication>
|
2022-05-24 07:22:34 +03:00
|
|
|
#include <QHostAddress>
|
|
|
|
#include <QHostInfo>
|
2024-03-18 00:26:05 +02:00
|
|
|
#include <QDir>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-02-16 08:49:13 +02:00
|
|
|
#include <sys/types.h>
|
2024-03-16 23:08:00 +02:00
|
|
|
#include <sys/stat.h>
|
2022-02-16 08:49:13 +02:00
|
|
|
#include <unistd.h>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
#define KIO_CURL_ERROR(CODE) \
|
|
|
|
const QString curlerror = QString::fromAscii(curl_easy_strerror(CODE)); \
|
|
|
|
kWarning(7103) << "curl error" << curlerror; \
|
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curlerror);
|
|
|
|
|
|
|
|
// for reference:
|
|
|
|
// https://linux.die.net/man/3/strmode
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc959
|
|
|
|
// https://curl.se/libcurl/c/pop3-stat.html
|
2024-03-18 00:31:30 +02:00
|
|
|
// https://curl.se/libcurl/c/CURLOPT_QUOTE.html
|
2024-03-16 23:08:00 +02:00
|
|
|
|
2024-03-18 07:19:40 +02:00
|
|
|
static inline QByteArray ftpPermissions(const int permissions)
|
2024-03-18 05:22:26 +02:00
|
|
|
{
|
2024-03-18 06:13:28 +02:00
|
|
|
return QByteArray::number(permissions & 0777, 8);
|
2024-03-18 05:22:26 +02:00
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
static inline int ftpUserModeFromChar(const char modechar, const int rmode, const int wmode, const int xmode)
|
|
|
|
{
|
|
|
|
mode_t result = 0;
|
|
|
|
switch (modechar) {
|
|
|
|
case '-': {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'r': {
|
|
|
|
result |= rmode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'w': {
|
|
|
|
result |= wmode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'x': {
|
|
|
|
result |= xmode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
kWarning(7103) << "Invalid FTP mode char" << modechar;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-18 07:19:40 +02:00
|
|
|
static inline mode_t ftpModeFromString(const char* const modestring)
|
2024-03-16 23:08:00 +02:00
|
|
|
{
|
|
|
|
mode_t result = 0;
|
|
|
|
switch (modestring[0]) {
|
|
|
|
case '-': {
|
|
|
|
result |= S_IFREG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'b': {
|
|
|
|
result |= S_IFBLK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'c': {
|
|
|
|
result |= S_IFCHR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'd': {
|
|
|
|
result |= S_IFDIR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'l': {
|
|
|
|
result |= S_IFLNK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'p': {
|
|
|
|
result |= S_IFIFO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's': {
|
|
|
|
result |= S_IFSOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
kWarning(7103) << "Invalid FTP mode string" << modestring;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result |= ftpUserModeFromChar(modestring[1], S_IRUSR, S_IWUSR, S_IXUSR);
|
|
|
|
result |= ftpUserModeFromChar(modestring[2], S_IRUSR, S_IWUSR, S_IXUSR);
|
|
|
|
result |= ftpUserModeFromChar(modestring[3], S_IRUSR, S_IWUSR, S_IXUSR);
|
|
|
|
|
|
|
|
result |= ftpUserModeFromChar(modestring[4], S_IRGRP, S_IWGRP, S_IXGRP);
|
|
|
|
result |= ftpUserModeFromChar(modestring[5], S_IRGRP, S_IWGRP, S_IXGRP);
|
|
|
|
result |= ftpUserModeFromChar(modestring[6], S_IRGRP, S_IWGRP, S_IXGRP);
|
|
|
|
|
|
|
|
result |= ftpUserModeFromChar(modestring[7], S_IROTH, S_IWOTH, S_IXOTH);
|
|
|
|
result |= ftpUserModeFromChar(modestring[8], S_IROTH, S_IWOTH, S_IXOTH);
|
|
|
|
result |= ftpUserModeFromChar(modestring[9], S_IROTH, S_IWOTH, S_IXOTH);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-20 00:17:06 +02:00
|
|
|
// for reference:
|
|
|
|
// https://files.stairways.com/other/ftp-list-specs-info.txt
|
2024-03-20 23:58:18 +02:00
|
|
|
qlonglong ftpTimeFromString(const QByteArray &ftpmonth, const QByteArray &ftpday, const QByteArray &ftphouroryear,
|
|
|
|
const int currentyear)
|
2024-03-20 00:17:06 +02:00
|
|
|
{
|
|
|
|
const QString ftptimestring = ftpmonth + QLatin1Char(' ') + ftpday + QLatin1Char(' ') + ftphouroryear;
|
|
|
|
QDateTime ftpdatetime;
|
|
|
|
if (ftphouroryear.contains(':')) {
|
|
|
|
ftpdatetime = QDateTime::fromString(ftptimestring, "MMM d hh:mm");
|
|
|
|
// year is the last occurance of that date, when is that?
|
|
|
|
const QDate ftpdate = ftpdatetime.date();
|
2024-03-20 23:58:18 +02:00
|
|
|
ftpdatetime.setDate(QDate(currentyear, ftpdate.month(), ftpdate.day()));
|
2024-03-20 00:17:06 +02:00
|
|
|
} else {
|
|
|
|
ftpdatetime = QDateTime::fromString(ftptimestring, "MMM d yyyy");
|
|
|
|
}
|
|
|
|
// qDebug() << Q_FUNC_INFO << ftptimestring << ftpdatetime.toString();
|
|
|
|
return ftpdatetime.toTime_t();
|
|
|
|
}
|
|
|
|
|
2022-05-13 21:43:38 +03:00
|
|
|
static inline QByteArray curlProxyBytes(const QString &proxy)
|
2022-03-03 00:53:09 +02:00
|
|
|
{
|
|
|
|
const KUrl proxyurl(proxy);
|
2022-05-13 21:43:38 +03:00
|
|
|
const QString proxyhost = proxyurl.host();
|
2022-03-03 00:53:09 +02:00
|
|
|
if (proxyurl.port() > 0) {
|
2022-05-13 21:43:38 +03:00
|
|
|
QByteArray curlproxybytes = proxyhost.toAscii();
|
|
|
|
curlproxybytes.append(':');
|
|
|
|
curlproxybytes.append(QByteArray::number(proxyurl.port()));
|
|
|
|
return curlproxybytes;
|
2022-03-03 00:53:09 +02:00
|
|
|
}
|
2022-05-13 21:43:38 +03:00
|
|
|
return proxyhost.toAscii();
|
2022-03-03 00:53:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline curl_proxytype curlProxyType(const QString &proxy)
|
|
|
|
{
|
|
|
|
const QString proxyprotocol = KUrl(proxy).protocol();
|
|
|
|
|
2022-04-10 23:43:08 +03:00
|
|
|
#if CURL_AT_LEAST_VERSION(7, 52, 0)
|
2022-03-03 00:53:09 +02:00
|
|
|
if (proxyprotocol.startsWith(QLatin1String("https"))) {
|
|
|
|
return CURLPROXY_HTTPS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (proxyprotocol.startsWith(QLatin1String("socks4"))) {
|
|
|
|
return CURLPROXY_SOCKS4;
|
|
|
|
} else if (proxyprotocol.startsWith(QLatin1String("socks4a"))) {
|
|
|
|
return CURLPROXY_SOCKS4A;
|
|
|
|
} else if (proxyprotocol.startsWith(QLatin1String("socks5"))) {
|
|
|
|
return CURLPROXY_SOCKS5;
|
|
|
|
}
|
|
|
|
return CURLPROXY_HTTP;
|
|
|
|
}
|
|
|
|
|
2022-02-19 19:26:22 +02:00
|
|
|
static inline QString HTTPMIMEType(const QString &contenttype)
|
2022-02-16 10:46:58 +02:00
|
|
|
{
|
2022-02-19 19:26:22 +02:00
|
|
|
const QList<QString> splitcontenttype = contenttype.split(QLatin1Char(';'));
|
2022-05-11 22:24:56 +03:00
|
|
|
if (splitcontenttype.isEmpty() || splitcontenttype.at(0).isEmpty()) {
|
2022-02-19 19:26:22 +02:00
|
|
|
return QString::fromLatin1("application/octet-stream");
|
2022-02-16 10:46:58 +02:00
|
|
|
}
|
|
|
|
return splitcontenttype.at(0);
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
static inline long HTTPCode(CURL *curl)
|
2022-05-13 00:55:00 +03:00
|
|
|
{
|
2022-05-13 17:13:51 +03:00
|
|
|
long curlresponsecode = 0;
|
|
|
|
const CURLcode curlresult = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curlresponsecode);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
kWarning(7103) << "Could not get response code info" << curl_easy_strerror(curlresult);
|
2022-05-13 00:55:00 +03:00
|
|
|
}
|
2022-12-20 13:42:45 +02:00
|
|
|
kDebug(7103) << "HTTP error" << curlresponsecode;
|
2022-05-13 17:13:51 +03:00
|
|
|
return curlresponsecode;
|
2022-05-13 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2022-12-19 19:35:10 +02:00
|
|
|
// for reference:
|
|
|
|
// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
|
2022-05-13 19:46:43 +03:00
|
|
|
static inline KIO::Error HTTPToKIOError(const long httpcode)
|
|
|
|
{
|
|
|
|
switch (httpcode) {
|
|
|
|
case 401:
|
|
|
|
case 403:
|
|
|
|
case 407: {
|
2022-12-22 20:05:21 +02:00
|
|
|
return KIO::ERR_COULD_NOT_LOGIN;
|
2022-05-13 19:46:43 +03:00
|
|
|
}
|
2023-06-29 06:10:52 +03:00
|
|
|
case 408:
|
|
|
|
case 504: {
|
2022-05-13 19:46:43 +03:00
|
|
|
return KIO::ERR_SERVER_TIMEOUT;
|
|
|
|
}
|
2024-03-22 02:31:42 +02:00
|
|
|
case 416: {
|
|
|
|
return KIO::ERR_CANNOT_RESUME;
|
|
|
|
}
|
2022-05-13 19:46:43 +03:00
|
|
|
case 500: {
|
|
|
|
return KIO::ERR_INTERNAL_SERVER;
|
|
|
|
}
|
2022-12-19 19:35:10 +02:00
|
|
|
case 404:
|
2024-03-22 02:31:42 +02:00
|
|
|
case 501:
|
2022-12-19 19:35:10 +02:00
|
|
|
case 503: {
|
|
|
|
return KIO::ERR_SERVICE_NOT_AVAILABLE;
|
|
|
|
}
|
2022-05-13 19:46:43 +03:00
|
|
|
default: {
|
|
|
|
return KIO::ERR_NO_CONTENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline KIO::Error curlToKIOError(const CURLcode curlcode, CURL *curl)
|
2022-04-10 21:56:21 +03:00
|
|
|
{
|
2022-12-20 13:42:45 +02:00
|
|
|
kWarning(7103) << "curl error" << curl_easy_strerror(curlcode);
|
2022-04-10 21:56:21 +03:00
|
|
|
switch (curlcode) {
|
2022-05-13 19:46:43 +03:00
|
|
|
case CURLE_HTTP_RETURNED_ERROR:
|
|
|
|
case CURLE_ABORTED_BY_CALLBACK: {
|
|
|
|
const long httpcode = HTTPCode(curl);
|
|
|
|
return HTTPToKIOError(httpcode);
|
|
|
|
}
|
2022-04-10 21:56:21 +03:00
|
|
|
case CURLE_URL_MALFORMAT: {
|
|
|
|
return KIO::ERR_MALFORMED_URL;
|
|
|
|
}
|
2022-04-10 23:43:08 +03:00
|
|
|
#if CURL_AT_LEAST_VERSION(7, 73, 0)
|
2022-04-10 21:56:21 +03:00
|
|
|
case CURLE_PROXY:
|
2022-04-10 23:43:08 +03:00
|
|
|
#endif
|
2022-04-10 21:56:21 +03:00
|
|
|
case CURLE_COULDNT_RESOLVE_PROXY: {
|
|
|
|
return KIO::ERR_UNKNOWN_PROXY_HOST;
|
|
|
|
}
|
|
|
|
case CURLE_AUTH_ERROR:
|
2024-03-19 22:37:47 +02:00
|
|
|
case CURLE_LOGIN_DENIED: {
|
2022-12-22 20:05:21 +02:00
|
|
|
return KIO::ERR_COULD_NOT_LOGIN;
|
2022-04-10 21:56:21 +03:00
|
|
|
}
|
2024-03-19 22:37:47 +02:00
|
|
|
case CURLE_REMOTE_ACCESS_DENIED: {
|
|
|
|
return KIO::ERR_ACCESS_DENIED;
|
|
|
|
}
|
2022-04-10 21:56:21 +03:00
|
|
|
case CURLE_FILE_COULDNT_READ_FILE:
|
|
|
|
case CURLE_READ_ERROR: {
|
|
|
|
return KIO::ERR_COULD_NOT_READ;
|
|
|
|
}
|
|
|
|
case CURLE_WRITE_ERROR: {
|
|
|
|
return KIO::ERR_COULD_NOT_WRITE;
|
|
|
|
}
|
|
|
|
case CURLE_OUT_OF_MEMORY: {
|
|
|
|
return KIO::ERR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
case CURLE_BAD_DOWNLOAD_RESUME: {
|
|
|
|
return KIO::ERR_CANNOT_RESUME;
|
|
|
|
}
|
|
|
|
case CURLE_REMOTE_FILE_NOT_FOUND: {
|
|
|
|
return KIO::ERR_DOES_NOT_EXIST;
|
|
|
|
}
|
|
|
|
case CURLE_GOT_NOTHING: {
|
|
|
|
return KIO::ERR_NO_CONTENT;
|
|
|
|
}
|
|
|
|
case CURLE_REMOTE_DISK_FULL: {
|
|
|
|
return KIO::ERR_DISK_FULL;
|
|
|
|
}
|
|
|
|
case CURLE_OPERATION_TIMEDOUT: {
|
|
|
|
return KIO::ERR_SERVER_TIMEOUT;
|
|
|
|
}
|
2022-12-23 19:58:56 +02:00
|
|
|
case CURLE_REMOTE_FILE_EXISTS: {
|
|
|
|
return KIO::ERR_FILE_ALREADY_EXIST;
|
|
|
|
}
|
|
|
|
case CURLE_COULDNT_RESOLVE_HOST: {
|
|
|
|
return KIO::ERR_UNKNOWN_HOST;
|
|
|
|
}
|
2022-04-10 21:56:21 +03:00
|
|
|
case CURLE_COULDNT_CONNECT:
|
|
|
|
default: {
|
|
|
|
return KIO::ERR_COULD_NOT_CONNECT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2022-02-19 16:40:06 +02:00
|
|
|
size_t curlWriteCallback(char *ptr, size_t size, size_t nmemb, void *userdata)
|
|
|
|
{
|
2024-03-16 23:08:00 +02:00
|
|
|
CurlProtocol* curlprotocol = static_cast<CurlProtocol*>(userdata);
|
|
|
|
if (!curlprotocol) {
|
2022-02-19 16:40:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2022-02-19 19:44:16 +02:00
|
|
|
// size should always be 1
|
|
|
|
Q_ASSERT(size == 1);
|
2024-03-16 23:08:00 +02:00
|
|
|
curlprotocol->slotData(ptr, nmemb);
|
2022-02-19 16:40:06 +02:00
|
|
|
return nmemb;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:53:31 +02:00
|
|
|
size_t curlReadCallback(char *ptr, size_t size, size_t nmemb, void *userdata)
|
|
|
|
{
|
|
|
|
CurlProtocol* curlprotocol = static_cast<CurlProtocol*>(userdata);
|
|
|
|
if (!curlprotocol) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
curlprotocol->dataReq();
|
|
|
|
QByteArray kioreadbuffer;
|
|
|
|
const int kioreadresult = curlprotocol->readData(kioreadbuffer);
|
|
|
|
if (kioreadbuffer.size() > nmemb) {
|
|
|
|
kWarning(7103) << "Request data size larger than the buffer size";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
::memcpy(ptr, kioreadbuffer.constData(), kioreadbuffer.size() * sizeof(char));
|
|
|
|
return kioreadresult;
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:30:52 +03:00
|
|
|
int curlXFERCallback(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
2022-02-19 16:40:06 +02:00
|
|
|
{
|
2024-03-16 23:08:00 +02:00
|
|
|
CurlProtocol* curlprotocol = static_cast<CurlProtocol*>(userdata);
|
|
|
|
if (!curlprotocol) {
|
2022-02-19 16:40:06 +02:00
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
}
|
2024-03-21 07:42:42 +02:00
|
|
|
if (curlprotocol->p_aborttransfer) {
|
2022-05-13 17:13:51 +03:00
|
|
|
return CURLE_HTTP_RETURNED_ERROR;
|
|
|
|
}
|
2024-03-21 07:42:42 +02:00
|
|
|
if (curlprotocol->p_upload) {
|
2024-03-20 23:47:49 +02:00
|
|
|
curlprotocol->slotProgress(KIO::filesize_t(ulnow), KIO::filesize_t(ultotal));
|
|
|
|
} else {
|
|
|
|
curlprotocol->slotProgress(KIO::filesize_t(dlnow), KIO::filesize_t(dltotal));
|
|
|
|
}
|
2022-02-19 16:40:06 +02:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2024-03-21 07:42:42 +02:00
|
|
|
int curlKeyCallback(CURL *curl,
|
|
|
|
const struct curl_khkey *knownkey, const struct curl_khkey *foundkey,
|
|
|
|
enum curl_khmatch match,
|
|
|
|
void *userdata)
|
|
|
|
{
|
2024-03-21 07:46:00 +02:00
|
|
|
Q_UNUSED(curl);
|
|
|
|
Q_UNUSED(knownkey);
|
|
|
|
Q_UNUSED(foundkey);
|
2024-03-21 07:42:42 +02:00
|
|
|
CurlProtocol* curlprotocol = static_cast<CurlProtocol*>(userdata);
|
|
|
|
if (!curlprotocol) {
|
|
|
|
return CURLKHSTAT_REJECT;
|
|
|
|
}
|
|
|
|
if (match == CURLKHMATCH_OK) {
|
|
|
|
// nothing to do
|
|
|
|
return CURLKHSTAT_FINE;
|
|
|
|
}
|
2024-03-21 08:14:13 +02:00
|
|
|
const QString urlhost = curlprotocol->p_url.host();
|
|
|
|
// NOTE: the key is saved in kioslaverc
|
|
|
|
const QString kiodontaskagain = QLatin1String("kio_curl_") + urlhost;
|
2024-03-21 07:42:42 +02:00
|
|
|
const QString kiomessage = i18n(
|
2024-03-21 08:14:13 +02:00
|
|
|
"The host %1 cannot be verified,\ndo you want to accept the key and continue?",
|
|
|
|
urlhost
|
2024-03-21 07:42:42 +02:00
|
|
|
);
|
|
|
|
const QString kiocaption = i18n("Key mismatch");
|
2024-03-21 08:14:13 +02:00
|
|
|
const int messageresult = curlprotocol->messageBox(
|
|
|
|
kiomessage, KIO::SlaveBase::WarningContinueCancel, kiocaption,
|
|
|
|
i18n("&Yes"), i18n("&No"), kiodontaskagain
|
|
|
|
);
|
2024-03-21 07:42:42 +02:00
|
|
|
kWarning(7103) << "Key mismatch for" << curlprotocol->p_url << messageresult;
|
2024-03-21 08:14:13 +02:00
|
|
|
if (messageresult == KMessageBox::Continue) {
|
2024-03-21 07:42:42 +02:00
|
|
|
return CURLKHSTAT_FINE;
|
|
|
|
}
|
|
|
|
return CURLKHSTAT_REJECT;
|
|
|
|
}
|
|
|
|
|
2022-12-06 04:10:58 +02:00
|
|
|
int main(int argc, char **argv)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-05-13 22:12:38 +03:00
|
|
|
QApplication app(argc, argv);
|
2024-03-16 23:08:00 +02:00
|
|
|
KComponentData componentData("kio_curl", "kdelibs4");
|
2022-02-16 08:49:13 +02:00
|
|
|
|
2022-02-21 06:27:41 +02:00
|
|
|
kDebug(7103) << "Starting" << ::getpid();
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-12-06 04:10:58 +02:00
|
|
|
if (argc != 2) {
|
2024-03-16 23:08:00 +02:00
|
|
|
::fprintf(stderr, "Usage: kio_curl app-socket\n");
|
2022-02-17 13:55:51 +02:00
|
|
|
::exit(-1);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
CurlProtocol slave(argv[1]);
|
2014-11-13 01:04:59 +02:00
|
|
|
slave.dispatchLoop();
|
|
|
|
|
2022-02-16 08:49:13 +02:00
|
|
|
kDebug(7103) << "Done";
|
|
|
|
return 0;
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
CurlProtocol::CurlProtocol(const QByteArray &app)
|
|
|
|
: SlaveBase("curl", app),
|
2024-03-21 07:42:42 +02:00
|
|
|
p_aborttransfer(false), p_upload(false),
|
2024-03-21 00:51:11 +02:00
|
|
|
m_emitmime(true), m_ishttp(false), m_isftp(false), m_collectdata(false),
|
2024-03-18 05:22:26 +02:00
|
|
|
m_curl(nullptr), m_curlheaders(nullptr), m_curlquotes(nullptr)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-02-19 16:40:06 +02:00
|
|
|
m_curl = curl_easy_init();
|
|
|
|
if (!m_curl) {
|
|
|
|
kWarning(7103) << "Could not create context";
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
CurlProtocol::~CurlProtocol()
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-05-11 23:06:14 +03:00
|
|
|
if (m_curlheaders) {
|
|
|
|
curl_slist_free_all(m_curlheaders);
|
|
|
|
}
|
2024-03-18 05:22:26 +02:00
|
|
|
if (m_curlquotes) {
|
|
|
|
curl_slist_free_all(m_curlquotes);
|
|
|
|
}
|
2022-02-19 16:40:06 +02:00
|
|
|
if (m_curl) {
|
|
|
|
curl_easy_cleanup(m_curl);
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
void CurlProtocol::stat(const KUrl &url)
|
2022-05-11 23:06:14 +03:00
|
|
|
{
|
2024-03-16 23:08:00 +02:00
|
|
|
kDebug(7103) << "Stat URL" << url.prettyUrl();
|
2022-05-11 23:06:14 +03:00
|
|
|
|
2024-03-18 00:26:05 +02:00
|
|
|
KUrl staturl(url);
|
2024-03-20 03:39:37 +02:00
|
|
|
QString staturlfilename = staturl.fileName();
|
2024-03-18 03:38:19 +02:00
|
|
|
const QString staturlprotocol = staturl.protocol();
|
|
|
|
if (staturlprotocol == QLatin1String("ftp") || staturlprotocol == QLatin1String("sftp")) {
|
2024-03-20 03:39:37 +02:00
|
|
|
staturl.adjustPath(KUrl::RemoveTrailingSlash);
|
|
|
|
staturlfilename = staturl.fileName();
|
|
|
|
staturl.setFileName(QString());
|
|
|
|
staturl.adjustPath(KUrl::AddTrailingSlash);
|
2024-03-18 00:26:05 +02:00
|
|
|
}
|
2024-03-20 03:39:37 +02:00
|
|
|
kDebug(7103) << "Actual stat URL" << staturl << "filename" << staturlfilename;
|
2024-03-18 00:26:05 +02:00
|
|
|
|
2024-03-18 07:47:46 +02:00
|
|
|
if (!setupCurl(staturl, false)) {
|
2022-05-11 23:06:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-21 00:51:11 +02:00
|
|
|
if (m_isftp) {
|
2024-03-18 00:26:05 +02:00
|
|
|
m_collectdata = true;
|
2024-03-16 23:08:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 20:59:51 +02:00
|
|
|
KUrl redirecturl;
|
2024-03-20 03:31:08 +02:00
|
|
|
CURLcode curlresult = performCurl(url, &redirecturl);
|
2024-03-16 23:08:00 +02:00
|
|
|
kDebug(7103) << "Stat result" << curlresult;
|
2022-05-11 23:06:14 +03:00
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-17 20:59:51 +02:00
|
|
|
const KIO::Error kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
error(kioerror, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redirecturl.isValid()) {
|
|
|
|
redirection(redirecturl);
|
|
|
|
finished();
|
|
|
|
return;
|
2022-05-11 23:06:14 +03:00
|
|
|
}
|
|
|
|
|
2024-03-21 00:51:11 +02:00
|
|
|
if (m_isftp) {
|
2024-03-18 00:26:05 +02:00
|
|
|
foreach (const KIO::UDSEntry &kioudsentry, udsEntries()) {
|
2024-03-20 03:39:37 +02:00
|
|
|
if (kioudsentry.stringValue(KIO::UDSEntry::UDS_NAME) == staturlfilename) {
|
2024-03-18 00:26:05 +02:00
|
|
|
statEntry(kioudsentry);
|
|
|
|
finished();
|
|
|
|
return;
|
|
|
|
}
|
2024-03-16 23:08:00 +02:00
|
|
|
}
|
2024-03-20 03:39:37 +02:00
|
|
|
// HACK: fake the root directory for servers which do not include entries for "." and ".."
|
|
|
|
// when listing
|
|
|
|
if (staturlfilename.isEmpty() || staturlfilename == QDir::separator()) {
|
|
|
|
kDebug(7103) << "Faking root directory for" << url.prettyUrl();
|
|
|
|
KIO::UDSEntry kioudsentry;
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_NAME, QLatin1String("."));
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_MIME_TYPE, QLatin1String("inode/directory"));
|
|
|
|
statEntry(kioudsentry);
|
|
|
|
finished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kWarning(7103) << "Could not find entry for" << staturlfilename;
|
2024-03-18 00:26:05 +02:00
|
|
|
error(KIO::ERR_COULD_NOT_STAT, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString httpmimetype;
|
|
|
|
char *curlcontenttype = nullptr;
|
|
|
|
curlresult = curl_easy_getinfo(m_curl, CURLINFO_CONTENT_TYPE, &curlcontenttype);
|
|
|
|
if (curlresult == CURLE_OK) {
|
|
|
|
httpmimetype = HTTPMIMEType(QString::fromAscii(curlcontenttype));
|
|
|
|
} else {
|
|
|
|
kWarning(7103) << "Could not get content type info" << curl_easy_strerror(curlresult);
|
2022-05-13 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
curl_off_t curlfiletime = 0;
|
|
|
|
curlresult = curl_easy_getinfo(m_curl, CURLINFO_FILETIME_T, &curlfiletime);
|
2022-05-13 00:55:00 +03:00
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-13 17:13:51 +03:00
|
|
|
kWarning(7103) << "Could not get filetime info" << curl_easy_strerror(curlresult);
|
2022-05-13 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
curl_off_t curlcontentlength = 0;
|
|
|
|
curlresult = curl_easy_getinfo(m_curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &curlcontentlength);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
kWarning(7103) << "Could not get content length info" << curl_easy_strerror(curlresult);
|
2022-05-13 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
KIO::UDSEntry kioudsentry;
|
2024-03-16 23:08:00 +02:00
|
|
|
kDebug(7103) << "File time" << curlfiletime;
|
|
|
|
kDebug(7103) << "Content length" << curlcontentlength;
|
|
|
|
kDebug(7103) << "MIME type" << httpmimetype;
|
2024-03-18 03:38:19 +02:00
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_NAME, staturlfilename);
|
2022-05-13 17:13:51 +03:00
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_SIZE, qlonglong(curlcontentlength));
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME, qlonglong(curlfiletime));
|
|
|
|
if (!httpmimetype.isEmpty()) {
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_MIME_TYPE, httpmimetype);
|
|
|
|
}
|
2022-05-13 00:55:00 +03:00
|
|
|
statEntry(kioudsentry);
|
|
|
|
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
void CurlProtocol::listDir(const KUrl &url)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-03-16 23:08:00 +02:00
|
|
|
kDebug(7103) << "List URL" << url.prettyUrl();
|
2022-02-16 10:09:14 +02:00
|
|
|
|
2024-03-18 03:38:19 +02:00
|
|
|
// listing has to be done via URL ending with a slash, otherwise it is like file query
|
|
|
|
KUrl listurl(url);
|
|
|
|
listurl.adjustPath(KUrl::AddTrailingSlash);
|
2024-03-18 05:22:26 +02:00
|
|
|
|
2024-03-18 07:47:46 +02:00
|
|
|
if (!setupCurl(listurl, true)) {
|
2022-05-11 23:06:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
m_collectdata = true;
|
|
|
|
|
2024-03-17 20:59:51 +02:00
|
|
|
KUrl redirecturl;
|
2024-03-20 03:31:08 +02:00
|
|
|
CURLcode curlresult = performCurl(url, &redirecturl);
|
2024-03-16 23:08:00 +02:00
|
|
|
kDebug(7103) << "List result" << curlresult;
|
2022-05-11 23:06:14 +03:00
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-17 20:59:51 +02:00
|
|
|
const KIO::Error kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
error(kioerror, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redirecturl.isValid()) {
|
|
|
|
redirection(redirecturl);
|
|
|
|
finished();
|
|
|
|
return;
|
2022-05-11 23:06:14 +03:00
|
|
|
}
|
|
|
|
|
2024-03-18 00:26:05 +02:00
|
|
|
foreach (const KIO::UDSEntry &kioudsentry, udsEntries()) {
|
|
|
|
listEntry(kioudsentry, false);
|
2024-03-16 23:08:00 +02:00
|
|
|
}
|
2024-03-18 00:26:05 +02:00
|
|
|
KIO::UDSEntry kioudsentry;
|
|
|
|
listEntry(kioudsentry, true);
|
2024-03-16 23:08:00 +02:00
|
|
|
|
2022-05-11 23:06:14 +03:00
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
void CurlProtocol::get(const KUrl &url)
|
2022-05-30 21:09:36 +03:00
|
|
|
{
|
2024-03-16 23:08:00 +02:00
|
|
|
kDebug(7103) << "Get URL" << url.prettyUrl();
|
2023-06-27 04:04:18 +03:00
|
|
|
|
2024-03-18 07:47:46 +02:00
|
|
|
if (!setupCurl(url, false)) {
|
2022-05-30 21:09:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-20 08:23:22 +02:00
|
|
|
CURLcode curlresult = CURLE_OK;
|
|
|
|
if (hasMetaData(QLatin1String("resume"))) {
|
|
|
|
Q_ASSERT(sizeof(qlonglong) == sizeof(curl_off_t));
|
|
|
|
const qlonglong resumeoffset = metaData(QLatin1String("resume")).toLongLong();
|
|
|
|
kDebug(7103) << "Resume offset" << resumeoffset;
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_RESUME_FROM_LARGE, curl_off_t(resumeoffset));
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
canResume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 20:59:51 +02:00
|
|
|
KUrl redirecturl;
|
2024-03-20 08:23:22 +02:00
|
|
|
curlresult = performCurl(url, &redirecturl);
|
2024-03-16 23:08:00 +02:00
|
|
|
kDebug(7103) << "Get result" << curlresult;
|
2022-05-30 21:09:36 +03:00
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-17 20:59:51 +02:00
|
|
|
const KIO::Error kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
error(kioerror, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redirecturl.isValid()) {
|
|
|
|
redirection(redirecturl);
|
|
|
|
finished();
|
|
|
|
return;
|
2022-05-30 21:09:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:53:31 +02:00
|
|
|
void CurlProtocol::put(const KUrl &url, int permissions, KIO::JobFlags flags)
|
|
|
|
{
|
|
|
|
// NOTE: CURLOPT_NEW_FILE_PERMS is documented to work only for some protocols, ftp is not one
|
|
|
|
// of them but sftp is
|
|
|
|
|
|
|
|
kDebug(7103) << "Put URL" << url.prettyUrl() << permissions << flags;
|
|
|
|
|
|
|
|
if (!setupCurl(url, false)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-21 07:42:42 +02:00
|
|
|
p_upload = true;
|
2024-03-20 23:47:49 +02:00
|
|
|
|
2024-03-19 22:53:31 +02:00
|
|
|
CURLcode curlresult = CURLE_OK;
|
|
|
|
if (m_ishttp) {
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_POST, 1L);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return;
|
|
|
|
}
|
2024-03-19 23:07:32 +02:00
|
|
|
|
2024-03-20 08:23:22 +02:00
|
|
|
if (flags & KIO::Resume) {
|
|
|
|
// it is optional anyway
|
|
|
|
kWarning(7103) << "Resuming not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & KIO::Overwrite)) {
|
2024-03-22 10:57:30 +02:00
|
|
|
kWarning(7103) << "Not overwriting not implemented";
|
2024-03-20 08:23:22 +02:00
|
|
|
// TODO: check if destination exists, emit ERR_DIR_ALREADY_EXIST or ERR_FILE_ALREADY_EXIST
|
|
|
|
}
|
|
|
|
|
2024-03-19 23:07:32 +02:00
|
|
|
const QString putfilename = url.path();
|
|
|
|
const QByteArray putpermissions = ftpPermissions(permissions);
|
|
|
|
kDebug(7103) << "Filename" << putfilename << "permissions" << putpermissions;
|
|
|
|
|
|
|
|
const QByteArray putfilenamebytes = remoteEncoding()->encode(putfilename);
|
|
|
|
m_curlquotes = curl_slist_append(m_curlquotes, QByteArray("SITE CHMOD ") + putpermissions + " " + putfilenamebytes);
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_POSTQUOTE, m_curlquotes);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return;
|
|
|
|
}
|
2024-03-19 22:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KUrl redirecturl;
|
|
|
|
curlresult = performCurl(url, &redirecturl);
|
|
|
|
kDebug(7103) << "Put result" << curlresult;
|
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-19 23:07:32 +02:00
|
|
|
if (curlresult == CURLE_QUOTE_ERROR) {
|
|
|
|
error(KIO::ERR_CANNOT_CHMOD, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
2024-03-19 22:53:31 +02:00
|
|
|
const KIO::Error kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
error(kioerror, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redirecturl.isValid()) {
|
|
|
|
redirection(redirecturl);
|
|
|
|
finished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
2024-03-18 05:22:26 +02:00
|
|
|
void CurlProtocol::chmod(const KUrl &url, int permissions)
|
|
|
|
{
|
2024-03-18 06:13:28 +02:00
|
|
|
kDebug(7103) << "Chmod URL" << url.prettyUrl() << permissions;
|
2024-03-18 05:22:26 +02:00
|
|
|
|
|
|
|
KUrl chmodurl(url);
|
|
|
|
QString chmodfilename = chmodurl.path();
|
|
|
|
chmodurl.setPath(QString());
|
|
|
|
chmodurl.adjustPath(KUrl::AddTrailingSlash);
|
|
|
|
if (chmodfilename.isEmpty() || chmodfilename == QDir::separator()) {
|
|
|
|
// must be the root directory
|
|
|
|
chmodfilename = QLatin1String(".");
|
|
|
|
}
|
2024-03-18 07:19:40 +02:00
|
|
|
const QByteArray chmodpermissions = ftpPermissions(permissions);
|
2024-03-18 06:13:28 +02:00
|
|
|
kDebug(7103) << "Actual chmod URL" << chmodurl << "filename" << chmodfilename << "permissions" << chmodpermissions;
|
2024-03-18 05:22:26 +02:00
|
|
|
|
2024-03-18 07:47:46 +02:00
|
|
|
if (!setupCurl(chmodurl, true)) {
|
2024-03-18 05:22:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray chmodfilenamebytes = remoteEncoding()->encode(chmodfilename);
|
2024-03-18 06:13:28 +02:00
|
|
|
m_curlquotes = curl_slist_append(m_curlquotes, QByteArray("SITE CHMOD ") + chmodpermissions + " " + chmodfilenamebytes);
|
2024-03-18 05:22:26 +02:00
|
|
|
CURLcode curlresult = curl_easy_setopt(m_curl, CURLOPT_QUOTE, m_curlquotes);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
KUrl redirecturl;
|
2024-03-20 03:31:08 +02:00
|
|
|
curlresult = performCurl(url, &redirecturl);
|
2024-03-18 05:22:26 +02:00
|
|
|
kDebug(7103) << "Chmod result" << curlresult;
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
if (curlresult == CURLE_QUOTE_ERROR) {
|
|
|
|
error(KIO::ERR_CANNOT_CHMOD, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const KIO::Error kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
error(kioerror, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redirecturl.isValid()) {
|
|
|
|
redirection(redirecturl);
|
|
|
|
finished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
2024-03-18 06:13:28 +02:00
|
|
|
void CurlProtocol::mkdir(const KUrl &url, int permissions)
|
|
|
|
{
|
|
|
|
kDebug(7103) << "Mkdir URL" << url.prettyUrl() << permissions;
|
|
|
|
|
|
|
|
KUrl mkdirurl(url);
|
|
|
|
QString mkdirfilename = mkdirurl.path();
|
|
|
|
mkdirurl.setPath(QString());
|
|
|
|
mkdirurl.adjustPath(KUrl::AddTrailingSlash);
|
|
|
|
if (mkdirfilename.isEmpty() || mkdirfilename == QDir::separator()) {
|
|
|
|
// must be the root directory
|
|
|
|
mkdirfilename = QLatin1String(".");
|
|
|
|
}
|
2024-03-18 07:19:40 +02:00
|
|
|
const QByteArray mkdirpermissions = ftpPermissions(permissions);
|
2024-03-18 06:13:28 +02:00
|
|
|
kDebug(7103) << "Actual mkdir URL" << mkdirurl << "filename" << mkdirfilename << "permissions" << mkdirpermissions;
|
|
|
|
|
2024-03-18 07:47:46 +02:00
|
|
|
if (!setupCurl(mkdirurl, true)) {
|
2024-03-18 06:13:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray mkdirfilenamebytes = remoteEncoding()->encode(mkdirfilename);
|
|
|
|
m_curlquotes = curl_slist_append(m_curlquotes, QByteArray("MKD ") + mkdirfilenamebytes);
|
|
|
|
m_curlquotes = curl_slist_append(m_curlquotes, QByteArray("SITE CHMOD ") + mkdirpermissions + " " + mkdirfilenamebytes);
|
|
|
|
CURLcode curlresult = curl_easy_setopt(m_curl, CURLOPT_QUOTE, m_curlquotes);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
KUrl redirecturl;
|
2024-03-20 03:31:08 +02:00
|
|
|
curlresult = performCurl(url, &redirecturl);
|
2024-03-18 06:13:28 +02:00
|
|
|
kDebug(7103) << "Mkdir result" << curlresult;
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
if (curlresult == CURLE_QUOTE_ERROR) {
|
|
|
|
error(KIO::ERR_COULD_NOT_MKDIR, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const KIO::Error kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
error(kioerror, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redirecturl.isValid()) {
|
|
|
|
redirection(redirecturl);
|
|
|
|
finished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CurlProtocol::del(const KUrl &url, bool isfile)
|
|
|
|
{
|
2024-03-18 07:01:12 +02:00
|
|
|
kDebug(7103) << "Delete URL" << url.prettyUrl() << isfile;
|
2024-03-18 06:13:28 +02:00
|
|
|
|
|
|
|
KUrl delurl(url);
|
|
|
|
QString delfilename = delurl.path();
|
|
|
|
delurl.setPath(QString());
|
|
|
|
delurl.adjustPath(KUrl::AddTrailingSlash);
|
|
|
|
if (delfilename.isEmpty() || delfilename == QDir::separator()) {
|
|
|
|
// must be the root directory
|
|
|
|
delfilename = QLatin1String(".");
|
|
|
|
}
|
2024-03-18 07:01:12 +02:00
|
|
|
kDebug(7103) << "Actual Delete URL" << delurl << "filename" << delfilename;
|
2024-03-18 06:13:28 +02:00
|
|
|
|
2024-03-18 07:47:46 +02:00
|
|
|
if (!setupCurl(delurl, true)) {
|
2024-03-18 06:13:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray delfilenamebytes = remoteEncoding()->encode(delfilename);
|
|
|
|
if (isfile) {
|
|
|
|
m_curlquotes = curl_slist_append(m_curlquotes, QByteArray("DELE ") + delfilenamebytes);
|
|
|
|
} else {
|
|
|
|
m_curlquotes = curl_slist_append(m_curlquotes, QByteArray("RMD ") + delfilenamebytes);
|
|
|
|
}
|
|
|
|
CURLcode curlresult = curl_easy_setopt(m_curl, CURLOPT_QUOTE, m_curlquotes);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
KUrl redirecturl;
|
2024-03-20 03:31:08 +02:00
|
|
|
curlresult = performCurl(url, &redirecturl);
|
2024-03-18 07:01:12 +02:00
|
|
|
kDebug(7103) << "Delete result" << curlresult;
|
2024-03-18 06:13:28 +02:00
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
if (curlresult == CURLE_QUOTE_ERROR) {
|
|
|
|
error(KIO::ERR_CANNOT_DELETE, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const KIO::Error kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
error(kioerror, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redirecturl.isValid()) {
|
|
|
|
redirection(redirecturl);
|
|
|
|
finished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
void CurlProtocol::slotData(const char* curldata, const size_t curldatasize)
|
2022-05-11 23:06:14 +03:00
|
|
|
{
|
2024-03-21 07:42:42 +02:00
|
|
|
if (p_aborttransfer) {
|
2022-05-13 17:13:51 +03:00
|
|
|
kDebug(7103) << "Transfer still in progress";
|
2022-05-11 23:06:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
const QByteArray bytedata = QByteArray::fromRawData(curldata, curldatasize);
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
if (m_emitmime) {
|
|
|
|
m_emitmime = false;
|
2022-05-11 23:06:14 +03:00
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
if (m_ishttp) {
|
|
|
|
// if it's HTTP error do not send data and MIME, abort transfer
|
|
|
|
const long httpcode = HTTPCode(m_curl);
|
|
|
|
if (httpcode >= 400) {
|
2024-03-21 07:42:42 +02:00
|
|
|
p_aborttransfer = true;
|
2024-03-16 23:08:00 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-05-13 17:13:51 +03:00
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
QString httpmimetype = QString::fromLatin1("application/octet-stream");
|
|
|
|
char *curlcontenttype = nullptr;
|
|
|
|
CURLcode curlresult = curl_easy_getinfo(m_curl, CURLINFO_CONTENT_TYPE, &curlcontenttype);
|
|
|
|
if (curlresult == CURLE_OK) {
|
|
|
|
httpmimetype = HTTPMIMEType(QString::fromAscii(curlcontenttype));
|
|
|
|
} else {
|
|
|
|
kWarning(7103) << "Could not get content type info" << curl_easy_strerror(curlresult);
|
|
|
|
}
|
|
|
|
mimeType(httpmimetype);
|
2022-05-13 17:13:51 +03:00
|
|
|
} else {
|
2024-03-21 07:42:42 +02:00
|
|
|
KMimeType::Ptr kmimetype = KMimeType::findByNameAndContent(p_url.url(), bytedata);
|
2024-03-18 03:38:19 +02:00
|
|
|
// default MIME type should be returned in the worst case
|
|
|
|
Q_ASSERT(kmimetype);
|
2024-03-16 23:08:00 +02:00
|
|
|
mimeType(kmimetype->name());
|
2022-05-13 17:13:51 +03:00
|
|
|
}
|
2022-05-11 23:06:14 +03:00
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
if (m_collectdata) {
|
|
|
|
m_writedata.append(bytedata);
|
|
|
|
} else {
|
|
|
|
data(bytedata);
|
|
|
|
}
|
2022-05-11 23:06:14 +03:00
|
|
|
|
|
|
|
curl_off_t curlspeeddownload = 0;
|
|
|
|
CURLcode curlresult = curl_easy_getinfo(m_curl, CURLINFO_SPEED_DOWNLOAD_T, &curlspeeddownload);
|
|
|
|
if (curlresult == CURLE_OK) {
|
|
|
|
kDebug(7103) << "Download speed" << curlspeeddownload;
|
|
|
|
speed(ulong(curlspeeddownload));
|
|
|
|
} else {
|
|
|
|
kWarning(7103) << "Could not get download speed info" << curl_easy_strerror(curlresult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 08:23:22 +02:00
|
|
|
void CurlProtocol::slotProgress(const KIO::filesize_t progress, const KIO::filesize_t total)
|
2022-05-11 23:06:14 +03:00
|
|
|
{
|
2024-03-20 08:23:22 +02:00
|
|
|
kDebug(7103) << "Progress is" << progress << "from" << total;
|
|
|
|
processedSize(progress);
|
|
|
|
if (total > 0 && progress != total) {
|
2023-06-13 02:38:02 +03:00
|
|
|
totalSize(total);
|
2022-05-11 23:06:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:14:28 +02:00
|
|
|
CURLcode CurlProtocol::setupAuth(const QString &username, const QString &password)
|
|
|
|
{
|
|
|
|
CURLcode curlresult = CURLE_OK;
|
|
|
|
const QByteArray urlusernamebytes = username.toAscii();
|
|
|
|
if (!urlusernamebytes.isEmpty()) {
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_USERNAME, urlusernamebytes.constData());
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
return curlresult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const QByteArray urlpasswordbytes = password.toAscii();
|
|
|
|
if (!urlpasswordbytes.isEmpty()) {
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_PASSWORD, urlpasswordbytes.constData());
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
return curlresult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return curlresult;
|
|
|
|
}
|
|
|
|
|
2024-03-21 00:51:11 +02:00
|
|
|
bool CurlProtocol::setupCurl(const KUrl &url, const bool ftp)
|
2022-05-24 07:22:34 +03:00
|
|
|
{
|
2024-03-18 07:47:46 +02:00
|
|
|
if (Q_UNLIKELY(!m_curl)) {
|
|
|
|
error(KIO::ERR_OUT_OF_MEMORY, QString::fromLatin1("Null context"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-24 07:22:34 +03:00
|
|
|
// curl cannot verify certs if the host is address, CURLOPT_USE_SSL set to CURLUSESSL_TRY
|
|
|
|
// does not bypass such cases so resolving it manually
|
|
|
|
const QHostAddress urladdress(url.host());
|
|
|
|
if (!urladdress.isNull()) {
|
|
|
|
const QHostInfo urlinfo = QHostInfo::fromName(url.host());
|
|
|
|
if (urlinfo.error() == QHostInfo::NoError) {
|
|
|
|
KUrl newurl(url);
|
|
|
|
newurl.setHost(urlinfo.hostName());
|
|
|
|
kDebug(7103) << "Rewrote" << url << "to" << newurl;
|
2024-03-29 06:07:01 +02:00
|
|
|
// NOTE: redirect to the same URL is cycril link error
|
|
|
|
if (url != newurl) {
|
|
|
|
redirection(newurl);
|
|
|
|
finished();
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-24 07:22:34 +03:00
|
|
|
} else {
|
2024-03-19 00:09:02 +02:00
|
|
|
kWarning(7103) << "Could not resolve" << url.host();
|
2022-05-24 07:22:34 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-19 16:40:06 +02:00
|
|
|
|
2024-03-21 07:42:42 +02:00
|
|
|
p_aborttransfer = false;
|
|
|
|
p_upload = false;
|
|
|
|
p_url = url;
|
2022-05-13 22:12:38 +03:00
|
|
|
m_emitmime = true;
|
2024-03-16 23:08:00 +02:00
|
|
|
const QString urlprotocol = url.protocol();
|
|
|
|
m_ishttp = (urlprotocol == QLatin1String("http") || urlprotocol == QLatin1String("https"));
|
2024-03-21 00:51:11 +02:00
|
|
|
m_isftp = (urlprotocol == QLatin1String("ftp") || urlprotocol == QLatin1String("sftp"));
|
2024-03-16 23:08:00 +02:00
|
|
|
m_collectdata = false;
|
|
|
|
m_writedata.clear();
|
2024-03-18 07:47:46 +02:00
|
|
|
|
2024-03-21 00:51:11 +02:00
|
|
|
if (ftp && !m_isftp) {
|
2024-03-18 07:47:46 +02:00
|
|
|
// only for FTP or SFTP
|
|
|
|
error(KIO::ERR_INTERNAL, url.prettyUrl());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:23 +02:00
|
|
|
curl_easy_reset(m_curl);
|
2022-02-23 18:14:27 +02:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1L);
|
2022-05-13 17:13:51 +03:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_FILETIME, 1L);
|
2024-03-21 18:46:39 +02:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_FAILONERROR, 1L);
|
2022-02-21 12:42:23 +02:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
|
2022-03-03 00:53:09 +02:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_MAXREDIRS, 100L); // proxies apparently cause a lot of redirects
|
2022-03-05 05:30:07 +02:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, SlaveBase::connectTimeout());
|
2022-04-22 09:30:52 +03:00
|
|
|
// curl_easy_setopt(m_curl, CURLOPT_IGNORE_CONTENT_LENGTH, 1L); // breaks XFER info, fixes transfer of chunked content
|
2022-02-21 12:42:23 +02:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, this);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
|
2024-03-19 22:53:31 +02:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_READDATA, this);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, curlReadCallback);
|
2022-04-22 09:30:52 +03:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L); // otherwise the XFER info callback is not called
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, this);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, curlXFERCallback);
|
2024-03-21 07:42:42 +02:00
|
|
|
// CURLOPT_SSH_KNOWNHOSTS has to be set for the callback, it is conditionally bellow
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_SSH_KEYDATA, this);
|
2024-03-21 18:46:39 +02:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_SSH_KEYFUNCTION, curlKeyCallback);
|
2022-12-14 14:24:42 +02:00
|
|
|
// curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L); // debugging
|
2022-03-05 04:38:52 +02:00
|
|
|
|
2024-03-18 23:34:16 +02:00
|
|
|
// NOTE: the URL path has to be percentage-encoded, otherwise curl will reject it if it
|
|
|
|
// contains whitespace for example
|
|
|
|
const QByteArray urlbytes = url.toEncoded();
|
2022-02-21 12:51:49 +02:00
|
|
|
CURLcode curlresult = curl_easy_setopt(m_curl, CURLOPT_URL, urlbytes.constData());
|
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-16 23:08:00 +02:00
|
|
|
KIO_CURL_ERROR(curlresult);
|
2022-05-11 23:06:14 +03:00
|
|
|
return false;
|
2022-02-21 12:51:49 +02:00
|
|
|
}
|
2022-02-17 11:34:46 +02:00
|
|
|
|
2023-01-01 04:57:40 +02:00
|
|
|
#if CURL_AT_LEAST_VERSION(7, 85, 0)
|
2024-03-16 23:08:00 +02:00
|
|
|
static const char* const curlprotocols = "http,https,ftp,sftp";
|
2023-01-01 04:57:40 +02:00
|
|
|
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROTOCOLS_STR, curlprotocols);
|
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-16 23:08:00 +02:00
|
|
|
KIO_CURL_ERROR(curlresult);
|
2023-01-01 04:57:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_REDIR_PROTOCOLS_STR, curlprotocols);
|
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-16 23:08:00 +02:00
|
|
|
KIO_CURL_ERROR(curlresult);
|
2023-01-01 04:57:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// deprecated since v7.85.0
|
2024-03-16 23:08:00 +02:00
|
|
|
static const long const curlprotocols = (CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FTP | CURLPROTO_SFTP);
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROTOCOLS, curlprotocols);
|
2022-03-16 09:18:32 +02:00
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-16 23:08:00 +02:00
|
|
|
KIO_CURL_ERROR(curlresult);
|
2022-05-11 23:06:14 +03:00
|
|
|
return false;
|
2022-03-16 09:31:47 +02:00
|
|
|
}
|
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_REDIR_PROTOCOLS, curlprotocols);
|
2022-03-16 09:31:47 +02:00
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-16 23:08:00 +02:00
|
|
|
KIO_CURL_ERROR(curlresult);
|
2022-05-11 23:06:14 +03:00
|
|
|
return false;
|
2022-03-16 09:18:32 +02:00
|
|
|
}
|
2023-01-01 04:57:40 +02:00
|
|
|
#endif
|
2022-03-16 09:18:32 +02:00
|
|
|
|
2022-02-18 17:43:59 +02:00
|
|
|
kDebug(7103) << "Metadata" << allMetaData();
|
2022-03-05 04:38:52 +02:00
|
|
|
|
2022-02-19 19:44:16 +02:00
|
|
|
if (hasMetaData(QLatin1String("UserAgent"))) {
|
2022-02-19 16:40:06 +02:00
|
|
|
const QByteArray useragentbytes = metaData("UserAgent").toAscii();
|
2022-02-21 12:51:49 +02:00
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_USERAGENT, useragentbytes.constData());
|
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-16 23:08:00 +02:00
|
|
|
KIO_CURL_ERROR(curlresult);
|
2022-05-13 01:31:00 +03:00
|
|
|
return false;
|
2022-02-21 12:51:49 +02:00
|
|
|
}
|
2022-02-18 17:43:59 +02:00
|
|
|
}
|
2022-03-05 04:38:52 +02:00
|
|
|
|
|
|
|
const bool noauth = (metaData("no-auth") == QLatin1String("yes"));
|
2022-02-19 19:44:16 +02:00
|
|
|
if (hasMetaData(QLatin1String("UseProxy"))) {
|
2022-03-03 00:53:09 +02:00
|
|
|
const QString proxystring = metaData("UseProxy");
|
2022-05-13 21:43:38 +03:00
|
|
|
const QByteArray proxybytes = curlProxyBytes(proxystring);
|
2022-03-03 00:53:09 +02:00
|
|
|
const curl_proxytype curlproxytype = curlProxyType(proxystring);
|
|
|
|
kDebug(7103) << "Proxy" << proxybytes << curlproxytype;
|
2022-02-21 12:51:49 +02:00
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROXY, proxybytes.constData());
|
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-16 23:08:00 +02:00
|
|
|
KIO_CURL_ERROR(curlresult);
|
2022-05-11 23:06:14 +03:00
|
|
|
return false;
|
2022-02-21 12:51:49 +02:00
|
|
|
}
|
2022-03-05 04:38:52 +02:00
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROXYTYPE, curlproxytype);
|
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-16 23:08:00 +02:00
|
|
|
KIO_CURL_ERROR(curlresult);
|
2022-05-13 01:31:00 +03:00
|
|
|
return false;
|
2022-03-05 04:38:52 +02:00
|
|
|
}
|
2022-03-03 00:53:09 +02:00
|
|
|
|
2022-03-05 04:38:52 +02:00
|
|
|
const bool noproxyauth = (noauth || metaData("no-proxy-auth") == QLatin1String("yes"));
|
2022-05-23 08:32:03 +03:00
|
|
|
kDebug(7103) << "No proxy auth" << noproxyauth;
|
2022-03-05 04:38:52 +02:00
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROXYAUTH, noproxyauth ? CURLAUTH_NONE : CURLAUTH_ANY);
|
|
|
|
if (curlresult != CURLE_OK) {
|
2024-03-16 23:08:00 +02:00
|
|
|
KIO_CURL_ERROR(curlresult);
|
2022-05-13 01:31:00 +03:00
|
|
|
return false;
|
2022-03-05 04:38:52 +02:00
|
|
|
}
|
2022-02-19 19:18:35 +02:00
|
|
|
}
|
2022-03-05 04:38:52 +02:00
|
|
|
|
2022-05-23 08:32:03 +03:00
|
|
|
const bool nowwwauth = (noauth || metaData("no-www-auth") == QLatin1String("true"));
|
|
|
|
kDebug(7103) << "No WWW auth" << nowwwauth;
|
2024-03-16 23:08:00 +02:00
|
|
|
if (m_ishttp) {
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_HTTPAUTH, nowwwauth ? CURLAUTH_NONE : CURLAUTH_ANY);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-05 04:38:52 +02:00
|
|
|
}
|
2024-03-17 07:22:54 +02:00
|
|
|
|
|
|
|
curlresult = setupAuth(url.userName(), url.password());
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return false;
|
2022-05-13 21:43:38 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 23:06:14 +03:00
|
|
|
if (m_curlheaders) {
|
|
|
|
curl_slist_free_all(m_curlheaders);
|
|
|
|
m_curlheaders = nullptr;
|
|
|
|
}
|
2024-03-16 23:08:00 +02:00
|
|
|
if (m_ishttp) {
|
|
|
|
if (hasMetaData(QLatin1String("Languages"))) {
|
|
|
|
m_curlheaders = curl_slist_append(m_curlheaders, QByteArray("Accept-Language: ") + metaData("Languages").toAscii());
|
|
|
|
}
|
2022-03-05 04:38:52 +02:00
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
if (hasMetaData(QLatin1String("Charsets"))) {
|
|
|
|
m_curlheaders = curl_slist_append(m_curlheaders, QByteArray("Accept-Charset: ") + metaData("Charsets").toAscii());
|
|
|
|
}
|
2022-03-05 04:38:52 +02:00
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
if (hasMetaData(QLatin1String("accept"))) {
|
|
|
|
m_curlheaders = curl_slist_append(m_curlheaders, QByteArray("Accept: ") + metaData("accept").toAscii());
|
|
|
|
}
|
2022-03-05 04:38:52 +02:00
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
if (hasMetaData(QLatin1String("Authorization"))) {
|
|
|
|
m_curlheaders = curl_slist_append(m_curlheaders, QByteArray("Authorization: ") + metaData("Authorization").toAscii());
|
|
|
|
}
|
2023-09-16 13:46:36 +03:00
|
|
|
|
2024-03-16 23:08:00 +02:00
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, m_curlheaders);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
curl_slist_free_all(m_curlheaders);
|
|
|
|
m_curlheaders = nullptr;
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-28 01:00:05 +03:00
|
|
|
}
|
|
|
|
|
2024-03-18 05:22:26 +02:00
|
|
|
if (m_curlquotes) {
|
|
|
|
curl_slist_free_all(m_curlquotes);
|
|
|
|
m_curlquotes = nullptr;
|
|
|
|
}
|
2024-03-21 00:51:11 +02:00
|
|
|
if (m_isftp) {
|
2024-03-17 22:06:04 +02:00
|
|
|
// NOTE: this is stored in kio_ftprc
|
|
|
|
const long disablepassivemode = config()->readEntry("DisablePassiveMode", false);
|
|
|
|
kDebug(7103) << "Disable passive mode" << disablepassivemode;
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_FTP_SKIP_PASV_IP, disablepassivemode);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-21 06:52:45 +02:00
|
|
|
|
|
|
|
// no callback for keyboard input, disable CURLSSH_AUTH_KEYBOARD so that curl does not
|
|
|
|
// under any circumstances try to access stdin
|
|
|
|
static long curlsshauthtypes = (
|
|
|
|
CURLSSH_AUTH_PUBLICKEY | CURLSSH_AUTH_PASSWORD | CURLSSH_AUTH_HOST |
|
|
|
|
CURLSSH_AUTH_AGENT | CURLSSH_AUTH_GSSAPI
|
|
|
|
);
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_SSH_AUTH_TYPES, curlsshauthtypes);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-21 07:42:42 +02:00
|
|
|
|
|
|
|
const QByteArray curlsshknownhosts = QFile::encodeName(KStandardDirs::locateLocal("data", "known_hosts"));
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_SSH_KNOWNHOSTS, curlsshknownhosts.constData());
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO_CURL_ERROR(curlresult);
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-17 22:06:04 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 23:06:14 +03:00
|
|
|
return true;
|
2022-02-17 14:17:40 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 20:59:51 +02:00
|
|
|
// NOTE: redirection is done so that the URL in navigation is corrected, notably its user and
|
|
|
|
// password part
|
2024-03-18 03:38:19 +02:00
|
|
|
CURLcode CurlProtocol::performCurl(const KUrl &url, KUrl *redirecturl)
|
2024-03-17 20:59:51 +02:00
|
|
|
{
|
|
|
|
CURLcode curlresult = curl_easy_perform(m_curl);
|
2024-03-18 03:38:19 +02:00
|
|
|
const QString urlusername = url.userName();
|
2024-03-17 20:59:51 +02:00
|
|
|
KIO::AuthInfo kioauthinfo;
|
2024-03-18 03:38:19 +02:00
|
|
|
kioauthinfo.url = url;
|
2024-03-17 20:59:51 +02:00
|
|
|
kioauthinfo.username = urlusername;
|
2024-03-18 03:38:19 +02:00
|
|
|
kioauthinfo.password = url.password();
|
2024-03-17 20:59:51 +02:00
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO::Error kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
if (kioerror == KIO::ERR_COULD_NOT_LOGIN) {
|
2024-03-18 03:38:19 +02:00
|
|
|
kDebug(7103) << "Authorizing from cache" << url.prettyUrl();
|
2024-03-17 20:59:51 +02:00
|
|
|
if (checkCachedAuthentication(kioauthinfo)) {
|
|
|
|
curlresult = setupAuth(kioauthinfo.username, kioauthinfo.password);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
return curlresult;
|
|
|
|
}
|
|
|
|
curlresult = curl_easy_perform(m_curl);
|
2024-03-23 03:41:59 +02:00
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
if (kioerror != KIO::ERR_COULD_NOT_LOGIN) {
|
|
|
|
kDebug(7103) << "Going to redirect for cache authorization";
|
|
|
|
KUrl newurl(url);
|
|
|
|
newurl.setUserName(kioauthinfo.username);
|
|
|
|
newurl.setPassword(kioauthinfo.password);
|
|
|
|
*redirecturl = newurl;
|
|
|
|
}
|
2024-03-17 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
KIO::Error kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
if (kioerror == KIO::ERR_COULD_NOT_LOGIN) {
|
2024-03-18 03:38:19 +02:00
|
|
|
kDebug(7103) << "Authorizing" << url.prettyUrl();
|
2024-03-17 20:59:51 +02:00
|
|
|
kioauthinfo.keepPassword = true;
|
|
|
|
kioauthinfo.prompt = i18n("You need to supply a username and a password to access this URL.");
|
|
|
|
kioauthinfo.commentLabel = i18n("URL:");
|
2024-03-18 03:38:19 +02:00
|
|
|
kioauthinfo.comment = i18n("<b>%1</b>", url.prettyUrl());
|
2024-03-17 20:59:51 +02:00
|
|
|
if (openPasswordDialog(kioauthinfo)) {
|
|
|
|
curlresult = setupAuth(kioauthinfo.username, kioauthinfo.password);
|
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
return curlresult;
|
|
|
|
}
|
|
|
|
if (kioauthinfo.keepPassword) {
|
|
|
|
kDebug(7103) << "Caching authorization";
|
|
|
|
cacheAuthentication(kioauthinfo);
|
|
|
|
}
|
|
|
|
curlresult = curl_easy_perform(m_curl);
|
2024-03-23 03:41:59 +02:00
|
|
|
if (curlresult != CURLE_OK) {
|
|
|
|
kioerror = curlToKIOError(curlresult, m_curl);
|
|
|
|
if (kioerror != KIO::ERR_COULD_NOT_LOGIN) {
|
|
|
|
kDebug(7103) << "Going to redirect for authorization";
|
|
|
|
KUrl newurl(url);
|
|
|
|
newurl.setUserName(kioauthinfo.username);
|
|
|
|
newurl.setPassword(kioauthinfo.password);
|
|
|
|
*redirecturl = newurl;
|
|
|
|
}
|
2024-03-17 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return curlresult;
|
|
|
|
}
|
|
|
|
|
2024-03-18 00:26:05 +02:00
|
|
|
QList<KIO::UDSEntry> CurlProtocol::udsEntries()
|
|
|
|
{
|
|
|
|
QList<KIO::UDSEntry> result;
|
|
|
|
|
|
|
|
kDebug(7103) << "Encoding" << remoteEncoding()->encoding();
|
|
|
|
|
2024-03-20 00:17:06 +02:00
|
|
|
// sample line:
|
|
|
|
// drwxr-xr-x 1 nobody nobody 512 Mar 19 19:17 .
|
2024-03-19 00:17:23 +02:00
|
|
|
static const QByteArray linkseparator = QByteArray("->");
|
2024-03-20 23:58:18 +02:00
|
|
|
const QDate currentdate = QDate::currentDate();
|
2024-03-18 00:26:05 +02:00
|
|
|
foreach(const QByteArray &line, m_writedata.split('\n')) {
|
|
|
|
if (line.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-03-18 08:38:26 +02:00
|
|
|
QList<QByteArray> lineparts;
|
|
|
|
foreach (const QByteArray &linepart, line.split(' ')) {
|
|
|
|
if (linepart.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lineparts.append(linepart);
|
|
|
|
}
|
|
|
|
// qDebug() << Q_FUNC_INFO << lineparts;
|
|
|
|
|
|
|
|
// basic validation
|
|
|
|
if (lineparts.size() < 9) {
|
|
|
|
kWarning(7103) << "Invalid FTP data line" << line;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// take out the link parts, if any
|
|
|
|
QByteArray ftplinkpath;
|
2024-03-19 00:17:23 +02:00
|
|
|
const int linkseparatorindex = lineparts.indexOf(linkseparator);
|
2024-03-18 08:38:26 +02:00
|
|
|
if (linkseparatorindex > 0) {
|
|
|
|
foreach (const QByteArray &linkpart, lineparts.mid(linkseparatorindex)) {
|
|
|
|
ftplinkpath.append(linkpart);
|
|
|
|
ftplinkpath.append(' ');
|
|
|
|
}
|
|
|
|
ftplinkpath.chop(1);
|
|
|
|
lineparts = lineparts.mid(0, linkseparatorindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// another validation just in case
|
|
|
|
if (lineparts.size() < 9) {
|
|
|
|
kWarning(7103) << "Invalid FTP data line" << line;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:04:36 +02:00
|
|
|
// now take out everything but the filepath parts
|
2024-03-18 08:38:26 +02:00
|
|
|
const QByteArray ftpmode = lineparts.at(0);
|
|
|
|
const QByteArray ftpowner = lineparts.at(2);
|
|
|
|
const QByteArray ftpgroup = lineparts.at(3);
|
|
|
|
const qlonglong ftpsize = lineparts.at(4).toLongLong();
|
2024-03-20 00:17:06 +02:00
|
|
|
const QByteArray ftpmonth = lineparts.at(5);
|
|
|
|
const QByteArray ftpday = lineparts.at(6);
|
|
|
|
const QByteArray ftphouroryear = lineparts.at(7);
|
2024-03-18 08:38:26 +02:00
|
|
|
lineparts = lineparts.mid(8);
|
2024-03-19 22:04:36 +02:00
|
|
|
|
|
|
|
// and finally the filepath parts
|
2024-03-18 08:38:26 +02:00
|
|
|
QByteArray ftpfilepath;
|
|
|
|
foreach (const QByteArray &filepart, lineparts) {
|
|
|
|
ftpfilepath.append(filepart);
|
|
|
|
ftpfilepath.append(' ');
|
|
|
|
}
|
|
|
|
ftpfilepath.chop(1);
|
|
|
|
|
2024-03-20 00:17:06 +02:00
|
|
|
// qDebug() << Q_FUNC_INFO << ftpmode << ftpowner << ftpgroup << ftpsize << ftpmonth << ftpday << ftphouroryear << ftpfilepath << ftplinkpath;
|
2024-03-18 08:38:26 +02:00
|
|
|
|
|
|
|
KIO::UDSEntry kioudsentry;
|
|
|
|
const mode_t stdmode = ftpModeFromString(ftpmode);
|
2024-03-20 23:58:18 +02:00
|
|
|
const qlonglong ftpmodtime = ftpTimeFromString(ftpmonth, ftpday, ftphouroryear, currentdate.year());
|
2024-03-18 08:38:26 +02:00
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_NAME, remoteEncoding()->decode(ftpfilepath));
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_FILE_TYPE, stdmode & S_IFMT);
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_ACCESS, stdmode & 07777);
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_SIZE, ftpsize);
|
2024-03-22 12:07:13 +02:00
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_USER, QString::fromLatin1(ftpowner.constData(), ftpowner.size()));
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_GROUP, QString::fromLatin1(ftpgroup.constData(), ftpgroup.size()));
|
2024-03-20 00:17:06 +02:00
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME, ftpmodtime);
|
2024-03-18 08:38:26 +02:00
|
|
|
if (!ftplinkpath.isEmpty()) {
|
2024-03-18 00:26:05 +02:00
|
|
|
// link paths to current path causes KIO to do strange things
|
2024-03-18 08:38:26 +02:00
|
|
|
if (ftplinkpath.at(0) != '.' && ftplinkpath.size() != 1) {
|
2024-03-18 00:26:05 +02:00
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_LINK_DEST, remoteEncoding()->decode(ftplinkpath));
|
|
|
|
}
|
|
|
|
if (ftpsize <= 0) {
|
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_GUESSED_MIME_TYPE, QString::fromLatin1("application/x-zerosize"));
|
|
|
|
}
|
|
|
|
}
|
2024-03-18 08:38:26 +02:00
|
|
|
result.append(kioudsentry);
|
2024-03-18 00:26:05 +02:00
|
|
|
}
|
2024-03-18 07:52:20 +02:00
|
|
|
// at this point the transfer should be complete, release the memory
|
|
|
|
m_writedata.clear();
|
2024-03-18 00:26:05 +02:00
|
|
|
return result;
|
|
|
|
}
|