Use standard percent-encode for tracker request query

This commit is contained in:
Tatsuhiro Tsujikawa 2016-05-23 22:16:04 +09:00
parent 0b906f40f1
commit c57259f8e7
2 changed files with 40 additions and 41 deletions

View file

@ -161,25 +161,24 @@ std::string DefaultBtAnnounce::getAnnounceUrl()
const size_t keyLen = 8; const size_t keyLen = 8;
std::string uri = announceList_.getAnnounce(); std::string uri = announceList_.getAnnounce();
uri += uriHasQuery(uri) ? "&" : "?"; uri += uriHasQuery(uri) ? "&" : "?";
uri += uri += fmt("info_hash=%s&"
fmt("info_hash=%s&" "peer_id=%s&"
"peer_id=%s&" "uploaded=%" PRId64 "&"
"uploaded=%" PRId64 "&" "downloaded=%" PRId64 "&"
"downloaded=%" PRId64 "&" "left=%" PRId64 "&"
"left=%" PRId64 "&" "compact=1&"
"compact=1&" "key=%s&"
"key=%s&" "numwant=%d&"
"numwant=%d&" "no_peer_id=1",
"no_peer_id=1", util::percentEncode(bittorrent::getInfoHash(downloadContext_),
util::torrentPercentEncode(bittorrent::getInfoHash(downloadContext_), INFO_HASH_LENGTH).c_str(),
INFO_HASH_LENGTH).c_str(), util::percentEncode(bittorrent::getStaticPeerId(), PEER_ID_LENGTH)
util::torrentPercentEncode(bittorrent::getStaticPeerId(), .c_str(),
PEER_ID_LENGTH).c_str(), stat.getSessionUploadLength(), stat.getSessionDownloadLength(),
stat.getSessionUploadLength(), stat.getSessionDownloadLength(), left, left, util::percentEncode(bittorrent::getStaticPeerId() +
util::torrentPercentEncode(bittorrent::getStaticPeerId() + PEER_ID_LENGTH - keyLen,
PEER_ID_LENGTH - keyLen, keyLen).c_str(),
keyLen).c_str(), numWant);
numWant);
if (tcpPort_) { if (tcpPort_) {
uri += fmt("&port=%u", tcpPort_); uri += fmt("&port=%u", tcpPort_);
} }
@ -190,7 +189,7 @@ std::string DefaultBtAnnounce::getAnnounceUrl()
} }
if (!trackerId_.empty()) { if (!trackerId_.empty()) {
uri += "&trackerid="; uri += "&trackerid=";
uri += util::torrentPercentEncode(trackerId_); uri += util::percentEncode(trackerId_);
} }
if (option_->getAsBool(PREF_BT_FORCE_ENCRYPTION) || if (option_->getAsBool(PREF_BT_FORCE_ENCRYPTION) ||
option_->getAsBool(PREF_BT_REQUIRE_CRYPTO)) { option_->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {

View file

@ -158,7 +158,7 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost/" std::string("http://localhost/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=started&" "numwant=50&no_peer_id=1&port=6989&event=started&"
"supportcrypto=1"), "supportcrypto=1"),
@ -169,7 +169,7 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost/" std::string("http://localhost/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&supportcrypto=1"), "numwant=50&no_peer_id=1&port=6989&supportcrypto=1"),
btAnnounce.getAnnounceUrl()); btAnnounce.getAnnounceUrl());
@ -179,7 +179,7 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://backup/" std::string("http://backup/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=started&" "numwant=50&no_peer_id=1&port=6989&event=started&"
"supportcrypto=1"), "supportcrypto=1"),
@ -192,7 +192,7 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost/" std::string("http://localhost/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=completed&" "numwant=50&no_peer_id=1&port=6989&event=completed&"
"supportcrypto=1"), "supportcrypto=1"),
@ -203,7 +203,7 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://backup/" std::string("http://backup/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=completed&" "numwant=50&no_peer_id=1&port=6989&event=completed&"
"supportcrypto=1"), "supportcrypto=1"),
@ -216,7 +216,7 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost/" std::string("http://localhost/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=0&no_peer_id=1&port=6989&event=stopped&" "numwant=0&no_peer_id=1&port=6989&event=stopped&"
"supportcrypto=1"), "supportcrypto=1"),
@ -227,7 +227,7 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://backup/" std::string("http://backup/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=0&no_peer_id=1&port=6989&event=stopped&" "numwant=0&no_peer_id=1&port=6989&event=stopped&"
"supportcrypto=1"), "supportcrypto=1"),
@ -255,7 +255,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost/" std::string("http://localhost/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=started&" "numwant=50&no_peer_id=1&port=6989&event=started&"
"supportcrypto=1"), "supportcrypto=1"),
@ -281,7 +281,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost/" std::string("http://localhost/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&supportcrypto=1"), "numwant=50&no_peer_id=1&port=6989&supportcrypto=1"),
btAnnounce.getAnnounceUrl()); btAnnounce.getAnnounceUrl());
@ -296,7 +296,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost/" std::string("http://localhost/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=completed&" "numwant=50&no_peer_id=1&port=6989&event=completed&"
"supportcrypto=1"), "supportcrypto=1"),
@ -312,7 +312,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost/" std::string("http://localhost/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=0&no_peer_id=1&port=6989&event=stopped&" "numwant=0&no_peer_id=1&port=6989&event=stopped&"
"supportcrypto=1"), "supportcrypto=1"),
@ -339,7 +339,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl_withQuery()
std::string( std::string(
"http://localhost/announce?k=v&" "http://localhost/announce?k=v&"
"info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&" "info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&"
"peer_id=%2Daria2%2Dultrafastdltl&" "peer_id=-aria2-ultrafastdltl&"
"uploaded=1572864&downloaded=1310720&left=1572864&compact=1&" "uploaded=1572864&downloaded=1310720&left=1572864&compact=1&"
"key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&" "key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&"
"supportcrypto=1"), "supportcrypto=1"),
@ -364,7 +364,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl_externalIP()
std::string( std::string(
"http://localhost/announce?" "http://localhost/announce?"
"info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&" "info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&"
"peer_id=%2Daria2%2Dultrafastdltl&" "peer_id=-aria2-ultrafastdltl&"
"uploaded=1572864&downloaded=1310720&left=1572864&compact=1&" "uploaded=1572864&downloaded=1310720&left=1572864&compact=1&"
"key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&" "key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&"
"supportcrypto=1&ip=192.168.1.1"), "supportcrypto=1&ip=192.168.1.1"),
@ -395,7 +395,7 @@ void DefaultBtAnnounceTest::testIsAllAnnounceFailed()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost/" std::string("http://localhost/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=started&" "numwant=50&no_peer_id=1&port=6989&event=started&"
"supportcrypto=1"), "supportcrypto=1"),
@ -406,7 +406,7 @@ void DefaultBtAnnounceTest::testIsAllAnnounceFailed()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://backup/" std::string("http://backup/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=started&" "numwant=50&no_peer_id=1&port=6989&event=started&"
"supportcrypto=1"), "supportcrypto=1"),
@ -442,7 +442,7 @@ void DefaultBtAnnounceTest::testURLOrderInStoppedEvent()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost1/" std::string("http://localhost1/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=started&" "numwant=50&no_peer_id=1&port=6989&event=started&"
"supportcrypto=1"), "supportcrypto=1"),
@ -455,7 +455,7 @@ void DefaultBtAnnounceTest::testURLOrderInStoppedEvent()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost1/" std::string("http://localhost1/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=0&no_peer_id=1&port=6989&event=stopped&" "numwant=0&no_peer_id=1&port=6989&event=stopped&"
"supportcrypto=1"), "supportcrypto=1"),
@ -466,7 +466,7 @@ void DefaultBtAnnounceTest::testURLOrderInStoppedEvent()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost2/" std::string("http://localhost2/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=0&no_peer_id=1&port=6989&event=stopped&" "numwant=0&no_peer_id=1&port=6989&event=stopped&"
"supportcrypto=1"), "supportcrypto=1"),
@ -494,7 +494,7 @@ void DefaultBtAnnounceTest::testURLOrderInCompletedEvent()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost1/" std::string("http://localhost1/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=started&" "numwant=50&no_peer_id=1&port=6989&event=started&"
"supportcrypto=1"), "supportcrypto=1"),
@ -507,7 +507,7 @@ void DefaultBtAnnounceTest::testURLOrderInCompletedEvent()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost1/" std::string("http://localhost1/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=completed&" "numwant=50&no_peer_id=1&port=6989&event=completed&"
"supportcrypto=1"), "supportcrypto=1"),
@ -518,7 +518,7 @@ void DefaultBtAnnounceTest::testURLOrderInCompletedEvent()
CPPUNIT_ASSERT_EQUAL( CPPUNIT_ASSERT_EQUAL(
std::string("http://localhost2/" std::string("http://localhost2/"
"announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%" "announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%"
"01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&" "01%23Eg&peer_id=-aria2-ultrafastdltl&uploaded=1572864&"
"downloaded=1310720&left=1572864&compact=1&key=fastdltl&" "downloaded=1310720&left=1572864&compact=1&key=fastdltl&"
"numwant=50&no_peer_id=1&port=6989&event=completed&" "numwant=50&no_peer_id=1&port=6989&event=completed&"
"supportcrypto=1"), "supportcrypto=1"),