premiere-libtorrent/src/http_tracker_connection.cpp

846 lines
22 KiB
C++
Raw Normal View History

2004-01-31 11:46:15 +01:00
/*
Copyright (c) 2003, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <vector>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <sstream>
#include "zlib.h"
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <boost/bind.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
2004-01-31 11:46:15 +01:00
#include "libtorrent/tracker_manager.hpp"
#include "libtorrent/http_tracker_connection.hpp"
#include "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/torrent.hpp"
2004-03-12 17:42:33 +01:00
#include "libtorrent/io.hpp"
2004-01-31 11:46:15 +01:00
using namespace libtorrent;
using boost::bind;
2004-01-31 11:46:15 +01:00
namespace
{
enum
{
minimum_tracker_response_length = 3,
http_buffer_size = 2048
};
enum
{
FTEXT = 0x01,
FHCRC = 0x02,
FEXTRA = 0x04,
FNAME = 0x08,
FCOMMENT = 0x10,
FRESERVED = 0xe0,
GZIP_MAGIC0 = 0x1f,
GZIP_MAGIC1 = 0x8b
};
}
2004-10-14 03:17:04 +02:00
using namespace boost::posix_time;
2004-01-31 11:46:15 +01:00
namespace libtorrent
{
http_parser::http_parser()
: m_recv_pos(0)
, m_status_code(-1)
, m_content_length(-1)
, m_content_encoding(plain)
, m_state(read_status)
, m_recv_buffer(0, 0)
, m_body_start_pos(0)
, m_finished(false)
{}
boost::tuple<int, int> http_parser::incoming(buffer::const_interval recv_buffer)
{
m_recv_buffer = recv_buffer;
boost::tuple<int, int> ret(0, 0);
char const* pos = recv_buffer.begin + m_recv_pos;
if (m_state == read_status)
{
assert(!m_finished);
char const* newline = std::find(pos, recv_buffer.end, '\n');
// if we don't have a full line yet, wait.
if (newline == recv_buffer.end) return ret;
if (newline == pos)
throw std::runtime_error("unexpected newline in HTTP response");
std::istringstream line(std::string(pos, newline - 1));
++newline;
int incoming = (int)std::distance(pos, newline);
m_recv_pos += incoming;
boost::get<1>(ret) += incoming;
pos = newline;
line >> m_protocol;
if (m_protocol.substr(0, 5) != "HTTP/")
{
throw std::runtime_error("unknown protocol in HTTP response: "
+ m_protocol);
}
line >> m_status_code;
std::getline(line, m_server_message);
m_state = read_header;
}
if (m_state == read_header)
{
assert(!m_finished);
char const* newline = std::find(pos, recv_buffer.end, '\n');
std::string line;
while (newline != recv_buffer.end && m_state == read_header)
{
if (newline == pos)
throw std::runtime_error("unexpected newline in HTTP response");
line.assign(pos, newline - 1);
m_recv_pos += newline - pos;
boost::get<1>(ret) += newline - pos;
pos = newline;
std::string::size_type separator = line.find(": ");
if (separator == std::string::npos)
{
++pos;
++m_recv_pos;
boost::get<1>(ret) += 1;
m_state = read_body;
m_body_start_pos = m_recv_pos;
break;
}
std::string name = line.substr(0, separator);
std::string value = line.substr(separator + 2, std::string::npos);
m_header.insert(std::make_pair(name, value));
if (name == "Content-Length")
{
try
{
m_content_length = boost::lexical_cast<int>(value);
}
catch(boost::bad_lexical_cast&) {}
}
else if (name == "Content-Encoding")
{
if (value == "gzip" || value == "x-gzip")
{
m_content_encoding = gzip;
}
else
{
std::string error_str = "unknown content encoding in response: \"";
error_str += value;
error_str += "\"";
throw std::runtime_error(error_str);
}
}
// TODO: make sure we don't step outside of the buffer
++pos;
++m_recv_pos;
assert(m_recv_pos <= (int)recv_buffer.left());
newline = std::find(pos, recv_buffer.end, '\n');
}
}
if (m_state == read_body)
{
int incoming = recv_buffer.end - pos;
if (m_recv_pos - m_body_start_pos + incoming > m_content_length
&& m_content_length >= 0)
incoming = m_content_length - m_recv_pos + m_body_start_pos;
assert(incoming >= 0);
m_recv_pos += incoming;
boost::get<0>(ret) += incoming;
if (m_content_length >= 0
&& m_recv_pos - m_body_start_pos >= m_content_length)
{
m_finished = true;
}
}
return ret;
}
buffer::const_interval http_parser::get_body()
{
char const* body_begin = m_recv_buffer.begin + m_body_start_pos;
char const* body_end = m_recv_buffer.begin + m_recv_pos;
m_recv_pos = 0;
m_body_start_pos = 0;
m_status_code = -1;
m_content_length = -1;
m_finished = false;
m_state = read_status;
m_header.clear();
return buffer::const_interval(body_begin, body_end);
}
2004-01-31 11:46:15 +01:00
http_tracker_connection::http_tracker_connection(
demuxer& d
, tracker_manager& man
2004-07-25 22:57:44 +02:00
, tracker_request const& req
2004-01-31 11:46:15 +01:00
, std::string const& hostname
, unsigned short port
2005-03-11 18:21:56 +01:00
, std::string request
2004-09-16 03:14:16 +02:00
, boost::weak_ptr<request_callback> c
, session_settings const& stn
, std::string const& auth)
: tracker_connection(man, req, d, c)
2004-07-25 22:57:44 +02:00
, m_man(man)
2004-01-31 11:46:15 +01:00
, m_state(read_status)
, m_content_encoding(plain)
, m_content_length(0)
, m_name_lookup(d)
, m_port(port)
2004-01-31 11:46:15 +01:00
, m_recv_pos(0)
, m_buffer(http_buffer_size)
2005-08-25 15:11:39 +02:00
, m_settings(stn)
, m_password(auth)
2004-07-25 22:57:44 +02:00
, m_code(0)
, m_timed_out(false)
2004-01-31 11:46:15 +01:00
{
const std::string* connect_to_host;
bool using_proxy = false;
m_send_buffer.assign("GET ");
2004-01-31 11:46:15 +01:00
// should we use the proxy?
2005-08-25 15:11:39 +02:00
if (!m_settings.proxy_ip.empty())
2004-01-31 11:46:15 +01:00
{
2005-08-25 15:11:39 +02:00
connect_to_host = &m_settings.proxy_ip;
2004-01-31 11:46:15 +01:00
using_proxy = true;
m_send_buffer += "http://";
m_send_buffer += hostname;
if (port != 80)
{
m_send_buffer += ":";
2004-01-31 11:46:15 +01:00
m_send_buffer += boost::lexical_cast<std::string>(port);
}
m_port = m_settings.proxy_port != 0
? m_settings.proxy_port : 80 ;
}
else
{
connect_to_host = &hostname;
2004-01-31 11:46:15 +01:00
}
if (tracker_req().kind == tracker_request::scrape_request)
2005-03-11 18:21:56 +01:00
{
// find and replace "announce" with "scrape"
2005-03-11 18:21:56 +01:00
// in request
std::size_t pos = request.find("announce");
if (pos == std::string::npos)
throw std::runtime_error("scrape is not available on url: '"
+ tracker_req().url +"'");
2005-03-11 18:21:56 +01:00
request.replace(pos, 8, "scrape");
}
2004-01-31 11:46:15 +01:00
m_send_buffer += request;
2004-02-23 23:54:54 +01:00
2005-03-11 18:21:56 +01:00
// if request-string already contains
// some parameters, append an ampersand instead
// of a question mark
if (request.find('?') != std::string::npos)
m_send_buffer += "&";
else
m_send_buffer += "?";
m_send_buffer += "info_hash=";
2004-01-31 11:46:15 +01:00
m_send_buffer += escape_string(
reinterpret_cast<const char*>(req.info_hash.begin()), 20);
if (tracker_req().kind == tracker_request::announce_request)
2005-03-11 18:21:56 +01:00
{
m_send_buffer += "&peer_id=";
m_send_buffer += escape_string(
reinterpret_cast<const char*>(req.pid.begin()), 20);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
m_send_buffer += "&port=";
m_send_buffer += boost::lexical_cast<std::string>(req.listen_port);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
m_send_buffer += "&uploaded=";
m_send_buffer += boost::lexical_cast<std::string>(req.uploaded);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
m_send_buffer += "&downloaded=";
m_send_buffer += boost::lexical_cast<std::string>(req.downloaded);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
m_send_buffer += "&left=";
m_send_buffer += boost::lexical_cast<std::string>(req.left);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
if (req.event != tracker_request::none)
{
const char* event_string[] = {"completed", "started", "stopped"};
m_send_buffer += "&event=";
m_send_buffer += event_string[req.event - 1];
}
m_send_buffer += "&key=";
std::stringstream key_string;
key_string << std::hex << req.key;
m_send_buffer += key_string.str();
m_send_buffer += "&compact=1";
m_send_buffer += "&numwant=";
m_send_buffer += boost::lexical_cast<std::string>(
std::min(req.num_want, 999));
// extension that tells the tracker that
// we don't need any peer_id's in the response
m_send_buffer += "&no_peer_id=1";
2004-01-31 11:46:15 +01:00
}
2004-02-23 23:54:54 +01:00
2004-01-31 11:46:15 +01:00
m_send_buffer += " HTTP/1.0\r\nAccept-Encoding: gzip\r\n"
"User-Agent: ";
m_send_buffer += m_settings.user_agent;
m_send_buffer += "\r\n"
2004-01-31 11:46:15 +01:00
"Host: ";
m_send_buffer += hostname;
if (port != 80)
{
m_send_buffer += ':';
m_send_buffer += boost::lexical_cast<std::string>(port);
}
2005-08-25 15:11:39 +02:00
if (using_proxy && !m_settings.proxy_login.empty())
2004-01-31 11:46:15 +01:00
{
m_send_buffer += "\r\nProxy-Authorization: Basic ";
2005-08-25 15:11:39 +02:00
m_send_buffer += base64encode(m_settings.proxy_login + ":" + m_settings.proxy_password);
2004-01-31 11:46:15 +01:00
}
if (auth != "")
{
m_send_buffer += "\r\nAuthorization: Basic ";
m_send_buffer += base64encode(auth);
}
2004-01-31 11:46:15 +01:00
m_send_buffer += "\r\n\r\n";
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester())
{
2004-09-16 03:14:16 +02:00
requester().debug_log("==> TRACKER_REQUEST [ str: " + m_send_buffer + " ]");
std::stringstream info_hash_str;
info_hash_str << req.info_hash;
2004-09-16 03:14:16 +02:00
requester().debug_log("info_hash: " + info_hash_str.str() + "\n");
}
2005-03-19 13:22:40 +01:00
#endif
2006-06-18 01:42:01 +02:00
tcp::resolver::query q(*connect_to_host, "0");
m_name_lookup.async_resolve(q
, boost::bind(&http_tracker_connection::name_lookup, self(), _1, _2));
set_timeout(m_settings.tracker_completion_timeout
, m_settings.tracker_receive_timeout);
2004-01-31 11:46:15 +01:00
}
void http_tracker_connection::on_timeout()
2004-01-31 11:46:15 +01:00
{
m_timed_out = true;
m_socket.reset();
m_name_lookup.cancel();
fail_timeout();
}
void http_tracker_connection::name_lookup(asio::error_code const& error
, tcp::resolver::iterator i) try
{
if (error == asio::error::operation_aborted) return;
if (m_timed_out) return;
if (error || i == tcp::resolver::iterator())
2004-05-21 01:26:40 +02:00
{
fail(-1, error.message().c_str());
return;
}
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
if (has_requester()) requester().debug_log("tracker name lookup successful");
2004-05-21 01:26:40 +02:00
#endif
restart_read_timeout();
m_socket.reset(new stream_socket(m_name_lookup.io_service()));
tcp::endpoint a(i->endpoint().address(), m_port);
if (has_requester()) requester().m_tracker_address = a;
m_socket->async_connect(a, bind(&http_tracker_connection::connected, self(), _1));
}
catch (std::exception& e)
{
assert(false);
fail(-1, e.what());
2006-05-20 19:59:17 +02:00
};
void http_tracker_connection::connected(asio::error_code const& error) try
{
if (error == asio::error::operation_aborted) return;
if (m_timed_out) return;
if (error)
{
fail(-1, error.message().c_str());
return;
}
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
if (has_requester()) requester().debug_log("tracker connection successful");
#endif
restart_read_timeout();
async_write(*m_socket, asio::buffer(m_send_buffer.c_str()
, m_send_buffer.size()), bind(&http_tracker_connection::sent
, self(), _1));
}
catch (std::exception& e)
{
assert(false);
fail(-1, e.what());
}
2004-01-31 11:46:15 +01:00
void http_tracker_connection::sent(asio::error_code const& error) try
{
if (error == asio::error::operation_aborted) return;
if (m_timed_out) return;
if (error)
2004-01-31 11:46:15 +01:00
{
fail(-1, error.message().c_str());
return;
2004-01-31 11:46:15 +01:00
}
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
if (has_requester()) requester().debug_log("tracker send data completed");
2005-03-19 13:22:40 +01:00
#endif
restart_read_timeout();
assert(m_buffer.size() - m_recv_pos > 0);
m_socket->async_read_some(asio::buffer(&m_buffer[m_recv_pos]
, m_buffer.size() - m_recv_pos), bind(&http_tracker_connection::receive
, self(), _1, _2));
}
catch (std::exception& e)
{
assert(false);
fail(-1, e.what());
}; // msvc 7.1 seems to require this semi-colon
2004-01-31 11:46:15 +01:00
void http_tracker_connection::receive(asio::error_code const& error
, std::size_t bytes_transferred) try
{
if (error == asio::error::operation_aborted) return;
if (m_timed_out) return;
2004-01-31 11:46:15 +01:00
if (error)
{
if (error == asio::error::eof)
2004-01-31 11:46:15 +01:00
{
on_response();
close();
return;
2004-01-31 11:46:15 +01:00
}
fail(-1, error.message().c_str());
return;
2004-10-10 02:42:48 +02:00
}
2004-01-31 11:46:15 +01:00
restart_read_timeout();
assert(bytes_transferred > 0);
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
if (has_requester()) requester().debug_log("tracker connection reading "
+ boost::lexical_cast<std::string>(bytes_transferred));
2005-03-19 13:22:40 +01:00
#endif
2004-01-31 11:46:15 +01:00
m_recv_pos += bytes_transferred;
2004-01-31 11:46:15 +01:00
// if the receive buffer is full, expand it with http_buffer_size
if ((int)m_buffer.size() == m_recv_pos)
{
if ((int)m_buffer.size() >= m_settings.tracker_maximum_response_length)
2004-01-31 11:46:15 +01:00
{
fail(200, "too large tracker response");
return;
2004-01-31 11:46:15 +01:00
}
assert(http_buffer_size > 0);
if ((int)m_buffer.size() + http_buffer_size
> m_settings.tracker_maximum_response_length)
m_buffer.resize(m_settings.tracker_maximum_response_length);
else
m_buffer.resize(m_buffer.size() + http_buffer_size);
2004-01-31 11:46:15 +01:00
}
if (m_state == read_status)
{
std::vector<char>::iterator end = m_buffer.begin()+m_recv_pos;
std::vector<char>::iterator newline = std::find(m_buffer.begin(), end, '\n');
// if we don't have a full line yet, wait.
if (newline != end)
{
2004-01-31 11:46:15 +01:00
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
if (has_requester()) requester().debug_log(std::string(m_buffer.begin(), newline));
2005-03-19 13:22:40 +01:00
#endif
2004-01-31 11:46:15 +01:00
std::istringstream line(std::string(m_buffer.begin(), newline));
++newline;
m_recv_pos -= (int)std::distance(m_buffer.begin(), newline);
m_buffer.erase(m_buffer.begin(), newline);
2004-01-31 11:46:15 +01:00
std::string protocol;
line >> m_server_protocol;
if (m_server_protocol.substr(0, 5) != "HTTP/")
{
std::string error_msg = "unknown protocol in response: " + m_server_protocol;
fail(-1, error_msg.c_str());
return;
}
line >> m_code;
std::getline(line, m_server_message);
m_state = read_header;
2004-01-31 11:46:15 +01:00
}
}
if (m_state == read_header)
{
std::vector<char>::iterator end = m_buffer.begin() + m_recv_pos;
2004-08-08 23:26:40 +02:00
std::vector<char>::iterator newline
= std::find(m_buffer.begin(), end, '\n');
2004-01-31 11:46:15 +01:00
std::string line;
while (newline != end && m_state == read_header)
{
line.assign(m_buffer.begin(), newline);
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log(line);
2005-03-19 13:22:40 +01:00
#endif
2004-01-31 11:46:15 +01:00
if (line.substr(0, 16) == "Content-Length: ")
{
try
{
2004-08-08 23:26:40 +02:00
m_content_length = boost::lexical_cast<int>(
line.substr(16, line.length() - 17));
2004-01-31 11:46:15 +01:00
}
catch(boost::bad_lexical_cast&)
{
fail(-1, "invalid content-length in tracker response");
return;
2004-01-31 11:46:15 +01:00
}
2005-08-25 15:11:39 +02:00
if (m_content_length > m_settings.tracker_maximum_response_length)
2004-01-31 11:46:15 +01:00
{
fail(-1, "content-length is greater than maximum response length");
return;
2004-01-31 11:46:15 +01:00
}
2004-07-25 22:57:44 +02:00
if (m_content_length < minimum_tracker_response_length && m_code == 200)
2004-01-31 11:46:15 +01:00
{
fail(-1, "content-length is smaller than minimum response length");
return;
2004-01-31 11:46:15 +01:00
}
}
else if (line.substr(0, 18) == "Content-Encoding: ")
{
if (line.substr(18, 4) == "gzip" || line.substr(18, 6) == "x-gzip")
{
m_content_encoding = gzip;
}
else
{
std::string error_str = "unknown content encoding in response: \"";
error_str += line.substr(18, line.length() - 18 - 2);
error_str += "\"";
fail(-1, error_str.c_str());
return;
2004-01-31 11:46:15 +01:00
}
}
2004-07-25 22:57:44 +02:00
else if (line.substr(0, 10) == "Location: ")
{
m_location.assign(line.begin() + 10, line.end());
}
2004-08-08 23:26:40 +02:00
else if (line.substr(0, 7) == "Server: ")
{
m_server.assign(line.begin() + 7, line.end());
}
2004-01-31 11:46:15 +01:00
else if (line.size() < 3)
{
m_state = read_body;
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log("end of http header");
2005-03-19 13:22:40 +01:00
#endif
2004-07-25 22:57:44 +02:00
if (m_code >= 300 && m_code < 400)
{
if (m_location.empty())
{
std::string error_str = "got redirection response (";
error_str += boost::lexical_cast<std::string>(m_code);
error_str += ") without 'Location' header";
fail(-1, error_str.c_str());
return;
2004-07-25 22:57:44 +02:00
}
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log("Redirecting to \"" + m_location + "\"");
2004-07-25 22:57:44 +02:00
#endif
tracker_request req = tracker_req();
2004-07-25 22:57:44 +02:00
std::string::size_type i = m_location.find('?');
if (i == std::string::npos)
req.url = m_location;
2004-07-25 22:57:44 +02:00
else
req.url.assign(m_location.begin(), m_location.begin() + i);
2004-07-25 22:57:44 +02:00
m_man.queue_request(m_socket->io_service(), req
, m_password, m_requester);
close();
return;
2004-07-25 22:57:44 +02:00
}
2004-01-31 11:46:15 +01:00
}
++newline;
assert(m_recv_pos <= (int)m_buffer.size());
m_recv_pos -= (int)std::distance(m_buffer.begin(), newline);
m_buffer.erase(m_buffer.begin(), newline);
assert(m_recv_pos <= (int)m_buffer.size());
end = m_buffer.begin() + m_recv_pos;
newline = std::find(m_buffer.begin(), end, '\n');
}
}
if (m_state == read_body)
{
if (m_recv_pos == m_content_length)
2004-01-31 11:46:15 +01:00
{
on_response();
close();
return;
2004-01-31 11:46:15 +01:00
}
}
else if (m_recv_pos > m_content_length && m_content_length > 0)
{
fail(-1, "invalid tracker response (body > content_length)");
return;
}
assert(m_buffer.size() - m_recv_pos > 0);
m_socket->async_read_some(asio::buffer(&m_buffer[m_recv_pos]
, m_buffer.size() - m_recv_pos), bind(&http_tracker_connection::receive
, self(), _1, _2));
}
catch (std::exception& e)
{
assert(false);
fail(-1, e.what());
};
void http_tracker_connection::on_response()
{
// GZIP
if (m_content_encoding == gzip)
{
boost::shared_ptr<request_callback> r = m_requester.lock();
if (!r)
2004-01-31 11:46:15 +01:00
{
close();
return;
}
if (inflate_gzip(m_buffer, tracker_request(), r.get(),
m_settings.tracker_maximum_response_length))
{
close();
return;
2004-01-31 11:46:15 +01:00
}
}
// handle tracker response
try
{
entry e = bdecode(m_buffer.begin(), m_buffer.end());
parse(e);
2004-05-21 01:26:40 +02:00
}
catch (std::exception& e)
2004-05-21 01:26:40 +02:00
{
std::string error_str(e.what());
error_str += ": ";
error_str.append(m_buffer.begin(), m_buffer.end());
fail(m_code, error_str.c_str());
2004-05-21 01:26:40 +02:00
}
#ifndef NDEBUG
catch (...)
{
assert(false);
}
#endif
}
2004-01-31 11:46:15 +01:00
2004-03-17 13:14:44 +01:00
peer_entry http_tracker_connection::extract_peer_info(const entry& info)
2004-01-31 11:46:15 +01:00
{
peer_entry ret;
// extract peer id (if any)
2004-03-17 13:14:44 +01:00
entry const* i = info.find_key("peer id");
if (i != 0)
2004-01-31 11:46:15 +01:00
{
2004-03-17 13:14:44 +01:00
if (i->string().length() != 20)
throw std::runtime_error("invalid response from tracker");
std::copy(i->string().begin(), i->string().end(), ret.pid.begin());
2004-01-31 11:46:15 +01:00
}
else
{
// if there's no peer_id, just initialize it to a bunch of zeroes
std::fill_n(ret.pid.begin(), 20, 0);
2004-01-31 11:46:15 +01:00
}
// extract ip
2004-03-17 13:14:44 +01:00
i = info.find_key("ip");
if (i == 0) throw std::runtime_error("invalid response from tracker");
ret.ip = i->string();
2004-01-31 11:46:15 +01:00
// extract port
2004-03-17 13:14:44 +01:00
i = info.find_key("port");
if (i == 0) throw std::runtime_error("invalid response from tracker");
ret.port = (unsigned short)i->integer();
2004-01-31 11:46:15 +01:00
return ret;
}
void http_tracker_connection::parse(entry const& e)
2004-01-31 11:46:15 +01:00
{
2004-09-16 03:14:16 +02:00
if (!has_requester()) return;
2004-01-31 11:46:15 +01:00
try
{
// parse the response
2004-03-05 13:04:47 +01:00
try
2004-01-31 11:46:15 +01:00
{
entry const& failure = e["failure reason"];
2004-10-03 13:39:34 +02:00
fail(m_code, failure.string().c_str());
2004-10-03 13:39:34 +02:00
return;
2004-01-31 11:46:15 +01:00
}
catch (type_error const&) {}
2004-01-31 11:46:15 +01:00
2005-08-11 01:32:39 +02:00
try
{
entry const& warning = e["warning message"];
if (has_requester())
requester().tracker_warning(warning.string());
}
catch(type_error const&) {}
2005-03-11 18:21:56 +01:00
std::vector<peer_entry> peer_list;
if (tracker_req().kind == tracker_request::scrape_request)
2005-03-11 18:21:56 +01:00
{
std::string ih;
std::copy(tracker_req().info_hash.begin(), tracker_req().info_hash.end()
2005-03-11 18:21:56 +01:00
, std::back_inserter(ih));
entry scrape_data = e["files"][ih];
int complete = scrape_data["complete"].integer();
int incomplete = scrape_data["incomplete"].integer();
requester().tracker_response(tracker_request(), peer_list, 0, complete
2005-03-11 18:21:56 +01:00
, incomplete);
return;
}
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
int interval = (int)e["interval"].integer();
2004-01-31 11:46:15 +01:00
2004-03-12 17:42:33 +01:00
if (e["peers"].type() == entry::string_t)
2004-01-31 11:46:15 +01:00
{
2004-03-12 17:42:33 +01:00
std::string const& peers = e["peers"].string();
for (std::string::const_iterator i = peers.begin();
i != peers.end();)
{
if (std::distance(i, peers.end()) < 6) break;
peer_entry p;
p.pid.clear();
2004-03-12 17:42:33 +01:00
std::stringstream ip_str;
ip_str << (int)detail::read_uint8(i) << ".";
ip_str << (int)detail::read_uint8(i) << ".";
ip_str << (int)detail::read_uint8(i) << ".";
ip_str << (int)detail::read_uint8(i);
p.ip = ip_str.str();
p.port = detail::read_uint16(i);
peer_list.push_back(p);
}
}
else
{
entry::list_type const& l = e["peers"].list();
2004-03-12 17:42:33 +01:00
for(entry::list_type::const_iterator i = l.begin(); i != l.end(); ++i)
{
peer_entry p = extract_peer_info(*i);
peer_list.push_back(p);
}
2004-01-31 11:46:15 +01:00
}
// look for optional scrape info
int complete = -1;
int incomplete = -1;
2005-02-23 09:57:54 +01:00
try { complete = e["complete"].integer(); }
2005-06-11 01:12:50 +02:00
catch(type_error&) {}
2005-02-23 09:57:54 +01:00
try { incomplete = e["incomplete"].integer(); }
2005-06-11 01:12:50 +02:00
catch(type_error&) {}
requester().tracker_response(tracker_request(), peer_list, interval, complete
2005-02-23 09:57:54 +01:00
, incomplete);
2004-01-31 11:46:15 +01:00
}
catch(type_error& e)
{
requester().tracker_request_error(tracker_request(), m_code, e.what());
2004-01-31 11:46:15 +01:00
}
catch(std::runtime_error& e)
{
requester().tracker_request_error(tracker_request(), m_code, e.what());
2004-01-31 11:46:15 +01:00
}
}
}
2005-03-19 13:22:40 +01:00