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"
|
|
|
|
|
|
|
|
#include "libtorrent/tracker_manager.hpp"
|
|
|
|
#include "libtorrent/http_tracker_connection.hpp"
|
|
|
|
#include "libtorrent/udp_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;
|
|
|
|
|
|
|
|
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_tracker_connection::http_tracker_connection(
|
2004-07-25 22:57:44 +02:00
|
|
|
tracker_manager& man
|
|
|
|
, tracker_request const& req
|
2004-01-31 11:46:15 +01:00
|
|
|
, std::string const& hostname
|
|
|
|
, unsigned short port
|
|
|
|
, std::string const& request
|
2004-09-16 03:14:16 +02:00
|
|
|
, boost::weak_ptr<request_callback> c
|
2004-02-22 23:40:45 +01:00
|
|
|
, const http_settings& stn
|
2005-02-23 10:13:42 +01:00
|
|
|
, std::string const& auth)
|
2004-01-31 11:46:15 +01:00
|
|
|
: tracker_connection(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_recv_pos(0)
|
2004-10-14 03:17:04 +02:00
|
|
|
, m_request_time(second_clock::universal_time())
|
2004-01-31 11:46:15 +01:00
|
|
|
, m_settings(stn)
|
2004-07-25 22:57:44 +02:00
|
|
|
, m_req(req)
|
2005-02-23 10:13:42 +01:00
|
|
|
, m_password(auth)
|
2004-07-25 22:57:44 +02:00
|
|
|
, m_code(0)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
|
|
|
const std::string* connect_to_host;
|
|
|
|
bool using_proxy = false;
|
|
|
|
|
|
|
|
// should we use the proxy?
|
|
|
|
if (!m_settings.proxy_ip.empty())
|
|
|
|
{
|
|
|
|
connect_to_host = &m_settings.proxy_ip;
|
|
|
|
if (m_settings.proxy_port != 0) port = m_settings.proxy_port;
|
|
|
|
using_proxy = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connect_to_host = &hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this is a problem. DNS-lookup is blocking!
|
|
|
|
// (may block up to 5 seconds)
|
2004-02-26 01:27:06 +01:00
|
|
|
address a(connect_to_host->c_str(), port);
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester()) requester().m_tracker_address = a;
|
2004-01-31 11:46:15 +01:00
|
|
|
boost::shared_ptr<socket> s(new socket(socket::tcp, false));
|
|
|
|
s->connect(a);
|
|
|
|
|
|
|
|
m_send_buffer.assign("GET ");
|
|
|
|
if (using_proxy)
|
|
|
|
{
|
|
|
|
m_send_buffer += "http://";
|
|
|
|
m_send_buffer += hostname;
|
|
|
|
if (port != 80)
|
|
|
|
m_send_buffer += boost::lexical_cast<std::string>(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_send_buffer += request;
|
2004-02-23 23:54:54 +01:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
m_send_buffer += "?info_hash=";
|
|
|
|
m_send_buffer += escape_string(
|
|
|
|
reinterpret_cast<const char*>(req.info_hash.begin()), 20);
|
|
|
|
|
|
|
|
m_send_buffer += "&peer_id=";
|
|
|
|
m_send_buffer += escape_string(
|
|
|
|
reinterpret_cast<const char*>(req.id.begin()), 20);
|
|
|
|
|
|
|
|
m_send_buffer += "&port=";
|
|
|
|
m_send_buffer += boost::lexical_cast<std::string>(req.listen_port);
|
|
|
|
|
|
|
|
m_send_buffer += "&uploaded=";
|
|
|
|
m_send_buffer += boost::lexical_cast<std::string>(req.uploaded);
|
|
|
|
|
|
|
|
m_send_buffer += "&downloaded=";
|
|
|
|
m_send_buffer += boost::lexical_cast<std::string>(req.downloaded);
|
|
|
|
|
|
|
|
m_send_buffer += "&left=";
|
|
|
|
m_send_buffer += boost::lexical_cast<std::string>(req.left);
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2004-03-21 03:03:37 +01:00
|
|
|
m_send_buffer += "&key=";
|
2004-03-27 23:02:31 +01:00
|
|
|
std::stringstream key_string;
|
|
|
|
key_string << std::hex << req.key;
|
|
|
|
m_send_buffer += key_string.str();
|
2004-03-21 03:03:37 +01:00
|
|
|
m_send_buffer += "&compact=1";
|
2004-03-27 23:02:31 +01:00
|
|
|
m_send_buffer += "&numwant=";
|
2004-09-12 15:53:00 +02:00
|
|
|
m_send_buffer += boost::lexical_cast<std::string>(
|
|
|
|
std::min(req.num_want, 999));
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
// 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-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 += " (libtorrent)\r\n"
|
|
|
|
"Host: ";
|
|
|
|
m_send_buffer += hostname;
|
|
|
|
if (port != 80)
|
|
|
|
{
|
|
|
|
m_send_buffer += ':';
|
|
|
|
m_send_buffer += boost::lexical_cast<std::string>(port);
|
|
|
|
}
|
|
|
|
if (using_proxy && !m_settings.proxy_login.empty())
|
|
|
|
{
|
|
|
|
m_send_buffer += "\r\nProxy-Authorization: Basic ";
|
|
|
|
m_send_buffer += base64encode(m_settings.proxy_login + ":" + m_settings.proxy_password);
|
|
|
|
}
|
2005-02-23 10:13:42 +01:00
|
|
|
if (auth != "")
|
2004-02-22 23:40:45 +01:00
|
|
|
{
|
|
|
|
m_send_buffer += "\r\nAuthorization: Basic ";
|
2005-02-23 10:13:42 +01:00
|
|
|
m_send_buffer += base64encode(auth);
|
2004-02-22 23:40:45 +01:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
m_send_buffer += "\r\n\r\n";
|
|
|
|
#ifndef NDEBUG
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
requester().debug_log("==> TRACKER_REQUEST [ str: " + m_send_buffer + " ]");
|
2004-06-14 01:30:42 +02:00
|
|
|
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");
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
#endif
|
|
|
|
m_socket = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if this connection is finished and should be removed from
|
|
|
|
// the connections list.
|
|
|
|
bool http_tracker_connection::tick()
|
|
|
|
{
|
2004-05-21 01:26:40 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
using namespace boost::posix_time;
|
|
|
|
|
2004-10-14 03:17:04 +02:00
|
|
|
time_duration d = second_clock::universal_time() - m_request_time;
|
2004-01-31 11:46:15 +01:00
|
|
|
if (d > seconds(m_settings.tracker_timeout) ||
|
2004-09-16 03:14:16 +02:00
|
|
|
(!has_requester() && d > seconds(m_settings.stop_tracker_timeout)))
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester()) requester().tracker_request_timed_out();
|
2004-01-31 11:46:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester()) requester().debug_log("tracker connection tick");
|
2004-01-31 11:46:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// if we have a send buffer and the socket is ready for writing
|
|
|
|
// send the buffer
|
|
|
|
if (!m_send_buffer.empty() && m_socket->is_writable())
|
|
|
|
{
|
|
|
|
int sent = m_socket->send(m_send_buffer.c_str(),(int)m_send_buffer.size());
|
|
|
|
|
|
|
|
if (sent == (int)m_send_buffer.size())
|
|
|
|
{
|
2004-02-19 12:35:08 +01:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
m_send_buffer.erase(m_send_buffer.begin(), m_send_buffer.end());
|
|
|
|
#else
|
2004-01-31 11:46:15 +01:00
|
|
|
m_send_buffer.clear();
|
2004-02-19 12:35:08 +01:00
|
|
|
#endif
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
else if (sent > 0)
|
|
|
|
{
|
|
|
|
m_send_buffer.erase(
|
|
|
|
m_send_buffer.begin()
|
|
|
|
, m_send_buffer.begin() + sent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sent != 0)
|
2004-10-14 03:17:04 +02:00
|
|
|
m_request_time = second_clock::universal_time();
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
if (m_socket->has_error())
|
|
|
|
{
|
|
|
|
if (has_requester()) requester().tracker_request_error(
|
|
|
|
-1, "connection refused");
|
|
|
|
return true;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
// if the socket isn't ready for reading, there's no point in continuing
|
|
|
|
// trying to read from it
|
|
|
|
if (!m_socket->is_readable()) return false;
|
2004-10-14 03:17:04 +02:00
|
|
|
m_request_time = second_clock::universal_time();
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester()) requester().debug_log("tracker connection socket readable");
|
2004-01-31 11:46:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
requester().tracker_request_error(
|
2004-01-31 11:46:15 +01:00
|
|
|
200
|
|
|
|
, "too large tracker response");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
assert(http_buffer_size > 0);
|
|
|
|
m_buffer.resize(m_buffer.size() + http_buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
assert(m_recv_pos >= 0);
|
|
|
|
assert(m_recv_pos < (int)m_buffer.size());
|
|
|
|
int received = m_socket->receive(&m_buffer[m_recv_pos], (int)m_buffer.size() - m_recv_pos);
|
|
|
|
|
|
|
|
assert(received <= (int)m_buffer.size() - m_recv_pos);
|
|
|
|
|
|
|
|
if (received > 0) m_recv_pos += received;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester()) requester().debug_log("received: " + boost::lexical_cast<std::string>(m_recv_pos));
|
2004-01-31 11:46:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (m_state == read_status)
|
|
|
|
{
|
|
|
|
if (received <= 0)
|
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
requester().tracker_request_error(
|
2004-01-31 11:46:15 +01:00
|
|
|
-1
|
|
|
|
, "invalid tracker response, connection closed");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) return false;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester()) requester().debug_log(std::string(m_buffer.begin(), newline));
|
2004-01-31 11:46:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester()) requester().tracker_request_error(-1, error_msg.c_str());
|
2004-01-31 11:46:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
2004-07-25 22:57:44 +02:00
|
|
|
line >> m_code;
|
2004-01-31 11:46:15 +01:00
|
|
|
std::getline(line, m_server_message);
|
|
|
|
m_state = read_header;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_state == read_header)
|
|
|
|
{
|
|
|
|
if (received <= 0)
|
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
|
|
|
requester().tracker_request_error(-1, "invalid tracker "
|
2004-08-08 23:26:40 +02:00
|
|
|
"response, connection closed while reading header");
|
2004-01-31 11:46:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester()) requester().debug_log(line);
|
2004-01-31 11:46:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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));
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
catch(boost::bad_lexical_cast&)
|
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
requester().tracker_request_error(
|
2004-01-31 11:46:15 +01:00
|
|
|
-1,
|
|
|
|
"invalid content-length in tracker response");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (m_content_length > m_settings.tracker_maximum_response_length)
|
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
requester().tracker_request_error(
|
2004-01-31 11:46:15 +01:00
|
|
|
-1
|
|
|
|
, "content-length is greater than maximum response length");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
requester().tracker_request_error(
|
2004-01-31 11:46:15 +01:00
|
|
|
-1
|
|
|
|
, "content-length is smaller than minimum response length");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 += "\"";
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
|
|
|
requester().tracker_request_error(-1, error_str.c_str());
|
2004-01-31 11:46:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
#ifndef NDEBUG
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester()) requester().debug_log("end of http header");
|
2004-01-31 11:46:15 +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";
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
|
|
|
requester().tracker_request_error(m_code, error_str.c_str());
|
2004-07-25 22:57:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
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
|
|
|
|
std::string::size_type i = m_location.find('?');
|
|
|
|
if (i == std::string::npos)
|
|
|
|
m_req.url = m_location;
|
|
|
|
else
|
|
|
|
m_req.url.assign(m_location.begin(), m_location.begin() + i);
|
|
|
|
|
2005-02-23 10:13:42 +01:00
|
|
|
m_man.queue_request(m_req, m_password, m_requester);
|
2004-07-25 22:57:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
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 || received <= 0)
|
|
|
|
{
|
|
|
|
// GZIP
|
|
|
|
if (m_content_encoding == gzip)
|
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
boost::shared_ptr<request_callback> r = m_requester.lock();
|
|
|
|
if (!r) return true;
|
|
|
|
if (inflate_gzip(m_buffer, r.get(),
|
2004-01-31 11:46:15 +01:00
|
|
|
m_settings.tracker_maximum_response_length))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle tracker response
|
|
|
|
entry e = bdecode(m_buffer.begin(), m_buffer.end());
|
|
|
|
parse(e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (m_recv_pos > m_content_length && m_content_length > 0)
|
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
requester().tracker_request_error(
|
2004-01-31 11:46:15 +01:00
|
|
|
-1
|
|
|
|
, "invalid tracker response (body > content_length)");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2004-05-21 01:26:40 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
if (has_requester())
|
|
|
|
requester().debug_log(std::string(m_buffer.begin(), m_buffer.end()));
|
2004-05-21 01:26:40 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
#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.id.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.id.begin(), 20, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2005-02-21 14:59:24 +01:00
|
|
|
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
|
|
|
|
|
|
|
std::vector<peer_entry> peer_list;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// parse the response
|
|
|
|
|
2004-03-05 13:04:47 +01:00
|
|
|
try
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2005-02-21 14:59:24 +01:00
|
|
|
entry const& failure = e["failure reason"];
|
2004-10-03 13:39:34 +02:00
|
|
|
|
|
|
|
if (has_requester()) requester().tracker_request_error(
|
|
|
|
m_code, failure.string().c_str());
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2005-02-21 14:59:24 +01:00
|
|
|
catch (type_error const&) {}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
int interval = (int)e["interval"].integer();
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
peer_list.clear();
|
|
|
|
|
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.id.clear();
|
|
|
|
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
|
|
|
|
{
|
2005-02-21 14:59:24 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-02-21 14:59:24 +01:00
|
|
|
// look for optional crape info
|
|
|
|
int complete = -1;
|
|
|
|
int incomplete = -1;
|
2005-02-23 09:57:54 +01:00
|
|
|
|
|
|
|
try { complete = e["complete"].integer(); }
|
|
|
|
catch(type_error& e) {}
|
|
|
|
|
|
|
|
try { incomplete = e["incomplete"].integer(); }
|
2005-02-21 14:59:24 +01:00
|
|
|
catch(type_error& e) {}
|
|
|
|
|
|
|
|
requester().tracker_response(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)
|
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
requester().tracker_request_error(-1, e.what());
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
catch(std::runtime_error& e)
|
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
requester().tracker_request_error(-1, e.what());
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|