2006-06-18 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

* src/TorrentMan.cc (setupInternal1): Fixed peerId generation 
bug.

	* src/SimpleLogger.h (writeHeader): New function.
	* src/SimpleLogger.cc (writeHeader): New function.
	(writeLog): Fixed the bug that causes segfaults if exception 
message
	contains an unescaped "%" character.

	* src/TrackerWatcherCommand.cc (execute): Added a short sleep
	when a tracker request fails.

	* src/Request.cc (parseUrl): Query string is now handled 
properly.
pull/1/head
Tatsuhiro Tsujikawa 2006-06-18 09:23:25 +00:00
parent d380b7b6ab
commit 70ff0c0b3b
9 changed files with 107 additions and 16 deletions

View File

@ -1,3 +1,19 @@
2006-06-18 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* src/TorrentMan.cc (setupInternal1): Fixed peerId generation bug.
* src/SimpleLogger.h (writeHeader): New function.
* src/SimpleLogger.cc (writeHeader): New function.
(writeLog): Fixed the bug that causes segfaults if exception message
contains an unescaped "%" character.
* src/TrackerWatcherCommand.cc (execute): Added a short sleep
when a tracker request fails.
* src/Request.cc (parseUrl): Query string is now handled properly.
* Release 0.5.1
2006-06-12 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2006-06-12 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To add Time class which represents a specific instant in time and To add Time class which represents a specific instant in time and

1
TODO
View File

@ -13,5 +13,4 @@
* Distinguish seeder from leecher * Distinguish seeder from leecher
* Add Mainline-compatible DHT support * Add Mainline-compatible DHT support
* Add Message stream encryption support * Add Message stream encryption support
* Add announce-list support
* Refacturing HttpConnection and FtpConnection * Refacturing HttpConnection and FtpConnection

View File

