2010-10-10 20:43:58 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, Arvid Norberg
|
2010-10-10 20:43:58 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-08-20 01:33:20 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2010-10-10 20:43:58 +02:00
|
|
|
#include <limits>
|
2016-07-10 05:17:55 +02:00
|
|
|
#include <cstdlib>
|
2010-10-10 20:43:58 +02:00
|
|
|
|
|
|
|
#include "libtorrent/web_connection_base.hpp"
|
|
|
|
#include "libtorrent/invariant_check.hpp"
|
|
|
|
#include "libtorrent/parse_url.hpp"
|
|
|
|
#include "libtorrent/peer_info.hpp"
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2010-10-10 20:43:58 +02:00
|
|
|
web_connection_base::web_connection_base(
|
2014-07-14 06:32:41 +02:00
|
|
|
peer_connection_args const& pack
|
2014-12-31 23:05:34 +01:00
|
|
|
, web_seed_t& web)
|
2014-07-14 06:32:41 +02:00
|
|
|
: peer_connection(pack)
|
2010-10-17 18:15:32 +02:00
|
|
|
, m_first_request(true)
|
|
|
|
, m_ssl(false)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_external_auth(web.auth)
|
|
|
|
, m_extra_headers(web.extra_headers)
|
|
|
|
, m_parser(http_parser::dont_parse_chunks)
|
2010-10-17 18:15:32 +02:00
|
|
|
, m_body_start(0)
|
2010-10-10 20:43:58 +02:00
|
|
|
{
|
2014-07-14 06:32:41 +02:00
|
|
|
TORRENT_ASSERT(&web.peer_info == pack.peerinfo);
|
2014-09-28 06:05:44 +02:00
|
|
|
// when going through a proxy, we don't necessarily have an endpoint here,
|
|
|
|
// since the proxy might be resolving the hostname, not us
|
|
|
|
TORRENT_ASSERT(web.endpoints.empty() || web.endpoints.front() == pack.endp);
|
2014-07-14 06:32:41 +02:00
|
|
|
|
2010-10-10 20:43:58 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2015-10-03 17:20:10 +02:00
|
|
|
TORRENT_ASSERT(is_outgoing());
|
|
|
|
|
2017-08-11 00:42:56 +02:00
|
|
|
TORRENT_ASSERT(!m_torrent.lock()->is_upload_only());
|
|
|
|
|
2010-10-10 20:43:58 +02:00
|
|
|
// we only want left-over bandwidth
|
2014-07-06 21:18:00 +02:00
|
|
|
// TODO: introduce a web-seed default class which has a low download priority
|
2015-10-03 17:20:10 +02:00
|
|
|
|
2010-10-10 20:43:58 +02:00
|
|
|
std::string protocol;
|
|
|
|
error_code ec;
|
2016-06-20 17:32:06 +02:00
|
|
|
std::tie(protocol, m_basic_auth, m_host, m_port, m_path)
|
2013-10-20 04:40:43 +02:00
|
|
|
= parse_url_components(web.url, ec);
|
2010-10-10 20:43:58 +02:00
|
|
|
TORRENT_ASSERT(!ec);
|
|
|
|
|
2013-02-26 06:57:29 +01:00
|
|
|
if (m_port == -1 && protocol == "http")
|
|
|
|
m_port = 80;
|
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2013-02-26 06:57:29 +01:00
|
|
|
if (protocol == "https")
|
|
|
|
{
|
|
|
|
m_ssl = true;
|
|
|
|
if (m_port == -1) m_port = 443;
|
|
|
|
}
|
2010-10-12 10:57:43 +02:00
|
|
|
#endif
|
|
|
|
|
2010-10-10 20:43:58 +02:00
|
|
|
if (!m_basic_auth.empty())
|
|
|
|
m_basic_auth = base64encode(m_basic_auth);
|
|
|
|
|
|
|
|
m_server_string = "URL seed @ ";
|
|
|
|
m_server_string += m_host;
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int web_connection_base::timeout() const
|
|
|
|
{
|
|
|
|
// since this is a web seed, change the timeout
|
|
|
|
// according to the settings.
|
|
|
|
return m_settings.get_int(settings_pack::urlseed_timeout);
|
|
|
|
}
|
|
|
|
|
2010-10-10 20:43:58 +02:00
|
|
|
void web_connection_base::start()
|
|
|
|
{
|
2017-03-01 05:25:41 +01:00
|
|
|
// avoid calling torrent::set_seed because it calls torrent::check_invariant
|
|
|
|
// which fails because the m_num_connecting count is not consistent until
|
|
|
|
// after we call peer_connection::start
|
|
|
|
m_upload_only = true;
|
2010-10-10 20:43:58 +02:00
|
|
|
peer_connection::start();
|
2017-08-11 00:42:56 +02:00
|
|
|
// disconnect_if_redundant must be called after start to keep
|
|
|
|
// m_num_connecting consistent
|
|
|
|
disconnect_if_redundant();
|
2010-10-10 20:43:58 +02:00
|
|
|
}
|
|
|
|
|
2016-07-10 13:34:45 +02:00
|
|
|
web_connection_base::~web_connection_base() = default;
|
2010-10-10 20:43:58 +02:00
|
|
|
|
|
|
|
void web_connection_base::on_connected()
|
|
|
|
{
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<torrent> t = associated_torrent().lock();
|
2010-10-10 20:43:58 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2016-07-10 13:34:45 +02:00
|
|
|
|
2010-10-10 20:43:58 +02:00
|
|
|
// it is always possible to request pieces
|
|
|
|
incoming_unchoke();
|
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
m_recv_buffer.reset(t->block_size() + 1024);
|
2010-10-10 20:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void web_connection_base::add_headers(std::string& request
|
2014-07-06 21:18:00 +02:00
|
|
|
, aux::session_settings const& sett, bool using_proxy) const
|
2010-10-10 20:43:58 +02:00
|
|
|
{
|
|
|
|
request += "Host: ";
|
|
|
|
request += m_host;
|
2016-09-19 00:56:12 +02:00
|
|
|
if ((m_first_request || m_settings.get_bool(settings_pack::always_send_user_agent))
|
2017-03-12 17:49:41 +01:00
|
|
|
&& !m_settings.get_bool(settings_pack::anonymous_mode))
|
|
|
|
{
|
2010-10-10 20:43:58 +02:00
|
|
|
request += "\r\nUser-Agent: ";
|
2014-07-06 21:18:00 +02:00
|
|
|
request += m_settings.get_str(settings_pack::user_agent);
|
2010-10-10 20:43:58 +02:00
|
|
|
}
|
2017-03-12 17:49:41 +01:00
|
|
|
if (!m_external_auth.empty())
|
|
|
|
{
|
2010-10-10 20:43:58 +02:00
|
|
|
request += "\r\nAuthorization: ";
|
|
|
|
request += m_external_auth;
|
2017-03-12 17:49:41 +01:00
|
|
|
}
|
|
|
|
else if (!m_basic_auth.empty())
|
|
|
|
{
|
2010-10-10 20:43:58 +02:00
|
|
|
request += "\r\nAuthorization: Basic ";
|
|
|
|
request += m_basic_auth;
|
|
|
|
}
|
2017-03-12 17:49:41 +01:00
|
|
|
if (sett.get_int(settings_pack::proxy_type) == settings_pack::http_pw)
|
|
|
|
{
|
2010-10-10 20:43:58 +02:00
|
|
|
request += "\r\nProxy-Authorization: Basic ";
|
2014-07-06 21:18:00 +02:00
|
|
|
request += base64encode(sett.get_str(settings_pack::proxy_username)
|
|
|
|
+ ":" + sett.get_str(settings_pack::proxy_password));
|
2010-10-10 20:43:58 +02:00
|
|
|
}
|
2017-03-14 17:55:13 +01:00
|
|
|
for (auto const& h : m_extra_headers)
|
2017-03-12 17:49:41 +01:00
|
|
|
{
|
|
|
|
request += "\r\n";
|
2017-03-14 17:55:13 +01:00
|
|
|
request += h.first;
|
2017-03-12 17:49:41 +01:00
|
|
|
request += ": ";
|
2017-03-14 17:55:13 +01:00
|
|
|
request += h.second;
|
2010-10-10 20:43:58 +02:00
|
|
|
}
|
|
|
|
if (using_proxy) {
|
|
|
|
request += "\r\nProxy-Connection: keep-alive";
|
|
|
|
}
|
|
|
|
if (m_first_request || using_proxy) {
|
|
|
|
request += "\r\nConnection: keep-alive";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
// RECEIVE DATA
|
|
|
|
// --------------------------
|
|
|
|
|
|
|
|
void web_connection_base::get_specific_peer_info(peer_info& p) const
|
|
|
|
{
|
|
|
|
if (is_interesting()) p.flags |= peer_info::interesting;
|
|
|
|
if (is_choked()) p.flags |= peer_info::choked;
|
|
|
|
if (!is_connecting() && m_server_string.empty())
|
|
|
|
p.flags |= peer_info::handshake;
|
2014-10-03 22:56:57 +02:00
|
|
|
if (is_connecting()) p.flags |= peer_info::connecting;
|
2010-10-10 20:43:58 +02:00
|
|
|
|
|
|
|
p.client = m_server_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool web_connection_base::in_handshake() const
|
|
|
|
{
|
|
|
|
return m_server_string.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void web_connection_base::on_sent(error_code const& error
|
|
|
|
, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
if (error) return;
|
2016-04-25 23:22:09 +02:00
|
|
|
sent_bytes(0, int(bytes_transferred));
|
2010-10-10 20:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2010-10-10 20:43:58 +02:00
|
|
|
void web_connection_base::check_invariant() const
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
TORRENT_ASSERT(m_num_pieces == std::count(
|
|
|
|
m_have_piece.begin()
|
|
|
|
, m_have_piece.end()
|
|
|
|
, true));
|
|
|
|
*/ }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|