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

Seprate the implementation to load old mozilla format of cookie 
to
	NsCookieParser class.
	* src/CookieBoxFactory.cc
	* src/CookieBoxFactory.h
	* src/NsCookieParser.cc
	* src/NsCookieParser.h
	* test/NsCookieParserTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2008-08-17 12:58:56 +00:00
parent aabb6bb2d9
commit adfcf57e32
10 changed files with 287 additions and 85 deletions

View File

@ -1,3 +1,13 @@
2008-08-17 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Seprate the implementation to load old mozilla format of cookie to
NsCookieParser class.
* src/CookieBoxFactory.cc
* src/CookieBoxFactory.h
* src/NsCookieParser.cc
* src/NsCookieParser.h
* test/NsCookieParserTest.cc
2008-08-17 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Made parse() const

View File

@ -37,9 +37,9 @@
#include "CookieBox.h"
#include "Util.h"
#include "RecoverableException.h"
#include "A2STR.h"
#include "LogFactory.h"
#include "Logger.h"
#include "NsCookieParser.h"
#ifdef HAVE_SQLITE3
# include "Sqlite3MozCookieParser.h"
#endif // HAVE_SQLITE3
@ -48,8 +48,6 @@
namespace aria2 {
const std::string CookieBoxFactory::C_TRUE("TRUE");
CookieBoxFactory::CookieBoxFactory():_logger(LogFactory::getInstance()) {}
CookieBoxFactory::~CookieBoxFactory() {}
@ -63,67 +61,31 @@ CookieBoxHandle CookieBoxFactory::createNewInstance()
void CookieBoxFactory::loadDefaultCookie(const std::string& filename)
{
std::ifstream s(filename.c_str());
char header[16]; // "SQLite format 3" plus \0
s.get(header, sizeof(header));
if(s.bad()) {
_logger->error("Failed to read header of cookie file %s", filename.c_str());
return;
}
if(std::string(header) == "SQLite format 3") {
#ifdef HAVE_SQLITE3
try {
defaultCookies = Sqlite3MozCookieParser().parse(filename);
} catch(RecoverableException& e) {
_logger->error("Failed to load cookies from %s, cause: %s",
filename.c_str(), e.what());
{
std::ifstream s(filename.c_str());
s.get(header, sizeof(header));
if(s.bad()) {
_logger->error("Failed to read header of cookie file %s",
filename.c_str());
return;
}
}
try {
if(std::string(header) == "SQLite format 3") {
#ifdef HAVE_SQLITE3
defaultCookies = Sqlite3MozCookieParser().parse(filename);
#else // !HAVE_SQLITE3
_logger->notice("Cannot read SQLite3 database because SQLite3 support is"
" disabled by configuration.");
#endif // !HAVE_SQLITE3
} else {
s.seekg(0);
std::string line;
while(getline(s, line)) {
if(Util::startsWith(line, A2STR::SHARP_C)) {
continue;
}
try {
Cookie c = parseNsCookie(line);
if(c.good()) {
defaultCookies.push_back(c);
}
} catch(RecoverableException& e) {
// ignore malformed cookie entry
// TODO better to log it
}
} else {
defaultCookies = NsCookieParser().parse(filename);
}
} catch(RecoverableException& e) {
_logger->error("Failed to load cookies from %s, cause: %s",
filename.c_str(), e.what());
}
}
Cookie CookieBoxFactory::parseNsCookie(const std::string& nsCookieStr) const
{
std::deque<std::string> vs;
Util::slice(vs, nsCookieStr, '\t', true);
Cookie c;
if(vs.size() < 6 ) {
return c;
}
c.domain = vs[0];
c.path = vs[2];
c.secure = vs[3] == C_TRUE ? true : false;
int64_t expireDate = Util::parseLLInt(vs[4]);
// TODO assuming time_t is int32_t...
if(expireDate > INT32_MAX) {
expireDate = INT32_MAX;
}
c.expires = expireDate;
c.name = vs[5];
if(vs.size() >= 7) {
c.value = vs[6];
}
return c;
}
} // namespace aria2

View File

@ -61,15 +61,10 @@ public:
void loadDefaultCookie(const std::string& filename);
Cookie parseNsCookie(const std::string& nsCookieStr) const;
const Cookies& getDefaultCookies() const
{
return defaultCookies;
}
private:
static const std::string C_TRUE;
};
typedef SharedHandle<CookieBoxFactory> CookieBoxFactoryHandle;

View File

@ -194,7 +194,8 @@ SRCS = Socket.h\
ServerStatMan.cc ServerStatMan.h\
URISelector.h\
InOrderURISelector.cc InOrderURISelector.h\
ServerStatURISelector.cc ServerStatURISelector.h
ServerStatURISelector.cc ServerStatURISelector.h\
NsCookieParser.cc NsCookieParser.h
if HAVE_LIBZ
SRCS += GZipDecoder.cc GZipDecoder.h

View File

@ -414,10 +414,10 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
Signature.h ServerStat.cc ServerStat.h ServerStatMan.cc \
ServerStatMan.h URISelector.h InOrderURISelector.cc \
InOrderURISelector.h ServerStatURISelector.cc \
ServerStatURISelector.h GZipDecoder.cc GZipDecoder.h \
Sqlite3MozCookieParser.cc Sqlite3MozCookieParser.h \
AsyncNameResolver.cc AsyncNameResolver.h \
IteratableChunkChecksumValidator.cc \
ServerStatURISelector.h NsCookieParser.cc NsCookieParser.h \
GZipDecoder.cc GZipDecoder.h Sqlite3MozCookieParser.cc \
Sqlite3MozCookieParser.h AsyncNameResolver.cc \
AsyncNameResolver.h IteratableChunkChecksumValidator.cc \
IteratableChunkChecksumValidator.h \
IteratableChecksumValidator.cc IteratableChecksumValidator.h \
CheckIntegrityCommand.cc CheckIntegrityCommand.h \
@ -808,13 +808,13 @@ am__objects_18 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
RarestPieceSelector.$(OBJEXT) ChunkedDecoder.$(OBJEXT) \
Signature.$(OBJEXT) ServerStat.$(OBJEXT) \
ServerStatMan.$(OBJEXT) InOrderURISelector.$(OBJEXT) \
ServerStatURISelector.$(OBJEXT) $(am__objects_1) \
$(am__objects_2) $(am__objects_3) $(am__objects_4) \
$(am__objects_5) $(am__objects_6) $(am__objects_7) \
$(am__objects_8) $(am__objects_9) $(am__objects_10) \
$(am__objects_11) $(am__objects_12) $(am__objects_13) \
$(am__objects_14) $(am__objects_15) $(am__objects_16) \
$(am__objects_17)
ServerStatURISelector.$(OBJEXT) NsCookieParser.$(OBJEXT) \
$(am__objects_1) $(am__objects_2) $(am__objects_3) \
$(am__objects_4) $(am__objects_5) $(am__objects_6) \
$(am__objects_7) $(am__objects_8) $(am__objects_9) \
$(am__objects_10) $(am__objects_11) $(am__objects_12) \
$(am__objects_13) $(am__objects_14) $(am__objects_15) \
$(am__objects_16) $(am__objects_17)
am_libaria2c_a_OBJECTS = $(am__objects_18)
libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS)
am__installdirs = "$(DESTDIR)$(bindir)"
@ -1138,12 +1138,13 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \
Signature.h ServerStat.cc ServerStat.h ServerStatMan.cc \
ServerStatMan.h URISelector.h InOrderURISelector.cc \
InOrderURISelector.h ServerStatURISelector.cc \
ServerStatURISelector.h $(am__append_1) $(am__append_2) \
$(am__append_3) $(am__append_4) $(am__append_5) \
$(am__append_6) $(am__append_7) $(am__append_8) \
$(am__append_9) $(am__append_10) $(am__append_11) \
$(am__append_12) $(am__append_13) $(am__append_14) \
$(am__append_15) $(am__append_16) $(am__append_17)
ServerStatURISelector.h NsCookieParser.cc NsCookieParser.h \
$(am__append_1) $(am__append_2) $(am__append_3) \
$(am__append_4) $(am__append_5) $(am__append_6) \
$(am__append_7) $(am__append_8) $(am__append_9) \
$(am__append_10) $(am__append_11) $(am__append_12) \
$(am__append_13) $(am__append_14) $(am__append_15) \
$(am__append_16) $(am__append_17)
noinst_LIBRARIES = libaria2c.a
libaria2c_a_SOURCES = $(SRCS)
aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\
@ -1443,6 +1444,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/NameResolver.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Netrc.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/NetrcAuthResolver.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/NsCookieParser.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/OSMetalinkParserState.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Option.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/OptionHandlerFactory.Po@am__quote@

100
src/NsCookieParser.cc Normal file
View File

@ -0,0 +1,100 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "NsCookieParser.h"
#include "Util.h"
#include "A2STR.h"
#include "RecoverableException.h"
#include "StringFormat.h"
#include <fstream>
namespace aria2 {
NsCookieParser::NsCookieParser() {}
NsCookieParser::~NsCookieParser() {}
static const std::string C_TRUE("TRUE");
static Cookie parseNsCookie(const std::string& nsCookieStr)
{
std::deque<std::string> vs;
Util::slice(vs, nsCookieStr, '\t', true);
if(vs.size() < 6 ) {
return Cookie();
}
int64_t expireDate = Util::parseLLInt(vs[4]);
// TODO assuming time_t is int32_t...
if(expireDate > INT32_MAX) {
expireDate = INT32_MAX;
}
Cookie c(vs[5], // name
vs.size() >= 7? vs[6]:A2STR::NIL, // value
expireDate, // expires
vs[2], // path
vs[0], // domain
vs[3] == C_TRUE ? true : false);
return c;
}
std::deque<Cookie> NsCookieParser::parse(const std::string& filename)
{
std::ifstream s(filename.c_str());
if(!s) {
throw RecoverableException
(StringFormat("Failed to open file %s", filename.c_str()).str());
}
std::string line;
std::deque<Cookie> cookies;
while(getline(s, line)) {
if(Util::startsWith(line, A2STR::SHARP_C)) {
continue;
}
try {
Cookie c = parseNsCookie(line);
if(c.good()) {
cookies.push_back(c);
}
} catch(RecoverableException& e) {
// ignore malformed cookie entry
// TODO better to log it
}
}
return cookies;
}
} // namespace aria2

56
src/NsCookieParser.h Normal file
View File

@ -0,0 +1,56 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#ifndef _D_NS_COOKIE_PARSER_H_
#define _D_NS_COOKIE_PARSER_H_
#include "common.h"
#include "Cookie.h"
#include <string>
#include <deque>
namespace aria2 {
class NsCookieParser {
public:
NsCookieParser();
~NsCookieParser();
std::deque<Cookie> parse(const std::string& filename);
};
} // namespace aria2
#endif // _D_NS_COOKIE_PARSER_H_

View File

@ -57,7 +57,8 @@ aria2c_SOURCES = AllTest.cc\
ServerStatManTest.cc\
ServerStatURISelectorTest.cc\
InOrderURISelectorTest.cc\
ServerStatTest.cc
ServerStatTest.cc\
NsCookieParserTest.cc
if HAVE_LIBZ
aria2c_SOURCES += GZipDecoderTest.cc

View File

@ -192,7 +192,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc SocketCoreTest.cc \
DownloadHandlerFactoryTest.cc ChunkedDecoderTest.cc \
SignatureTest.cc ServerStatManTest.cc \
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
ServerStatTest.cc GZipDecoderTest.cc \
ServerStatTest.cc NsCookieParserTest.cc GZipDecoderTest.cc \
Sqlite3MozCookieParserTest.cc MessageDigestHelperTest.cc \
IteratableChunkChecksumValidatorTest.cc \
IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
@ -363,8 +363,8 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) SocketCoreTest.$(OBJEXT) \
ServerStatManTest.$(OBJEXT) \
ServerStatURISelectorTest.$(OBJEXT) \
InOrderURISelectorTest.$(OBJEXT) ServerStatTest.$(OBJEXT) \
$(am__objects_1) $(am__objects_2) $(am__objects_3) \
$(am__objects_4) $(am__objects_5)
NsCookieParserTest.$(OBJEXT) $(am__objects_1) $(am__objects_2) \
$(am__objects_3) $(am__objects_4) $(am__objects_5)
aria2c_OBJECTS = $(am_aria2c_OBJECTS)
am__DEPENDENCIES_1 =
aria2c_DEPENDENCIES = ../src/libaria2c.a $(am__DEPENDENCIES_1)
@ -583,8 +583,9 @@ aria2c_SOURCES = AllTest.cc SocketCoreTest.cc array_funTest.cc \
DownloadHandlerFactoryTest.cc ChunkedDecoderTest.cc \
SignatureTest.cc ServerStatManTest.cc \
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
ServerStatTest.cc $(am__append_1) $(am__append_2) \
$(am__append_3) $(am__append_4) $(am__append_5)
ServerStatTest.cc NsCookieParserTest.cc $(am__append_1) \
$(am__append_2) $(am__append_3) $(am__append_4) \
$(am__append_5)
#aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64
#aria2c_LDFLAGS = ${CPPUNIT_LIBS}
@ -773,6 +774,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MultiFileAllocationIteratorTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/NetrcAuthResolverTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/NetrcTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/NsCookieParserTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/OptionHandlerTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/OptionTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PStringBuildVisitorTest.Po@am__quote@

