From f36e1414b745380a50d9d8db9c20dc8721636810 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 3 Dec 2013 16:42:54 +0100 Subject: [PATCH] Cookie Jar: allow setting cookies on "file" URLs. * These are shared with HTTP cookies set for localhost. We probably want to split them apart later on, so cookies should store and check the protocol, additionally to the path and domain. * Fixes #10195. --- src/kits/network/libnetapi/NetworkCookie.cpp | 20 +++++++++++++++++-- .../network/libnetapi/NetworkCookieJar.cpp | 8 ++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/kits/network/libnetapi/NetworkCookie.cpp b/src/kits/network/libnetapi/NetworkCookie.cpp index bb85614f2e..a9f3a333c5 100644 --- a/src/kits/network/libnetapi/NetworkCookie.cpp +++ b/src/kits/network/libnetapi/NetworkCookie.cpp @@ -38,6 +38,13 @@ BNetworkCookie::BNetworkCookie(const char* name, const char* value, fValue = value; SetDomain(url.Host()); + + if(url.Protocol() == "file" && url.Host().Length() == 0) + { + SetDomain("localhost"); + // make sure cookies set from a file:// URL are stored somewhere. + } + SetPath(_DefaultPathForUrl(url)); } @@ -93,6 +100,13 @@ BNetworkCookie::ParseCookieString(const BString& string, const BUrl& url) SetPath(_DefaultPathForUrl(url)); SetDomain(url.Host()); fHostOnly = true; + if(url.Protocol() == "file" && url.Host().Length() == 0) + { + fDomain = "localhost"; + // make sure cookies set from a file:// URL are stored somewhere. + // not going through SetDomain as it requires at least one '.' + // in the domain (to avoid setting cookies on TLDs). + } BString name; BString value; @@ -166,13 +180,12 @@ BNetworkCookie::ParseCookieString(const BString& string, const BUrl& url) } } - if (!IsValidForDomain(url.Host())) { + if (!IsValidForUrl(url)) { // Invalidate the cookie. _Reset(); return B_NOT_ALLOWED; } - return result; } @@ -423,6 +436,9 @@ BNetworkCookie::IsValidForUrl(const BUrl& url) const if (Secure() && url.Protocol() != "https") return false; + if (url.Protocol() == "file") + return Domain() == "localhost" && IsValidForPath(url.Path()); + return IsValidForDomain(url.Host()) && IsValidForPath(url.Path()); } diff --git a/src/kits/network/libnetapi/NetworkCookieJar.cpp b/src/kits/network/libnetapi/NetworkCookieJar.cpp index ae97f37dc0..d4a9fd31f3 100644 --- a/src/kits/network/libnetapi/NetworkCookieJar.cpp +++ b/src/kits/network/libnetapi/NetworkCookieJar.cpp @@ -591,8 +591,12 @@ BNetworkCookieJar::UrlIterator::UrlIterator(const BNetworkCookieJar* cookieJar, { BString domain = url.Host(); - if (!domain.Length()) - return; + if (!domain.Length()) { + if (url.Protocol() == "file") + domain = "localhost"; + else + return; + } fIterator = new(std::nothrow) PrivateIterator( fCookieJar->fCookieHashMap->fHashMap.GetIterator());