entry and scrape bug fix

This commit is contained in:
Arvid Norberg 2008-05-19 07:15:44 +00:00
parent 777954ab3e
commit 8613554c2a
4 changed files with 32 additions and 28 deletions

View File

@ -167,6 +167,8 @@ namespace libtorrent
#endif
entry* find_key(char const* key);
entry const* find_key(char const* key) const;
entry* find_key(std::string const& key);
entry const* find_key(std::string const& key) const;
void print(std::ostream& os, int indent = 0) const;

View File

@ -142,6 +142,9 @@ namespace libtorrent
iterator begin() { return m_number; }
iterator end() { return m_number+number_size; }
std::string to_string() const
{ return std::string((char const*)&m_number[0], number_size); }
private:
unsigned char m_number[number_size];

View File

@ -54,19 +54,6 @@ namespace
TORRENT_ASSERT(o);
o->~T();
}
struct compare_string
{
compare_string(char const* s): m_str(s) {}
bool operator()(
std::pair<std::string
, libtorrent::entry> const& e) const
{
return m_str && e.first == m_str;
}
char const* m_str;
};
}
namespace libtorrent
@ -94,6 +81,16 @@ namespace libtorrent
}
entry& entry::operator[](char const* key)
{
dictionary_type::iterator i = dict().find(key);
if (i != dict().end()) return i->second;
dictionary_type::iterator ret = dict().insert(
dict().begin()
, std::make_pair(key, entry()));
return ret->second;
}
entry& entry::operator[](std::string const& key)
{
dictionary_type::iterator i = dict().find(key);
if (i != dict().end()) return i->second;
@ -103,21 +100,11 @@ namespace libtorrent
return ret->second;
}
entry& entry::operator[](std::string const& key)
{
return (*this)[key.c_str()];
}
entry* entry::find_key(char const* key)
{
dictionary_type::iterator i = std::find_if(
dict().begin()
, dict().end()
, compare_string(key));
dictionary_type::iterator i = dict().find(key);
if (i == dict().end()) return 0;
return &i->second;
}
entry const* entry::find_key(char const* key) const
@ -127,6 +114,20 @@ namespace libtorrent
return &i->second;
}
entry* entry::find_key(std::string const& key)
{
dictionary_type::iterator i = dict().find(key);
if (i == dict().end()) return 0;
return &i->second;
}
entry const* entry::find_key(std::string const& key) const
{
dictionary_type::const_iterator i = dict().find(key);
if (i == dict().end()) return 0;
return &i->second;
}
#ifndef BOOST_NO_EXCEPTIONS
const entry& entry::operator[](char const* key) const
{

View File

@ -313,9 +313,7 @@ namespace libtorrent
if (tracker_req().kind == tracker_request::scrape_request)
{
std::string ih;
std::copy(tracker_req().info_hash.begin(), tracker_req().info_hash.end()
, std::back_inserter(ih));
std::string ih = tracker_req().info_hash.to_string();
entry const* files = e.find_key("files");
if (files == 0 || files->type() != entry::dictionary_t)
@ -324,7 +322,7 @@ namespace libtorrent
return;
}
entry const* scrape_data = e.find_key(ih.c_str());
entry const* scrape_data = files->find_key(ih);
if (scrape_data == 0 || scrape_data->type() != entry::dictionary_t)
{
fail(-1, "missing or invalid info-hash entry in scrape response");