Browse Source

2008-08-17 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	If a cookie whose expire date is later than 2038-01-19 03:14:07 
GMT is
	sent from server, its expire date is assumed to 2038-01-19 
03:14:07 GMT.
	If Util::httpGMT is failed, then Cookie::onetime is set to true.
	* src/Util.cc
	* src/Util.h
	* src/CookieParser.cc
	* test/UtilTest.cc
	* test/CookieParserTest.cc
Tatsuhiro Tsujikawa 17 years ago
parent
commit
0e6c0498a3
6 changed files with 55 additions and 3 deletions
  1. 11 0
      ChangeLog
  2. 9 2
      src/CookieParser.cc
  3. 9 1
      src/Util.cc
  4. 8 0
      src/Util.h
  5. 5 0
      test/CookieParserTest.cc
  6. 13 0
      test/UtilTest.cc

+ 11 - 0
ChangeLog

@@ -1,3 +1,14 @@
+2008-08-17  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	If a cookie whose expire date is later than 2038-01-19 03:14:07 GMT is
+	sent from server, its expire date is assumed to 2038-01-19 03:14:07 GMT.
+	If Util::httpGMT is failed, then Cookie::onetime is set to true.
+	* src/Util.cc
+	* src/Util.h
+	* src/CookieParser.cc
+	* test/UtilTest.cc
+	* test/CookieParserTest.cc
+	
 2008-08-17  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Seprate the implementation to load old mozilla format of cookie to

+ 9 - 2
src/CookieParser.cc

@@ -58,8 +58,15 @@ void CookieParser::setField(Cookie& cookie, const std::string& name, const std::
   } else if(name == C_PATH) {
     cookie.path = value;
   } else if(name == C_EXPIRES) {
-    cookie.expires = Util::httpGMT(value);
-    cookie.onetime = false;
+    time_t expires = Util::httpGMT(value);
+    if(expires == -1) {
+      // If parsing expire date is failed, it is assumed as a session scope
+      // cookie.
+      cookie.onetime = true;
+    } else {
+      cookie.expires = expires;
+      cookie.onetime = false;
+    }
   } else {
     cookie.name = name;
     cookie.value = value;

+ 9 - 1
src/Util.cc

@@ -695,8 +695,16 @@ time_t Util::httpGMT(const std::string& httpStdTime)
 {
   struct tm tm;
   memset(&tm, 0, sizeof(tm));
-  strptime(httpStdTime.c_str(), "%a, %Y-%m-%d %H:%M:%S GMT", &tm);
+  char* r = strptime(httpStdTime.c_str(), "%a, %Y-%m-%d %H:%M:%S GMT", &tm);
+  if(r != httpStdTime.c_str()+httpStdTime.size()) {
+    return -1;
+  }
   time_t thetime = timegm(&tm);
+  if(thetime == -1) {
+    if(tm.tm_year >= 2038-1900) {
+      thetime = INT32_MAX;
+    }
+  }
   return thetime;
 }
 

+ 8 - 0
src/Util.h

@@ -209,6 +209,14 @@ public:
 
   static std::string abbrevSize(int64_t size);
 
+  /**
+   * Parses given httpTimeFormat and returns seconds ellapsed since epoc.
+   * The available format is "%a, %Y-%m-%d %H:%M:%S GMT".
+   * If specified date is later than "Tue, 2038-01-19 3:14:7 GMT",
+   * this function returns INT32_MAX.
+   * This function also cannot handle prior 1900-1-1 0:0:0 GMT.
+   * If parse operation is failed, then return -1.
+   */
   static time_t httpGMT(const std::string& httpTimeFormat);
 
   static void toStream(std::ostream& os,

+ 5 - 0
test/CookieParserTest.cc

@@ -51,6 +51,11 @@ void CookieParserTest::testParse()
   std::string str3 = "";
   c = CookieParser().parse(str3);
   CPPUNIT_ASSERT(!c.good());
+
+  std::string str4 = "UID=300; expires=Wed, 1890-01-01 0:0:0 GMT;";
+  c = CookieParser().parse(str4, "localhost", "/");
+  CPPUNIT_ASSERT(c.good());
+  CPPUNIT_ASSERT(c.onetime);
 }
 
 void CookieParserTest::testParse_file()

+ 13 - 0
test/UtilTest.cc

@@ -45,6 +45,7 @@ class UtilTest:public CppUnit::TestFixture {
   CPPUNIT_TEST(testToString_binaryStream);
   CPPUNIT_TEST(testItos);
   CPPUNIT_TEST(testUitos);
+  CPPUNIT_TEST(testHttpGMT);
   CPPUNIT_TEST_SUITE_END();
 private:
 
@@ -83,6 +84,7 @@ public:
   void testToString_binaryStream();
   void testItos();
   void testUitos();
+  void testHttpGMT();
 };
 
 
@@ -668,4 +670,15 @@ void UtilTest::testUitos()
   }
 }
 
+void UtilTest::testHttpGMT()
+{
+  CPPUNIT_ASSERT_EQUAL((time_t)0, Util::httpGMT("Thu, 1970-01-01 0:0:0 GMT"));
+  CPPUNIT_ASSERT_EQUAL((time_t)2147483647,
+		       Util::httpGMT("Tue, 2038-01-19 3:14:7 GMT"));
+  CPPUNIT_ASSERT_EQUAL((time_t)2147483647,
+		       Util::httpGMT("Tue, 2038-01-19 3:14:8 GMT"));
+  CPPUNIT_ASSERT_EQUAL((time_t)-1,
+		       Util::httpGMT("Tue, 2008/10/10 23:33:33 UTC"));
+}
+
 } // namespace aria2