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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "http.h"
|
2022-02-16 08:49:13 +02:00
|
|
|
#include "kdebug.h"
|
|
|
|
#include "kcomponentdata.h"
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-02-16 08:49:13 +02:00
|
|
|
#include <QCoreApplication>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-02-16 08:49:13 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-03-03 00:53:09 +02:00
|
|
|
static inline QByteArray curlProxyString(const QString &proxy)
|
|
|
|
{
|
|
|
|
const KUrl proxyurl(proxy);
|
|
|
|
if (proxyurl.port() > 0) {
|
|
|
|
return QString::fromLatin1("%1:%2").arg(proxyurl.host()).arg(proxyurl.port()).toAscii();
|
|
|
|
}
|
|
|
|
return proxyurl.host().toAscii();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-21 06:09:45 +02:00
|
|
|
static inline QString HTTPCharset(const QString &contenttype)
|
|
|
|
{
|
|
|
|
const QList<QString> splitcontenttype = contenttype.split(QLatin1Char(';'));
|
2022-02-21 06:27:41 +02:00
|
|
|
if (splitcontenttype.size() < 2) {
|
2022-02-21 06:09:45 +02:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
return splitcontenttype.at(1);
|
|
|
|
}
|
|
|
|
|
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-05-13 17:13:51 +03:00
|
|
|
return curlresponsecode;
|
2022-05-13 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 23:06:14 +03:00
|
|
|
static inline KIO::Error curlToKIOError(const CURLcode curlcode)
|
2022-04-10 21:56:21 +03:00
|
|
|
{
|
|
|
|
switch (curlcode) {
|
|
|
|
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:
|
|
|
|
case CURLE_LOGIN_DENIED:
|
|
|
|
case CURLE_REMOTE_ACCESS_DENIED: {
|
|
|
|
return KIO::ERR_COULD_NOT_AUTHENTICATE;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
case CURLE_COULDNT_CONNECT:
|
|
|
|
default: {
|
|
|
|
return KIO::ERR_COULD_NOT_CONNECT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2022-05-11 23:06:14 +03:00
|
|
|
static inline KIO::Error HTTPToKIOError(const int httpcode)
|
|
|
|
{
|
|
|
|
switch (httpcode) {
|
2022-05-11 23:25:33 +03:00
|
|
|
case 401:
|
|
|
|
case 403:
|
|
|
|
case 407: {
|
2022-05-11 23:06:14 +03:00
|
|
|
return KIO::ERR_COULD_NOT_AUTHENTICATE;
|
|
|
|
}
|
2022-05-11 23:25:33 +03:00
|
|
|
case 408: {
|
|
|
|
return KIO::ERR_SERVER_TIMEOUT;
|
|
|
|
}
|
|
|
|
case 500: {
|
|
|
|
return KIO::ERR_INTERNAL_SERVER;
|
|
|
|
}
|
2022-05-11 23:06:14 +03:00
|
|
|
default: {
|
|
|
|
return KIO::ERR_NO_CONTENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2022-02-19 16:40:06 +02:00
|
|
|
size_t curlWriteCallback(char *ptr, size_t size, size_t nmemb, void *userdata)
|
|
|
|
{
|
|
|
|
HttpProtocol* httpprotocol = static_cast<HttpProtocol*>(userdata);
|
|
|
|
if (!httpprotocol) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-02-19 19:44:16 +02:00
|
|
|
// size should always be 1
|
|
|
|
Q_ASSERT(size == 1);
|
2022-02-19 16:40:06 +02:00
|
|
|
httpprotocol->slotData(ptr, nmemb);
|
|
|
|
return nmemb;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
HttpProtocol* httpprotocol = static_cast<HttpProtocol*>(userdata);
|
|
|
|
if (!httpprotocol) {
|
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
}
|
2022-05-13 17:13:51 +03:00
|
|
|
if (httpprotocol->aborttransfer) {
|
|
|
|
return CURLE_HTTP_RETURNED_ERROR;
|
|
|
|
}
|
2022-04-22 09:30:52 +03:00
|
|
|
httpprotocol->slotProgress(KIO::filesize_t(dlnow), KIO::filesize_t(dltotal));
|
2022-02-19 16:40:06 +02:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2022-02-17 13:55:51 +02:00
|
|
|
extern "C" int Q_DECL_EXPORT kdemain(int argc, char **argv)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-02-16 08:49:13 +02:00
|
|
|
QCoreApplication app(argc, argv);
|
2022-02-17 13:55:51 +02:00
|
|
|
KComponentData componentData("kio_http", "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-02-17 13:55:51 +02:00
|
|
|
if (argc != 4) {
|
|
|
|
::fprintf(stderr, "Usage: kio_http protocol domain-socket1 domain-socket2\n");
|
|
|
|
::exit(-1);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 08:49:13 +02:00
|
|
|
HttpProtocol slave(argv[2], argv[3]);
|
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
|
|
|
}
|
|
|
|
|
2022-02-17 13:55:51 +02:00
|
|
|
HttpProtocol::HttpProtocol(const QByteArray &pool, const QByteArray &app)
|
2022-05-13 17:13:51 +03:00
|
|
|
: SlaveBase("http", pool, app),
|
|
|
|
m_emitmime(true), aborttransfer(false),
|
2022-05-11 23:06:14 +03:00
|
|
|
m_curl(nullptr), m_curlheaders(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";
|
|
|
|
return;
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 08:49:13 +02:00
|
|
|
HttpProtocol::~HttpProtocol()
|
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);
|
|
|
|
}
|
2022-02-19 16:40:06 +02:00
|
|
|
if (m_curl) {
|
|
|
|
curl_easy_cleanup(m_curl);
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
void HttpProtocol::stat(const KUrl &url)
|
2022-05-11 23:06:14 +03:00
|
|
|
{
|
|
|
|
kDebug(7103) << "URL" << url.prettyUrl();
|
|
|
|
|
|
|
|
if (!setupCurl(url)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
// NOTE: do not set CURLOPT_NOBODY, server may not send some headers
|
|
|
|
CURLcode curlresult = curl_easy_perform(m_curl);
|
2022-05-11 23:06:14 +03:00
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(curlToKIOError(curlresult), url.prettyUrl());
|
2022-05-11 23:06:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
const long httpcode = HTTPCode(m_curl);
|
|
|
|
if (httpcode >= 400) {
|
|
|
|
kDebug(7103) << "HTTP error" << httpcode;
|
|
|
|
error(HTTPToKIOError(httpcode), url.prettyUrl());
|
2022-05-11 23:06:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
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;
|
2022-05-13 17:13:51 +03:00
|
|
|
kDebug(7103) << "HTTP last-modified" << curlfiletime;
|
|
|
|
kDebug(7103) << "HTTP content-length" << curlcontentlength;
|
|
|
|
kDebug(7103) << "HTTP content-type" << httpmimetype;
|
2022-05-13 00:55:00 +03:00
|
|
|
kioudsentry.insert(KIO::UDSEntry::UDS_NAME, url.fileName());
|
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();
|
|
|
|
}
|
|
|
|
|
2022-02-17 13:55:51 +02:00
|
|
|
void HttpProtocol::get(const KUrl &url)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-02-17 14:30:55 +02:00
|
|
|
kDebug(7103) << "URL" << url.prettyUrl();
|
2022-02-16 10:09:14 +02:00
|
|
|
|
2022-05-11 23:06:14 +03:00
|
|
|
if (!setupCurl(url)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode curlresult = curl_easy_perform(m_curl);
|
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(curlToKIOError(curlresult), url.prettyUrl());
|
2022-05-11 23:06:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
const long httpcode = HTTPCode(m_curl);
|
|
|
|
if (httpcode >= 400) {
|
|
|
|
kDebug(7103) << "HTTP error" << httpcode;
|
|
|
|
error(HTTPToKIOError(httpcode), url.prettyUrl());
|
2022-05-11 23:06:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
void HttpProtocol::slotData(const char* curldata, const size_t curldatasize)
|
2022-05-11 23:06:14 +03:00
|
|
|
{
|
2022-05-13 17:13:51 +03:00
|
|
|
if (aborttransfer) {
|
|
|
|
kDebug(7103) << "Transfer still in progress";
|
2022-05-11 23:06:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
if (m_emitmime) {
|
|
|
|
m_emitmime = false;
|
2022-05-11 23:06:14 +03:00
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
// if it's HTTP error do not send data and MIME, abort transfer
|
|
|
|
const long httpcode = HTTPCode(m_curl);
|
|
|
|
if (httpcode >= 400) {
|
|
|
|
aborttransfer = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11 23:06:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data(QByteArray::fromRawData(curldata, curldatasize));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpProtocol::slotProgress(KIO::filesize_t received, KIO::filesize_t total)
|
|
|
|
{
|
|
|
|
kDebug(7103) << "Received" << received << "from" << total;
|
|
|
|
emit processedSize(received);
|
|
|
|
if (total > 0 && received != total) {
|
|
|
|
emit totalSize(total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HttpProtocol::setupCurl(const KUrl &url)
|
|
|
|
{
|
2022-02-19 16:40:06 +02:00
|
|
|
if (Q_UNLIKELY(!m_curl)) {
|
|
|
|
error(KIO::ERR_OUT_OF_MEMORY, QString::fromLatin1("Null context"));
|
2022-05-11 23:06:14 +03:00
|
|
|
return false;
|
2022-02-19 16:40:06 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 17:13:51 +03:00
|
|
|
m_emitmime = true;
|
|
|
|
aborttransfer = 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);
|
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);
|
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);
|
2022-03-05 04:38:52 +02:00
|
|
|
|
2022-02-19 16:40:06 +02:00
|
|
|
const QByteArray urlbytes = url.prettyUrl().toLocal8Bit();
|
2022-02-21 12:51:49 +02:00
|
|
|
CURLcode curlresult = curl_easy_setopt(m_curl, CURLOPT_URL, urlbytes.constData());
|
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(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
|
|
|
|
2022-03-16 09:18:32 +02:00
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
|
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
|
2022-05-11 23:06:14 +03:00
|
|
|
return false;
|
2022-03-16 09:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
|
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
|
2022-05-11 23:06:14 +03:00
|
|
|
return false;
|
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) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
|
|
|
|
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");
|
|
|
|
const QByteArray proxybytes = curlProxyString(proxystring);
|
|
|
|
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) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(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) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
|
|
|
|
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-03-03 00:53:09 +02:00
|
|
|
kDebug(7103) << "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) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
|
|
|
|
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
|
|
|
|
|
|
|
kDebug(7103) << "Auth" << noauth;
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_HTTPAUTH, noauth ? CURLAUTH_NONE : CURLAUTH_ANY);
|
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
|
|
|
|
return false;
|
2022-03-05 04:38:52 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 09:11:33 +02:00
|
|
|
if (hasMetaData(QLatin1String("referrer"))) {
|
2022-03-26 07:44:02 +02:00
|
|
|
const QByteArray referrerbytes = metaData("referrer").toAscii();
|
2022-03-16 09:11:33 +02:00
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_REFERER, referrerbytes.constData());
|
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
|
|
|
|
return false;
|
2022-03-16 09:11:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:44:02 +02:00
|
|
|
if (hasMetaData(QLatin1String("resume"))) {
|
|
|
|
const qlonglong resumeoffset = metaData(QLatin1String("resume")).toLongLong();
|
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_RESUME_FROM_LARGE, resumeoffset);
|
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
|
|
|
|
return false;
|
2022-03-26 07:44:02 +02:00
|
|
|
} else {
|
|
|
|
canResume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 23:06:14 +03:00
|
|
|
if (m_curlheaders) {
|
|
|
|
curl_slist_free_all(m_curlheaders);
|
|
|
|
m_curlheaders = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-03-05 04:38:52 +02:00
|
|
|
if (hasMetaData(QLatin1String("Languages"))) {
|
2022-05-11 23:06:14 +03:00
|
|
|
m_curlheaders = curl_slist_append(m_curlheaders, QByteArray("Accept-Language: ") + metaData("Languages").toAscii());
|
2022-03-05 04:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasMetaData(QLatin1String("Charsets"))) {
|
2022-05-11 23:06:14 +03:00
|
|
|
m_curlheaders = curl_slist_append(m_curlheaders, QByteArray("Accept-Charset: ") + metaData("Charsets").toAscii());
|
2022-03-05 04:38:52 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 06:09:45 +02:00
|
|
|
if (hasMetaData(QLatin1String("accept"))) {
|
2022-05-11 23:06:14 +03:00
|
|
|
m_curlheaders = curl_slist_append(m_curlheaders, QByteArray("Accept: ") + metaData("accept").toAscii());
|
2022-02-21 06:09:45 +02:00
|
|
|
}
|
2022-03-05 04:38:52 +02:00
|
|
|
|
2022-05-11 23:06:14 +03:00
|
|
|
curlresult = curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, m_curlheaders);
|
2022-02-19 16:40:06 +02:00
|
|
|
if (curlresult != CURLE_OK) {
|
2022-05-11 23:06:14 +03:00
|
|
|
curl_slist_free_all(m_curlheaders);
|
|
|
|
m_curlheaders = nullptr;
|
2022-05-13 01:31:00 +03:00
|
|
|
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
|
2022-05-11 23:06:14 +03:00
|
|
|
return false;
|
2022-03-28 01:00:05 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 23:06:14 +03:00
|
|
|
return true;
|
2022-02-17 14:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_http.cpp"
|