View File

@ -0,0 +1,73 @@
#include "NsCookieParser.h"
#include "RecoverableException.h"
#include "Util.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {
class NsCookieParserTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(NsCookieParserTest);
CPPUNIT_TEST(testParse);
CPPUNIT_TEST(testParse_fileNotFound);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testParse();
void testParse_fileNotFound();
};
CPPUNIT_TEST_SUITE_REGISTRATION(NsCookieParserTest);
void NsCookieParserTest::testParse()
{
NsCookieParser parser;
std::deque<Cookie> cookies = parser.parse("nscookietest.txt");
CPPUNIT_ASSERT_EQUAL((size_t)4, cookies.size());
Cookie c = cookies[0];
CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.name);
CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.value);
CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c.expires);
CPPUNIT_ASSERT_EQUAL(std::string("/"), c.path);
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c.domain);
c = cookies[1];
CPPUNIT_ASSERT_EQUAL(std::string("user"), c.name);
CPPUNIT_ASSERT_EQUAL(std::string("me"), c.value);
CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c.expires);
CPPUNIT_ASSERT_EQUAL(std::string("/"), c.path);
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c.domain);
c = cookies[2];
CPPUNIT_ASSERT_EQUAL(std::string("passwd"), c.name);
CPPUNIT_ASSERT_EQUAL(std::string("secret"), c.value);
CPPUNIT_ASSERT_EQUAL((time_t)2147483647, c.expires);
CPPUNIT_ASSERT_EQUAL(std::string("/cgi-bin"), c.path);
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c.domain);
c = cookies[3];
CPPUNIT_ASSERT_EQUAL(std::string("novalue"), c.name);
CPPUNIT_ASSERT_EQUAL(std::string(""), c.value);
CPPUNIT_ASSERT_EQUAL((time_t)2147483647, c.expires);
CPPUNIT_ASSERT_EQUAL(std::string("/"), c.path);
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c.domain);
}
void NsCookieParserTest::testParse_fileNotFound()
{
NsCookieParser parser;
try {
parser.parse("fileNotFound");
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// SUCCESS
}
}
} // namespace aria2