kio Library API Documentation

ksslcsessioncache.cc

00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2003 Stefan Rompf <sux@loplof.de>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include <qpair.h>
00022 #include <qstring.h>
00023 #include <qptrlist.h>
00024 
00025 #include <kdebug.h>
00026 #include <kstaticdeleter.h>
00027 #include <kurl.h>
00028 
00029 #include "ksslconfig.h"
00030 
00031 #include "ksslcsessioncache.h"
00032 
00033 /* 
00034  * Operation:
00035  *
00036  * Sessions will be stored per running application, not KDE
00037  * wide, to avoid security problems with hostile programs
00038  * that negotiate sessions with weak cryptographic keys and store
00039  * them for everybody to use - I really don't want that.
00040  *
00041  * Retrieval is organised similiar to George's thoughts in the KSSLD
00042  * certificate cache: The cache is organised as a list, with the
00043  * recently fetched (or stored) session first.
00044  *
00045  * The cache has an artificial limit of 32 sessions (should really
00046  * be enough), and relies on the peer server for timeouts
00047  *
00048  */
00049 #define MAX_ENTRIES 32
00050 
00051 #ifdef KSSL_HAVE_SSL
00052 
00053 typedef QPair<QString,QString> KSSLCSession;
00054 typedef QPtrList<KSSLCSession> KSSLCSessions;
00055 
00056 static KSSLCSessions *sessions = 0L;
00057 static KStaticDeleter<KSSLCSessions> med;
00058 
00059 
00060 static QString URLtoKey(const KURL &kurl) {
00061     return kurl.host() + ":" + kurl.protocol() + ":" + QString::number(kurl.port());
00062 }
00063 
00064 
00065 static void setup() {
00066     KSSLCSessions *ses = new KSSLCSessions;
00067     ses->setAutoDelete(true);
00068     med.setObject(sessions, ses);
00069 }
00070 
00071 #endif
00072 
00073 QString KSSLCSessionCache::getSessionForURL(const KURL &kurl) {
00074 #ifdef KSSL_HAVE_SSL
00075     if (!sessions) return QString::null;
00076     QString key = URLtoKey(kurl);
00077 
00078     for(KSSLCSession *it = sessions->first(); it; it=sessions->next()) {
00079     if (it->first == key) {
00080         sessions->take();
00081         sessions->prepend(it);
00082         return it->second;
00083     }
00084     }
00085 
00086     // Negative caching disabled: cache pollution
00087 #if 0 
00088     kdDebug(7029) <<"Negative caching " <<key <<endl;
00089     if (sessions->count() >= MAX_ENTRIES) sessions->removeLast();
00090     sessions->prepend(new KSSLCSession(key, QString::null));
00091 #endif
00092 
00093 #endif
00094     return QString::null;
00095 }
00096 
00097 
00098 void KSSLCSessionCache::putSessionForURL(const KURL &kurl, const QString &session) {
00099 #ifdef KSSL_HAVE_SSL
00100     if (!sessions) setup();
00101     QString key = URLtoKey(kurl);
00102     KSSLCSession *it;
00103 
00104     for(it = sessions->first(); it && it->first != key; it=sessions->next());
00105 
00106     if (it) {
00107     sessions->take();
00108     it->second = session;
00109     } else {
00110     it = new KSSLCSession(key, session);
00111     if (sessions->count() >= MAX_ENTRIES) sessions->removeLast();
00112     }
00113 
00114     sessions->prepend(it);
00115 #endif
00116 }
KDE Logo
This file is part of the documentation for kio Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:33:29 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003