From c096a3a55386d8b801196729a169162bec7f41e1 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 28 Aug 2006 12:40:41 +0000 Subject: [PATCH] 2006-08-28 Tatsuhiro Tsujikawa To make filename URL-decoded: * src/HttpResponseCommand.h: Updated doc. * src/HttpResponseCommand.cc (determinFilename): Made filename URL-decoded. * src/FtpInitiateConnectionCommand.cc (executeInternal): Made filename URL-decoded. * src/Util.h (urldecode): New function. * src/Util.cc (urldecode): New function. --- ChangeLog | 11 +++++++++++ TODO | 4 ++-- src/FtpInitiateConnectionCommand.cc | 2 +- src/HandshakeMessage.cc | 2 +- src/HttpResponseCommand.cc | 4 ++-- src/HttpResponseCommand.h | 3 +++ src/LogFactory.cc | 2 +- src/SharedHandle.h | 16 ++++++++++++++-- src/TorrentMan.cc | 4 ++-- src/TrackerWatcherCommand.cc | 2 +- src/Util.cc | 29 +++++++++++++++++++++++++++-- src/Util.h | 6 ++++-- test/OptionTest.cc | 9 +++++++++ test/ShareRatioSeedCriteriaTest.cc | 2 +- test/UtilTest.cc | 25 +++++++++++++++++++++++++ 15 files changed, 104 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index c2e41149..c030f332 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2006-08-28 Tatsuhiro Tsujikawa + + To make filename URL-decoded: + * src/HttpResponseCommand.h: Updated doc. + * src/HttpResponseCommand.cc + (determinFilename): Made filename URL-decoded. + * src/FtpInitiateConnectionCommand.cc + (executeInternal): Made filename URL-decoded. + * src/Util.h (urldecode): New function. + * src/Util.cc (urldecode): New function. + 2006-08-27 Tatsuhiro Tsujikawa To add --seed-time and --seed-ratio command-line option: diff --git a/TODO b/TODO index b0ac3088..21a115f7 100644 --- a/TODO +++ b/TODO @@ -13,5 +13,5 @@ * List available os, version, etc for metalink * ipv6(RFC2428 for ftp) * Add silent mode. -* Add NOTIFY log level. A message categorized in this level is printed in - a console along with a log file. +* Add upload speed limit command-line option. +* Save URLs and command-line arguments to .aria2 file. diff --git a/src/FtpInitiateConnectionCommand.cc b/src/FtpInitiateConnectionCommand.cc index b55c0095..8a964870 100644 --- a/src/FtpInitiateConnectionCommand.cc +++ b/src/FtpInitiateConnectionCommand.cc @@ -45,7 +45,7 @@ FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() { bool FtpInitiateConnectionCommand::executeInternal(Segment segment) { if(!e->segmentMan->downloadStarted) { - e->segmentMan->filename = req->getFile(); + e->segmentMan->filename = Util::urldecode(req->getFile()); bool segFileExists = e->segmentMan->segmentFileExists(); if(segFileExists) { e->segmentMan->load(); diff --git a/src/HandshakeMessage.cc b/src/HandshakeMessage.cc index c2a72519..fd64afde 100644 --- a/src/HandshakeMessage.cc +++ b/src/HandshakeMessage.cc @@ -74,7 +74,7 @@ int HandshakeMessage::getMessageLength() { string HandshakeMessage::toString() const { return "handshake peerId="+ - Util::urlencode((unsigned char*)peerId, sizeof(peerId))+ + Util::urlencode(peerId, sizeof(peerId))+ " reserved="+Util::toHex(reserved, sizeof(reserved)); } diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index d9f16977..feea4a06 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -99,11 +99,11 @@ string HttpResponseCommand::determinFilename(const HttpHeader& headers) { string contentDisposition = Util::getContentDispositionFilename(headers.getFirst("Content-Disposition")); if(contentDisposition.empty()) { - return req->getFile(); + return Util::urldecode(req->getFile()); } else { logger->info("CUID#%d - Content-Disposition Detected. Use %s as filename", cuid, contentDisposition.c_str()); - return contentDisposition; + return Util::urldecode(contentDisposition); } } diff --git a/src/HttpResponseCommand.h b/src/HttpResponseCommand.h index 19a092c4..664fe026 100644 --- a/src/HttpResponseCommand.h +++ b/src/HttpResponseCommand.h @@ -33,6 +33,9 @@ private: bool handleOtherEncoding(const string& transferEncoding, const HttpHeader& headers); void createHttpDownloadCommand(const string& transferEncoding = ""); void retrieveCookie(const HttpHeader& headers); + /** + * Returned filename is URL-decoded. + */ string determinFilename(const HttpHeader& headers); HttpConnection* http; protected: diff --git a/src/LogFactory.cc b/src/LogFactory.cc index c11009e1..7c274f4f 100644 --- a/src/LogFactory.cc +++ b/src/LogFactory.cc @@ -22,7 +22,7 @@ #include "LogFactory.h" #include "SimpleLogger.h" -string LogFactory::filename; +string LogFactory::filename = "/dev/null"; Logger* LogFactory::logger = NULL; Logger* LogFactory::getInstance() { diff --git a/src/SharedHandle.h b/src/SharedHandle.h index 69da6f8a..a346fff4 100644 --- a/src/SharedHandle.h +++ b/src/SharedHandle.h @@ -47,7 +47,9 @@ private: public: SharedHandle():obj(new T()), ucount(new int(1)) {} SharedHandle(T* obj):obj(obj), ucount(new int(1)) {} - + SharedHandle(const SharedHandle& t):obj(t.get()), ucount(t.getRefCount()) { + ++*ucount; + } template SharedHandle(const SharedHandle& t):obj(t.get()), ucount(t.getRefCount()) { ++*ucount; @@ -60,8 +62,18 @@ public: } } + SharedHandle& operator=(const SharedHandle& t) { + ++*t.getRefCount(); + if(--*ucount == 0) { + delete obj; + delete ucount; + } + obj = t.get(); + ucount = t.getRefCount(); + return *this; + } template - SharedHandle& operator=(const SharedHandle& t) { + SharedHandle& operator=(const SharedHandle& t) { ++*t.getRefCount(); if(--*ucount == 0) { delete obj; diff --git a/src/TorrentMan.cc b/src/TorrentMan.cc index 0ef41347..904e387d 100644 --- a/src/TorrentMan.cc +++ b/src/TorrentMan.cc @@ -653,10 +653,10 @@ void TorrentMan::onDownloadComplete() { save(); diskAdaptor->onDownloadComplete(); if(isSelectiveDownloadingMode()) { - logger->notice("Download of selected files has completed."); + logger->notice("Download of selected files was complete."); finishSelectiveDownloadingMode(); } else { - logger->info("The download has completed."); + logger->info("The download was complete."); } } diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc index 7e4cbe67..42ce6c63 100644 --- a/src/TrackerWatcherCommand.cc +++ b/src/TrackerWatcherCommand.cc @@ -85,7 +85,7 @@ bool TrackerWatcherCommand::execute() { break; } string url = e->torrentMan->announce+"?"+ - "info_hash="+Util::torrentUrlencode(e->torrentMan->getInfoHash(), 20)+"&"+ + "info_hash="+Util::torrentUrlencode((const char*)e->torrentMan->getInfoHash(), 20)+"&"+ "peer_id="+e->torrentMan->peerId+"&"+ "port="+Util::itos(e->torrentMan->getPort())+"&"+ "uploaded="+Util::llitos(e->torrentMan->getSessionUploadLength())+"&"+ diff --git a/src/Util.cc b/src/Util.cc index 330ca0fa..9415d658 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -166,7 +166,7 @@ string Util::replace(const string& target, const string& oldstr, const string& n return result; } -string Util::urlencode(const unsigned char* target, int len) { +string Util::urlencode(const char* target, int len) { string dest; for(int i = 0; i < len; i++) { if(!('0' <= target[i] && target[i] <= '9' || @@ -189,7 +189,7 @@ string Util::urlencode(const unsigned char* target, int len) { return dest; } -string Util::torrentUrlencode(const unsigned char* target, int len) { +string Util::torrentUrlencode(const char* target, int len) { string dest; for(int i = 0; i < len; i++) { if(isalpha(target[i]) || isdigit(target[i])) { @@ -204,6 +204,31 @@ string Util::torrentUrlencode(const unsigned char* target, int len) { return dest; } +string Util::urldecode(const string& target) { + string result; + for(string::const_iterator itr = target.begin(); + itr != target.end(); itr++) { + if(*itr == '+') { + result += ' '; + } else if(*itr == '%') { + if(itr+1 != target.end() && itr+2 != target.end() && + isxdigit(*(itr+1)) && isxdigit(*(itr+2))) { + char temp[3]; + temp[0] = *(itr+1); + temp[1] = *(itr+2); + temp[2] = '\0'; + result += strtol(temp, 0, 16); + itr += 2; + } else { + result += *itr; + } + } else { + result += *itr; + } + } + return result; +} + string Util::toHex(const unsigned char* src, int len) { char* temp = new char[len*2+1]; for(int i = 0; i < len; i++) { diff --git a/src/Util.h b/src/Util.h index c5d331a6..57e62012 100644 --- a/src/Util.h +++ b/src/Util.h @@ -62,9 +62,11 @@ public: static string replace(const string& target, const string& oldstr, const string& newstr); - static string urlencode(const unsigned char* target, int len); + static string urlencode(const char* target, int len); - static string torrentUrlencode(const unsigned char* target, int len); + static string urldecode(const string& target); + + static string torrentUrlencode(const char* target, int len); static string toHex(const unsigned char* src, int len); diff --git a/test/OptionTest.cc b/test/OptionTest.cc index 2d0e001b..3bb1b683 100644 --- a/test/OptionTest.cc +++ b/test/OptionTest.cc @@ -9,6 +9,7 @@ class OptionTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(OptionTest); CPPUNIT_TEST(testPutAndGet); CPPUNIT_TEST(testPutAndGetAsInt); + CPPUNIT_TEST(testPutAndGetAsDouble); CPPUNIT_TEST_SUITE_END(); private: @@ -18,6 +19,7 @@ public: void testPutAndGet(); void testPutAndGetAsInt(); + void testPutAndGetAsDouble(); }; @@ -38,3 +40,10 @@ void OptionTest::testPutAndGetAsInt() { CPPUNIT_ASSERT(op.defined("key")); CPPUNIT_ASSERT_EQUAL(1000, op.getAsInt("key")); } + +void OptionTest::testPutAndGetAsDouble() { + Option op; + op.put("key", "10.0"); + + CPPUNIT_ASSERT_EQUAL(10.0, op.getAsDouble("key")); +} diff --git a/test/ShareRatioSeedCriteriaTest.cc b/test/ShareRatioSeedCriteriaTest.cc index 9e11e117..f0ad36ca 100644 --- a/test/ShareRatioSeedCriteriaTest.cc +++ b/test/ShareRatioSeedCriteriaTest.cc @@ -22,7 +22,7 @@ void ShareRatioSeedCriteriaTest::testEvaluate() { ShareRatioSeedCriteria cri(1.0, &torrentMan); CPPUNIT_ASSERT(cri.evaluate()); - + cri.setRatio(2.0); CPPUNIT_ASSERT(!cri.evaluate()); // check div by zero diff --git a/test/UtilTest.cc b/test/UtilTest.cc index a7255f8f..3cdd68c1 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -20,6 +20,7 @@ class UtilTest:public CppUnit::TestFixture { CPPUNIT_TEST(testFileChecksum); CPPUNIT_TEST(testToUpper); CPPUNIT_TEST(testToLower); + CPPUNIT_TEST(testUrldecode); CPPUNIT_TEST_SUITE_END(); private: @@ -40,6 +41,7 @@ public: void testFileChecksum(); void testToUpper(); void testToLower(); + void testUrldecode(); }; @@ -248,3 +250,26 @@ void UtilTest::testToLower() { CPPUNIT_ASSERT_EQUAL(upp, Util::toLower(src)); } + +#include "SharedHandle.h" + +void UtilTest::testUrldecode() { + string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html"; + CPPUNIT_ASSERT_EQUAL(string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"), + Util::urldecode(src)); + + string src2 = "aria2+aria2"; + CPPUNIT_ASSERT_EQUAL(string("aria2 aria2"), Util::urldecode(src2)); + + string src3 = "%5t%20"; + CPPUNIT_ASSERT_EQUAL(string("%5t "), Util::urldecode(src3)); + + string src4 = "%"; + CPPUNIT_ASSERT_EQUAL(string("%"), Util::urldecode(src4)); + + string src5 = "%3"; + CPPUNIT_ASSERT_EQUAL(string("%3"), Util::urldecode(src5)); + + string src6 = "%2f"; + CPPUNIT_ASSERT_EQUAL(string("/"), Util::urldecode(src6)); +}