39 #include <sys/types.h>
41 #ifdef HAVE_SYS_PARAM_H
42 #include <sys/param.h>
59 #include <tqstrlist.h>
60 #include <tqptrlist.h>
61 #include <tqptrdict.h>
68 #include <tdeconfig.h>
69 #include <ksavefile.h>
72 #include "kcookiejar.h"
81 #undef MAX_COOKIE_LIMIT
83 #define MAX_COOKIES_PER_HOST 25
84 #define READ_BUFFER_SIZE 8192
85 #define IP_ADDRESS_EXPRESSION "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
92 #define L1(x) TQString::fromLatin1(x)
94 template class TQPtrList<KHttpCookie>;
95 template class TQPtrDict<KHttpCookieList>;
97 TQString KCookieJar::adviceToStr(KCookieAdvice _advice)
101 case KCookieAccept:
return L1(
"Accept");
102 case KCookieReject:
return L1(
"Reject");
103 case KCookieAsk:
return L1(
"Ask");
104 default:
return L1(
"Dunno");
108 KCookieAdvice KCookieJar::strToAdvice(
const TQString &_str)
113 TQCString advice = _str.lower().latin1();
115 if (advice ==
"accept")
116 return KCookieAccept;
117 else if (advice ==
"reject")
118 return KCookieReject;
119 else if (advice ==
"ask")
131 KHttpCookie::KHttpCookie(
const TQString &_host,
132 const TQString &_domain,
133 const TQString &_path,
134 const TQString &_name,
135 const TQString &_value,
137 int _protocolVersion,
140 bool _explicitPath) :
143 mPath(_path.isEmpty() ? TQString::null : _path),
146 mExpireDate(_expireDate),
147 mProtocolVersion(_protocolVersion),
150 mHttpOnly(_httpOnly),
151 mExplicitPath(_explicitPath)
158 bool KHttpCookie::isExpired(time_t currentDate)
160 return (mExpireDate != 0) && (mExpireDate < currentDate);
166 TQString KHttpCookie::cookieStr(
bool useDOMFormat)
170 if (useDOMFormat || (mProtocolVersion == 0))
172 if ( !mName.isEmpty() )
173 result = mName +
'=';
178 result = mName +
'=' + mValue;
180 result += L1(
"; $Path=\"") + mPath + L1(
"\"");
181 if (!mDomain.isEmpty())
182 result += L1(
"; $Domain=\"") + mDomain + L1(
"\"");
189 bool KHttpCookie::match(
const TQString &fqdn,
const TQStringList &domains,
190 const TQString &path)
193 if (mDomain.isEmpty())
198 else if (!domains.contains(mDomain))
200 if (mDomain[0] ==
'.')
204 TQString domain =
'.' + mDomain;
205 if ( !domains.contains( domain ) )
206 if ( fqdn != mDomain )
219 if( path.startsWith(mPath) &&
221 (path.length() == mPath.length() ) ||
222 (path[mPath.length()-1] ==
'/') ||
223 (path[mPath.length()] ==
'/')
233 int KHttpCookieList::compareItems(
void * item1,
void * item2)
235 int pathLen1 = ((KHttpCookie *)item1)->path().length();
236 int pathLen2 = ((KHttpCookie *)item2)->path().length();
237 if (pathLen1 > pathLen2)
239 if (pathLen1 < pathLen2)
253 KCookieJar::KCookieJar()
255 m_cookieDomains.setAutoDelete(
true );
256 m_globalAdvice = KCookieDunno;
257 m_configChanged =
false;
258 m_cookiesChanged =
false;
260 TDEConfig cfg(
"tdehtml/domain_info",
true,
false,
"data");
261 TQStringList countries = cfg.readListEntry(
"twoLevelTLD");
262 for(TQStringList::ConstIterator it = countries.begin();
263 it != countries.end(); ++it)
265 m_twoLevelTLD.replace(*it, (
int *) 1);
274 KCookieJar::~KCookieJar()
279 static void removeDuplicateFromList(KHttpCookieList *list, KHttpCookie *cookiePtr,
bool nameMatchOnly=
false,
bool updateWindowId=
false)
281 TQString domain1 = cookiePtr->domain();
282 if (domain1.isEmpty())
283 domain1 = cookiePtr->host();
285 for ( KHttpCookiePtr cookie=list->first(); cookie != 0; )
287 TQString domain2 = cookie->domain();
288 if (domain2.isEmpty())
289 domain2 = cookie->host();
292 (cookiePtr->name() == cookie->name()) &&
295 ( (domain1 == domain2) && (cookiePtr->path() == cookie->path()) )
301 for(TQValueList<long>::ConstIterator it = cookie->windowIds().begin();
302 it != cookie->windowIds().end(); ++it)
305 if (windowId && (cookiePtr->windowIds().find(windowId) == cookiePtr->windowIds().end()))
307 cookiePtr->windowIds().append(windowId);
311 KHttpCookiePtr old_cookie = cookie;
312 cookie = list->next();
313 list->removeRef( old_cookie );
318 cookie = list->next();
329 TQString KCookieJar::findCookies(
const TQString &_url,
bool useDOMFormat,
long windowId, KHttpCookieList *pendingCookies)
332 TQStringList domains;
335 KHttpCookiePtr cookie;
336 KCookieAdvice advice = m_globalAdvice;
338 if (!parseURL(_url, fqdn, path))
341 bool secureRequest = (_url.find( L1(
"https://"), 0,
false) == 0 ||
342 _url.find( L1(
"webdavs://"), 0,
false) == 0);
346 extractDomains(fqdn, domains);
348 KHttpCookieList allCookies;
350 for(TQStringList::ConstIterator it = domains.begin();
354 KHttpCookieList *cookieList;
355 if (it == domains.end())
357 cookieList = pendingCookies;
364 TQString key = (*it).isNull() ? L1(
"") : (*it);
365 cookieList = m_cookieDomains[key];
370 if (cookieList->getAdvice() != KCookieDunno)
371 advice = cookieList->getAdvice();
373 for ( cookie=cookieList->first(); cookie != 0; cookie=cookieList->next() )
378 if (advice == KCookieReject &&
379 !(m_autoAcceptSessionCookies &&
380 (m_ignoreCookieExpirationDate || cookie->expireDate() == 0)))
383 if (!cookie->match(fqdn, domains, path))
386 if( cookie->isSecure() && !secureRequest )
389 if( cookie->isHttpOnly() && useDOMFormat )
393 if ( cookie->isExpired (time(0)) )
399 m_cookiesChanged =
true;
403 if (windowId && (cookie->windowIds().find(windowId) == cookie->windowIds().end()))
405 cookie->windowIds().append(windowId);
408 if (it == domains.end())
409 removeDuplicateFromList(&allCookies, cookie);
411 allCookies.append(cookie);
413 if (it == domains.end())
420 for ( cookie=allCookies.first(); cookie != 0; cookie=allCookies.next() )
422 if (cookie->protocolVersion() > protVersion)
423 protVersion = cookie->protocolVersion();
426 for ( cookie=allCookies.first(); cookie != 0; cookie=allCookies.next() )
431 cookieStr += L1(
"; ");
432 cookieStr += cookie->cookieStr(
true);
436 if (cookieCount == 0)
438 cookieStr += L1(
"Cookie: ");
442 version.sprintf(
"$Version=%d; ", protVersion);
443 cookieStr += version;
448 cookieStr += L1(
"; ");
450 cookieStr += cookie->cookieStr(
false);
468 static const char * parseNameValue(
const char *header,
471 bool keepQuotes=
false,
472 bool rfcQuotes=
false)
474 const char *s = header;
476 for(; (*s !=
'='); s++)
478 if ((*s==
'\0') || (*s==
';') || (*s==
'\n'))
483 Value = TQString::fromLatin1(header);
484 Value.truncate( s - header );
485 Value = Value.stripWhiteSpace();
491 Name.truncate( s - header );
492 Name = Name.stripWhiteSpace();
498 for(; (*s ==
' ') || (*s ==
'\t'); s++)
500 if ((*s==
'\0') || (*s==
';') || (*s==
'\n'))
508 if ((rfcQuotes || !keepQuotes) && (*s ==
'\"'))
515 for(;(*s !=
'\"');s++)
517 if ((*s==
'\0') || (*s==
'\n'))
520 Value = TQString::fromLatin1(header);
521 Value.truncate(s - header);
525 Value = TQString::fromLatin1(header);
528 Value.truncate( ++s - header );
530 Value.truncate( s++ - header );
535 if ((*s==
'\0') || (*s==
';') || (*s==
'\n'))
543 while ((*s !=
'\0') && (*s !=
';') && (*s !=
'\n'))
546 Value = TQString::fromLatin1(header);
547 Value.truncate( s - header );
548 Value = Value.stripWhiteSpace();
554 void KCookieJar::stripDomain(
const TQString &_fqdn, TQString &_domain)
556 TQStringList domains;
557 extractDomains(_fqdn, domains);
558 if (domains.count() > 3)
559 _domain = domains[3];
561 _domain = domains[0];
564 TQString KCookieJar::stripDomain( KHttpCookiePtr cookiePtr)
567 if (cookiePtr->domain().isEmpty())
568 stripDomain( cookiePtr->host(), domain);
570 stripDomain (cookiePtr->domain(), domain);
574 bool KCookieJar::parseURL(
const TQString &_url,
582 _fqdn = kurl.host().lower();
585 if (((kurl.protocol() == L1(
"http")) && (kurl.port() != 80)) ||
586 ((kurl.protocol() == L1(
"https")) && (kurl.port() != 443)))
588 _fqdn = L1(
"%1:%2").arg(kurl.port()).arg(_fqdn);
595 if(_fqdn.find(
'/') > -1 || _fqdn.find(
'%') > -1)
604 TQRegExp exp(L1(
"[\\\\/]\\.\\.[\\\\/]"));
606 if (exp.search(_path) != -1)
612 void KCookieJar::extractDomains(
const TQString &_fqdn,
613 TQStringList &_domains)
618 _domains.append( _fqdn );
622 if ((_fqdn.at(0) >= TQChar(
'0')) && (_fqdn.at(0) <= TQChar(
'9')))
624 if (_fqdn.find(TQRegExp(IP_ADDRESS_EXPRESSION)) > -1)
626 _domains.append( _fqdn );
631 TQStringList partList = TQStringList::split(
'.', _fqdn,
false);
633 if (partList.count())
634 partList.remove(partList.begin());
636 while(partList.count())
639 if (partList.count() == 1)
642 if ((partList.count() == 2) && (m_twoLevelTLD[partList[1].lower()]))
648 if ((partList.count() == 2) && (partList[1].length() == 2))
652 if (partList[0].length() <= 2)
657 TQCString t = partList[0].lower().utf8();
658 if ((t ==
"com") || (t ==
"net") || (t ==
"org") || (t ==
"gov") || (t ==
"edu") || (t ==
"mil") || (t ==
"int"))
662 TQString domain = partList.join(L1(
"."));
663 _domains.append(domain);
664 _domains.append(
'.' + domain);
665 partList.remove(partList.begin());
670 _domains.prepend(
'.' + _fqdn );
671 _domains.prepend( _fqdn );
685 static TQString fixupDateTime(
const TQString& dt)
687 const int index = dt.find(TQRegExp(
"[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}"));
691 TQStringList dateStrList = TQStringList::split(
' ', dt.mid(index));
692 if (dateStrList.count() > 1)
694 TQString date = dateStrList[0];
695 dateStrList[0] = dateStrList[1];
696 dateStrList[1] = date;
698 return date.replace(index, date.length(), dateStrList.join(
" "));
713 KHttpCookieList KCookieJar::makeCookies(
const TQString &_url,
714 const TQCString &cookie_headers,
717 KHttpCookieList cookieList;
718 KHttpCookieList cookieList2;
719 KHttpCookiePtr lastCookie = 0;
720 const char *cookieStr = cookie_headers.data();
725 bool crossDomain =
false;
727 if (!parseURL(_url, fqdn, path))
730 return KHttpCookieList();
732 TQString defaultPath;
733 int i = path.findRev(
'/');
735 defaultPath = path.left(i);
741 if (strncmp(cookieStr,
"Cross-Domain\n", 13) == 0)
746 else if (strncasecmp(cookieStr,
"Set-Cookie:", 11) == 0)
748 cookieStr = parseNameValue(cookieStr+11, Name, Value,
true);
754 KHttpCookie *cookie =
new KHttpCookie(fqdn, L1(
""), defaultPath, Name, Value);
756 cookie->mWindowIds.append(windowId);
757 cookie->mCrossDomain = crossDomain;
760 cookieList.append(cookie);
763 else if (strncasecmp(cookieStr,
"Set-Cookie2:", 12) == 0)
766 cookieStr = parseNameValue(cookieStr+12, Name, Value,
true,
true);
772 KHttpCookie *cookie =
new KHttpCookie(fqdn, L1(
""), defaultPath, Name, Value);
774 cookie->mWindowIds.append(windowId);
775 cookie->mCrossDomain = crossDomain;
778 cookieList2.append(cookie);
784 while (*cookieStr && *cookieStr !=
'\n')
787 if (*cookieStr ==
'\n')
796 while ((*cookieStr ==
';') || (*cookieStr ==
' '))
801 cookieStr = parseNameValue(cookieStr, Name, Value);
803 TQCString cName = Name.lower().latin1();
804 if (cName ==
"domain")
806 TQString dom = Value.lower();
809 if(dom.length() && dom[0] !=
'.')
812 if(dom.length() > 2 && dom[dom.length()-1] ==
'.')
813 dom = dom.left(dom.length()-1);
815 if(dom.contains(
'.') > 1 || dom ==
".local")
816 lastCookie->mDomain = dom;
818 else if (cName ==
"max-age")
820 int max_age = Value.toInt();
822 lastCookie->mExpireDate = 1;
824 lastCookie->mExpireDate = time(0)+max_age;
826 else if (cName ==
"expires")
829 lastCookie->mExpireDate = KRFCDate::parseDate(Value);
833 if (lastCookie->mExpireDate == 0)
834 lastCookie->mExpireDate = KRFCDate::parseDate(fixupDateTime(Value));
836 else if (cName ==
"path")
839 lastCookie->mPath = TQString::null;
841 lastCookie->mPath = KURL::decode_string(Value);
842 lastCookie->mExplicitPath =
true;
844 else if (cName ==
"version")
846 lastCookie->mProtocolVersion = Value.toInt();
848 else if ((cName ==
"secure") ||
849 (cName.isEmpty() && Value.lower() == L1(
"secure")))
851 lastCookie->mSecure =
true;
853 else if ((cName ==
"httponly") ||
854 (cName.isEmpty() && Value.lower() == L1(
"httponly")))
856 lastCookie->mHttpOnly =
true;
860 if (*cookieStr ==
'\0')
868 while( !cookieList2.isEmpty() && (lastCookie = cookieList2.take(0)) )
870 removeDuplicateFromList(&cookieList, lastCookie,
true);
871 cookieList.append(lastCookie);
883 KHttpCookieList KCookieJar::makeDOMCookies(
const TQString &_url,
884 const TQCString &cookie_domstring,
888 KHttpCookieList cookieList;
889 KHttpCookiePtr lastCookie = 0;
891 const char *cookieStr = cookie_domstring.data();
897 if (!parseURL(_url, fqdn, path))
900 return KHttpCookieList();
906 cookieStr = parseNameValue(cookieStr, Name, Value);
911 KHttpCookie *cookie =
new KHttpCookie(fqdn, TQString::null, TQString::null,
914 cookie->mWindowIds.append(windowId);
916 cookieList.append(cookie);
919 if (*cookieStr !=
'\0')
926 #ifdef MAX_COOKIE_LIMIT
927 static void makeRoom(KHttpCookieList *cookieList, KHttpCookiePtr &cookiePtr)
930 KHttpCookiePtr lastCookie = 0;
931 for(KHttpCookiePtr cookie = cookieList->first(); cookie; cookie = cookieList->next())
933 if (cookieList->compareItems(cookie, cookiePtr) < 0)
938 lastCookie = cookieList->first();
939 cookieList->removeRef(lastCookie);
948 void KCookieJar::addCookie(KHttpCookiePtr &cookiePtr)
950 TQStringList domains;
951 KHttpCookieList *cookieList = 0L;
956 extractDomains( cookiePtr->host(), domains );
957 for ( TQStringList::ConstIterator it = domains.begin();
958 (it != domains.end() && !cookieList);
961 TQString key = (*it).isNull() ? L1(
"") : (*it);
962 KHttpCookieList *list= m_cookieDomains[key];
963 if ( !list )
continue;
965 removeDuplicateFromList(list, cookiePtr,
false,
true);
968 TQString domain = stripDomain( cookiePtr );
969 TQString key = domain.isNull() ? L1(
"") : domain;
970 cookieList = m_cookieDomains[ key ];
974 cookieList =
new KHttpCookieList();
975 cookieList->setAutoDelete(
true);
980 cookieList->setAdvice( KCookieDunno );
982 m_cookieDomains.insert( domain, cookieList);
985 m_domainList.append(domain);
990 if (!cookiePtr->isExpired(time(0)))
992 #ifdef MAX_COOKIE_LIMIT
993 if (cookieList->count() >= MAX_COOKIES_PER_HOST)
994 makeRoom(cookieList, cookiePtr);
996 cookieList->inSort( cookiePtr );
997 m_cookiesChanged =
true;
1010 KCookieAdvice KCookieJar::cookieAdvice(KHttpCookiePtr cookiePtr)
1012 if (m_rejectCrossDomainCookies && cookiePtr->isCrossDomain())
1013 return KCookieReject;
1015 TQStringList domains;
1017 extractDomains(cookiePtr->host(), domains);
1022 if (!cookiePtr->domain().isEmpty())
1024 if (!domains.contains(cookiePtr->domain()) &&
1025 !cookiePtr->domain().endsWith(
"."+cookiePtr->host()))
1026 cookiePtr->fixDomain(TQString::null);
1029 if (m_autoAcceptSessionCookies && (cookiePtr->expireDate() == 0 ||
1030 m_ignoreCookieExpirationDate))
1031 return KCookieAccept;
1033 KCookieAdvice advice = KCookieDunno;
1035 TQStringList::Iterator it = domains.begin();
1036 while( (advice == KCookieDunno) && (it != domains.end()))
1038 TQString domain = *it;
1040 if ( domain[0] ==
'.' || isFQDN )
1043 KHttpCookieList *cookieList = m_cookieDomains[domain];
1045 advice = cookieList->getAdvice();
1048 it = domains.begin();
1051 if (advice == KCookieDunno)
1052 advice = m_globalAdvice;
1061 KCookieAdvice KCookieJar::getDomainAdvice(
const TQString &_domain)
1063 KHttpCookieList *cookieList = m_cookieDomains[_domain];
1064 KCookieAdvice advice;
1068 advice = cookieList->getAdvice();
1072 advice = KCookieDunno;
1082 void KCookieJar::setDomainAdvice(
const TQString &_domain, KCookieAdvice _advice)
1084 TQString domain(_domain);
1085 KHttpCookieList *cookieList = m_cookieDomains[domain];
1089 if (cookieList->getAdvice() != _advice)
1091 m_configChanged =
true;
1093 cookieList->setAdvice( _advice);
1096 if ((cookieList->isEmpty()) &&
1097 (_advice == KCookieDunno))
1100 m_cookieDomains.remove(domain);
1101 m_domainList.remove(domain);
1107 if (_advice != KCookieDunno)
1110 m_configChanged =
true;
1112 cookieList =
new KHttpCookieList();
1113 cookieList->setAutoDelete(
true);
1114 cookieList->setAdvice( _advice);
1115 m_cookieDomains.insert( domain, cookieList);
1117 m_domainList.append( domain);
1126 void KCookieJar::setDomainAdvice(KHttpCookiePtr cookiePtr, KCookieAdvice _advice)
1129 stripDomain(cookiePtr->host(), domain);
1131 setDomainAdvice(domain, _advice);
1137 void KCookieJar::setGlobalAdvice(KCookieAdvice _advice)
1139 if (m_globalAdvice != _advice)
1140 m_configChanged =
true;
1141 m_globalAdvice = _advice;
1147 const TQStringList& KCookieJar::getDomainList()
1149 return m_domainList;
1155 const KHttpCookieList *KCookieJar::getCookieList(
const TQString & _domain,
1156 const TQString & _fqdn )
1160 if (_domain.isEmpty())
1161 stripDomain( _fqdn, domain );
1165 return m_cookieDomains[domain];
1172 void KCookieJar::eatCookie(KHttpCookiePtr cookiePtr)
1174 TQString domain = stripDomain(cookiePtr);
1175 KHttpCookieList *cookieList = m_cookieDomains[domain];
1180 if (cookieList->removeRef( cookiePtr ))
1181 m_cookiesChanged =
true;
1183 if ((cookieList->isEmpty()) &&
1184 (cookieList->getAdvice() == KCookieDunno))
1187 m_cookieDomains.remove(domain);
1189 m_domainList.remove(domain);
1194 void KCookieJar::eatCookiesForDomain(
const TQString &domain)
1196 KHttpCookieList *cookieList = m_cookieDomains[domain];
1197 if (!cookieList || cookieList->isEmpty())
return;
1199 cookieList->clear();
1200 if (cookieList->getAdvice() == KCookieDunno)
1203 m_cookieDomains.remove(domain);
1204 m_domainList.remove(domain);
1206 m_cookiesChanged =
true;
1209 void KCookieJar::eatSessionCookies(
long windowId )
1214 TQStringList::Iterator it=m_domainList.begin();
1215 for ( ; it != m_domainList.end(); ++it )
1216 eatSessionCookies( *it, windowId,
false );
1219 void KCookieJar::eatAllCookies()
1221 for ( TQStringList::Iterator it=m_domainList.begin();
1222 it != m_domainList.end();)
1224 TQString domain = *it++;
1226 eatCookiesForDomain(domain);
1230 void KCookieJar::eatSessionCookies(
const TQString& fqdn,
long windowId,
1233 KHttpCookieList* cookieList;
1235 cookieList = m_cookieDomains[fqdn];
1239 stripDomain( fqdn, domain );
1240 cookieList = m_cookieDomains[domain];
1245 KHttpCookiePtr cookie=cookieList->first();
1246 for (; cookie != 0;)
1248 if ((cookie->expireDate() != 0) && !m_ignoreCookieExpirationDate)
1250 cookie = cookieList->next();
1254 TQValueList<long> &ids = cookie->windowIds();
1255 if (!ids.remove(windowId) || !ids.isEmpty())
1257 cookie = cookieList->next();
1260 KHttpCookiePtr old_cookie = cookie;
1261 cookie = cookieList->next();
1262 cookieList->removeRef( old_cookie );
1271 bool KCookieJar::saveCookies(
const TQString &_filename)
1273 KSaveFile saveFile(_filename, 0600);
1275 if (saveFile.status() != 0)
1278 FILE *fStream = saveFile.fstream();
1280 time_t curTime = time(0);
1282 fprintf(fStream,
"# KDE Cookie File v2\n#\n");
1284 fprintf(fStream,
"%-20s %-20s %-12s %-10s %-4s %-20s %-4s %s\n",
1285 "# Host",
"Domain",
"Path",
"Exp.date",
"Prot",
1286 "Name",
"Sec",
"Value");
1288 for ( TQStringList::Iterator it=m_domainList.begin(); it != m_domainList.end();
1291 const TQString &domain = *it;
1292 bool domainPrinted =
false;
1294 KHttpCookieList *cookieList = m_cookieDomains[domain];
1295 KHttpCookiePtr cookie=cookieList->last();
1297 for (; cookie != 0;)
1299 if (cookie->isExpired(curTime))
1302 KHttpCookiePtr old_cookie = cookie;
1303 cookie = cookieList->prev();
1304 cookieList->removeRef( old_cookie );
1306 else if (cookie->expireDate() != 0 && !m_ignoreCookieExpirationDate)
1310 domainPrinted =
true;
1311 fprintf(fStream,
"[%s]\n", domain.local8Bit().data());
1314 TQString path = L1(
"\"");
1315 path += cookie->path();
1317 TQString domain = L1(
"\"");
1318 domain += cookie->domain();
1320 fprintf(fStream,
"%-20s %-20s %-12s %10lu %3d %-20s %-4i %s\n",
1321 cookie->host().latin1(), domain.latin1(),
1322 path.latin1(), (
unsigned long) cookie->expireDate(),
1323 cookie->protocolVersion(),
1324 cookie->name().isEmpty() ? cookie->value().latin1() : cookie->name().latin1(),
1325 (cookie->isSecure() ? 1 : 0) + (cookie->isHttpOnly() ? 2 : 0) +
1326 (cookie->hasExplicitPath() ? 4 : 0) + (cookie->name().isEmpty() ? 8 : 0),
1327 cookie->value().latin1());
1328 cookie = cookieList->prev();
1333 cookie = cookieList->prev();
1338 return saveFile.close();
1341 typedef char *charPtr;
1343 static const char *parseField(charPtr &buffer,
bool keepQuotes=
false)
1346 if (!keepQuotes && (*buffer ==
'\"'))
1351 while((*buffer !=
'\"') && (*buffer))
1358 while((*buffer !=
' ') && (*buffer !=
'\t') && (*buffer !=
'\n') && (*buffer))
1367 while((*buffer ==
' ') || (*buffer ==
'\t') || (*buffer ==
'\n'))
1378 bool KCookieJar::loadCookies(
const TQString &_filename)
1380 FILE *fStream = fopen( TQFile::encodeName(_filename),
"r");
1386 time_t curTime = time(0);
1388 char *buffer =
new char[READ_BUFFER_SIZE];
1391 err = (fgets(buffer, READ_BUFFER_SIZE, fStream) == 0);
1396 if (strcmp(buffer,
"# KDE Cookie File\n") == 0)
1400 else if (sscanf(buffer,
"# KDE Cookie File v%d\n", &version) != 1)
1408 while(fgets(buffer, READ_BUFFER_SIZE, fStream) != 0)
1410 char *line = buffer;
1412 if ((line[0] ==
'#') || (line[0] ==
'['))
1415 const char *host( parseField(line) );
1416 const char *domain( parseField(line) );
1417 const char *path( parseField(line) );
1418 const char *expStr( parseField(line) );
1419 if (!expStr)
continue;
1420 int expDate = (time_t) strtoul(expStr, 0, 10);
1421 const char *verStr( parseField(line) );
1422 if (!verStr)
continue;
1423 int protVer = (time_t) strtoul(verStr, 0, 10);
1424 const char *name( parseField(line) );
1425 bool keepQuotes =
false;
1426 bool secure =
false;
1427 bool httpOnly =
false;
1428 bool explicitPath =
false;
1429 const char *value = 0;
1430 if ((version == 2) || (protVer >= 200))
1434 int i = atoi( parseField(line) );
1437 explicitPath = i & 4;
1440 line[strlen(line)-1] =
'\0';
1450 value = parseField(line, keepQuotes);
1451 secure = atoi( parseField(line) );
1455 if (!value)
continue;
1458 if ((expDate == 0) || (expDate < curTime))
1461 KHttpCookie *cookie =
new KHttpCookie(TQString::fromLatin1(host),
1462 TQString::fromLatin1(domain),
1463 TQString::fromLatin1(path),
1464 TQString::fromLatin1(name),
1465 TQString::fromLatin1(value),
1467 secure, httpOnly, explicitPath);
1472 m_cookiesChanged =
false;
1482 void KCookieJar::saveConfig(TDEConfig *_config)
1484 if (!m_configChanged)
1487 _config->setGroup(
"Cookie Dialog");
1488 _config->writeEntry(
"PreferredPolicy", m_preferredPolicy);
1489 _config->writeEntry(
"ShowCookieDetails", m_showCookieDetails );
1490 _config->setGroup(
"Cookie Policy");
1491 _config->writeEntry(
"CookieGlobalAdvice", adviceToStr( m_globalAdvice));
1493 TQStringList domainSettings;
1494 for ( TQStringList::Iterator it=m_domainList.begin();
1495 it != m_domainList.end();
1498 const TQString &domain = *it;
1499 KCookieAdvice advice = getDomainAdvice( domain);
1500 if (advice != KCookieDunno)
1502 TQString value(domain);
1504 value += adviceToStr(advice);
1505 domainSettings.append(value);
1508 _config->writeEntry(
"CookieDomainAdvice", domainSettings);
1510 m_configChanged =
false;
1518 void KCookieJar::loadConfig(TDEConfig *_config,
bool reparse )
1521 _config->reparseConfiguration();
1523 _config->setGroup(
"Cookie Dialog");
1524 m_showCookieDetails = _config->readBoolEntry(
"ShowCookieDetails" );
1525 m_preferredPolicy = _config->readNumEntry(
"PreferredPolicy", 0 );
1527 _config->setGroup(
"Cookie Policy");
1528 TQStringList domainSettings = _config->readListEntry(
"CookieDomainAdvice");
1529 m_rejectCrossDomainCookies = _config->readBoolEntry(
"RejectCrossDomainCookies",
true );
1530 m_autoAcceptSessionCookies = _config->readBoolEntry(
"AcceptSessionCookies",
true );
1531 m_ignoreCookieExpirationDate = _config->readBoolEntry(
"IgnoreExpirationDate",
false );
1532 TQString value = _config->readEntry(
"CookieGlobalAdvice", L1(
"Ask"));
1533 m_globalAdvice = strToAdvice(value);
1536 for ( TQStringList::Iterator it=m_domainList.begin(); it != m_domainList.end(); )
1540 TQString domain = *it++;
1541 setDomainAdvice(domain, KCookieDunno);
1545 for ( TQStringList::Iterator it=domainSettings.begin();
1546 it != domainSettings.end(); )
1548 const TQString &value = *it++;
1550 int sepPos = value.findRev(
':');
1555 TQString domain(value.left(sepPos));
1556 KCookieAdvice advice = strToAdvice( value.mid(sepPos + 1) );
1557 setDomainAdvice(domain, advice);