kcookiejar.cpp
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 1,549 行 · 第 1/3 页
CPP
1,549 行
/* This file is part of the KDE File Manager Copyright (C) 1998-2000 Waldo Bastian (bastian@kde.org) Copyright (C) 2000,2001 Dawit Alemayehu (adawit@kde.org) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*///----------------------------------------------------------------------------//// KDE File Manager -- HTTP Cookies// $Id: kcookiejar.cpp 452328 2005-08-23 01:48:51Z mueller $//// The cookie protocol is a mess. RFC2109 is a joke since nobody seems to// use it. Apart from that it is badly written.// We try to implement Netscape Cookies and try to behave us according to// RFC2109 as much as we can.//// We assume cookies do not contain any spaces (Netscape spec.)// According to RFC2109 this is allowed though.//#include <config.h>#include <sys/types.h>#include <sys/stat.h>#ifdef HAVE_SYS_PARAM_H#include <sys/param.h>#endif#include <fcntl.h>#include <unistd.h>#include <stdio.h>#include <string.h>#ifdef USE_SOLARIS#include <strings.h>#endif#include <stdlib.h>//#include <netinet/in.h>//#include <arpa/inet.h>#include <qstring.h>#include <qstrlist.h>#include <qptrlist.h>#include <qptrdict.h>#include <qfile.h>#include <qdir.h>#include <qregexp.h>#include <kurl.h>#include <krfcdate.h>#include <kconfig.h>#include <ksavefile.h>#include <kdebug.h>#include "kcookiejar.h"// BR87227// Waba: Should the number of cookies be limited?// I am not convinced of the need of such limit// Mozilla seems to limit to 20 cookies / domain// but it is unclear which policy it uses to expire// cookies when it exceeds that amount#undef MAX_COOKIE_LIMIT#define MAX_COOKIES_PER_HOST 25#define READ_BUFFER_SIZE 8192// Note with respect to QString::fromLatin1( )// Cookies are stored as 8 bit data and passed to kio_http as// latin1 regardless of their actual encoding.// L1 is used to indicate latin1 constants#define L1(x) QString::fromLatin1(x)template class QPtrList<KHttpCookie>;template class QPtrDict<KHttpCookieList>;QString KCookieJar::adviceToStr(KCookieAdvice _advice){ switch( _advice ) { case KCookieAccept: return L1("Accept"); case KCookieReject: return L1("Reject"); case KCookieAsk: return L1("Ask"); default: return L1("Dunno"); }}KCookieAdvice KCookieJar::strToAdvice(const QString &_str){ if (_str.isEmpty()) return KCookieDunno; QCString advice = _str.lower().latin1(); if (advice == "accept") return KCookieAccept; else if (advice == "reject") return KCookieReject; else if (advice == "ask") return KCookieAsk; return KCookieDunno;}// KHttpCookie/////////////////////////////////////////////////////////////////////////////// Cookie constructor//KHttpCookie::KHttpCookie(const QString &_host, const QString &_domain, const QString &_path, const QString &_name, const QString &_value, time_t _expireDate, int _protocolVersion, bool _secure, bool _httpOnly, bool _explicitPath) : mHost(_host), mDomain(_domain), mPath(_path.isEmpty() ? QString::null : _path), mName(_name), mValue(_value), mExpireDate(_expireDate), mProtocolVersion(_protocolVersion), mSecure(_secure), mHttpOnly(_httpOnly), mExplicitPath(_explicitPath){}//// Checks if a cookie has been expired//bool KHttpCookie::isExpired(time_t currentDate){ return (mExpireDate != 0) && (mExpireDate < currentDate);}//// Returns a string for a HTTP-header//QString KHttpCookie::cookieStr(bool useDOMFormat){ QString result; if (useDOMFormat || (mProtocolVersion == 0)) { if ( !mName.isEmpty() ) result = mName + '='; result += mValue; } else { result = mName + '=' + mValue; if (mExplicitPath) result += L1("; $Path=\"") + mPath + L1("\""); if (!mDomain.isEmpty()) result += L1("; $Domain=\"") + mDomain + L1("\""); } return result;}//// Returns whether this cookie should be send to this location.bool KHttpCookie::match(const QString &fqdn, const QStringList &domains, const QString &path){ // Cookie domain match check if (mDomain.isEmpty()) { if (fqdn != mHost) return false; } else if (!domains.contains(mDomain)) { if (mDomain[0] == '.') return false; // Maybe the domain needs an extra dot. QString domain = '.' + mDomain; if ( !domains.contains( domain ) ) if ( fqdn != mDomain ) return false; } // Cookie path match check if (mPath.isEmpty()) return true; // According to the netscape spec both http://www.acme.com/foobar, // http://www.acme.com/foo.bar and http://www.acme.com/foo/bar // match http://www.acme.com/foo. // We only match http://www.acme.com/foo/bar if( path.startsWith(mPath) && ( (path.length() == mPath.length() ) || // Paths are exact match (path[mPath.length()-1] == '/') || // mPath ended with a slash (path[mPath.length()] == '/') // A slash follows. )) return true; // Path of URL starts with cookie-path return false;}// KHttpCookieList///////////////////////////////////////////////////////////////////////////int KHttpCookieList::compareItems( void * item1, void * item2){ int pathLen1 = ((KHttpCookie *)item1)->path().length(); int pathLen2 = ((KHttpCookie *)item2)->path().length(); if (pathLen1 > pathLen2) return -1; if (pathLen1 < pathLen2) return 1; return 0;}// KCookieJar/////////////////////////////////////////////////////////////////////////////// Constructs a new cookie jar//// One jar should be enough for all cookies.//KCookieJar::KCookieJar(){ m_cookieDomains.setAutoDelete( true ); m_globalAdvice = KCookieDunno; m_configChanged = false; m_cookiesChanged = false; KConfig cfg("khtml/domain_info", true, false, "data"); QStringList countries = cfg.readListEntry("twoLevelTLD"); for(QStringList::ConstIterator it = countries.begin(); it != countries.end(); ++it) { m_twoLevelTLD.replace(*it, (int *) 1); }}//// Destructs the cookie jar//// Poor little cookies, they will all be eaten by the cookie monster!//KCookieJar::~KCookieJar(){ // Not much to do here}static void removeDuplicateFromList(KHttpCookieList *list, KHttpCookie *cookiePtr, bool nameMatchOnly=false, bool updateWindowId=false){ QString domain1 = cookiePtr->domain(); if (domain1.isEmpty()) domain1 = cookiePtr->host(); for ( KHttpCookiePtr cookie=list->first(); cookie != 0; ) { QString domain2 = cookie->domain(); if (domain2.isEmpty()) domain2 = cookie->host(); if ( (cookiePtr->name() == cookie->name()) && ( nameMatchOnly || ( (domain1 == domain2) && (cookiePtr->path() == cookie->path()) ) ) ) { if (updateWindowId) { for(QValueList<long>::ConstIterator it = cookie->windowIds().begin(); it != cookie->windowIds().end(); ++it) { long windowId = *it; if (windowId && (cookiePtr->windowIds().find(windowId) == cookiePtr->windowIds().end())) { cookiePtr->windowIds().append(windowId); } } } KHttpCookiePtr old_cookie = cookie; cookie = list->next(); list->removeRef( old_cookie ); break; } else { cookie = list->next(); } }}//// Looks for cookies in the cookie jar which are appropriate for _url.// Returned is a string containing all appropriate cookies in a format// which can be added to a HTTP-header without any additional processing.//QString KCookieJar::findCookies(const QString &_url, bool useDOMFormat, long windowId, KHttpCookieList *pendingCookies){ QString cookieStr; QStringList domains; QString fqdn; QString path; KHttpCookiePtr cookie; KCookieAdvice advice = m_globalAdvice; if (!parseURL(_url, fqdn, path)) return cookieStr; bool secureRequest = (_url.find( L1("https://"), 0, false) == 0 || _url.find( L1("webdavs://"), 0, false) == 0); extractDomains(fqdn, domains); KHttpCookieList allCookies; for(QStringList::ConstIterator it = domains.begin(); true; ++it) { KHttpCookieList *cookieList; if (it == domains.end()) { cookieList = pendingCookies; // Add pending cookies pendingCookies = 0; if (!cookieList) break; } else { QString key = (*it).isNull() ? L1("") : (*it); cookieList = m_cookieDomains[key]; if (!cookieList) continue; // No cookies for this domain } if (cookieList->getAdvice() != KCookieDunno) advice = cookieList->getAdvice(); // Do not send cookies for this domain if policy is set to reject // and we are not setup to automatically accept all cookies as // session cookies... if (advice == KCookieReject && !(m_ignoreCookieExpirationDate && m_autoAcceptSessionCookies)) { if (it == domains.end()) break; // Finished. continue; } for ( cookie=cookieList->first(); cookie != 0; cookie=cookieList->next() ) { if (!cookie->match(fqdn, domains, path)) continue; if( cookie->isSecure() && !secureRequest ) continue; if( cookie->isHttpOnly() && useDOMFormat ) continue; // Do not send expired cookies. if ( cookie->isExpired (time(0)) ) { // Note there is no need to actually delete the cookie here // since the cookieserver will invoke ::saveCookieJar because // of the state change below. This will then do the job of // deleting the cookie for us. m_cookiesChanged = true; continue; } if (windowId && (cookie->windowIds().find(windowId) == cookie->windowIds().end())) { cookie->windowIds().append(windowId); } if (it == domains.end()) // Only needed when processing pending cookies removeDuplicateFromList(&allCookies, cookie); allCookies.append(cookie); } if (it == domains.end()) break; // Finished. } int cookieCount = 0; int protVersion=0; for ( cookie=allCookies.first(); cookie != 0; cookie=allCookies.next() ) { if (cookie->protocolVersion() > protVersion) protVersion = cookie->protocolVersion(); } for ( cookie=allCookies.first(); cookie != 0; cookie=allCookies.next() ) { if (useDOMFormat) { if (cookieCount > 0) cookieStr += L1("; "); cookieStr += cookie->cookieStr(true); } else { if (cookieCount == 0) { cookieStr += L1("Cookie: "); if (protVersion > 0) { QString version; version.sprintf("$Version=%d; ", protVersion); // Without quotes cookieStr += version; } } else { cookieStr += L1("; "); } cookieStr += cookie->cookieStr(false); } cookieCount++; } return cookieStr;}//// This function parses a string like 'my_name="my_value";' and returns// 'my_name' in Name and 'my_value' in Value.//// A pointer to the end of the parsed part is returned.// This pointer points either to:// '\0' - The end of the string has reached.// ';' - Another my_name="my_value" pair follows// ',' - Another cookie follows// '\n' - Another header followsstatic const char * parseNameValue(const char *header, QString &Name, QString &Value, bool keepQuotes=false, bool rfcQuotes=false){ const char *s = header; // Parse 'my_name' part for(; (*s != '='); s++) { if ((*s=='\0') || (*s==';') || (*s=='\n')) { // No '=' sign -> use string as the value, name is empty // (behavior found in Mozilla and IE) Name = ""; Value = QString::fromLatin1(header); Value.truncate( s - header ); Value = Value.stripWhiteSpace(); return (s); } } Name = header; Name.truncate( s - header ); Name = Name.stripWhiteSpace(); // *s == '=' s++; // Skip any whitespace for(; (*s == ' ') || (*s == '\t'); s++) { if ((*s=='\0') || (*s==';') || (*s=='\n')) { // End of Name Value = ""; return (s); } } if ((rfcQuotes || !keepQuotes) && (*s == '\"')) { // Parse '"my_value"' part (quoted value) if (keepQuotes) header = s++; else header = ++s; // skip " for(;(*s != '\"');s++) { if ((*s=='\0') || (*s=='\n')) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?