• Skip to content
  • Skip to link menu
KDE 4.3 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 
00026 #include "accessmanagerreply_p.h"
00027 
00028 #include <kdebug.h>
00029 #include <kio/job.h>
00030 #include <kio/scheduler.h>
00031 
00032 #include <QtNetwork/QNetworkRequest>
00033 #include <QtNetwork/QNetworkReply>
00034 
00035 
00036 namespace KIO {
00037 
00038 class AccessManager::AccessManagerPrivate
00039 {
00040 public:
00041     AccessManagerPrivate():externalContentAllowed(true) {}
00042     bool externalContentAllowed;
00043     static KIO::MetaData metaDataForRequest(QNetworkRequest request);
00044 };
00045 
00046 }
00047 
00048 using namespace KIO;
00049 
00050 AccessManager::AccessManager(QObject *parent)
00051     : QNetworkAccessManager(parent), d(new AccessManager::AccessManagerPrivate())
00052 {
00053 }
00054 
00055 AccessManager::~AccessManager()
00056 {
00057     delete d;
00058 }
00059 
00060 void AccessManager::setExternalContentAllowed(bool allowed)
00061 {
00062     d->externalContentAllowed = allowed;
00063 }
00064 
00065 bool AccessManager::isExternalContentAllowed() const
00066 {
00067     return d->externalContentAllowed;
00068 }
00069 
00070 QNetworkReply *AccessManager::createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData)
00071 {
00072     KIO::SimpleJob *kioJob = 0;
00073 
00074     if ( !d->externalContentAllowed && req.url().scheme() != "file" && !req.url().scheme().isEmpty() ) {
00075         kDebug() << "Blocked: " << req.url().scheme() <<  req.url();
00076         /* if kioJob equals zero, the AccessManagerReply will block the request */
00077         return new KDEPrivate::AccessManagerReply(op, req, kioJob, this);
00078     }
00079 
00080     switch (op) {
00081         case HeadOperation: {
00082             kDebug() << "HeadOperation:" << req.url();
00083             kioJob = KIO::mimetype(req.url(), KIO::HideProgressInfo);
00084             break;
00085         }
00086         case GetOperation: {
00087             kDebug() << "GetOperation:" << req.url();
00088             kioJob = KIO::get(req.url(), KIO::NoReload, KIO::HideProgressInfo);
00089             break;
00090         }
00091         case PutOperation: {
00092             kDebug() << "PutOperation:" << req.url();
00093             kioJob = KIO::put(req.url(), -1, KIO::HideProgressInfo);
00094             break;
00095         }
00096         case PostOperation: {
00097             kDebug() << "PostOperation:" << req.url();
00098             kioJob = KIO::http_post(req.url(), outgoingData->readAll(), KIO::HideProgressInfo);
00099             break;
00100         }
00101         default:
00102             kDebug() << "Unknown operation";
00103             return 0;
00104     }
00105 
00106     KIO::Scheduler::scheduleJob(kioJob);
00107     KDEPrivate::AccessManagerReply *reply = new KDEPrivate::AccessManagerReply(op, req, kioJob, this);
00108 
00109     kioJob->addMetaData(d->metaDataForRequest(req));
00110 
00111     if ( op == PostOperation && !kioJob->metaData().contains("content-type"))  {
00112         QString contentType (QLatin1String("Content-Type: "));
00113         QVariant header = req.header(QNetworkRequest::ContentTypeHeader);
00114 
00115         if (header.isValid())
00116             contentType += header.toString();
00117         else
00118             contentType += QLatin1String("application/x-www-form-urlencoded");
00119 
00120         kioJob->addMetaData("content-type", contentType);
00121     }
00122 
00123     //kDebug () << "Job '" << kioJob << "' started...";
00124     return reply;
00125 }
00126 
00127 
00128 KIO::MetaData AccessManager::AccessManagerPrivate::metaDataForRequest(QNetworkRequest request)
00129 {
00130     KIO::MetaData metaData;
00131 
00132     // Add the user-specified meta data first...
00133     QVariant userMetaData = request.attribute (static_cast<QNetworkRequest::Attribute>(MetaData));
00134     if (userMetaData.isValid() && userMetaData.type() == QVariant::Map) {
00135       metaData += userMetaData.toMap();
00136     }
00137 
00138     metaData.insert("PropagateHttpHeader", "true");
00139 
00140     metaData.insert("UserAgent", request.rawHeader("User-Agent"));
00141     request.setRawHeader("User-Agent", QByteArray());
00142 
00143     metaData.insert("accept", request.rawHeader("Accept"));
00144     request.setRawHeader("Accept", QByteArray());
00145 
00146     request.setRawHeader("content-length", QByteArray());
00147     request.setRawHeader("Connection", QByteArray());
00148 
00149     QString additionHeaders;
00150     QListIterator<QByteArray> headersIt (request.rawHeaderList());
00151 
00152     while (headersIt.hasNext()) {
00153         const QByteArray key = headersIt.next();
00154         const QByteArray value = request.rawHeader(key);
00155 
00156         if (value.isNull())
00157             continue;
00158 
00159         // createRequest() checks later for existence "content-type" metadata
00160         if (QString::compare(key, QLatin1String("Content-Type"), Qt::CaseInsensitive) == 0) {
00161             metaData.insert("content-type", value);
00162             continue;
00163         }
00164 
00165         if (additionHeaders.length() > 0) {
00166             additionHeaders += QLatin1String("\r\n");
00167         }
00168 
00169         additionHeaders += key;
00170         additionHeaders += QLatin1String(": ");
00171         additionHeaders += value;
00172     }
00173 
00174     metaData.insert("customHTTPHeader", additionHeaders);
00175     return metaData;
00176 }
00177 
00178 #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
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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