KIO
ksslcsessioncache.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ksslcsessioncache.h"
00022
00023 #include <QtCore/QCoreApplication>
00024 #include <QtCore/QPair>
00025 #include <QtCore/QString>
00026
00027 #include <kdebug.h>
00028 #include <kurl.h>
00029
00030 #include <ksslconfig.h>
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #define MAX_ENTRIES 32
00049
00050 #ifdef KSSL_HAVE_SSL
00051
00052 typedef QPair<QString,QString> KSSLCSession;
00053 typedef QList<KSSLCSession> KSSLCSessions;
00054
00055 static KSSLCSessions *sessions = 0L;
00056
00057 static QString URLtoKey(const KUrl &kurl) {
00058 return kurl.host() + ':' + kurl.protocol() + ':' + QString::number(kurl.port());
00059 }
00060
00061
00062 static void cleanupKSSLCSessions() {
00063 delete sessions;
00064 sessions = 0;
00065 }
00066
00067 static void setup() {
00068 sessions = new KSSLCSessions;
00069 qAddPostRoutine(cleanupKSSLCSessions);
00070 }
00071
00072 #endif
00073
00074 QString KSSLCSessionCache::getSessionForUrl(const KUrl &kurl) {
00075 #ifdef KSSL_HAVE_SSL
00076 if (!sessions) return QString();
00077 QString key = URLtoKey(kurl);
00078
00079 for (int i = 0; i < sessions->size(); ++i) {
00080 if (sessions->at(i).first == key) {
00081 QString snd = sessions->at(i).second;
00082 sessions->prepend(sessions->takeAt(i));
00083 return snd;
00084 }
00085 }
00086
00087
00088 #if 0
00089 kDebug(7029) <<"Negative caching " <<key;
00090 if (sessions->count() >= MAX_ENTRIES) sessions->removeLast();
00091 sessions->prepend(new KSSLCSession(key, QString()));
00092 #endif
00093
00094 #endif
00095 return QString();
00096 }
00097
00098
00099 void KSSLCSessionCache::putSessionForUrl(const KUrl &kurl, const QString &session) {
00100 #ifdef KSSL_HAVE_SSL
00101 if (!sessions) setup();
00102 QString key = URLtoKey(kurl);
00103 KSSLCSessions::iterator it = sessions->begin();
00104
00105 while ( it != sessions->end() ) {
00106 if ( it->first == key )
00107 break;
00108 ++it;
00109 }
00110
00111 if (it != sessions->end()) {
00112 it->second = session;
00113 } else {
00114 if (sessions->size() >= MAX_ENTRIES)
00115 sessions->removeLast();
00116 sessions->prepend(KSSLCSession(key, session));
00117 }
00118
00119 #endif
00120 }