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>
|
2022-02-18 20:04:44 +02:00
|
|
|
#include <QThread>
|
2022-02-16 10:09:14 +02:00
|
|
|
#include <QNetworkAccessManager>
|
2022-02-17 11:34:46 +02:00
|
|
|
#include <QNetworkDiskCache>
|
2022-02-16 10:09:14 +02:00
|
|
|
#include <QNetworkReply>
|
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-02-16 10:46:58 +02:00
|
|
|
static inline QByteArray HTTPMIMEType(const QByteArray &contenttype)
|
|
|
|
{
|
2022-02-17 13:55:51 +02:00
|
|
|
const QList<QByteArray> splitcontenttype = contenttype.split(';');
|
2022-02-16 10:46:58 +02:00
|
|
|
if (splitcontenttype.isEmpty()) {
|
2022-02-17 13:55:51 +02:00
|
|
|
return QByteArray("application/octet-stream");
|
2022-02-16 10:46:58 +02:00
|
|
|
}
|
|
|
|
return splitcontenttype.at(0);
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
(void)KGlobal::locale();
|
2022-02-16 08:49:13 +02:00
|
|
|
|
2022-02-17 13:55:51 +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)
|
|
|
|
: SlaveBase("http", pool, app)
|
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-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
|
|
|
|
|
|
|
QNetworkAccessManager netmanager(this);
|
2022-02-17 11:34:46 +02:00
|
|
|
QNetworkDiskCache netcache(this);
|
|
|
|
QNetworkRequest netrequest(url);
|
|
|
|
|
2022-02-18 17:43:59 +02:00
|
|
|
kDebug(7103) << "Metadata" << allMetaData();
|
2022-02-17 11:34:46 +02:00
|
|
|
// metadata from scheduler
|
|
|
|
if (hasMetaData("Languages")) {
|
|
|
|
netrequest.setRawHeader("Accept-Language", metaData("Languages").toAscii());
|
|
|
|
}
|
|
|
|
if (hasMetaData("Charsets")) {
|
|
|
|
netrequest.setRawHeader("Accept-Charset", metaData("Charsets").toAscii());
|
|
|
|
}
|
|
|
|
if (hasMetaData("CacheDir")) {
|
|
|
|
netcache.setCacheDirectory(metaData("CacheDir"));
|
|
|
|
}
|
|
|
|
if (hasMetaData("UserAgent")) {
|
|
|
|
netrequest.setRawHeader("User-Agent", metaData("UserAgent").toAscii());
|
|
|
|
}
|
|
|
|
netmanager.setCache(&netcache);
|
2022-02-18 17:43:59 +02:00
|
|
|
// KIO metadata
|
|
|
|
if (hasMetaData("cache")) {
|
|
|
|
const QByteArray kiocache = metaData("cache").toAscii();
|
|
|
|
if (qstricmp(kiocache.constData(), "cache") == 0) {
|
|
|
|
netrequest.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(QNetworkRequest::PreferCache));
|
|
|
|
} else if (qstricmp(kiocache.constData(), "cacheonly") == 0) {
|
|
|
|
netrequest.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(QNetworkRequest::AlwaysCache));
|
|
|
|
} else if (qstricmp(kiocache.constData(), "verify") == 0 || qstricmp(kiocache.constData(), "refresh") == 0) {
|
|
|
|
netrequest.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(QNetworkRequest::PreferNetwork));
|
|
|
|
} else {
|
|
|
|
// reload or unknown
|
|
|
|
netrequest.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(QNetworkRequest::AlwaysNetwork));
|
|
|
|
}
|
|
|
|
}
|
2022-02-17 11:34:46 +02:00
|
|
|
|
|
|
|
QNetworkReply* netreply = netmanager.get(netrequest);
|
2022-02-17 14:17:40 +02:00
|
|
|
connect(netreply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(slotProgress(qint64,qint64)));
|
2022-02-16 10:09:14 +02:00
|
|
|
while (!netreply->isFinished()) {
|
|
|
|
QCoreApplication::processEvents();
|
2022-02-18 20:04:44 +02:00
|
|
|
QThread::msleep(400);
|
2022-02-16 10:09:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (netreply->error() != QNetworkReply::NoError) {
|
2022-02-17 14:30:55 +02:00
|
|
|
kWarning(7103) << "Error" << netreply->url() << netreply->error();
|
2022-02-16 10:09:14 +02:00
|
|
|
error(KIO::ERR_COULD_NOT_CONNECT, url.prettyUrl());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-17 13:55:51 +02:00
|
|
|
const QVariant netredirect = netreply->attribute(QNetworkRequest::RedirectionTargetAttribute);
|
|
|
|
if (netredirect.isValid()) {
|
|
|
|
const QUrl netredirecturl = netreply->url().resolved(netredirect.toUrl());
|
|
|
|
kDebug(7103) << "Redirecting to" << netredirecturl;
|
|
|
|
redirection(netredirecturl);
|
|
|
|
finished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-17 14:17:40 +02:00
|
|
|
kDebug(7103) << "Headers" << netreply->rawHeaderPairs();
|
2022-02-17 13:55:51 +02:00
|
|
|
|
|
|
|
const QByteArray httpmimetype = HTTPMIMEType(netreply->rawHeader("content-type"));
|
|
|
|
kDebug(7103) << "MIME type" << httpmimetype;
|
|
|
|
emit mimeType(httpmimetype);
|
2022-02-17 14:17:40 +02:00
|
|
|
|
2022-02-16 10:09:14 +02:00
|
|
|
data(netreply->readAll());
|
2022-02-17 13:55:51 +02:00
|
|
|
|
2022-02-16 10:09:14 +02:00
|
|
|
finished();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
2022-02-17 14:17:40 +02:00
|
|
|
|
|
|
|
void HttpProtocol::slotProgress(qint64 received, qint64 total)
|
|
|
|
{
|
|
|
|
kDebug(7103) << "Received" << received << "from" << total;
|
|
|
|
emit processedSize(static_cast<KIO::filesize_t>(received));
|
|
|
|
emit totalSize(static_cast<KIO::filesize_t>(total));
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_http.cpp"
|