@ -57,6 +57,8 @@ bool Request::redirectUrl(const string& url) {
bool Request::parseUrl(const string& url) { bool Request::parseUrl(const string& url) {
currentUrl = url; currentUrl = url;
string tempUrl = url;
string query;
host = ""; host = "";
port = 0; port = 0;
dir = ""; dir = "";
@ -64,21 +66,26 @@ bool Request::parseUrl(const string& url) {
if(url.find_first_not_of(SAFE_CHARS) != string::npos) { if(url.find_first_not_of(SAFE_CHARS) != string::npos) {
return false; return false;
} }
string::size_type hp = url.find("://"); string::size_type startQueryIndex = tempUrl.find("?");
if(startQueryIndex != string::npos) {
query = tempUrl.substr(startQueryIndex);
tempUrl.erase(startQueryIndex);
}
string::size_type hp = tempUrl.find("://");
if(hp == string::npos) return false; if(hp == string::npos) return false;
protocol = url.substr(0, hp); protocol = tempUrl.substr(0, hp);
int defPort; int defPort;
if((defPort = defaultPorts[protocol]) == 0) { if((defPort = defaultPorts[protocol]) == 0) {
return false; return false;
} }
hp += 3; hp += 3;
if(url.size() <= hp) return false; if(tempUrl.size() <= hp) return false;
string::size_type hep = url.find("/", hp); string::size_type hep = tempUrl.find("/", hp);
if(hep == string::npos) { if(hep == string::npos) {
hep = url.size(); hep = tempUrl.size();
} }
pair<string, string> hostAndPort; pair<string, string> hostAndPort;
Util::split(hostAndPort, url.substr(hp, hep-hp), ':'); Util::split(hostAndPort, tempUrl.substr(hp, hep-hp), ':');
host = hostAndPort.first; host = hostAndPort.first;
if(hostAndPort.second != "") { if(hostAndPort.second != "") {
port = (int)strtol(hostAndPort.second.c_str(), NULL, 10); port = (int)strtol(hostAndPort.second.c_str(), NULL, 10);
@ -89,15 +96,16 @@ bool Request::parseUrl(const string& url) {
// If port is not specified, then we set it to default port of its protocol.. // If port is not specified, then we set it to default port of its protocol..
port = defPort; port = defPort;
} }
string::size_type direp = url.find_last_of("/"); string::size_type direp = tempUrl.find_last_of("/");
if(direp == string::npos || direp <= hep) { if(direp == string::npos || direp <= hep) {
dir = "/"; dir = "/";
direp = hep; direp = hep;
} else { } else {
dir = url.substr(hep, direp-hep); dir = tempUrl.substr(hep, direp-hep);
} }
if(url.size() > direp+1) { if(tempUrl.size() > direp+1) {
file = url.substr(direp+1); file = tempUrl.substr(direp+1);
} }
file += query;
return true; return true;
} }

View File

@ -34,8 +34,8 @@
SegmentMan::SegmentMan():totalSize(0), SegmentMan::SegmentMan():totalSize(0),
isSplittable(true), isSplittable(true),
downloadStarted(false), downloadStarted(false),
errors(0),
dir("."), dir("."),
errors(0),
splitter(NULL), splitter(NULL),
diskWriter(NULL) { diskWriter(NULL) {
logger = LogFactory::getInstance(); logger = LogFactory::getInstance();

View File

@ -63,6 +63,10 @@ void SimpleLogger::closeFile() {
} }
} }
void SimpleLogger::writeHeader(string date, string level) const {
fprintf(file, "%s - %s - ", date.c_str(), level.c_str());
}
void SimpleLogger::writeLog(int level, const char* msg, va_list ap, Exception* e) const void SimpleLogger::writeLog(int level, const char* msg, va_list ap, Exception* e) const
{ {
string levelStr; string levelStr;
@ -84,9 +88,11 @@ void SimpleLogger::writeLog(int level, const char* msg, va_list ap, Exception* e
char datestr[26]; char datestr[26];
ctime_r(&now, datestr); ctime_r(&now, datestr);
datestr[strlen(datestr)-1] = '\0'; datestr[strlen(datestr)-1] = '\0';
vfprintf(file, string(string(datestr)+" - "+levelStr+" - "+Util::replace(msg, "\r", "")+"\n").c_str(), ap); writeHeader(datestr, levelStr);
vfprintf(file, string(Util::replace(msg, "\r", "")+"\n").c_str(), ap);
if(e != NULL) { if(e != NULL) {
fprintf(file, string(string(datestr)+" - "+levelStr+" - exception: "+Util::replace(e->getMsg(), "\r", "")+"\n").c_str()); writeHeader(datestr, levelStr);
fprintf(file, "exception: %s\n", Util::replace(e->getMsg(), "\r", "").c_str());
} }
fflush(file); fflush(file);
} }

View File

@ -26,6 +26,7 @@
class SimpleLogger:public Logger { class SimpleLogger:public Logger {
private: private:
void writeHeader(string date, string level) const;
void writeLog(int level, const char* msg, va_list ap, Exception* e = NULL) const; void writeLog(int level, const char* msg, va_list ap, Exception* e = NULL) const;
FILE* file; FILE* file;
public: public:

View File

@ -399,7 +399,8 @@ void TorrentMan::readFileEntry(FileEntries& fileEntries, Directory** pTopDir, co
void TorrentMan::setupInternal1(const string& metaInfoFile) { void TorrentMan::setupInternal1(const string& metaInfoFile) {
peerId = "-aria2-"; peerId = "-aria2-";
for(int i = 0; i < 20-(int)peerId.size(); i++) { int randomSize = 20-peerId.size();
for(int i = 0; i < randomSize; i++) {
peerId += Util::itos((int)(((double)10)*random()/(RAND_MAX+1.0))); peerId += Util::itos((int)(((double)10)*random()/(RAND_MAX+1.0)));
} }

View File

@ -22,6 +22,8 @@
#include "TrackerWatcherCommand.h" #include "TrackerWatcherCommand.h"
#include "InitiateConnectionCommandFactory.h" #include "InitiateConnectionCommandFactory.h"
#include "Util.h" #include "Util.h"
#include "SleepCommand.h"
#include "prefs.h"
TrackerWatcherCommand::TrackerWatcherCommand(int cuid, TrackerWatcherCommand::TrackerWatcherCommand(int cuid,
TorrentDownloadEngine* e, TorrentDownloadEngine* e,
@ -35,6 +37,11 @@ bool TrackerWatcherCommand::execute() {
// we assume the tracker request has failed. // we assume the tracker request has failed.
e->torrentMan->trackers = 0; e->torrentMan->trackers = 0;
e->segmentMan->init(); e->segmentMan->init();
// sleep a few seconds.
SleepCommand* sleepCommand =
new SleepCommand(cuid, e, this, e->option->getAsInt(PREF_RETRY_WAIT));
e->commands.push_back(sleepCommand);
return false;
} }
if(e->torrentMan->trackers == 0 && if(e->torrentMan->trackers == 0 &&
(e->torrentMan->connections < MAX_PEER_UPDATE || (e->torrentMan->connections < MAX_PEER_UPDATE ||

View File

@ -15,6 +15,10 @@ class RequestTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testSetUrl8); CPPUNIT_TEST(testSetUrl8);
CPPUNIT_TEST(testSetUrl9); CPPUNIT_TEST(testSetUrl9);
CPPUNIT_TEST(testSetUrl10); CPPUNIT_TEST(testSetUrl10);
CPPUNIT_TEST(testSetUrl11);
CPPUNIT_TEST(testSetUrl12);
CPPUNIT_TEST(testSetUrl13);
CPPUNIT_TEST(testSetUrl14);
CPPUNIT_TEST(testRedirectUrl); CPPUNIT_TEST(testRedirectUrl);
CPPUNIT_TEST(testRedirectUrl2); CPPUNIT_TEST(testRedirectUrl2);
CPPUNIT_TEST(testResetUrl); CPPUNIT_TEST(testResetUrl);
@ -32,6 +36,10 @@ public:
void testSetUrl8(); void testSetUrl8();
void testSetUrl9(); void testSetUrl9();
void testSetUrl10(); void testSetUrl10();
void testSetUrl11();
void testSetUrl12();
void testSetUrl13();
void testSetUrl14();
void testRedirectUrl(); void testRedirectUrl();
void testRedirectUrl2(); void testRedirectUrl2();
void testResetUrl(); void testResetUrl();
@ -150,6 +158,51 @@ void RequestTest::testSetUrl10() {
CPPUNIT_ASSERT(!v); CPPUNIT_ASSERT(!v);
} }
void RequestTest::testSetUrl11() {
Request req;
bool v = req.setUrl("http://host?query/");
CPPUNIT_ASSERT(v);
CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
CPPUNIT_ASSERT_EQUAL(string("host"), req.getHost());
CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
CPPUNIT_ASSERT_EQUAL(string("?query/"), req.getFile());
}
void RequestTest::testSetUrl12() {
Request req;
bool v = req.setUrl("http://host?query");
CPPUNIT_ASSERT(v);
CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
CPPUNIT_ASSERT_EQUAL(string("host"), req.getHost());
CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
CPPUNIT_ASSERT_EQUAL(string("?query"), req.getFile());
}
void RequestTest::testSetUrl13() {
Request req;
bool v = req.setUrl("http://host/?query");
CPPUNIT_ASSERT(v);
CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
CPPUNIT_ASSERT_EQUAL(string("host"), req.getHost());
CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
CPPUNIT_ASSERT_EQUAL(string("?query"), req.getFile());
}
void RequestTest::testSetUrl14() {
Request req;
bool v = req.setUrl("http://host:8080/abc?query");
CPPUNIT_ASSERT(v);
CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
CPPUNIT_ASSERT_EQUAL(string("host"), req.getHost());
CPPUNIT_ASSERT_EQUAL(8080, req.getPort());
CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
CPPUNIT_ASSERT_EQUAL(string("abc?query"), req.getFile());
}
void RequestTest::testRedirectUrl() { void RequestTest::testRedirectUrl() {
Request req; Request req;
bool v = req.setUrl("http://aria.rednoah.com:8080/aria2/index.html"); bool v = req.setUrl("http://aria.rednoah.com:8080/aria2/index.html");