• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

accessmanager.cpp

Go to the documentation of this file.
00001 /*
00002  * This file is part of the KDE project.
00003  *
00004  * Copyright (C) 2008 - 2009 Urs Wolfer <uwolfer @ kde.org>
00005  * Copyright (C) 2007 Trolltech ASA
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public License
00018  * along with this library; see the file COPYING.LIB.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021  *
00022  */
00023 
00024 #include "accessmanager.h"
00025 #include "accessmanagerreply_p.h"
00026 
00027 #include <kdebug.h>
00028 #include <kio/job.h>
00029 #include <kio/scheduler.h>
00030 #include <kconfiggroup.h>
00031 #include <ksharedconfig.h>
00032 
00033 #include <QtCore/QUrl>
00034 #include <QtNetwork/QNetworkReply>
00035 #include <QtNetwork/QNetworkRequest>
00036 #include <QtDBus/QDBusInterface>
00037 #include <QtDBus/QDBusConnection>
00038 #include <QtDBus/QDBusReply>
00039 
00040 
00041 namespace KIO {
00042 
00043 class AccessManager::AccessManagerPrivate
00044 {
00045 public:
00046     AccessManagerPrivate():externalContentAllowed(true) {}
00047     bool externalContentAllowed;
00048     static KIO::MetaData metaDataForRequest(QNetworkRequest request);
00049 };
00050 
00051 namespace Integration {
00052 
00053 class CookieJar::CookieJarPrivate
00054 {
00055 public:
00056   CookieJarPrivate(): windowId(-1), enabled(true) {}
00057 
00058   qlonglong windowId;
00059   bool enabled;
00060 };
00061 
00062 }
00063 
00064 }
00065 
00066 using namespace KIO;
00067 
00068 AccessManager::AccessManager(QObject *parent)
00069     : QNetworkAccessManager(parent), d(new AccessManager::AccessManagerPrivate())
00070 {
00071 }
00072 
00073 AccessManager::~AccessManager()
00074 {
00075     delete d;
00076 }
00077 
00078 void AccessManager::setExternalContentAllowed(bool allowed)
00079 {
00080     d->externalContentAllowed = allowed;
00081 }
00082 
00083 bool AccessManager::isExternalContentAllowed() const
00084 {
00085     return d->externalContentAllowed;
00086 }
00087 
00088 QNetworkReply *AccessManager::createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData)
00089 {
00090     KIO::SimpleJob *kioJob = 0;
00091 
00092     if ( !d->externalContentAllowed && req.url().scheme() != "file" && !req.url().scheme().isEmpty() ) {
00093         kDebug( 7044 ) << "Blocked: " << req.url().scheme() <<  req.url();
00094         /* if kioJob equals zero, the AccessManagerReply will block the request */
00095         return new KDEPrivate::AccessManagerReply(op, req, kioJob, this);
00096     }
00097 
00098     switch (op) {
00099         case HeadOperation: {
00100             kDebug( 7044 ) << "HeadOperation:" << req.url();
00101             kioJob = KIO::mimetype(req.url(), KIO::HideProgressInfo);
00102             break;
00103         }
00104         case GetOperation: {
00105             kDebug( 7044 ) << "GetOperation:" << req.url();
00106             kioJob = KIO::get(req.url(), KIO::NoReload, KIO::HideProgressInfo);
00107             break;
00108         }
00109         case PutOperation: {
00110             kDebug( 7044 ) << "PutOperation:" << req.url();
00111             kioJob = KIO::put(req.url(), -1, KIO::HideProgressInfo);
00112             break;
00113         }
00114         case PostOperation: {
00115             kDebug( 7044 ) << "PostOperation:" << req.url();
00116             kioJob = KIO::http_post(req.url(), outgoingData->readAll(), KIO::HideProgressInfo);
00117             break;
00118         }
00119         default:
00120             kDebug( 7044 ) << "Unknown operation";
00121             return 0;
00122     }
00123 
00124     KIO::Scheduler::scheduleJob(kioJob);
00125     KDEPrivate::AccessManagerReply *reply = new KDEPrivate::AccessManagerReply(op, req, kioJob, this);
00126 
00127     kioJob->addMetaData(d->metaDataForRequest(req));
00128 
00129     if ( op == PostOperation && !kioJob->metaData().contains("content-type"))  {
00130         QVariant header = req.header(QNetworkRequest::ContentTypeHeader);
00131         if (header.isValid())
00132           kioJob->addMetaData("content-type",
00133                               QString::fromLatin1("Content-Type: %1").arg(header.toString()));
00134         else
00135           kioJob->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
00136     }
00137 
00138     return reply;
00139 }
00140 
00141 
00142 KIO::MetaData AccessManager::AccessManagerPrivate::metaDataForRequest(QNetworkRequest request)
00143 {
00144     KIO::MetaData metaData;
00145 
00146     // Add the user-specified meta data first...
00147     QVariant userMetaData = request.attribute (static_cast<QNetworkRequest::Attribute>(MetaData));
00148     if (userMetaData.isValid() && userMetaData.type() == QVariant::Map) {
00149       metaData += userMetaData.toMap();
00150     }
00151 
00152     metaData.insert("PropagateHttpHeader", "true");
00153 
00154     metaData.insert("UserAgent", request.rawHeader("User-Agent"));
00155     request.setRawHeader("User-Agent", QByteArray());
00156 
00157     metaData.insert("accept", request.rawHeader("Accept"));
00158     request.setRawHeader("Accept", QByteArray());
00159 
00160     request.setRawHeader("content-length", QByteArray());
00161     request.setRawHeader("Connection", QByteArray());
00162 
00163     QString additionHeaders;
00164     Q_FOREACH(const QByteArray &headerKey, request.rawHeaderList()) {
00165         const QByteArray value = request.rawHeader(headerKey);
00166         if (value.isNull())
00167             continue;
00168 
00169         // createRequest() checks later for existence "content-type" metadata
00170         if (headerKey=="Content-Type") {
00171             metaData.insert("content-type", value);
00172             continue;
00173         }
00174 
00175         if (additionHeaders.length() > 0) {
00176             additionHeaders += "\r\n";
00177         }
00178         additionHeaders += headerKey + ": " + value;
00179     }
00180     metaData.insert("customHTTPHeader", additionHeaders);
00181 
00182     return metaData;
00183 }
00184 
00185 
00186 using namespace KIO::Integration;
00187 
00188 CookieJar::CookieJar(QObject* parent)
00189           :QNetworkCookieJar(parent), d(new CookieJar::CookieJarPrivate) {
00190     reparseConfiguration();
00191 }
00192 
00193 CookieJar::~CookieJar() {
00194     delete d;
00195 }
00196 
00197 qlonglong CookieJar::windowId() const {
00198     return d->windowId;
00199 }
00200 
00201 QList<QNetworkCookie> CookieJar::cookiesForUrl(const QUrl &url) const {
00202     QList<QNetworkCookie> cookieList;
00203 
00204     if (d->enabled) {
00205         QDBusInterface kcookiejar("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer");
00206         QDBusReply<QString> reply = kcookiejar.call("findDOMCookies", url.toString(), d->windowId);
00207 
00208         if (reply.isValid()) {
00209             cookieList << reply.value().toUtf8();
00210             //kDebug() << url.host() << reply.value();
00211         } else {
00212             kWarning() << "Unable to communicate with the cookiejar!";
00213         }
00214     }
00215 
00216     return cookieList;
00217 }
00218 
00219 bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url) {
00220     if (d->enabled) {
00221         QDBusInterface kcookiejar("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer");
00222 
00223         QByteArray cookieHeader;
00224         Q_FOREACH(const QNetworkCookie &cookie, cookieList) {
00225             cookieHeader = "Set-Cookie: ";
00226             cookieHeader += cookie.toRawForm();
00227             kcookiejar.call("addCookies", url.toString(), cookieHeader, d->windowId);
00228             //kDebug() << "[" << d->windowId << "] Got Cookie: " << cookieHeader << " from " << url;
00229         }
00230 
00231         return !kcookiejar.lastError().isValid();
00232     }
00233 
00234     return false;
00235 }
00236 
00237 void CookieJar::setWindowId(qlonglong id) {
00238     d->windowId = id;
00239 }
00240 
00241 void CookieJar::reparseConfiguration() {
00242     KConfigGroup cfg = KSharedConfig::openConfig("kcookiejarrc", KConfig::NoGlobals)->group("Cookie Policy");
00243     d->enabled = cfg.readEntry("Cookies", true);
00244 }
00245 
00246 
00247 #include "accessmanager.moc"

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  •     Sodep
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal