kcookieserver.cpp
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 614 行 · 第 1/2 页
CPP
614 行
/*This file is part of KDE Copyright (C) 1998-2000 Waldo Bastian (bastian@kde.org)Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER INAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *///----------------------------------------------------------------------------//// KDE Cookie Server// $Id: kcookieserver.cpp 393950 2005-02-28 23:13:52Z waba $#define SAVE_DELAY 3 // Save after 3 minutes#include <unistd.h>#include <qtimer.h>#include <qptrlist.h>#include <qfile.h>#include <dcopclient.h>#include <kconfig.h>#include <kdebug.h>#include <kapplication.h>#include <kcmdlineargs.h>#include <kstandarddirs.h>#include "kcookiejar.h"#include "kcookiewin.h"#include "kcookieserver.h"extern "C" { KDE_EXPORT KDEDModule *create_kcookiejar(const QCString &name) { return new KCookieServer(name); }}// Cookie field indexesenum CookieDetails { CF_DOMAIN=0, CF_PATH, CF_NAME, CF_HOST, CF_VALUE, CF_EXPIRE, CF_PROVER, CF_SECURE };class CookieRequest {public: DCOPClient *client; DCOPClientTransaction *transaction; QString url; bool DOM; long windowId;};template class QPtrList<CookieRequest>;class RequestList : public QPtrList<CookieRequest>{public: RequestList() : QPtrList<CookieRequest>() { }};KCookieServer::KCookieServer(const QCString &name) :KDEDModule(name){ mOldCookieServer = new DCOPClient(); // backwards compatibility. mOldCookieServer->registerAs("kcookiejar", false); mOldCookieServer->setDaemonMode( true ); mCookieJar = new KCookieJar; mPendingCookies = new KHttpCookieList; mPendingCookies->setAutoDelete(true); mRequestList = new RequestList; mAdvicePending = false; mTimer = 0; mConfig = new KConfig("kcookiejarrc"); mCookieJar->loadConfig( mConfig ); QString filename = locateLocal("data", "kcookiejar/cookies"); // Stay backwards compatible! QString filenameOld = locate("data", "kfm/cookies"); if (!filenameOld.isEmpty()) { mCookieJar->loadCookies( filenameOld ); if (mCookieJar->saveCookies( filename)) { unlink(QFile::encodeName(filenameOld)); // Remove old kfm cookie file } } else { mCookieJar->loadCookies( filename); } connect(this, SIGNAL(windowUnregistered(long)), this, SLOT(slotDeleteSessionCookies(long)));}KCookieServer::~KCookieServer(){ if (mCookieJar->changed()) slotSave(); delete mOldCookieServer; delete mCookieJar; delete mTimer; delete mPendingCookies; delete mConfig;}bool KCookieServer::cookiesPending( const QString &url, KHttpCookieList *cookieList ){ QString fqdn; QStringList domains; QString path; // Check whether 'url' has cookies on the pending list if (mPendingCookies->isEmpty()) return false; if (!KCookieJar::parseURL(url, fqdn, path)) return false; mCookieJar->extractDomains( fqdn, domains ); for( KHttpCookie *cookie = mPendingCookies->first(); cookie != 0L; cookie = mPendingCookies->next()) { if (cookie->match( fqdn, domains, path)) { if (!cookieList) return true; cookieList->append(cookie); } } if (!cookieList) return false; return cookieList->isEmpty();}void KCookieServer::addCookies( const QString &url, const QCString &cookieHeader, long windowId, bool useDOMFormat ){ KHttpCookieList cookieList; if (useDOMFormat) cookieList = mCookieJar->makeDOMCookies(url, cookieHeader, windowId); else cookieList = mCookieJar->makeCookies(url, cookieHeader, windowId); checkCookies(&cookieList); for(KHttpCookiePtr cookie = cookieList.first(); cookie; cookie = cookieList.first()) mPendingCookies->append(cookieList.take()); if (!mAdvicePending) { mAdvicePending = true; while (!mPendingCookies->isEmpty()) { checkCookies(0); } mAdvicePending = false; }}void KCookieServer::checkCookies( KHttpCookieList *cookieList){ KHttpCookieList *list; if (cookieList) list = cookieList; else list = mPendingCookies; KHttpCookiePtr cookie = list->first(); while (cookie) { KCookieAdvice advice = mCookieJar->cookieAdvice(cookie); switch(advice) { case KCookieAccept: list->take(); mCookieJar->addCookie(cookie); cookie = list->current(); break; case KCookieReject: list->take(); delete cookie; cookie = list->current(); break; default: cookie = list->next(); break; } } if (cookieList || list->isEmpty()) return; KHttpCookiePtr currentCookie = mPendingCookies->first(); KHttpCookieList currentList; currentList.append(currentCookie); QString currentHost = currentCookie->host(); cookie = mPendingCookies->next(); while (cookie) { if (cookie->host() == currentHost) { currentList.append(cookie); } cookie = mPendingCookies->next(); } KCookieWin *kw = new KCookieWin( 0L, currentList, mCookieJar->preferredDefaultPolicy(), mCookieJar->showCookieDetails() ); KCookieAdvice userAdvice = kw->advice(mCookieJar, currentCookie); delete kw; // Save the cookie config if it has changed mCookieJar->saveConfig( mConfig ); // Apply the user's choice to all cookies that are currently // queued for this host. cookie = mPendingCookies->first(); while (cookie) { if (cookie->host() == currentHost) { switch(userAdvice) { case KCookieAccept: mPendingCookies->take(); mCookieJar->addCookie(cookie); cookie = mPendingCookies->current(); break; case KCookieReject: mPendingCookies->take(); delete cookie; cookie = mPendingCookies->current(); break; default: qWarning(__FILE__":%d Problen!", __LINE__); cookie = mPendingCookies->next(); break; } } else { cookie = mPendingCookies->next(); } } // Check if we can handle any request for ( CookieRequest *request = mRequestList->first(); request;) { if (!cookiesPending( request->url )) { QCString replyType; QByteArray replyData; QString res = mCookieJar->findCookies( request->url, request->DOM, request->windowId ); QDataStream stream2(replyData, IO_WriteOnly); stream2 << res; replyType = "QString"; request->client->endTransaction( request->transaction, replyType, replyData); CookieRequest *tmp = request; request = mRequestList->next(); mRequestList->removeRef( tmp ); delete tmp; } else { request = mRequestList->next(); } } if (mCookieJar->changed() && !mTimer) saveCookieJar();}void KCookieServer::slotSave(){ delete mTimer; mTimer = 0; QString filename = locateLocal("data", "kcookiejar/cookies"); mCookieJar->saveCookies(filename);}void KCookieServer::saveCookieJar()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?