From 10b52bd9ac2ce48dfb45707a2cacdea20705d544 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 7 Mar 2010 14:24:21 +0000 Subject: [PATCH] 2010-03-07 Tatsuhiro Tsujikawa urlencode -> percentEncode, torrentUrlencode -> torrentPercentEncode * src/BtHandshakeMessage.cc * src/BtHandshakeMessageValidator.h * src/DHTMessageFactoryImpl.cc * src/DefaultBtAnnounce.cc * src/HandshakeExtensionMessage.cc * src/HttpRequest.cc * src/XmlRpcMethodImpl.cc * src/bittorrent_helper.cc * src/util.cc * src/util.h * test/DHTAnnouncePeerMessageTest.cc * test/DHTGetPeersMessageTest.cc * test/DHTGetPeersReplyMessageTest.cc * test/UTPexExtensionMessageTest.cc * test/UtilTest.cc --- ChangeLog | 20 ++++++++++++++++++++ src/BtHandshakeMessage.cc | 2 +- src/BtHandshakeMessageValidator.h | 3 ++- src/DHTMessageFactoryImpl.cc | 10 ++++++---- src/DefaultBtAnnounce.cc | 11 ++++++----- src/HandshakeExtensionMessage.cc | 4 ++-- src/HttpRequest.cc | 2 +- src/XmlRpcMethodImpl.cc | 4 ++-- src/bittorrent_helper.cc | 4 ++-- src/util.cc | 12 ++++++------ src/util.h | 8 ++++---- test/DHTAnnouncePeerMessageTest.cc | 4 ++-- test/DHTGetPeersMessageTest.cc | 4 ++-- test/DHTGetPeersReplyMessageTest.cc | 8 ++++---- test/UTPexExtensionMessageTest.cc | 4 ++-- test/UtilTest.cc | 12 ++++++------ 16 files changed, 68 insertions(+), 44 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2354bd33..6a3026ce 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2010-03-07 Tatsuhiro Tsujikawa + + urlencode -> percentEncode, + torrentUrlencode -> torrentPercentEncode + * src/BtHandshakeMessage.cc + * src/BtHandshakeMessageValidator.h + * src/DHTMessageFactoryImpl.cc + * src/DefaultBtAnnounce.cc + * src/HandshakeExtensionMessage.cc + * src/HttpRequest.cc + * src/XmlRpcMethodImpl.cc + * src/bittorrent_helper.cc + * src/util.cc + * src/util.h + * test/DHTAnnouncePeerMessageTest.cc + * test/DHTGetPeersMessageTest.cc + * test/DHTGetPeersReplyMessageTest.cc + * test/UTPexExtensionMessageTest.cc + * test/UtilTest.cc + 2010-03-07 Tatsuhiro Tsujikawa Fixed the bug that filename is percent encoded where it must be diff --git a/src/BtHandshakeMessage.cc b/src/BtHandshakeMessage.cc index 715a717c..7f44c751 100644 --- a/src/BtHandshakeMessage.cc +++ b/src/BtHandshakeMessage.cc @@ -104,7 +104,7 @@ size_t BtHandshakeMessage::getMessageLength() { std::string BtHandshakeMessage::toString() const { return strconcat(NAME, " peerId=", - util::urlencode(peerId, PEER_ID_LENGTH), + util::percentEncode(peerId, PEER_ID_LENGTH), ", reserved=", util::toHex(reserved, RESERVED_LENGTH)); } diff --git a/src/BtHandshakeMessageValidator.h b/src/BtHandshakeMessageValidator.h index 37a97631..62d74b4a 100644 --- a/src/BtHandshakeMessageValidator.h +++ b/src/BtHandshakeMessageValidator.h @@ -66,7 +66,8 @@ public: if(memcmp(BtHandshakeMessage::BT_PSTR, message->getPstr(), 19) != 0) { throw DL_ABORT_EX (StringFormat("invalid handshake pstr=%s", - util::urlencode(message->getPstr(), 19).c_str()).str()); + util::percentEncode + (message->getPstr(), 19).c_str()).str()); } if(memcmp(infoHash, message->getInfoHash(), 20) != 0) { throw DL_ABORT_EX diff --git a/src/DHTMessageFactoryImpl.cc b/src/DHTMessageFactoryImpl.cc index e498aeb1..aaca709c 100644 --- a/src/DHTMessageFactoryImpl.cc +++ b/src/DHTMessageFactoryImpl.cc @@ -232,9 +232,11 @@ DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType, // for now, just report error message arrived and throw exception. const BDE& e = getList(dict, DHTUnknownMessage::E); if(e.size() == 2) { - _logger->info("Received Error DHT message. code=%s, msg=%s", - util::itos(getInteger(e, 0).i()).c_str(), - util::urlencode(getString(e, 1).s()).c_str()); + if(_logger->info()) { + _logger->info("Received Error DHT message. code=%s, msg=%s", + util::itos(getInteger(e, 0).i()).c_str(), + util::percentEncode(getString(e, 1).s()).c_str()); + } } else { if(_logger->debug()) { _logger->debug("e doesn't have 2 elements."); @@ -244,7 +246,7 @@ DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType, } else if(y.s() != DHTResponseMessage::R) { throw DL_ABORT_EX (StringFormat("Malformed DHT message. y != r: y=%s", - util::urlencode(y.s()).c_str()).str()); + util::percentEncode(y.s()).c_str()).str()); } const BDE& rDict = getDictionary(dict, DHTResponseMessage::R); const BDE& id = getString(rDict, DHTMessage::ID); diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index e77550f1..cfecf3a2 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -144,10 +144,11 @@ std::string DefaultBtAnnounce::getAnnounceUrl() { std::string uri = announceList.getAnnounce(); uri += uriHasQuery(uri) ? "&" : "?"; uri += "info_hash="; - uri += util::torrentUrlencode(bittorrent::getInfoHash(_downloadContext), - INFO_HASH_LENGTH); + uri += util::torrentPercentEncode(bittorrent::getInfoHash(_downloadContext), + INFO_HASH_LENGTH); uri += "&peer_id="; - uri += util::torrentUrlencode(bittorrent::getStaticPeerId(), PEER_ID_LENGTH); + uri += util::torrentPercentEncode(bittorrent::getStaticPeerId(), + PEER_ID_LENGTH); uri += "&uploaded="; uri += util::uitos(stat.getSessionUploadLength()); uri += "&downloaded="; @@ -158,7 +159,7 @@ std::string DefaultBtAnnounce::getAnnounceUrl() { uri += "&key="; // Use last 8 bytes of peer ID as a key size_t keyLen = 8; - uri += util::torrentUrlencode + uri += util::torrentPercentEncode (bittorrent::getStaticPeerId()+PEER_ID_LENGTH-keyLen, keyLen); uri += "&numwant="; uri += util::uitos(numWant); @@ -173,7 +174,7 @@ std::string DefaultBtAnnounce::getAnnounceUrl() { uri += event; } if(!trackerId.empty()) { - uri += "&trackerid="+util::torrentUrlencode(trackerId); + uri += "&trackerid="+util::torrentPercentEncode(trackerId); } if(option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) { uri += "&requirecrypto=1"; diff --git a/src/HandshakeExtensionMessage.cc b/src/HandshakeExtensionMessage.cc index 4637bb3e..e89483c9 100644 --- a/src/HandshakeExtensionMessage.cc +++ b/src/HandshakeExtensionMessage.cc @@ -83,7 +83,7 @@ std::string HandshakeExtensionMessage::toString() const { std::string s = getExtensionName(); if(!_clientVersion.empty()) { - strappend(s, " client=", util::urlencode(_clientVersion)); + strappend(s, " client=", util::percentEncode(_clientVersion)); } if(_tcpPort > 0) { strappend(s, ", tcpPort=", util::uitos(_tcpPort)); @@ -166,7 +166,7 @@ HandshakeExtensionMessage::create(const unsigned char* data, size_t length) if(LogFactory::getInstance()->debug()) { LogFactory::getInstance()->debug ("Creating HandshakeExtensionMessage from %s", - util::urlencode(data, length).c_str()); + util::percentEncode(data, length).c_str()); } const BDE dict = bencode::decode(data+1, length-1); if(!dict.isDict()) { diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index cfd76bef..e3123eb6 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -140,7 +140,7 @@ std::string HttpRequest::createRequest() // Insert user into URI, like ftp://USER@host/ std::string uri = getCurrentURI(); assert(uri.size() >= 6); - uri.insert(6, util::urlencode(_authConfig->getUser())+"@"); + uri.insert(6, util::percentEncode(_authConfig->getUser())+"@"); requestLine += uri; } else { requestLine += getCurrentURI(); diff --git a/src/XmlRpcMethodImpl.cc b/src/XmlRpcMethodImpl.cc index dd49d87a..46ab83d6 100644 --- a/src/XmlRpcMethodImpl.cc +++ b/src/XmlRpcMethodImpl.cc @@ -474,8 +474,8 @@ static void gatherPeer(BDE& peers, const SharedHandle& ps) for(std::vector >::const_iterator i = activePeers.begin(), eoi = activePeers.end(); i != eoi; ++i) { BDE peerEntry = BDE::dict(); - peerEntry[KEY_PEER_ID] = util::torrentUrlencode((*i)->getPeerId(), - PEER_ID_LENGTH); + peerEntry[KEY_PEER_ID] = util::torrentPercentEncode((*i)->getPeerId(), + PEER_ID_LENGTH); peerEntry[KEY_IP] = (*i)->ipaddr; peerEntry[KEY_PORT] = util::uitos((*i)->port); peerEntry[KEY_BITFIELD] = util::toHex((*i)->getBitfield(), diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index eaffd9fa..864a5396 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -940,7 +940,7 @@ std::string torrent2Magnet(const BDE& attrs) } if(attrs.containsKey(NAME)) { uri += "&dn="; - uri += util::urlencode(attrs[NAME].s()); + uri += util::percentEncode(attrs[NAME].s()); } if(attrs.containsKey(ANNOUNCE_LIST)) { const BDE& tiers = attrs[ANNOUNCE_LIST]; @@ -949,7 +949,7 @@ std::string torrent2Magnet(const BDE& attrs) for(BDE::List::const_iterator uriiter = (*tieriter).listBegin(), eoi2 = (*tieriter).listEnd(); uriiter != eoi2; ++uriiter) { uri += "&tr="; - uri += util::urlencode((*uriiter).s()); + uri += util::percentEncode((*uriiter).s()); } } } diff --git a/src/util.cc b/src/util.cc index d69967c1..2481067c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -290,7 +290,7 @@ bool inRFC2616HttpToken(const char c) &chars[arrayLength(chars)]; } -std::string urlencode(const unsigned char* target, size_t len) { +std::string percentEncode(const unsigned char* target, size_t len) { std::string dest; for(size_t i = 0; i < len; ++i) { if(!inRFC3986UnreservedChars(target[i])) { @@ -302,13 +302,13 @@ std::string urlencode(const unsigned char* target, size_t len) { return dest; } -std::string urlencode(const std::string& target) +std::string percentEncode(const std::string& target) { - return urlencode(reinterpret_cast(target.c_str()), + return percentEncode(reinterpret_cast(target.c_str()), target.size()); } -std::string torrentUrlencode(const unsigned char* target, size_t len) { +std::string torrentPercentEncode(const unsigned char* target, size_t len) { std::string dest; for(size_t i = 0; i < len; ++i) { if(isAlpha(target[i]) || isDigit(target[i])) { @@ -320,9 +320,9 @@ std::string torrentUrlencode(const unsigned char* target, size_t len) { return dest; } -std::string torrentUrlencode(const std::string& target) +std::string torrentPercentEncode(const std::string& target) { - return torrentUrlencode + return torrentPercentEncode (reinterpret_cast(target.c_str()), target.size()); } diff --git a/src/util.h b/src/util.h index e98a9103..7819cb80 100644 --- a/src/util.h +++ b/src/util.h @@ -137,9 +137,9 @@ bool endsWith(const std::string& target, const std::string& part); std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr); -std::string urlencode(const unsigned char* target, size_t len); +std::string percentEncode(const unsigned char* target, size_t len); -std::string urlencode(const std::string& target); +std::string percentEncode(const std::string& target); bool inRFC3986ReservedChars(const char c); @@ -147,9 +147,9 @@ bool inRFC3986UnreservedChars(const char c); std::string urldecode(const std::string& target); -std::string torrentUrlencode(const unsigned char* target, size_t len); +std::string torrentPercentEncode(const unsigned char* target, size_t len); -std::string torrentUrlencode(const std::string& target); +std::string torrentPercentEncode(const std::string& target); std::string toHex(const unsigned char* src, size_t len); diff --git a/test/DHTAnnouncePeerMessageTest.cc b/test/DHTAnnouncePeerMessageTest.cc index dc3a8b8c..2a7fc46f 100644 --- a/test/DHTAnnouncePeerMessageTest.cc +++ b/test/DHTAnnouncePeerMessageTest.cc @@ -70,8 +70,8 @@ void DHTAnnouncePeerMessageTest::testGetBencodedMessage() aDict["token"] = token; dict["a"] = aDict; - CPPUNIT_ASSERT_EQUAL(util::urlencode(bencode::encode(dict)), - util::urlencode(msgbody)); + CPPUNIT_ASSERT_EQUAL(util::percentEncode(bencode::encode(dict)), + util::percentEncode(msgbody)); } void DHTAnnouncePeerMessageTest::testDoReceivedAction() diff --git a/test/DHTGetPeersMessageTest.cc b/test/DHTGetPeersMessageTest.cc index c082fb93..c984e0a3 100644 --- a/test/DHTGetPeersMessageTest.cc +++ b/test/DHTGetPeersMessageTest.cc @@ -89,8 +89,8 @@ void DHTGetPeersMessageTest::testGetBencodedMessage() aDict["info_hash"] = BDE(infoHash, DHT_ID_LENGTH); dict["a"] = aDict; - CPPUNIT_ASSERT_EQUAL(util::urlencode(bencode::encode(dict)), - util::urlencode(msgbody)); + CPPUNIT_ASSERT_EQUAL(util::percentEncode(bencode::encode(dict)), + util::percentEncode(msgbody)); } void DHTGetPeersMessageTest::testDoReceivedAction() diff --git a/test/DHTGetPeersReplyMessageTest.cc b/test/DHTGetPeersReplyMessageTest.cc index 75571edd..12e5a312 100644 --- a/test/DHTGetPeersReplyMessageTest.cc +++ b/test/DHTGetPeersReplyMessageTest.cc @@ -70,8 +70,8 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage() rDict["nodes"] = compactNodeInfo; - CPPUNIT_ASSERT_EQUAL(util::urlencode(bencode::encode(dict)), - util::urlencode(msgbody)); + CPPUNIT_ASSERT_EQUAL(util::percentEncode(bencode::encode(dict)), + util::percentEncode(msgbody)); } rDict.removeKey("nodes"); { @@ -90,8 +90,8 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage() msg.setValues(peers); std::string msgbody = msg.getBencodedMessage(); - CPPUNIT_ASSERT_EQUAL(util::urlencode(bencode::encode(dict)), - util::urlencode(msgbody)); + CPPUNIT_ASSERT_EQUAL(util::percentEncode(bencode::encode(dict)), + util::percentEncode(msgbody)); } } diff --git a/test/UTPexExtensionMessageTest.cc b/test/UTPexExtensionMessageTest.cc index f7fe075b..13a38b62 100644 --- a/test/UTPexExtensionMessageTest.cc +++ b/test/UTPexExtensionMessageTest.cc @@ -96,8 +96,8 @@ void UTPexExtensionMessageTest::testGetBencodedData() std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+ "e"; std::string bd = msg.getPayload(); - CPPUNIT_ASSERT_EQUAL(util::urlencode(expected), - util::urlencode(bd)); + CPPUNIT_ASSERT_EQUAL(util::percentEncode(expected), + util::percentEncode(bd)); } void UTPexExtensionMessageTest::testToString() diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 3d807e83..b5169fbd 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -50,7 +50,7 @@ class UtilTest:public CppUnit::TestFixture { CPPUNIT_TEST(testItos); CPPUNIT_TEST(testUitos); CPPUNIT_TEST(testNtoh64); - CPPUNIT_TEST(testUrlencode); + CPPUNIT_TEST(testPercentEncode); CPPUNIT_TEST(testHtmlEscape); CPPUNIT_TEST(testJoinPath); CPPUNIT_TEST(testParseIndexPath); @@ -101,7 +101,7 @@ public: void testItos(); void testUitos(); void testNtoh64(); - void testUrlencode(); + void testPercentEncode(); void testHtmlEscape(); void testJoinPath(); void testParseIndexPath(); @@ -810,20 +810,20 @@ void UtilTest::testNtoh64() #endif // !WORDS_BIGENDIAN } -void UtilTest::testUrlencode() +void UtilTest::testPercentEncode() { CPPUNIT_ASSERT_EQUAL (std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"), - util::urlencode(":/?#[]@!%&'()*+,;=")); + util::percentEncode(":/?#[]@!%&'()*+,;=")); std::string unreserved = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789" "-._~"; - CPPUNIT_ASSERT_EQUAL(unreserved, util::urlencode(unreserved)); + CPPUNIT_ASSERT_EQUAL(unreserved, util::percentEncode(unreserved)); - CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::urlencode("1^A ")); + CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A ")); } void UtilTest::testHtmlEscape()