From 6ac7ba848b54e8dc237b52700a9ac50a5e8a63f5 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Fri, 6 Jun 2014 16:48:15 +0200 Subject: [PATCH] Sort cookies by path length (longest first) * This makes sure the most specific cookies are sent first, matching what other browsers do. --- src/kits/network/libnetapi/NetworkCookieJar.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/kits/network/libnetapi/NetworkCookieJar.cpp b/src/kits/network/libnetapi/NetworkCookieJar.cpp index 919a526c37..ba4949030b 100644 --- a/src/kits/network/libnetapi/NetworkCookieJar.cpp +++ b/src/kits/network/libnetapi/NetworkCookieJar.cpp @@ -160,7 +160,16 @@ BNetworkCookieJar::AddCookie(BNetworkCookie* cookie) delete cookie; } else { TRACE("Add cookie: %s\n", cookie->RawCookie(true).String()); - list->AddItem(cookie); + // Keep the list sorted by path length (longest first). This makes sure + // that cookies for most specific paths are returned first when + // iterating the cookie jar. + int32 i; + for (i = 0; i < list->CountItems(); i++) { + BNetworkCookie* current = list->ItemAt(i); + if (current->Path().Length() < cookie->Path().Length()) + break; + } + list->AddItem(cookie, i); } return B_OK;