status_t
BNetworkCookieJar::AddCookie(BNetworkCookie* cookie)
{
    if (cookie == NULL)
        return B_BAD_VALUE;

    HashString key(cookie->Domain());

    BNetworkCookieList* list = fCookieHashMap->fHashMap.Get(key);
    if (list == NULL) {
        list = new(std::nothrow) BNetworkCookieList();
        if (list == NULL || fCookieHashMap->fHashMap.Put(key, list) != B_OK)
            return B_NO_MEMORY;
    }

    for (int32 i = 0; i < list->CountItems(); i++) {
        BNetworkCookie* c
            = reinterpret_cast<BNetworkCookie*>(list->ItemAt(i));

        if (c->Name() == cookie->Name() && c->Path() == cookie->Path()) {
            list->RemoveItem(i);
            break;
        }
    }

    if (cookie->ShouldDeleteNow())
        delete cookie;
    else
        list->AddItem(cookie);

    return B_OK;
}
void
BNetworkCookieJar::_DoFlatten() const
{
    fFlattened.Truncate(0);

    BNetworkCookie* cookiePtr;
    for (Iterator it = GetIterator(); (cookiePtr = it.Next()) != NULL;) {
        fFlattened 	<< cookiePtr->Domain() << '\t' << "TRUE" << '\t'
                    << cookiePtr->Path() << '\t'
                    << (cookiePtr->Secure()?"TRUE":"FALSE") << '\t'
                    << (int32)cookiePtr->ExpirationDate() << '\t'
                    << cookiePtr->Name() << '\t' << cookiePtr->Value() << '\n';
    }
}
Пример #3
0
void explodeImplodeTest()
{
	uint8 testIndex;
	BNetworkCookie cookie;

	for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++)
	{
		cookie.ParseCookieString(kTestExplode[testIndex].cookieString);

		ASSERT(testIndex, BString(kTestExplode[testIndex].expected.name) == BString(cookie.Name()));
		ASSERT(testIndex, BString(kTestExplode[testIndex].expected.value) == BString(cookie.Value()));
		ASSERT(testIndex, BString(kTestExplode[testIndex].expected.domain) == BString(cookie.Domain()));
		ASSERT(testIndex, BString(kTestExplode[testIndex].expected.path) == BString(cookie.Path()));
		ASSERT(testIndex, kTestExplode[testIndex].expected.secure == cookie.Secure());
		ASSERT(testIndex, kTestExplode[testIndex].expected.discard == cookie.Discard());
		ASSERT(testIndex, kTestExplode[testIndex].expected.session == cookie.IsSessionCookie());

		if (!cookie.IsSessionCookie())
			ASSERT(testIndex, kTestExplode[testIndex].expected.maxAge == cookie.MaxAge());
	}
}
Пример #4
0
	CookieRow(BColumnListView* list, const BNetworkCookie& cookie)
		:
		BRow(),
		fCookie(cookie)
	{
		list->AddRow(this);
		SetField(new BStringField(cookie.Name().String()), 0);
		SetField(new BStringField(cookie.Path().String()), 1);
		time_t expiration = cookie.ExpirationDate();
		SetField(new BDateField(&expiration), 2);
		SetField(new BStringField(cookie.Value().String()), 3);

		BString flags;
		if (cookie.Secure())
			flags = "https ";
		if (cookie.HttpOnly())
			flags = "http ";

		if (cookie.IsHostOnly())
			flags += "hostOnly";
		SetField(new BStringField(flags.String()), 4);
	}
Пример #5
0
void
CookieTest::ExplodeTest()
{
	struct Test {
		const char* cookieString;
		const char*	url;
		struct
		{
			bool		valid;
			const char* name;
			const char* value;
			const char* domain;
			const char* path;
			bool 		secure;
			bool 		httponly;
			bool		session;
			BDateTime	expire;
		} expected;
	};

	Test tests[] = {
	//     Cookie string      URL
	//     ------------- -------------
	//		   Valid     Name     Value	       Domain         Path      Secure  HttpOnly Session  Expiration
	//       --------- -------- --------- ----------------- ---------  -------- -------- -------  ----------
		// Normal cookies
		{ "name=value", "http://www.example.com/path/path",
			{  true,    "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } },
		{ "name=value; domain=example.com; path=/; secure", "http://www.example.com/path/path",
			{  true,    "name",  "value",   "example.com",   "/"    ,   true,    false,   true,   BDateTime() } },
		{ "name=value; httponly; secure", "http://www.example.com/path/path",
			{  true,    "name",  "value", "www.example.com", "/path",   true,    true,    true,   BDateTime() } },
		{ "name=value; expires=Wed, 20-Feb-2013 20:00:00 UTC", "http://www.example.com/path/path",
			{  true,    "name",  "value", "www.example.com", "/path",   false,   false,   false,
				BDateTime(BDate(2013, 2, 20), BTime(20, 0, 0, 0)) } },
		// Valid cookie with bad form
		{ "name=  ;  domain   =example.com  ;path=/;  secure = yup  ; blahblah ;)", "http://www.example.com/path/path",
			{  true,    "name",  "",   "example.com",   "/"    ,   true,    false,   true,   BDateTime() } },
		// Invalid path
		{ "name=value; path=invalid", "http://www.example.com/path/path",
			{  false,    "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } },
		// Setting for other subdomain (invalid)
		{ "name=value; domain=subdomain.example.com", "http://www.example.com/path/path",
			{  false,   "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } },
		// Various invalid cookies
		{ "name", "http://www.example.com/path/path",
			{  false,   "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } },
		{ "; domain=example.com", "http://www.example.com/path/path",
			{  false,   "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } }
	};
	
	BNetworkCookie cookie;

	for (uint32 i = 0; i < (sizeof(tests) / sizeof(Test)); i++) {
		NextSubTest();

		BUrl url(tests[i].url);
		cookie.ParseCookieString(tests[i].cookieString, url);

		CPPUNIT_ASSERT(tests[i].expected.valid == cookie.IsValid());

		if (!tests[i].expected.valid)
			continue;

		CPPUNIT_ASSERT_EQUAL(BString(tests[i].expected.name), cookie.Name());
		CPPUNIT_ASSERT_EQUAL(BString(tests[i].expected.value), cookie.Value());
		CPPUNIT_ASSERT_EQUAL(BString(tests[i].expected.domain),
			cookie.Domain());
		CPPUNIT_ASSERT_EQUAL(BString(tests[i].expected.path), cookie.Path());
		CPPUNIT_ASSERT(tests[i].expected.secure == cookie.Secure());
		CPPUNIT_ASSERT(tests[i].expected.httponly == cookie.HttpOnly());
		CPPUNIT_ASSERT(tests[i].expected.session == cookie.IsSessionCookie());

		if (!cookie.IsSessionCookie())
			CPPUNIT_ASSERT_EQUAL(tests[i].expected.expire.Time_t(),
				cookie.ExpirationDate());
	}
}