2006-08-28 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

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.
This commit is contained in:
Tatsuhiro Tsujikawa 2006-08-28 12:40:41 +00:00
parent 531d456950
commit c096a3a553
15 changed files with 104 additions and 17 deletions

View File

@ -1,3 +1,14 @@
2006-08-28 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
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 <tujikawa at rednoah dot com>
To add --seed-time and --seed-ratio command-line option:

4
TODO
View File

@ -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.

View File

@ -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();

View File

@ -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));
}

View File

@ -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);
}
}

View File

@ -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:

View File

@ -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() {

View File

@ -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>& t):obj(t.get()), ucount(t.getRefCount()) {
++*ucount;
}
template<class S>
SharedHandle(const SharedHandle<S>& t):obj(t.get()), ucount(t.getRefCount()) {
++*ucount;
@ -60,8 +62,18 @@ public:
}
}
SharedHandle<T>& operator=(const SharedHandle<T>& t) {
++*t.getRefCount();
if(--*ucount == 0) {
delete obj;
delete ucount;
}
obj = t.get();
ucount = t.getRefCount();
return *this;
}
template<class S>
SharedHandle<T>& operator=(const SharedHandle<S>& t) {
SharedHandle<T>& operator=(const SharedHandle<S>& t) {
++*t.getRefCount();
if(--*ucount == 0) {
delete obj;

View File

@ -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.");
}
}

View File

@ -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())+"&"+

View File

@ -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++) {

View File

@ -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);

View File

@ -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"));
}

View File

@ -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

View File

@ -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));
}