47 #include <QtCore/QString>
48 #include <QtCore/QFile>
49 #include <QtCore/QDir>
50 #include <QtCore/QRegExp>
51 #include <QtCore/QTextStream>
59 #undef MAX_COOKIE_LIMIT
61 #define MAX_COOKIES_PER_HOST 25
62 #define READ_BUFFER_SIZE 8192
63 #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]?)"
68 #define QL1S(x) QLatin1String(x)
69 #define QL1C(x) QLatin1Char(x)
74 const int index = value.indexOf(
QL1C(
' '));
77 const QString weekday = value.left(index);
78 for (
int i = 1; i < 8; ++i) {
79 if (weekday.startsWith(QDate::shortDayName(i), Qt::CaseInsensitive) ||
80 weekday.startsWith(QDate::longDayName(i), Qt::CaseInsensitive)) {
86 return value.mid(pos);
104 static const char* date_formats[] = {
105 "%:B%t%d%t%H:%M:%S%t%Y%t%Z",
106 "%:B%t%d%t%Y%t%H:%M:%S%t%Z",
110 for (
int i = 0; date_formats[i]; ++i) {
111 qDebug() <<
"Checking" << value <<
"vs" << date_formats[i];
114 qDebug() <<
"Matched" << date_formats[i];
125 return (dt.toMSecsSinceEpoch()/1000);
130 return toEpochSecs(QDateTime::currentDateTimeUtc());
140 default:
return QL1S(
"Dunno");
149 QString advice = _str.toLower();
151 if (advice ==
QL1S(
"accept"))
153 else if (advice ==
QL1S(
"reject"))
155 else if (advice ==
QL1S(
"ask"))
173 int _protocolVersion,
176 bool _explicitPath) :
179 mPath(_path.isEmpty() ?
QString() : _path),
182 mExpireDate(_expireDate),
183 mProtocolVersion(_protocolVersion),
185 mHttpOnly(_httpOnly),
186 mExplicitPath(_explicitPath)
195 if (currentDate == -1)
196 currentDate =
epoch();
209 if (
mName.isEmpty() )
221 result +=
QL1S(
"; $Port");
224 Q_FOREACH(
int port,
mPorts)
225 portNums += QString::number(port) +
QL1C(
' ');
226 result +=
QL1S(
"; $Port=\"") + portNums.trimmed() +
QL1C(
'"');
236 const QString &path,
int port)
const
244 else if (!domains.contains(
mDomain))
251 if ( !domains.contains( domain ) )
269 if( path.startsWith(
mPath) &&
271 (path.length() ==
mPath.length() ) ||
315 if (domain1.isEmpty())
316 domain1 = cookiePtr.
host();
318 QMutableListIterator<KHttpCookie> cookieIterator(*list);
319 while (cookieIterator.hasNext()) {
322 if (domain2.isEmpty())
323 domain2 = cookie.
host();
325 if (cookiePtr.
name() == cookie.
name() &&
326 (nameMatchOnly || (domain1 == domain2 && cookiePtr.
path() == cookie.
path())))
328 if (updateWindowId) {
329 Q_FOREACH(
long windowId, cookie.
windowIds()) {
330 if (windowId && (!cookiePtr.
windowIds().contains(windowId))) {
335 cookieIterator.remove();
354 if (!
parseUrl(_url, fqdn, path, &port))
357 const bool secureRequest = (_url.startsWith(
QL1S(
"https://"), Qt::CaseInsensitive) ||
358 _url.startsWith(
QL1S(
"webdavs://"), Qt::CaseInsensitive));
360 port = (secureRequest ? 443 : 80);
365 for (QStringList::ConstIterator it = domains.constBegin(), itEnd = domains.constEnd();;++it)
370 cookieList = pendingCookies;
389 QMutableListIterator<KHttpCookie> cookieIt (*cookieList);
390 while (cookieIt.hasNext())
401 if (!cookie.
match(fqdn, domains, path, port))
404 if( cookie.
isSecure() && !secureRequest )
421 if (windowId && (cookie.
windowIds().indexOf(windowId) == -1))
427 allCookies.append(cookie);
440 if (!allCookies.isEmpty())
443 cookieStr =
QL1S(
"Cookie: ");
446 cookieStr = cookieStr +
QL1S(
"$Version=") + QString::number(protVersion) +
QL1S(
"; ");
449 cookieStr = cookieStr + cookie.
cookieStr(useDOMFormat) +
QL1S(
"; ");
451 cookieStr.truncate(cookieStr.length() - 2);
470 bool keepQuotes=
false,
471 bool rfcQuotes=
false)
473 const char *s = header;
475 for(; (*s !=
'='); s++)
477 if ((*s==
'\0') || (*s==
';') || (*s==
'\n'))
482 Value =
QL1S(header);
483 Value.truncate( s - header );
484 Value = Value.trimmed();
490 Name.truncate( s - header );
491 Name = Name.trimmed();
497 for(; (*s ==
' ') || (*s ==
'\t'); s++)
499 if ((*s==
'\0') || (*s==
';') || (*s==
'\n'))
507 if ((rfcQuotes || !keepQuotes) && (*s ==
'\"'))
514 for(;(*s !=
'\"');s++)
516 if ((*s==
'\0') || (*s==
'\n'))
519 Value =
QL1S(header);
520 Value.truncate(s - header);
524 Value =
QL1S(header);
527 Value.truncate( ++s - header );
529 Value.truncate( s++ - header );
534 if ((*s==
'\0') || (*s==
';') || (*s==
'\n'))
542 while ((*s !=
'\0') && (*s !=
';') && (*s !=
'\n'))
545 Value =
QL1S(header);
546 Value.truncate( s - header );
547 Value = Value.trimmed();
557 if (domains.count() > 3)
558 _domain = domains[3];
559 else if ( domains.count() > 0 )
560 _domain = domains[0];
568 if (cookie.
domain().isEmpty())
581 if (!kurl.isValid() || kurl.
protocol().isEmpty())
584 _fqdn = kurl.host().toLower();
588 if (_fqdn.contains(
QL1C(
'/')) || _fqdn.contains(
QL1C(
'%')))
606 if (_fqdn.isEmpty()) {
607 _domains.append(
QL1S(
"localhost") );
614 _domains.append( _fqdn );
620 _domains.append( _fqdn );
626 _domains.append(_fqdn);
627 _domains.append(
QL1C(
'.') + _fqdn);
629 QStringList partList = _fqdn.split(
QL1C(
'.'), QString::SkipEmptyParts);
631 if (partList.count())
632 partList.erase(partList.begin());
634 while(partList.count())
637 if (partList.count() == 1)
640 if ((partList.count() == 2) &&
m_twoLevelTLD.contains(partList[1].toLower()))
646 if ((partList.count() == 2) && (partList[1].length() == 2))
650 if (partList[0].length() <= 2)
655 if (
m_gTLDs.contains(partList[0].toLower()))
660 _domains.append(domain);
661 _domains.append(
QL1C(
'.') + domain);
662 partList.erase(partList.begin());
675 const QByteArray &cookie_headers,
686 bool isRFC2965 =
false;
687 bool crossDomain =
false;
688 const char *cookieStr = cookie_headers.constData();
691 const int index = path.lastIndexOf(
QL1C(
'/'));
693 defaultPath = path.left(index);
696 if (qstrncmp(cookieStr,
"Cross-Domain\n", 13) == 0)
706 if (qstrnicmp(cookieStr,
"Set-Cookie:", 11) == 0)
721 cookieList.append(cookie);
723 else if (qstrnicmp(cookieStr,
"Set-Cookie2:", 12) == 0)
727 cookieStr =
parseNameValue(cookieStr+12, Name, Value,
true,
true);
739 cookieList2.append(cookie);
744 while (*cookieStr && *cookieStr !=
'\n')
747 if (*cookieStr ==
'\n')
756 while ((*cookieStr ==
';') || (*cookieStr ==
' '))
762 KHttpCookie& lastCookie = (isRFC2965 ? cookieList2.last() : cookieList.last());
764 if (Name.compare(
QL1S(
"domain"), Qt::CaseInsensitive) == 0)
769 if(dom.length() && dom[0] !=
'.')
772 if(dom.length() > 2 && dom[dom.length()-1] ==
'.')
773 dom = dom.left(dom.length()-1);
775 if(dom.count(
QL1C(
'.')) > 1 || dom ==
".local")
778 else if (Name.compare(
QL1S(
"max-age"), Qt::CaseInsensitive) == 0)
780 int max_age = Value.toInt();
786 else if (Name.compare(
QL1S(
"expires"), Qt::CaseInsensitive) == 0)
796 else if (Name.compare(
QL1S(
"path"), Qt::CaseInsensitive) == 0)
799 lastCookie.
mPath.clear();
801 lastCookie.
mPath = QUrl::fromPercentEncoding(Value.toLatin1());
804 else if (Name.compare(
QL1S(
"version"), Qt::CaseInsensitive) == 0)
808 else if (Name.compare(
QL1S(
"secure"), Qt::CaseInsensitive) == 0 ||
809 (Name.isEmpty() && Value.compare(
QL1S(
"secure"), Qt::CaseInsensitive) == 0))
813 else if (Name.compare(
QL1S(
"httponly"), Qt::CaseInsensitive) == 0 ||
814 (Name.isEmpty() && Value.compare(
QL1S(
"httponly"), Qt::CaseInsensitive) == 0))
818 else if (isRFC2965 && (Name.compare(
QL1S(
"port"), Qt::CaseInsensitive) == 0 ||
819 (Name.isEmpty() && Value.compare(
QL1S(
"port"), Qt::CaseInsensitive) == 0)))
826 lastCookie.
mPorts.append(-1);
827 const bool secureRequest = (_url.startsWith(
QL1S(
"https://"), Qt::CaseInsensitive) ||
828 _url.startsWith(
QL1S(
"webdavs://"), Qt::CaseInsensitive));
830 lastCookie.
mPorts.append(443);
832 lastCookie.
mPorts.append(80);
837 const QStringList portNums = Value.split(
QL1C(
' '), QString::SkipEmptyParts);
838 Q_FOREACH(
const QString& portNum, portNums)
840 const int port = portNum.toInt(&ok);
842 lastCookie.
mPorts.append(port);
848 if (*cookieStr ==
'\0')
856 while(!cookieList2.isEmpty()) {
859 cookieList.append(lastCookie);
860 cookieList2.removeFirst();
873 const QByteArray &cookie_domstring,
879 const char *cookieStr = cookie_domstring.data();
904 cookieList.append(cookie);
906 if (*cookieStr !=
'\0')
919 return item1.
path().length() > item2.
path().length();
923 #ifdef MAX_COOKIE_LIMIT
928 for(
KHttpCookiePtr cookie = cookieList->first(); cookie; cookie = cookieList->next())
935 lastCookie = cookieList->first();
936 cookieList->removeRef(lastCookie);
951 QStringListIterator it (domains);
954 const QString& key = it.next();
993 #ifdef MAX_COOKIE_LIMIT
995 makeRoom(cookieList, cookie);
997 cookieList->push_back(cookie);
1000 qStableSort(cookieList->begin(), cookieList->end(),
compareCookies);
1021 if (!cookie.
domain().isEmpty()) {
1022 if (!domains.contains(cookie.
domain()) &&
1032 QStringListIterator it (domains);
1034 const QString& domain = it.next();
1035 if (domain.startsWith(
QL1C(
'.')) || cookie.
host() == domain) {
1075 if (cookieList->
getAdvice() != _advice) {
1081 if ((cookieList->isEmpty()) && (_advice ==
KCookieDunno)) {
1138 if (_domain.isEmpty())
1158 cookieList->erase(cookieIterator);
1160 if ((cookieList->isEmpty()) &&
1173 if (!cookieList || cookieList->isEmpty())
return;
1175 cookieList->clear();
1213 QMutableListIterator<KHttpCookie> cookieIterator(*cookieList);
1214 while (cookieIterator.hasNext()) {
1223 if (ids.contains(windowId)) {
1224 if (ids.count() > 1)
1225 kDebug(7104) <<
"removing window id" << windowId <<
"from session cookie";
1227 kDebug(7104) <<
"deleting session cookie";
1230 if (!ids.removeAll(windowId) || !ids.isEmpty()) {
1233 cookieIterator.remove();
1242 if (ports.isEmpty())
1243 return cookie->
host();
1246 Q_FOREACH(
int port, ports)
1247 portList << QString::number(port);
1249 return (cookie->
host() +
QL1C(
':') + portList.join(
QL1S(
",")));
1260 if (!cookieFile.
open())
1262 cookieFile.setPermissions(QFile::ReadUser|QFile::WriteUser);
1264 QTextStream ts(&cookieFile);
1266 ts <<
"# KDE Cookie File v2\n#\n";
1269 s.sprintf(
"%-20s %-20s %-12s %-10s %-4s %-20s %-4s %s\n",
1270 "# Host",
"Domain",
"Path",
"Exp.date",
"Prot",
1271 "Name",
"Sec",
"Value");
1272 ts << s.toLatin1().constData();
1275 while (it.hasNext())
1277 const QString& domain = it.next();
1278 bool domainPrinted =
false;
1284 QMutableListIterator<KHttpCookie> cookieIterator(*cookieList);
1285 while (cookieIterator.hasNext()) {
1286 const KHttpCookie& cookie = cookieIterator.next();
1289 cookieIterator.remove();
1294 if (!domainPrinted) {
1295 domainPrinted =
true;
1296 ts <<
'[' << domain.toLocal8Bit().data() <<
"]\n";
1304 s.sprintf(
"%-20s %-20s %-12s %10lld %3d %-20s %-4i %s\n",
1305 host.toLatin1().constData(), domain.toLatin1().constData(),
1306 path.toLatin1().constData(), cookie.
expireDate(),
1308 cookie.
name().isEmpty() ? cookie.
value().toLatin1().constData() : cookie.
name().toLatin1().constData(),
1311 cookie.
value().toLatin1().constData());
1312 ts << s.toLatin1().constData();
1320 static const char *
parseField(
char* &buffer,
bool keepQuotes=
false)
1323 if (!keepQuotes && (*buffer ==
'\"'))
1328 while((*buffer !=
'\"') && (*buffer))
1335 while((*buffer !=
' ') && (*buffer !=
'\t') && (*buffer !=
'\n') && (*buffer))
1344 while((*buffer ==
' ') || (*buffer ==
'\t') || (*buffer ==
'\n'))
1356 const int index = str.indexOf(
QL1C(
':'));
1360 const QString host = str.left(index);
1364 Q_FOREACH(
const QString& portStr, portList) {
1365 const int portNum = portStr.toInt(&ok);
1367 ports->append(portNum);
1380 QFile cookieFile (_filename);
1382 if (!cookieFile.open(QIODevice::ReadOnly))
1386 bool success =
false;
1392 if (qstrcmp(buffer,
"# KDE Cookie File\n") == 0)
1396 else if(qstrcmp(buffer,
"# KDE Cookie File v") > 0)
1399 const int verNum = QByteArray(buffer+19, len-19).trimmed().toInt(&ok);
1415 char *line = buffer;
1417 if ((line[0] ==
'#') || (line[0] ==
'['))
1422 if (host.isEmpty() && domain.isEmpty())
1426 if (expStr.isEmpty())
continue;
1427 const qint64 expDate = expStr.toLongLong();
1429 if (verStr.isEmpty())
continue;
1430 int protVer = verStr.toInt();
1432 bool keepQuotes =
false;
1433 bool secure =
false;
1434 bool httpOnly =
false;
1435 bool explicitPath =
false;
1436 const char *value = 0;
1437 if ((version == 2) || (protVer >= 200))
1444 explicitPath = i & 4;
1447 line[strlen(line)-1] =
'\0';
1458 secure = QByteArray(
parseField(line)).toShort();
1462 if (!value || expDate == 0 || expDate < currentTime)
1465 KHttpCookie cookie(host, domain, path, name, value, expDate,
1466 protVer, secure, httpOnly, explicitPath);
1495 while (it.hasNext())
1497 const QString& domain = it.next();
1502 domainSettings.append(value);
1505 policyGroup.
writeEntry(
"CookieDomainAdvice", domainSettings);
1537 for (QStringList::ConstIterator it = domainSettings.constBegin(), itEnd = domainSettings.constEnd();
1541 const int sepPos = value.lastIndexOf(
QL1C(
':'));
1545 const QString domain(value.left(sepPos));
1553 dbg.nospace() << cookie.
cookieStr(
false);