diff --git a/ChangeLog b/ChangeLog index 71def891..291dd5a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,33 @@ +2008-09-01 Tatsuhiro Tsujikawa + + Use CookieStorage class instead of CookieBox class. + * src/AbstractProxyRequestCommand.cc + * src/CookieStorage.cc + * src/DownloadEngine.cc + * src/DownloadEngine.h + * src/HttpConnection.cc + * src/HttpConnection.h + * src/HttpRequest.cc + * src/HttpRequest.h + * src/HttpRequestCommand.cc + * src/HttpResponse.cc + * src/HttpResponseCommand.cc + * src/HttpSkipResponseCommand.cc + * src/Makefile.am + * src/MultiUrlRequestInfo.cc + * src/Request.cc + * src/Request.h + * src/main.cc + * test/AllTest.cc + * test/CookieStorageTest.cc + * test/CookieTest.cc + * test/HttpRequestTest.cc + * test/HttpResponseTest.cc + * test/Makefile.am + * test/NsCookieParserTest.cc + * test/Sqlite3MozCookieParserTest.cc + * test/nscookietest.txt + 2008-08-28 Tatsuhiro Tsujikawa Updated diff --git a/src/AbstractProxyRequestCommand.cc b/src/AbstractProxyRequestCommand.cc index fcb03077..2499e2dc 100644 --- a/src/AbstractProxyRequestCommand.cc +++ b/src/AbstractProxyRequestCommand.cc @@ -42,6 +42,7 @@ #include "Option.h" #include "prefs.h" #include "Socket.h" +#include "CookieStorage.h" namespace aria2 { diff --git a/src/CookieStorage.cc b/src/CookieStorage.cc index 6f2ccaf5..31580a9b 100644 --- a/src/CookieStorage.cc +++ b/src/CookieStorage.cc @@ -86,9 +86,6 @@ bool CookieStorage::parseAndStore(const std::string& setCookieString, const std::string& requestHost, const std::string& requestPath) { - if(Util::isNumbersAndDotsNotation(requestHost)) { - return false; - } Cookie cookie = _parser.parse(setCookieString, requestHost, requestPath); if(cookie.validate(requestHost, requestPath)) { return store(cookie); diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 177347f4..f8cd1287 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -52,6 +52,7 @@ #include "a2functional.h" #include "DlAbortEx.h" #include "ServerStatMan.h" +#include "CookieStorage.h" #include #include #include @@ -393,7 +394,8 @@ void AsyncNameResolverEntry::process(fd_set* rfdsPtr, fd_set* wfdsPtr) DownloadEngine::DownloadEngine():logger(LogFactory::getInstance()), _haltRequested(false), - _noWait(false) + _noWait(false), + _cookieStorage(new CookieStorage()) { #ifdef HAVE_EPOLL @@ -881,6 +883,11 @@ void DownloadEngine::addRoutineCommand(Command* command) _routineCommands.push_back(command); } +SharedHandle DownloadEngine::getCookieStorage() const +{ + return _cookieStorage; +} + void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port, const SharedHandle& sock, time_t timeout) diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index 26700b74..61b64bfb 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -59,6 +59,7 @@ class FileAllocationMan; class StatCalc; class CheckIntegrityMan; class SocketCore; +class CookieStorage; class CommandEvent { @@ -277,6 +278,12 @@ private: // key = IP address:port, value = SocketPoolEntry std::multimap _socketPool; + bool _noWait; + + std::deque _routineCommands; + + SharedHandle _cookieStorage; + void shortSleep() const; /** @@ -287,12 +294,6 @@ private: void onEndOfRun(); void afterEachIteration(); - -private: - bool _noWait; - - std::deque _routineCommands; - public: std::deque commands; SharedHandle _requestGroupMan; @@ -371,6 +372,8 @@ public: SharedHandle popPooledSocket(const std::deque& ipaddrs, uint16_t port); + + SharedHandle getCookieStorage() const; }; typedef SharedHandle DownloadEngineHandle; diff --git a/src/HttpConnection.cc b/src/HttpConnection.cc index 3676f5a5..b37a6092 100644 --- a/src/HttpConnection.cc +++ b/src/HttpConnection.cc @@ -48,6 +48,7 @@ #include "Logger.h" #include "Socket.h" #include "Option.h" +#include "CookieStorage.h" #include namespace aria2 { @@ -169,4 +170,13 @@ bool HttpConnection::isIssued(const SegmentHandle& segment) const return false; } +SharedHandle HttpConnection::getFirstHttpRequest() const +{ + if(outstandingHttpRequests.empty()) { + return SharedHandle(); + } else { + return outstandingHttpRequests.front()->getHttpRequest(); + } +} + } // namespace aria2 diff --git a/src/HttpConnection.h b/src/HttpConnection.h index 3a6a21c3..02ac19b4 100644 --- a/src/HttpConnection.h +++ b/src/HttpConnection.h @@ -109,14 +109,7 @@ public: */ SharedHandle receiveResponse(); - SharedHandle getFirstHttpRequest() const - { - if(outstandingHttpRequests.size() > 0) { - return outstandingHttpRequests.front()->getHttpRequest(); - } else { - return SharedHandle(); - } - } + SharedHandle getFirstHttpRequest() const; bool isIssued(const SharedHandle& segment) const; }; diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index f0b4d89d..08235683 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -36,8 +36,7 @@ #include "Request.h" #include "Segment.h" #include "Range.h" -#include "Cookie.h" -#include "CookieBox.h" +#include "CookieStorage.h" #include "Option.h" #include "Util.h" #include "Base64.h" @@ -45,6 +44,7 @@ #include "AuthConfigFactory.h" #include "AuthConfig.h" #include "a2functional.h" +#include "TimeA2.h" #include namespace aria2 { @@ -200,17 +200,21 @@ std::string HttpRequest::createRequest() const if(getPreviousURI().size()) { requestLine += "Referer: "+getPreviousURI()+"\r\n"; } - std::string cookiesValue; - Cookies cookies = request->cookieBox->criteriaFind(getHost(), - getDir(), - time(0), - getProtocol() == Request::PROTO_HTTPS ? - true : false); - for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) { - cookiesValue += (*itr).toString()+";"; - } - if(cookiesValue.size()) { - requestLine += std::string("Cookie: ")+cookiesValue+"\r\n"; + if(!_cookieStorage.isNull()) { + std::string cookiesValue; + std::deque cookies = + _cookieStorage->criteriaFind(getHost(), + getDir(), + Time().getTime(), + getProtocol() == Request::PROTO_HTTPS ? + true : false); + for(std::deque::const_iterator itr = cookies.begin(); + itr != cookies.end(); ++itr) { + cookiesValue += (*itr).toString()+";"; + } + if(!cookiesValue.empty()) { + requestLine += std::string("Cookie: ")+cookiesValue+"\r\n"; + } } // append additional headers given by user. for(std::deque::const_iterator i = _headers.begin(); @@ -322,4 +326,15 @@ const std::string& HttpRequest::getQuery() const return request->getQuery(); } +void HttpRequest::setCookieStorage +(const SharedHandle& cookieStorage) +{ + _cookieStorage = cookieStorage; +} + +SharedHandle HttpRequest::getCookieStorage() const +{ + return _cookieStorage; +} + } // namespace aria2 diff --git a/src/HttpRequest.h b/src/HttpRequest.h index 2dce737b..c6f05b94 100644 --- a/src/HttpRequest.h +++ b/src/HttpRequest.h @@ -46,6 +46,7 @@ class Request; class Segment; class Range; class Option; +class CookieStorage; class HttpRequest { private: @@ -72,10 +73,11 @@ private: std::deque _acceptTypes; + SharedHandle _cookieStorage; + std::string getHostText(const std::string& host, uint16_t port) const; std::string getProxyAuthString() const; - public: HttpRequest(); @@ -187,6 +189,10 @@ public: { _acceptTypes.insert(_acceptTypes.end(), first, last); } + + void setCookieStorage(const SharedHandle& cookieStorage); + + SharedHandle getCookieStorage() const; }; typedef SharedHandle HttpRequestHandle; diff --git a/src/HttpRequestCommand.cc b/src/HttpRequestCommand.cc index 2e4707c5..3f8a3e65 100644 --- a/src/HttpRequestCommand.cc +++ b/src/HttpRequestCommand.cc @@ -46,6 +46,7 @@ #include "prefs.h" #include "a2functional.h" #include "Util.h" +#include "CookieStorage.h" #include namespace aria2 { @@ -71,7 +72,8 @@ createHttpRequest(const SharedHandle& req, const SharedHandle& segment, uint64_t totalLength, const Option* option, - const RequestGroup* rg) + const RequestGroup* rg, + const SharedHandle& cookieStorage) { HttpRequestHandle httpRequest(new HttpRequest()); httpRequest->setUserAgent(option->get(PREF_USER_AGENT)); @@ -79,6 +81,7 @@ createHttpRequest(const SharedHandle& req, httpRequest->setSegment(segment); httpRequest->setEntityLength(totalLength); httpRequest->addHeader(option->get(PREF_HEADER)); + httpRequest->setCookieStorage(cookieStorage); if(!rg->getAcceptFeatures().empty()) { const std::deque& acceptFeatures = rg->getAcceptFeatures(); std::string acceptFeaturesHeader = "Accept-Features: "; @@ -103,7 +106,8 @@ bool HttpRequestCommand::executeInternal() { HttpRequestHandle httpRequest (createHttpRequest(req, SharedHandle(), _requestGroup->getTotalLength(), e->option, - _requestGroup)); + _requestGroup, + e->getCookieStorage())); _httpConnection->sendRequest(httpRequest); } else { for(Segments::iterator itr = _segments.begin(); itr != _segments.end(); ++itr) { @@ -112,7 +116,8 @@ bool HttpRequestCommand::executeInternal() { HttpRequestHandle httpRequest (createHttpRequest(req, segment, _requestGroup->getTotalLength(), e->option, - _requestGroup)); + _requestGroup, + e->getCookieStorage())); _httpConnection->sendRequest(httpRequest); } } diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index db6c4a9a..6588d339 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -35,7 +35,6 @@ #include "HttpResponse.h" #include "Request.h" #include "Segment.h" -#include "CookieBox.h" #include "HttpRequest.h" #include "HttpHeader.h" #include "Range.h" @@ -52,6 +51,7 @@ #ifdef HAVE_LIBZ # include "GZipDecoder.h" #endif // HAVE_LIBZ +#include "CookieStorage.h" #include namespace aria2 { @@ -109,9 +109,10 @@ void HttpResponse::retrieveCookie() { std::deque v = httpHeader->get(HttpHeader::SET_COOKIE); for(std::deque::const_iterator itr = v.begin(); itr != v.end(); - itr++) { - httpRequest->getRequest()->cookieBox->add(*itr, httpRequest->getHost(), - httpRequest->getDir()); + ++itr) { + httpRequest->getCookieStorage()->parseAndStore(*itr, + httpRequest->getHost(), + httpRequest->getDir()); } } diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index 929d7992..e0c58848 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -63,6 +63,7 @@ #include "HttpHeader.h" #include "Decoder.h" #include "LogFactory.h" +#include "CookieStorage.h" namespace aria2 { diff --git a/src/HttpSkipResponseCommand.cc b/src/HttpSkipResponseCommand.cc index e89e0273..8ef54269 100644 --- a/src/HttpSkipResponseCommand.cc +++ b/src/HttpSkipResponseCommand.cc @@ -50,6 +50,7 @@ #include "HttpHeader.h" #include "prefs.h" #include "Option.h" +#include "CookieStorage.h" namespace aria2 { diff --git a/src/Makefile.am b/src/Makefile.am index 9126ddc0..92d97d2a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -195,7 +195,8 @@ SRCS = Socket.h\ URISelector.h\ InOrderURISelector.cc InOrderURISelector.h\ ServerStatURISelector.cc ServerStatURISelector.h\ - NsCookieParser.cc NsCookieParser.h + NsCookieParser.cc NsCookieParser.h\ + CookieStorage.cc CookieStorage.h if HAVE_LIBZ SRCS += GZipDecoder.cc GZipDecoder.h diff --git a/src/Makefile.in b/src/Makefile.in index 3cfe6985..578752f7 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -415,9 +415,10 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ ServerStatMan.h URISelector.h InOrderURISelector.cc \ InOrderURISelector.h ServerStatURISelector.cc \ ServerStatURISelector.h NsCookieParser.cc NsCookieParser.h \ - GZipDecoder.cc GZipDecoder.h Sqlite3MozCookieParser.cc \ - Sqlite3MozCookieParser.h AsyncNameResolver.cc \ - AsyncNameResolver.h IteratableChunkChecksumValidator.cc \ + CookieStorage.cc CookieStorage.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 \ @@ -809,12 +810,12 @@ am__objects_18 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \ Signature.$(OBJEXT) ServerStat.$(OBJEXT) \ ServerStatMan.$(OBJEXT) InOrderURISelector.$(OBJEXT) \ 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) + CookieStorage.$(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)" @@ -1139,12 +1140,13 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \ ServerStatMan.h URISelector.h InOrderURISelector.cc \ InOrderURISelector.h ServerStatURISelector.cc \ 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) + CookieStorage.cc CookieStorage.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@\ @@ -1297,6 +1299,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBox.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBoxFactory.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieParser.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieStorage.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CopyDiskAdaptor.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTAbstractMessage.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTAbstractNodeLookupTask.Po@am__quote@ diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index 17ef6c4e..87d4ba44 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -46,6 +46,8 @@ #include "Util.h" #include "Option.h" #include "StatCalc.h" +#include "CookieStorage.h" +#include "File.h" #include #include @@ -100,6 +102,20 @@ int MultiUrlRequestInfo::execute() DownloadEngineHandle e = DownloadEngineFactory().newDownloadEngine(_option, _requestGroups); + try { + if(_option->defined(PREF_LOAD_COOKIES)) { + File cookieFile(_option->get(PREF_LOAD_COOKIES)); + if(cookieFile.isFile()) { + e->getCookieStorage()->load(_option->get(PREF_LOAD_COOKIES)); + } else { + _logger->error(MSG_LOADING_COOKIE_FAILED, + _option->get(PREF_LOAD_COOKIES).c_str()); + } + } + } catch(RecoverableException& e) { + _logger->error(EX_EXCEPTION_CAUGHT, e); + } + std::string serverStatIf = _option->get(PREF_SERVER_STAT_IF); if(!serverStatIf.empty()) { e->_requestGroupMan->loadServerStat(serverStatIf); diff --git a/src/Request.cc b/src/Request.cc index e5674b08..4daf4aaf 100644 --- a/src/Request.cc +++ b/src/Request.cc @@ -35,8 +35,6 @@ #include "Request.h" #include "Util.h" #include "FeatureConfig.h" -#include "CookieBoxFactory.h" -#include "CookieBox.h" #include "RecoverableException.h" #include "StringFormat.h" #include "A2STR.h" @@ -60,8 +58,7 @@ Request::Request(): _supportsPersistentConnection(true), _keepAliveHint(false), _pipeliningHint(false), - method(METHOD_GET), - cookieBox(CookieBoxFactorySingletonHolder::instance()->createNewInstance()) + method(METHOD_GET) {} Request::~Request() {} diff --git a/src/Request.h b/src/Request.h index 2b5c1740..852193fd 100644 --- a/src/Request.h +++ b/src/Request.h @@ -50,8 +50,6 @@ namespace aria2 { -class CookieBox; - class Request { private: std::string url; @@ -93,9 +91,6 @@ private: bool isHexNumber(const char c) const; void urlencode(std::string& result, const std::string& src) const; - -public: - SharedHandle cookieBox; public: Request(); virtual ~Request(); diff --git a/src/main.cc b/src/main.cc index 7cc8d57e..a686bdfe 100644 --- a/src/main.cc +++ b/src/main.cc @@ -39,7 +39,6 @@ #include "Util.h" #include "BitfieldManFactory.h" #include "AuthConfigFactory.h" -#include "CookieBoxFactory.h" #include "FeatureConfig.h" #include "MultiUrlRequestInfo.h" #include "SimpleRandomizer.h" @@ -380,20 +379,6 @@ int main(int argc, char* argv[]) } } - CookieBoxFactoryHandle cookieBoxFactory(new CookieBoxFactory()); - CookieBoxFactorySingletonHolder::instance(cookieBoxFactory); - if(op->defined(PREF_LOAD_COOKIES)) { - File cookieFile(op->get(PREF_LOAD_COOKIES)); - if(cookieFile.isFile()) { - CookieBoxFactorySingletonHolder::instance()->loadDefaultCookie - (op->get(PREF_LOAD_COOKIES)); - } else { - logger->error(MSG_LOADING_COOKIE_FAILED, - op->get(PREF_LOAD_COOKIES).c_str()); - exit(EXIT_FAILURE); - } - } - AuthConfigFactorySingleton::instance(authConfigFactory); CUIDCounterHandle cuidCounter(new CUIDCounter()); CUIDCounterSingletonHolder::instance(cuidCounter); diff --git a/test/AllTest.cc b/test/AllTest.cc index a457673a..9fb27e0d 100644 --- a/test/AllTest.cc +++ b/test/AllTest.cc @@ -1,13 +1,9 @@ #include "Platform.h" -#include "CookieBoxFactory.h" #include #include #include #include -using aria2::SharedHandle; -using aria2::SingletonHolder; - int main(int argc, char* argv[]) { aria2::Platform platform; @@ -22,12 +18,6 @@ int main(int argc, char* argv[]) { runner.addTest(suite); runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr)); - - // setup - - SharedHandle cookieBoxFactory - (new aria2::CookieBoxFactory()); - SingletonHolder >::instance(cookieBoxFactory); // Run the tests. bool successfull = runner.run(); diff --git a/test/CookieStorageTest.cc b/test/CookieStorageTest.cc index 604efecd..3b4cef4a 100644 --- a/test/CookieStorageTest.cc +++ b/test/CookieStorageTest.cc @@ -65,6 +65,11 @@ void CookieStorageTest::testStore() CPPUNIT_ASSERT(!st.store(badCookie)); CPPUNIT_ASSERT_EQUAL((size_t)1, st.size()); CPPUNIT_ASSERT(std::find(st.begin(), st.end(), anotherCookie) != st.end()); + + Cookie fromNumericHost("k", "v", "/", "192.168.1.1", false); + CPPUNIT_ASSERT(st.store(fromNumericHost)); + CPPUNIT_ASSERT_EQUAL((size_t)2, st.size()); + CPPUNIT_ASSERT(std::find(st.begin(), st.end(), fromNumericHost) != st.end()); } void CookieStorageTest::testParseAndStore() @@ -81,6 +86,11 @@ void CookieStorageTest::testParseAndStore() CPPUNIT_ASSERT(!st.parseAndStore(localhostCookieStr, "127.0.0.1", "/downloads")); + + std::string numericHostCookieStr = "k=v;" + " expires=Fri, 2038-01-01 00:00:00 GMT; path=/; domain=192.168.1.1;"; + CPPUNIT_ASSERT(st.parseAndStore(numericHostCookieStr, "192.168.1.1", "/")); + } void CookieStorageTest::testCriteriaFind() @@ -94,12 +104,15 @@ void CookieStorageTest::testCriteriaFind() Cookie delta("delta", "DELTA", "/foo/bar", ".aria2.org", false); Cookie echo("echo", "ECHO", "/", "www.aria2.org", false); Cookie foxtrot("foxtrot", "FOXTROT", "/", ".sf.net", false); + Cookie golf("golf", "GOLF", "/", "192.168.1.1", false); + CPPUNIT_ASSERT(st.store(alpha)); CPPUNIT_ASSERT(st.store(bravo)); CPPUNIT_ASSERT(st.store(charlie)); CPPUNIT_ASSERT(st.store(delta)); CPPUNIT_ASSERT(st.store(echo)); CPPUNIT_ASSERT(st.store(foxtrot)); + CPPUNIT_ASSERT(st.store(golf)); std::deque aria2Slash = st.criteriaFind("www.aria2.org", "/", 0, false); @@ -130,6 +143,11 @@ void CookieStorageTest::testCriteriaFind() std::deque dlAria2 = st.criteriaFind("dl.aria2.org", "/", 0, false); CPPUNIT_ASSERT_EQUAL((size_t)1, dlAria2.size()); CPPUNIT_ASSERT_EQUAL(std::string("alpha"), dlAria2[0].name); + + std::deque numericHostCookies = st.criteriaFind("192.168.1.1", "/", 0, + false); + CPPUNIT_ASSERT_EQUAL((size_t)1, numericHostCookies.size()); + CPPUNIT_ASSERT_EQUAL(std::string("golf"), numericHostCookies[0].name); } void CookieStorageTest::testLoad() diff --git a/test/CookieTest.cc b/test/CookieTest.cc index 0221ae05..f04a390b 100644 --- a/test/CookieTest.cc +++ b/test/CookieTest.cc @@ -85,6 +85,11 @@ void CookieTest::testValidate() Cookie nameEmpty("", "v", "/", "localhost", false); CPPUNIT_ASSERT(!nameEmpty.validate("localhost", "/")); } + { + Cookie fromNumericHost("k", "v", "/", "192.168.1.1", false); + CPPUNIT_ASSERT(fromNumericHost.validate("192.168.1.1", "/")); + CPPUNIT_ASSERT(!fromNumericHost.validate("www.aria2.org", "/")); + } } void CookieTest::testOperatorEqual() @@ -127,6 +132,10 @@ void CookieTest::testMatch() CPPUNIT_ASSERT(expireTest.match("www.aria2.org", "/", 999, false)); CPPUNIT_ASSERT(!expireTest.match("www.aria2.org", "/", 1000, false)); CPPUNIT_ASSERT(!expireTest.match("www.aria2.org", "/", 1001, false)); + + Cookie fromNumericHost("k", "v", "/", "192.168.1.1", false); + CPPUNIT_ASSERT(fromNumericHost.match("192.168.1.1", "/", 0, false)); + CPPUNIT_ASSERT(!fromNumericHost.match("www.aria2.org", "/", 0, false)); } void CookieTest::testIsExpired() diff --git a/test/HttpRequestTest.cc b/test/HttpRequestTest.cc index 411fcde9..12491548 100644 --- a/test/HttpRequestTest.cc +++ b/test/HttpRequestTest.cc @@ -5,9 +5,9 @@ #include "Piece.h" #include "Range.h" #include "Request.h" -#include "CookieBox.h" #include "Option.h" #include "array_fun.h" +#include "CookieStorage.h" #include namespace aria2 { @@ -396,19 +396,21 @@ void HttpRequestTest::testCreateRequest_with_cookie() Cookie cookie1("name1", "value1", "/archives", "localhost", false); Cookie cookie2("name2", "value2", "/archives/download", "localhost", false); - Cookie cookie3("name3", "value3", "/archives/download", "tt.localhost", false); - Cookie cookie4("name4", "value4", "/archives/download", "tt.localhost", true); + Cookie cookie3("name3", "value3", "/archives/download", ".aria2.org", false); + Cookie cookie4("name4", "value4", "/archives/", ".aria2.org", true); - request->cookieBox->add(cookie1); - request->cookieBox->add(cookie2); - request->cookieBox->add(cookie3); - request->cookieBox->add(cookie4); + SharedHandle st(new CookieStorage()); + CPPUNIT_ASSERT(st->store(cookie1)); + CPPUNIT_ASSERT(st->store(cookie2)); + CPPUNIT_ASSERT(st->store(cookie3)); + CPPUNIT_ASSERT(st->store(cookie4)); HttpRequest httpRequest; httpRequest.disableContentEncoding(); httpRequest.setRequest(request); httpRequest.setSegment(segment); + httpRequest.setCookieStorage(st); std::string expectedText = "GET /archives/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n" "User-Agent: aria2\r\n" @@ -431,35 +433,36 @@ void HttpRequestTest::testCreateRequest_with_cookie() "Pragma: no-cache\r\n" "Cache-Control: no-cache\r\n" "Connection: close\r\n" - "Cookie: name1=value1;name2=value2;\r\n" + "Cookie: name2=value2;name1=value1;\r\n" "\r\n"; CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest()); - request->setUrl("http://tt.localhost/archives/download/aria2-1.0.0.tar.bz2"); + request->setUrl("http://www.aria2.org/archives/download/aria2-1.0.0.tar.bz2"); expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n" "User-Agent: aria2\r\n" "Accept: */*\r\n" - "Host: tt.localhost\r\n" + "Host: www.aria2.org\r\n" "Pragma: no-cache\r\n" "Cache-Control: no-cache\r\n" "Connection: close\r\n" - "Cookie: name1=value1;name2=value2;name3=value3;\r\n" + "Cookie: name3=value3;\r\n" "\r\n"; CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest()); - request->setUrl("https://tt.localhost/archives/download/aria2-1.0.0.tar.bz2"); + request->setUrl("https://www.aria2.org/archives/download/" + "aria2-1.0.0.tar.bz2"); expectedText = "GET /archives/download/aria2-1.0.0.tar.bz2 HTTP/1.1\r\n" "User-Agent: aria2\r\n" "Accept: */*\r\n" - "Host: tt.localhost\r\n" + "Host: www.aria2.org\r\n" "Pragma: no-cache\r\n" "Cache-Control: no-cache\r\n" "Connection: close\r\n" - "Cookie: name1=value1;name2=value2;name3=value3;name4=value4;\r\n" + "Cookie: name3=value3;name4=value4;\r\n" "\r\n"; CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest()); diff --git a/test/HttpResponseTest.cc b/test/HttpResponseTest.cc index 015ba2cd..496d8e37 100644 --- a/test/HttpResponseTest.cc +++ b/test/HttpResponseTest.cc @@ -9,6 +9,7 @@ #include "A2STR.h" #include "Decoder.h" #include "DlRetryEx.h" +#include "CookieStorage.h" #include #include @@ -40,6 +41,7 @@ class HttpResponseTest : public CppUnit::TestFixture { CPPUNIT_TEST(testValidateResponse_chunked); CPPUNIT_TEST(testHasRetryAfter); CPPUNIT_TEST(testProcessRedirect); + CPPUNIT_TEST(testRetrieveCookie); CPPUNIT_TEST_SUITE_END(); private: @@ -69,6 +71,7 @@ public: void testValidateResponse_chunked(); void testHasRetryAfter(); void testProcessRedirect(); + void testRetrieveCookie(); }; @@ -454,4 +457,33 @@ void HttpResponseTest::testProcessRedirect() } } +void HttpResponseTest::testRetrieveCookie() +{ + HttpResponse httpResponse; + SharedHandle httpHeader(new HttpHeader()); + httpResponse.setHttpHeader(httpHeader); + + SharedHandle httpRequest(new HttpRequest()); + SharedHandle request(new Request()); + request->setUrl("http://www.aria2.org/archives/aria2-1.0.0.tar.bz2"); + httpRequest->setRequest(request); + SharedHandle st(new CookieStorage()); + httpRequest->setCookieStorage(st); + httpResponse.setHttpRequest(httpRequest); + + httpHeader->put("Set-Cookie", "k1=v1; expires=Sun, 2007-06-10 11:00:00 GMT;" + "path=/; domain=.aria2.org;"); + httpHeader->put("Set-Cookie", "k2=v2; expires=Sun, 2038-01-01 00:00:00 GMT;" + "path=/; domain=.aria2.org;"); + httpHeader->put("Set-Cookie", "k3=v3;"); + + httpResponse.retrieveCookie(); + + CPPUNIT_ASSERT_EQUAL((size_t)2, st->size()); + + std::deque::const_iterator citer = st->begin(); + CPPUNIT_ASSERT_EQUAL(std::string("k2=v2"), (*(st->begin())).toString()); + CPPUNIT_ASSERT_EQUAL(std::string("k3=v3"), (*(st->begin()+1)).toString()); +} + } // namespace aria2 diff --git a/test/Makefile.am b/test/Makefile.am index 2d6b528f..e779f551 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -24,11 +24,9 @@ aria2c_SOURCES = AllTest.cc\ AlphaNumberDecoratorTest.cc\ UriListParserTest.cc\ HttpHeaderProcessorTest.cc\ - CookieBoxTest.cc\ RequestTest.cc\ CookieParserTest.cc\ HttpRequestTest.cc\ - CookieBoxFactoryTest.cc\ RequestGroupManTest.cc\ AuthConfigFactoryTest.cc\ NetrcAuthResolverTest.cc\ diff --git a/test/Makefile.in b/test/Makefile.in index 279b31b2..391636ac 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -178,17 +178,17 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \ SingleFileDownloadContextTest.cc RequestGroupTest.cc \ PStringBuildVisitorTest.cc ParameterizedStringParserTest.cc \ UtilTest.cc AlphaNumberDecoratorTest.cc UriListParserTest.cc \ - HttpHeaderProcessorTest.cc CookieBoxTest.cc RequestTest.cc \ - CookieParserTest.cc HttpRequestTest.cc CookieBoxFactoryTest.cc \ - RequestGroupManTest.cc AuthConfigFactoryTest.cc \ - NetrcAuthResolverTest.cc DefaultAuthResolverTest.cc \ - OptionHandlerTest.cc SegmentManTest.cc BitfieldManTest.cc \ - NetrcTest.cc SingletonHolderTest.cc HttpHeaderTest.cc \ - HttpResponseTest.cc SharedHandleTest.cc FileTest.cc \ - OptionTest.cc DefaultDiskWriterTest.cc FeatureConfigTest.cc \ - SpeedCalcTest.cc MultiDiskAdaptorTest.cc \ - MultiFileAllocationIteratorTest.cc FixedNumberRandomizer.h \ - ProtocolDetectorTest.cc StringFormatTest.cc ExceptionTest.cc \ + HttpHeaderProcessorTest.cc RequestTest.cc CookieParserTest.cc \ + HttpRequestTest.cc RequestGroupManTest.cc \ + AuthConfigFactoryTest.cc NetrcAuthResolverTest.cc \ + DefaultAuthResolverTest.cc OptionHandlerTest.cc \ + SegmentManTest.cc BitfieldManTest.cc NetrcTest.cc \ + SingletonHolderTest.cc HttpHeaderTest.cc HttpResponseTest.cc \ + SharedHandleTest.cc FileTest.cc OptionTest.cc \ + DefaultDiskWriterTest.cc FeatureConfigTest.cc SpeedCalcTest.cc \ + MultiDiskAdaptorTest.cc MultiFileAllocationIteratorTest.cc \ + FixedNumberRandomizer.h ProtocolDetectorTest.cc \ + StringFormatTest.cc ExceptionTest.cc \ DownloadHandlerFactoryTest.cc ChunkedDecoderTest.cc \ SignatureTest.cc ServerStatManTest.cc \ ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \ @@ -345,9 +345,8 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) TestUtil.$(OBJEXT) \ RequestGroupTest.$(OBJEXT) PStringBuildVisitorTest.$(OBJEXT) \ ParameterizedStringParserTest.$(OBJEXT) UtilTest.$(OBJEXT) \ AlphaNumberDecoratorTest.$(OBJEXT) UriListParserTest.$(OBJEXT) \ - HttpHeaderProcessorTest.$(OBJEXT) CookieBoxTest.$(OBJEXT) \ - RequestTest.$(OBJEXT) CookieParserTest.$(OBJEXT) \ - HttpRequestTest.$(OBJEXT) CookieBoxFactoryTest.$(OBJEXT) \ + HttpHeaderProcessorTest.$(OBJEXT) RequestTest.$(OBJEXT) \ + CookieParserTest.$(OBJEXT) HttpRequestTest.$(OBJEXT) \ RequestGroupManTest.$(OBJEXT) AuthConfigFactoryTest.$(OBJEXT) \ NetrcAuthResolverTest.$(OBJEXT) \ DefaultAuthResolverTest.$(OBJEXT) OptionHandlerTest.$(OBJEXT) \ @@ -574,17 +573,17 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \ SingleFileDownloadContextTest.cc RequestGroupTest.cc \ PStringBuildVisitorTest.cc ParameterizedStringParserTest.cc \ UtilTest.cc AlphaNumberDecoratorTest.cc UriListParserTest.cc \ - HttpHeaderProcessorTest.cc CookieBoxTest.cc RequestTest.cc \ - CookieParserTest.cc HttpRequestTest.cc CookieBoxFactoryTest.cc \ - RequestGroupManTest.cc AuthConfigFactoryTest.cc \ - NetrcAuthResolverTest.cc DefaultAuthResolverTest.cc \ - OptionHandlerTest.cc SegmentManTest.cc BitfieldManTest.cc \ - NetrcTest.cc SingletonHolderTest.cc HttpHeaderTest.cc \ - HttpResponseTest.cc SharedHandleTest.cc FileTest.cc \ - OptionTest.cc DefaultDiskWriterTest.cc FeatureConfigTest.cc \ - SpeedCalcTest.cc MultiDiskAdaptorTest.cc \ - MultiFileAllocationIteratorTest.cc FixedNumberRandomizer.h \ - ProtocolDetectorTest.cc StringFormatTest.cc ExceptionTest.cc \ + HttpHeaderProcessorTest.cc RequestTest.cc CookieParserTest.cc \ + HttpRequestTest.cc RequestGroupManTest.cc \ + AuthConfigFactoryTest.cc NetrcAuthResolverTest.cc \ + DefaultAuthResolverTest.cc OptionHandlerTest.cc \ + SegmentManTest.cc BitfieldManTest.cc NetrcTest.cc \ + SingletonHolderTest.cc HttpHeaderTest.cc HttpResponseTest.cc \ + SharedHandleTest.cc FileTest.cc OptionTest.cc \ + DefaultDiskWriterTest.cc FeatureConfigTest.cc SpeedCalcTest.cc \ + MultiDiskAdaptorTest.cc MultiFileAllocationIteratorTest.cc \ + FixedNumberRandomizer.h ProtocolDetectorTest.cc \ + StringFormatTest.cc ExceptionTest.cc \ DownloadHandlerFactoryTest.cc ChunkedDecoderTest.cc \ SignatureTest.cc ServerStatManTest.cc \ ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \ @@ -710,8 +709,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtUnchokeMessageTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ByteArrayDiskWriterTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ChunkedDecoderTest.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBoxFactoryTest.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBoxTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieParserTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieStorageTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieTest.Po@am__quote@ diff --git a/test/NsCookieParserTest.cc b/test/NsCookieParserTest.cc index 1775bd05..9535a7c8 100644 --- a/test/NsCookieParserTest.cc +++ b/test/NsCookieParserTest.cc @@ -28,12 +28,12 @@ void NsCookieParserTest::testParse() { NsCookieParser parser; std::deque cookies = parser.parse("nscookietest.txt"); - CPPUNIT_ASSERT_EQUAL((size_t)4, cookies.size()); + CPPUNIT_ASSERT_EQUAL((size_t)5, 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((time_t)2147483647, c.expires); CPPUNIT_ASSERT_EQUAL(std::string("/"), c.path); CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c.domain); @@ -42,7 +42,7 @@ void NsCookieParserTest::testParse() 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); + CPPUNIT_ASSERT_EQUAL(std::string("expired"), c.domain); c = cookies[2]; CPPUNIT_ASSERT_EQUAL(std::string("passwd"), c.name); @@ -52,6 +52,13 @@ void NsCookieParserTest::testParse() CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c.domain); c = cookies[3]; + CPPUNIT_ASSERT_EQUAL(std::string("TAX"), c.name); + CPPUNIT_ASSERT_EQUAL(std::string("1000"), c.value); + CPPUNIT_ASSERT_EQUAL((time_t)2147483647, c.expires); + CPPUNIT_ASSERT_EQUAL(std::string("/"), c.path); + CPPUNIT_ASSERT_EQUAL(std::string("overflow"), c.domain); + + c = cookies[4]; CPPUNIT_ASSERT_EQUAL(std::string("novalue"), c.name); CPPUNIT_ASSERT_EQUAL(std::string(""), c.value); CPPUNIT_ASSERT_EQUAL((time_t)2147483647, c.expires); diff --git a/test/Sqlite3MozCookieParserTest.cc b/test/Sqlite3MozCookieParserTest.cc index 04ab51c2..a9717ca1 100644 --- a/test/Sqlite3MozCookieParserTest.cc +++ b/test/Sqlite3MozCookieParserTest.cc @@ -37,7 +37,7 @@ void Sqlite3MozCookieParserTest::testParse() CPPUNIT_ASSERT_EQUAL(std::string("/"), localhost.path); CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), localhost.name); CPPUNIT_ASSERT_EQUAL(std::string("123456789"), localhost.value); - CPPUNIT_ASSERT_EQUAL((time_t)1200000000, localhost.expires); + CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, localhost.expires); CPPUNIT_ASSERT_EQUAL(true, localhost.secure); const Cookie& nullValue = cookies[1]; diff --git a/test/nscookietest.txt b/test/nscookietest.txt index 7903a710..9114fe4d 100644 --- a/test/nscookietest.txt +++ b/test/nscookietest.txt @@ -4,5 +4,6 @@ localhost FALSE / TRUE 2147483647 JSESSIONID 123456789 expired FALSE / FALSE 1181473200 user me localhost FALSE /cgi-bin FALSE 2147483647 passwd secret +badformat overflow FALSE / FALSE 9223372036854775807 TAX 1000 localhost FALSE / FALSE 2147483648 novalue