2008-10-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Treat Cookie object as session cookie if expiry = 0 is given.
	With this change, you can specify session scoped cookies in an
	external file setting expiry value to 0.
	* src/Cookie.cc
	* src/Cookie.h
	* test/CookieParserTest.cc
	* test/CookieStorageTest.cc
	* test/CookieTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2008-10-26 14:22:58 +00:00
parent 8a920ba5e3
commit 915aa676f8
6 changed files with 66 additions and 23 deletions

View File

@ -1,3 +1,14 @@
2008-10-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Treat Cookie object as session cookie if expiry = 0 is given.
With this change, you can specify session scoped cookies in an external
file setting expiry value to 0.
* src/Cookie.cc
* src/Cookie.h
* test/CookieParserTest.cc
* test/CookieStorageTest.cc
* test/CookieTest.cc
2008-10-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Changed signature of DHTMessageFactory::createResponseMessage().

View File

@ -33,10 +33,12 @@
*/
/* copyright --> */
#include "Cookie.h"
#include <algorithm>
#include "Util.h"
#include "A2STR.h"
#include "TimeA2.h"
#include <algorithm>
namespace aria2 {
@ -51,8 +53,7 @@ Cookie::Cookie(const std::string& name,
_expiry(expiry),
_path(path),
_domain(Util::toLower(domain)),
_secure(secure),
_onetime(false) {}
_secure(secure) {}
Cookie::Cookie(const std::string& name,
const std::string& value,
@ -64,10 +65,9 @@ Cookie::Cookie(const std::string& name,
_expiry(0),
_path(path),
_domain(Util::toLower(domain)),
_secure(secure),
_onetime(true) {}
_secure(secure) {}
Cookie::Cookie():_expiry(0), _secure(false), _onetime(true) {}
Cookie::Cookie():_expiry(0), _secure(false) {}
Cookie::~Cookie() {}
@ -115,7 +115,7 @@ bool Cookie::match(const std::string& requestHost,
if((secure || (!_secure && !secure)) &&
domainMatch(lowerRequestHost, _domain) &&
pathInclude(requestPath, _path) &&
(_onetime || (date < _expiry))) {
(isSessionCookie() || (date < _expiry))) {
return true;
} else {
return false;
@ -172,7 +172,7 @@ bool Cookie::operator==(const Cookie& cookie) const
bool Cookie::isExpired() const
{
return !_onetime && Time().getTime() >= _expiry;
return !_expiry == 0 && Time().getTime() >= _expiry;
}
const std::string& Cookie::getName() const
@ -205,9 +205,9 @@ bool Cookie::isSecureCookie() const
return _secure;
}
bool Cookie::isOnetimeCookie() const
bool Cookie::isSessionCookie() const
{
return _onetime;
return _expiry == 0;
}
} // namespace aria2

View File

@ -36,10 +36,12 @@
#define _D_COOKIE_H_
#include "common.h"
#include "a2time.h"
#include <string>
#include <deque>
#include "a2time.h"
namespace aria2 {
class Cookie {
@ -50,10 +52,10 @@ private:
std::string _path;
std::string _domain;
bool _secure;
// If true, this cookie will expire when aria2 exits.
bool _onetime;
public:
/*
* If expires = 0 is given, then the cookie becomes session cookie.
*/
Cookie(const std::string& name,
const std::string& value,
time_t expires,
@ -61,6 +63,10 @@ public:
const std::string& domain,
bool secure);
/*
* Creates session cookie. This is equivalent to
* Cookie(name, value, 0, path, domain, secure);
*/
Cookie(const std::string& name,
const std::string& value,
const std::string& path,
@ -97,7 +103,7 @@ public:
bool isSecureCookie() const;
bool isOnetimeCookie() const;
bool isSessionCookie() const;
};
typedef std::deque<Cookie> Cookies;

View File

@ -35,7 +35,7 @@ void CookieParserTest::testParse()
CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c.getDomain());
CPPUNIT_ASSERT_EQUAL(true, c.isSecureCookie());
CPPUNIT_ASSERT_EQUAL(false, c.isOnetimeCookie());
CPPUNIT_ASSERT_EQUAL(false, c.isSessionCookie());
std::string str2 = "JSESSIONID=123456789";
c = CookieParser().parse(str2, "default.domain", "/default/path");
@ -46,7 +46,7 @@ void CookieParserTest::testParse()
CPPUNIT_ASSERT_EQUAL(std::string("default.domain"), c.getDomain());
CPPUNIT_ASSERT_EQUAL(std::string("/default/path"), c.getPath());
CPPUNIT_ASSERT_EQUAL(false, c.isSecureCookie());
CPPUNIT_ASSERT_EQUAL(true, c.isOnetimeCookie());
CPPUNIT_ASSERT_EQUAL(true, c.isSessionCookie());
std::string str3 = "";
c = CookieParser().parse(str3);
@ -55,7 +55,7 @@ void CookieParserTest::testParse()
std::string str4 = "UID=300; expires=Wed, 01-Jan-1890 00:00:00 GMT;";
c = CookieParser().parse(str4, "localhost", "/");
CPPUNIT_ASSERT(c.good());
CPPUNIT_ASSERT(c.isOnetimeCookie());
CPPUNIT_ASSERT(c.isSessionCookie());
std::string str5 = "k=v; expires=Sun, 10-Jun-07 11:00:00 GMT";
c = CookieParser().parse(str5);
@ -66,7 +66,7 @@ void CookieParserTest::testParse()
CPPUNIT_ASSERT_EQUAL(std::string(""), c.getDomain());
CPPUNIT_ASSERT_EQUAL(std::string(""), c.getPath());
CPPUNIT_ASSERT(!c.isSecureCookie());
CPPUNIT_ASSERT(!c.isOnetimeCookie());
CPPUNIT_ASSERT(!c.isSessionCookie());
}
void CookieParserTest::testParse_file()

View File

@ -56,7 +56,7 @@ void CookieStorageTest::testStore()
CPPUNIT_ASSERT(std::find(st.begin(), st.end(), updateGoodCookie) != st.end());
CPPUNIT_ASSERT(std::find(st.begin(), st.end(), anotherCookie) != st.end());
Cookie expireGoodCookie("k", "v3", 0, "/", "localhost", false);
Cookie expireGoodCookie("k", "v3", 1, "/", "localhost", false);
CPPUNIT_ASSERT(!st.store(expireGoodCookie));
CPPUNIT_ASSERT_EQUAL((size_t)1, st.size());
CPPUNIT_ASSERT(std::find(st.begin(), st.end(), anotherCookie) != st.end());
@ -70,6 +70,18 @@ void CookieStorageTest::testStore()
CPPUNIT_ASSERT(st.store(fromNumericHost));
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
CPPUNIT_ASSERT(std::find(st.begin(), st.end(), fromNumericHost) != st.end());
Cookie sessionScopedGoodCookie("k", "v3", 0, "/", "localhost", false);
CPPUNIT_ASSERT(st.store(sessionScopedGoodCookie));
CPPUNIT_ASSERT_EQUAL((size_t)3, st.size());
CPPUNIT_ASSERT(std::find(st.begin(), st.end(),
sessionScopedGoodCookie) != st.end());
Cookie sessionScopedGoodCookie2("k2", "v3", "/", "localhost", false);
CPPUNIT_ASSERT(st.store(sessionScopedGoodCookie2));
CPPUNIT_ASSERT_EQUAL((size_t)4, st.size());
CPPUNIT_ASSERT(std::find(st.begin(), st.end(),
sessionScopedGoodCookie2) != st.end());
}
void CookieStorageTest::testParseAndStore()
@ -196,22 +208,34 @@ void CookieStorageTest::testLoad_sqlite3()
CookieStorage st;
#ifdef HAVE_SQLITE3
st.load("cookies.sqlite");
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
Cookie c = *st.begin();
CPPUNIT_ASSERT_EQUAL((size_t)3, st.size());
std::deque<Cookie>::const_iterator i = st.begin();
Cookie c = *i++;
CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.getName());
CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
CPPUNIT_ASSERT_EQUAL((time_t)2147483647, c.getExpiry());
CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c.getDomain());
CPPUNIT_ASSERT(c.isSecureCookie());
CPPUNIT_ASSERT(!c.isSessionCookie());
c = *(st.begin()+1);
c = *i++;
CPPUNIT_ASSERT_EQUAL(std::string("uid"), c.getName());
CPPUNIT_ASSERT_EQUAL(std::string(""), c.getValue());
CPPUNIT_ASSERT_EQUAL((time_t)0, c.getExpiry());
CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), c.getPath());
CPPUNIT_ASSERT_EQUAL(std::string("null_value"), c.getDomain());
CPPUNIT_ASSERT(!c.isSecureCookie());
CPPUNIT_ASSERT(c.isSessionCookie());
c = *i++;
CPPUNIT_ASSERT_EQUAL(std::string("foo"), c.getName());
CPPUNIT_ASSERT_EQUAL(std::string("bar"), c.getValue());
CPPUNIT_ASSERT_EQUAL((time_t)2147483647, c.getExpiry());
CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), c.getPath());
CPPUNIT_ASSERT_EQUAL(std::string("overflow_time_t"), c.getDomain());
CPPUNIT_ASSERT(!c.isSecureCookie());
CPPUNIT_ASSERT(!c.isSessionCookie());
#else // !HAVE_SQLITE3
try {

View File

@ -146,6 +146,8 @@ void CookieTest::testIsExpired()
CPPUNIT_ASSERT(!validCookie.isExpired());
Cookie sessionCookie("k", "v", "/", "localhost", false);
CPPUNIT_ASSERT(!sessionCookie.isExpired());
Cookie sessionCookie2("k", "v", 0, "/", "localhost", false);
CPPUNIT_ASSERT(!sessionCookie2.isExpired());
}
} // namespace aria2