kcookiejar.cpp
来自「将konqueror浏览器移植到ARM9 2410中」· C++ 代码 · 共 1,257 行 · 第 1/3 页
CPP
1,257 行
/* This file is part of the KDE File Manager Copyright (C) 1998,1999,2000,2001 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 HTTP Cookie Manager// $Id: kcookiejar.cpp,v 1.58.2.5 2001/11/04 04:21:39 adawit Exp $//// 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 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>#include <stdlib.h>#include <qstring.h>#include <qstrlist.h>#include <qlist.h>#include <qdict.h>#include <kurl.h>#include <krfcdate.h>#include <kconfig.h>#include <ksavefile.h>#include <kdebug.h>#include "kcookiejar.h"#define READ_BUFFER_SIZE 8192template class QList<KHttpCookie>;template class QDict<KHttpCookieList>;QString KCookieJar::adviceToStr(KCookieAdvice _advice){ switch( _advice ) { case KCookieAccept: return "Accept"; case KCookieReject: return "Reject"; case KCookieAsk: return "Ask"; default: return "Dunno"; }}KCookieAdvice KCookieJar::strToAdvice(const QString &_str){ if (_str.isEmpty()) return KCookieDunno; QString advice = _str.lower(); 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) : mHost(_host), mDomain(_domain), mPath(_path), mName(_name), mValue(_value), mExpireDate(_expireDate), mProtocolVersion(_protocolVersion), mSecure(_secure){ nextCookie = 0;}//// 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)) { result = mName + "=" + mValue; } else { result.sprintf("$Version=\"%d\"; ", mProtocolVersion); result += mName + "=\"" + mValue + "\""; if (!mPath.isEmpty()) result += "; $Path=\""+ mPath + "\""; if (!mDomain.isEmpty()) result += "; $Domain=\""+ mDomain + "\""; } 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()) { // No domain set, check hostname. 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( !path.isEmpty() && !path.startsWith(mPath) ) return false; // Path of URL does not start with cookie-path return true;}// 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(){ cookieDomains.setAutoDelete( true ); globalAdvice = KCookieDunno; configChanged = false; cookiesChanged = false;}//// Destructs the cookie jar//// Poor little cookies, they will all be eaten by the cookie monster!//KCookieJar::~KCookieJar(){ // Not much to do here}//// 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){ QString cookieStr; QStringList domains; QString fqdn; QString path; KHttpCookiePtr cookie; int protVersion = 1; int cookieCount = 0; if (!parseURL(_url, fqdn, path)) { return cookieStr; } extractDomains(fqdn, domains); bool secureRequest = (_url.find( "https://", 0, false) == 0); for(QStringList::ConstIterator it = domains.begin(); it != domains.end(); ++it) { KHttpCookieList *cookieList = cookieDomains[(*it)]; if (!cookieList) continue; // No cookies for this domain for ( cookie=cookieList->first(); cookie != 0; cookie=cookieList->next() ) { if (!cookie->match(fqdn, domains, path) && cookie->domain().isEmpty()) { // The following code is added because RFC 2109 is completely // ambigious when it comes what needs to be done when cookies // with empty "domain=" fields are present! The following code // makes such cookies available to all the domains/hosts under // the TLD of the cookie in question! QStringList cookieDomainList; extractDomains( cookie->host(), cookieDomainList ); int fqdnCount = domains.count(); int cookieDomainCount = cookieDomainList.count(); if ( domains[fqdnCount-2] != cookieDomainList[cookieDomainCount-2] && domains[fqdnCount-1] != cookieDomainList[cookieDomainCount-1] ) continue; } if( cookie->isSecure() && !secureRequest ) continue; // Use first cookie to determine protocol version if (cookieCount == 0) { protVersion = cookie->protocolVersion(); } if (useDOMFormat) { if (cookieCount > 0) cookieStr += "; "; cookieStr += cookie->cookieStr(true); } else if (protVersion == 0) { if (cookieCount == 0) cookieStr += "Cookie: "; else cookieStr += "; "; cookieStr += cookie->cookieStr(false); } else { if (cookieCount > 0) cookieStr += "\r\n"; cookieStr += "Cookie: "; 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){ const char *s = header; // Parse 'my_name' part for(; (*s != '='); s++) { if ((*s=='\0') || (*s==';') || (*s=='\n')) { // End of Name Value = ""; Name = header; Name.truncate( s - header ); Name = Name.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 (!keepQuotes && (*s == '\"')) { // Parse '"my_value"' part (quoted value) s++; // skip " header = s; for(;(*s != '\"');s++) { if ((*s=='\0') || (*s=='\n')) { // End of Name Value = header; Value.truncate(s - header); return (s); } } Value = header; Value.truncate( s - header ); // *s == '\"'; s++; // Skip any remaining garbage for(;; s++) { if ((*s=='\0') || (*s==';') || (*s=='\n')) break; } } else { // Parse 'my_value' part (unquoted value) header = s; while ((*s != '\0') && (*s != ';') && (*s != '\n')) s++; // End of Name Value = header; Value.truncate( s - header ); Value = Value.stripWhiteSpace(); } return (s);}static void stripDomain(const QString &_fqdn, QString &_domain){ QStringList domains; KCookieJar::extractDomains(_fqdn, domains); _domain = domains[0];}static QString stripDomain( KHttpCookiePtr cookiePtr){ QString domain; // We file the cookie under this domain. if (cookiePtr->domain().isEmpty()) stripDomain( cookiePtr->host(), domain); else domain = cookiePtr->domain(); return domain;}bool KCookieJar::parseURL(const QString &_url, QString &_fqdn,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?