diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/CookieBoxFactory.cc | 70 | ||||
| -rw-r--r-- | src/CookieBoxFactory.h | 5 | ||||
| -rw-r--r-- | src/Makefile.am | 3 | ||||
| -rw-r--r-- | src/Makefile.in | 36 | ||||
| -rw-r--r-- | src/NsCookieParser.cc | 100 | ||||
| -rw-r--r-- | src/NsCookieParser.h | 56 |
6 files changed, 193 insertions, 77 deletions
diff --git a/src/CookieBoxFactory.cc b/src/CookieBoxFactory.cc index a1fe0267..967dc2bf 100644 --- a/src/CookieBoxFactory.cc +++ b/src/CookieBoxFactory.cc @@ -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; + { + 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; + } } - if(std::string(header) == "SQLite format 3") { + try { + 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()); - } #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 diff --git a/src/CookieBoxFactory.h b/src/CookieBoxFactory.h index d10e52b1..0d55e790 100644 --- a/src/CookieBoxFactory.h +++ b/src/CookieBoxFactory.h @@ -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; diff --git a/src/Makefile.am b/src/Makefile.am index 81d5464a..9126ddc0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/Makefile.in b/src/Makefile.in index 21a8f1f9..3cfe6985 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -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@ diff --git a/src/NsCookieParser.cc b/src/NsCookieParser.cc new file mode 100644 index 00000000..41ad22be --- /dev/null +++ b/src/NsCookieParser.cc @@ -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 diff --git a/src/NsCookieParser.h b/src/NsCookieParser.h new file mode 100644 index 00000000..6882e44f --- /dev/null +++ b/src/NsCookieParser.h @@ -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_ |
