2008-12-30 04:54:07 +01:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2008-2016, Arvid Norberg
|
2008-12-30 04:54:07 +01: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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
#include <cinttypes> // for PRId64 et.al.
|
|
|
|
|
2016-08-02 23:58:15 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
2008-12-30 04:54:07 +01:00
|
|
|
#include "libtorrent/http_seed_connection.hpp"
|
|
|
|
#include "libtorrent/invariant_check.hpp"
|
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2009-11-26 06:45:43 +01:00
|
|
|
#include "libtorrent/peer_info.hpp"
|
2015-04-21 02:23:00 +02:00
|
|
|
#include "libtorrent/hex.hpp" // for is_hex
|
2008-12-30 04:54:07 +01:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2014-07-14 06:32:41 +02:00
|
|
|
http_seed_connection::http_seed_connection(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
|
|
|
: web_connection_base(pack, web)
|
2013-10-20 04:40:43 +02:00
|
|
|
, m_url(web.url)
|
2014-09-28 05:27:13 +02:00
|
|
|
, m_web(&web)
|
2012-05-03 04:05:34 +02:00
|
|
|
, m_response_left(0)
|
2010-10-31 04:05:11 +01:00
|
|
|
, m_chunk_pos(0)
|
|
|
|
, m_partial_chunk_header(0)
|
2008-12-30 04:54:07 +01:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!m_settings.get_bool(settings_pack::report_web_seed_downloads))
|
2010-07-15 08:27:44 +02:00
|
|
|
ignore_stats(true);
|
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<torrent> tor = pack.tor.lock();
|
2008-12-30 04:54:07 +01:00
|
|
|
TORRENT_ASSERT(tor);
|
|
|
|
int blocks_per_piece = tor->torrent_file().piece_length() / tor->block_size();
|
|
|
|
|
|
|
|
// multiply with the blocks per piece since that many requests are
|
|
|
|
// merged into one http request
|
2014-07-06 21:18:00 +02:00
|
|
|
max_out_request_queue(m_settings.get_int(settings_pack::urlseed_pipeline_size)
|
2013-11-26 08:47:48 +01:00
|
|
|
* blocks_per_piece);
|
2008-12-30 04:54:07 +01:00
|
|
|
|
2015-01-20 03:34:55 +01:00
|
|
|
prefer_contiguous_blocks(blocks_per_piece);
|
2010-10-10 20:43:58 +02:00
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
peer_log(peer_log_alert::info, "CONNECT", "http_seed_connection");
|
2008-12-30 04:54:07 +01:00
|
|
|
#endif
|
2009-08-08 17:27:07 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:20:13 +02:00
|
|
|
void http_seed_connection::disconnect(error_code const& ec
|
2015-02-15 06:17:09 +01:00
|
|
|
, operation_t op, int error)
|
2010-02-12 07:10:20 +01:00
|
|
|
{
|
2014-09-28 04:20:13 +02:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2015-02-15 06:17:09 +01:00
|
|
|
if (op == op_connect && m_web && !m_web->endpoints.empty())
|
2014-09-28 04:20:13 +02:00
|
|
|
{
|
|
|
|
// we failed to connect to this IP. remove it so that the next attempt
|
|
|
|
// uses the next IP in the list.
|
|
|
|
m_web->endpoints.erase(m_web->endpoints.begin());
|
|
|
|
}
|
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<torrent> t = associated_torrent().lock();
|
2014-07-06 21:18:00 +02:00
|
|
|
peer_connection::disconnect(ec, op, error);
|
2010-04-14 08:22:00 +02:00
|
|
|
if (t) t->disconnect_web_seed(this);
|
2010-02-12 07:10:20 +01:00
|
|
|
}
|
2016-05-22 01:05:42 +02:00
|
|
|
|
2016-09-04 18:31:02 +02:00
|
|
|
piece_block_progress http_seed_connection::downloading_piece_progress() const
|
2008-12-30 04:54:07 +01:00
|
|
|
{
|
|
|
|
if (m_requests.empty())
|
2016-09-04 18:31:02 +02:00
|
|
|
return piece_block_progress();
|
2008-12-30 04:54:07 +01:00
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<torrent> t = associated_torrent().lock();
|
2008-12-30 04:54:07 +01:00
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
|
|
|
|
piece_block_progress ret;
|
|
|
|
|
|
|
|
peer_request const& pr = m_requests.front();
|
|
|
|
ret.piece_index = pr.piece;
|
|
|
|
if (!m_parser.header_finished())
|
|
|
|
{
|
|
|
|
ret.bytes_downloaded = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-28 04:31:08 +02:00
|
|
|
int receive_buffer_size = int(m_recv_buffer.get().size()) - m_parser.body_start();
|
2013-01-21 00:21:53 +01:00
|
|
|
// TODO: 1 in chunked encoding mode, this assert won't hold.
|
2011-09-10 07:37:18 +02:00
|
|
|
// the chunk headers should be subtracted from the receive_buffer_size
|
2013-11-03 09:15:51 +01:00
|
|
|
TORRENT_ASSERT_VAL(receive_buffer_size <= t->block_size(), receive_buffer_size);
|
2008-12-30 04:54:07 +01:00
|
|
|
ret.bytes_downloaded = t->block_size() - receive_buffer_size;
|
|
|
|
}
|
2010-10-17 21:19:17 +02:00
|
|
|
// this is used to make sure that the block_index stays within
|
|
|
|
// bounds. If the entire piece is downloaded, the block_index
|
|
|
|
// would otherwise point to one past the end
|
2016-09-09 21:02:20 +02:00
|
|
|
int const correction = ret.bytes_downloaded ? -1 : 0;
|
2010-10-17 21:19:17 +02:00
|
|
|
ret.block_index = (pr.start + ret.bytes_downloaded + correction) / t->block_size();
|
2008-12-30 04:54:07 +01:00
|
|
|
ret.full_block_bytes = t->block_size();
|
2016-09-09 21:02:20 +02:00
|
|
|
int const last_piece = t->torrent_file().num_pieces() - 1;
|
2008-12-30 04:54:07 +01:00
|
|
|
if (ret.piece_index == last_piece && ret.block_index
|
|
|
|
== t->torrent_file().piece_size(last_piece) / t->block_size())
|
|
|
|
ret.full_block_bytes = t->torrent_file().piece_size(last_piece) % t->block_size();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_seed_connection::write_request(peer_request const& r)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<torrent> t = associated_torrent().lock();
|
2008-12-30 04:54:07 +01:00
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(t->valid_metadata());
|
|
|
|
// http_seeds don't support requesting more than one piece
|
|
|
|
// at a time
|
|
|
|
TORRENT_ASSERT(r.length <= t->torrent_file().piece_size(r.piece));
|
|
|
|
|
|
|
|
std::string request;
|
|
|
|
request.reserve(400);
|
|
|
|
|
|
|
|
int size = r.length;
|
|
|
|
const int block_size = t->block_size();
|
|
|
|
const int piece_size = t->torrent_file().piece_length();
|
|
|
|
peer_request pr;
|
|
|
|
while (size > 0)
|
|
|
|
{
|
|
|
|
int request_offset = r.start + r.length - size;
|
|
|
|
pr.start = request_offset % piece_size;
|
|
|
|
pr.length = (std::min)(block_size, size);
|
|
|
|
pr.piece = r.piece + request_offset / piece_size;
|
|
|
|
m_requests.push_back(pr);
|
|
|
|
size -= pr.length;
|
|
|
|
}
|
|
|
|
|
2014-07-13 00:32:55 +02:00
|
|
|
int proxy_type = m_settings.get_int(settings_pack::proxy_type);
|
2014-07-06 21:18:00 +02:00
|
|
|
bool using_proxy = (proxy_type == settings_pack::http
|
|
|
|
|| proxy_type == settings_pack::http_pw) && !m_ssl;
|
2008-12-30 04:54:07 +01:00
|
|
|
|
|
|
|
request += "GET ";
|
|
|
|
request += using_proxy ? m_url : m_path;
|
|
|
|
request += "?info_hash=";
|
2016-08-30 04:37:19 +02:00
|
|
|
request += escape_string({t->torrent_file().info_hash().data(), 20});
|
2008-12-30 04:54:07 +01:00
|
|
|
request += "&piece=";
|
2016-05-01 05:10:47 +02:00
|
|
|
request += to_string(r.piece).data();
|
2008-12-30 04:54:07 +01:00
|
|
|
|
|
|
|
// if we're requesting less than an entire piece we need to
|
|
|
|
// add ranges
|
|
|
|
if (r.start > 0 || r.length != t->torrent_file().piece_size(r.piece))
|
|
|
|
{
|
|
|
|
request += "&ranges=";
|
2016-05-01 05:10:47 +02:00
|
|
|
request += to_string(r.start).data();
|
2008-12-30 04:54:07 +01:00
|
|
|
request += "-";
|
2010-01-18 06:16:22 +01:00
|
|
|
// ranges are inclusive, just like HTTP
|
2016-05-01 05:10:47 +02:00
|
|
|
request += to_string(r.start + r.length - 1).data();
|
2008-12-30 04:54:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
request += " HTTP/1.1\r\n";
|
2014-07-13 00:32:55 +02:00
|
|
|
add_headers(request, m_settings, using_proxy);
|
2008-12-30 04:54:07 +01:00
|
|
|
request += "\r\n\r\n";
|
|
|
|
m_first_request = false;
|
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
peer_log(peer_log_alert::outgoing_message, "REQUEST", "%s", request.c_str());
|
2008-12-30 04:54:07 +01:00
|
|
|
#endif
|
|
|
|
|
2016-04-25 23:22:09 +02:00
|
|
|
send_buffer(request.c_str(), int(request.size()), message_type_request);
|
2008-12-30 04:54:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
// RECEIVE DATA
|
|
|
|
// --------------------------
|
|
|
|
|
|
|
|
void http_seed_connection::on_receive(error_code const& error
|
|
|
|
, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
2016-04-25 23:22:09 +02:00
|
|
|
received_bytes(0, int(bytes_transferred));
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-14 04:46:07 +02:00
|
|
|
if (should_log(peer_log_alert::info))
|
|
|
|
{
|
|
|
|
peer_log(peer_log_alert::info, "ERROR"
|
|
|
|
, "http_seed_connection error: %s", error.message().c_str());
|
|
|
|
}
|
2008-12-30 04:54:07 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<torrent> t = associated_torrent().lock();
|
2008-12-30 04:54:07 +01:00
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2016-07-28 04:31:08 +02:00
|
|
|
span<char const> recv_buffer = m_recv_buffer.get();
|
2008-12-30 04:54:07 +01:00
|
|
|
|
|
|
|
if (bytes_transferred == 0) break;
|
2016-07-28 04:31:08 +02:00
|
|
|
TORRENT_ASSERT(int(recv_buffer.size()) > 0);
|
2008-12-30 04:54:07 +01:00
|
|
|
|
|
|
|
TORRENT_ASSERT(!m_requests.empty());
|
|
|
|
if (m_requests.empty())
|
|
|
|
{
|
2016-04-25 23:22:09 +02:00
|
|
|
received_bytes(0, int(bytes_transferred));
|
2014-07-06 21:18:00 +02:00
|
|
|
disconnect(errors::http_error, op_bittorrent, 2);
|
2008-12-30 04:54:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer_request front_request = m_requests.front();
|
|
|
|
|
|
|
|
bool header_finished = m_parser.header_finished();
|
|
|
|
if (!header_finished)
|
|
|
|
{
|
2011-03-30 18:03:35 +02:00
|
|
|
bool parse_error = false;
|
2010-10-31 04:05:11 +01:00
|
|
|
int protocol = 0;
|
|
|
|
int payload = 0;
|
2016-07-28 04:31:08 +02:00
|
|
|
std::tie(payload, protocol) = m_parser.incoming(
|
|
|
|
recv_buffer, parse_error);
|
2014-07-06 21:18:00 +02:00
|
|
|
received_bytes(0, protocol);
|
2008-12-30 04:54:07 +01:00
|
|
|
bytes_transferred -= protocol;
|
2016-06-18 14:31:07 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2008-12-30 04:54:07 +01:00
|
|
|
if (payload > front_request.length) payload = front_request.length;
|
2013-06-14 18:41:47 +02:00
|
|
|
#endif
|
2008-12-30 04:54:07 +01:00
|
|
|
|
2011-03-30 18:03:35 +02:00
|
|
|
if (parse_error)
|
2008-12-30 04:54:07 +01:00
|
|
|
{
|
2016-04-25 23:22:09 +02:00
|
|
|
received_bytes(0, int(bytes_transferred));
|
2014-07-06 21:18:00 +02:00
|
|
|
disconnect(errors::http_parse_error, op_bittorrent, 2);
|
2008-12-30 04:54:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-28 04:31:08 +02:00
|
|
|
TORRENT_ASSERT(int(recv_buffer.size()) == 0 || recv_buffer.front() == 'H');
|
2016-05-22 01:05:42 +02:00
|
|
|
|
2016-07-28 04:31:08 +02:00
|
|
|
TORRENT_ASSERT(int(recv_buffer.size()) <= m_recv_buffer.packet_size());
|
2016-05-22 01:05:42 +02:00
|
|
|
|
2008-12-30 04:54:07 +01:00
|
|
|
// this means the entire status line hasn't been received yet
|
|
|
|
if (m_parser.status_code() == -1)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(payload == 0);
|
|
|
|
TORRENT_ASSERT(bytes_transferred == 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the status code is not one of the accepted ones, abort
|
2010-09-21 08:34:13 +02:00
|
|
|
if (!is_ok_status(m_parser.status_code()))
|
2008-12-30 04:54:07 +01:00
|
|
|
{
|
2010-09-21 08:34:13 +02:00
|
|
|
int retry_time = atoi(m_parser.header("retry-after").c_str());
|
|
|
|
if (retry_time <= 0) retry_time = 5 * 60;
|
|
|
|
// temporarily unavailable, retry later
|
|
|
|
t->retry_web_seed(this, retry_time);
|
|
|
|
|
2016-05-01 05:10:47 +02:00
|
|
|
std::string error_msg = to_string(m_parser.status_code()).data()
|
2009-01-27 07:17:55 +01:00
|
|
|
+ (" " + m_parser.message());
|
2014-07-06 21:18:00 +02:00
|
|
|
if (t->alerts().should_post<url_seed_alert>())
|
2008-12-30 04:54:07 +01:00
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
t->alerts().emplace_alert<url_seed_alert>(t->get_handle(), url()
|
|
|
|
, error_msg);
|
2008-12-30 04:54:07 +01:00
|
|
|
}
|
2016-04-25 23:22:09 +02:00
|
|
|
received_bytes(0, int(bytes_transferred));
|
2014-07-06 21:18:00 +02:00
|
|
|
disconnect(error_code(m_parser.status_code(), get_http_category()), op_bittorrent, 1);
|
2008-12-30 04:54:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!m_parser.header_finished())
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(payload == 0);
|
|
|
|
TORRENT_ASSERT(bytes_transferred == 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we just completed reading the header
|
|
|
|
if (!header_finished)
|
|
|
|
{
|
2010-09-21 08:34:13 +02:00
|
|
|
if (is_redirect(m_parser.status_code()))
|
2008-12-30 04:54:07 +01:00
|
|
|
{
|
|
|
|
// this means we got a redirection request
|
|
|
|
// look for the location header
|
|
|
|
std::string location = m_parser.header("location");
|
2016-04-25 23:22:09 +02:00
|
|
|
received_bytes(0, int(bytes_transferred));
|
2008-12-30 04:54:07 +01:00
|
|
|
|
|
|
|
if (location.empty())
|
|
|
|
{
|
|
|
|
// we should not try this server again.
|
2016-05-22 01:05:42 +02:00
|
|
|
t->remove_web_seed_conn(this, errors::missing_location, op_bittorrent, 2);
|
2008-12-30 04:54:07 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-22 01:05:42 +02:00
|
|
|
|
2008-12-30 04:54:07 +01:00
|
|
|
// add the redirected url and remove the current one
|
|
|
|
t->add_web_seed(location, web_seed_entry::http_seed);
|
2016-05-22 01:05:42 +02:00
|
|
|
t->remove_web_seed_conn(this, errors::redirecting, op_bittorrent, 2);
|
2008-12-30 04:54:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const& server_version = m_parser.header("server");
|
|
|
|
if (!server_version.empty())
|
|
|
|
{
|
|
|
|
m_server_string = "URL seed @ ";
|
|
|
|
m_server_string += m_host;
|
|
|
|
m_server_string += " (";
|
|
|
|
m_server_string += server_version;
|
|
|
|
m_server_string += ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
m_response_left = atol(m_parser.header("content-length").c_str());
|
|
|
|
if (m_response_left == -1)
|
|
|
|
{
|
2016-04-25 23:22:09 +02:00
|
|
|
received_bytes(0, int(bytes_transferred));
|
2008-12-30 04:54:07 +01:00
|
|
|
// we should not try this server again.
|
2016-05-22 01:05:42 +02:00
|
|
|
t->remove_web_seed_conn(this, errors::no_content_length, op_bittorrent, 2);
|
2008-12-30 04:54:07 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-10-30 21:45:50 +02:00
|
|
|
if (m_response_left != front_request.length)
|
|
|
|
{
|
2016-04-25 23:22:09 +02:00
|
|
|
received_bytes(0, int(bytes_transferred));
|
2010-10-30 21:45:50 +02:00
|
|
|
// we should not try this server again.
|
2016-05-22 01:05:42 +02:00
|
|
|
t->remove_web_seed_conn(this, errors::invalid_range, op_bittorrent, 2);
|
2010-10-30 21:45:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-12-30 04:54:07 +01:00
|
|
|
m_body_start = m_parser.body_start();
|
|
|
|
}
|
2010-10-31 04:05:11 +01:00
|
|
|
|
2016-07-28 04:31:08 +02:00
|
|
|
recv_buffer = recv_buffer.subspan(m_body_start);
|
2010-10-31 04:05:11 +01:00
|
|
|
|
|
|
|
// =========================
|
|
|
|
// === CHUNKED ENCODING ===
|
|
|
|
// =========================
|
|
|
|
while (m_parser.chunked_encoding()
|
|
|
|
&& m_chunk_pos >= 0
|
2016-07-28 04:31:08 +02:00
|
|
|
&& m_chunk_pos < int(recv_buffer.size()))
|
2008-12-30 04:54:07 +01:00
|
|
|
{
|
2010-10-31 04:05:11 +01:00
|
|
|
int header_size = 0;
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t chunk_size = 0;
|
2016-07-28 04:31:08 +02:00
|
|
|
span<char const> chunk_start(recv_buffer.begin() + m_chunk_pos, int(recv_buffer.size()) - m_chunk_pos);
|
2016-07-24 09:52:20 +02:00
|
|
|
TORRENT_ASSERT(chunk_start[0] == '\r'
|
2016-07-29 08:36:15 +02:00
|
|
|
|| aux::is_hex(chunk_start[0]));
|
2010-10-31 04:05:11 +01:00
|
|
|
bool ret = m_parser.parse_chunk_header(chunk_start, &chunk_size, &header_size);
|
|
|
|
if (!ret)
|
|
|
|
{
|
2016-07-24 09:52:20 +02:00
|
|
|
TORRENT_ASSERT(bytes_transferred >= size_t(chunk_start.size() - m_partial_chunk_header));
|
|
|
|
bytes_transferred -= chunk_start.size() - m_partial_chunk_header;
|
|
|
|
received_bytes(0, int(chunk_start.size() - m_partial_chunk_header));
|
|
|
|
m_partial_chunk_header = int(chunk_start.size());
|
2010-10-31 04:05:11 +01:00
|
|
|
if (bytes_transferred == 0) return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
peer_log(peer_log_alert::info, "CHUNKED_ENCODING"
|
|
|
|
, "parsed chunk: %" PRId64 " header_size: %d"
|
2015-04-25 06:22:51 +02:00
|
|
|
, chunk_size, header_size);
|
2010-10-31 04:05:11 +01:00
|
|
|
#endif
|
2011-02-15 11:05:25 +01:00
|
|
|
TORRENT_ASSERT(bytes_transferred >= size_t(header_size - m_partial_chunk_header));
|
2010-10-31 04:05:11 +01:00
|
|
|
bytes_transferred -= header_size - m_partial_chunk_header;
|
2011-03-24 17:46:26 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
received_bytes(0, header_size - m_partial_chunk_header);
|
2010-10-31 04:05:11 +01:00
|
|
|
m_partial_chunk_header = 0;
|
2016-07-24 09:52:20 +02:00
|
|
|
TORRENT_ASSERT(chunk_size != 0 || chunk_start.size() <= header_size || chunk_start[header_size] == 'H');
|
2010-10-31 04:05:11 +01:00
|
|
|
// cut out the chunk header from the receive buffer
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(m_chunk_pos + m_body_start < INT_MAX);
|
2014-11-23 07:14:47 +01:00
|
|
|
m_recv_buffer.cut(header_size, t->block_size() + 1024, int(m_chunk_pos + m_body_start));
|
|
|
|
recv_buffer = m_recv_buffer.get();
|
2016-07-28 04:31:08 +02:00
|
|
|
recv_buffer = recv_buffer.subspan(m_body_start);
|
2010-10-31 04:05:11 +01:00
|
|
|
m_chunk_pos += chunk_size;
|
|
|
|
if (chunk_size == 0)
|
|
|
|
{
|
2016-07-28 04:31:08 +02:00
|
|
|
TORRENT_ASSERT(int(m_recv_buffer.get().size()) < m_chunk_pos + m_body_start + 1
|
2014-11-23 07:14:47 +01:00
|
|
|
|| m_recv_buffer.get()[int(m_chunk_pos + m_body_start)] == 'H'
|
|
|
|
|| (m_parser.chunked_encoding() && m_recv_buffer.get()[int(m_chunk_pos + m_body_start)] == '\r'));
|
2010-10-31 04:05:11 +01:00
|
|
|
m_chunk_pos = -1;
|
|
|
|
}
|
|
|
|
}
|
2008-12-30 04:54:07 +01:00
|
|
|
}
|
2010-10-31 04:05:11 +01:00
|
|
|
|
2016-04-25 23:22:09 +02:00
|
|
|
int payload = int(bytes_transferred);
|
2011-02-21 06:24:41 +01:00
|
|
|
if (payload > m_response_left) payload = int(m_response_left);
|
2010-10-31 04:05:11 +01:00
|
|
|
if (payload > front_request.length) payload = front_request.length;
|
2014-07-06 21:18:00 +02:00
|
|
|
received_bytes(payload, 0);
|
2010-10-31 04:05:11 +01:00
|
|
|
incoming_piece_fragment(payload);
|
|
|
|
m_response_left -= payload;
|
2008-12-30 04:54:07 +01:00
|
|
|
|
|
|
|
if (m_parser.status_code() == 503)
|
|
|
|
{
|
|
|
|
if (!m_parser.finished()) return;
|
|
|
|
|
2016-07-28 04:31:08 +02:00
|
|
|
int retry_time = atol(std::string(recv_buffer.begin(), recv_buffer.end()).c_str());
|
2010-09-21 08:34:13 +02:00
|
|
|
if (retry_time <= 0) retry_time = 60;
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
peer_log(peer_log_alert::info, "CONNECT", "retrying in %d seconds", retry_time);
|
2008-12-30 04:54:07 +01:00
|
|
|
#endif
|
|
|
|
|
2016-04-25 23:22:09 +02:00
|
|
|
received_bytes(0, int(bytes_transferred));
|
2008-12-30 04:54:07 +01:00
|
|
|
// temporarily unavailable, retry later
|
2010-04-14 08:22:00 +02:00
|
|
|
t->retry_web_seed(this, retry_time);
|
2014-07-06 21:18:00 +02:00
|
|
|
disconnect(error_code(m_parser.status_code(), get_http_category()), op_bittorrent, 1);
|
2008-12-30 04:54:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-21 08:34:13 +02:00
|
|
|
|
2008-12-30 04:54:07 +01:00
|
|
|
// we only received the header, no data
|
2016-07-28 04:31:08 +02:00
|
|
|
if (int(recv_buffer.size()) == 0) break;
|
2008-12-30 04:54:07 +01:00
|
|
|
|
2016-07-28 04:31:08 +02:00
|
|
|
if (int(recv_buffer.size()) < front_request.length) break;
|
2008-12-30 04:54:07 +01:00
|
|
|
|
2010-10-31 04:05:11 +01:00
|
|
|
// if the response is chunked, we need to receive the last
|
|
|
|
// terminating chunk and the tail headers before we can proceed
|
|
|
|
if (m_parser.chunked_encoding() && m_chunk_pos >= 0) break;
|
|
|
|
|
2008-12-30 04:54:07 +01:00
|
|
|
m_requests.pop_front();
|
2016-07-28 04:31:08 +02:00
|
|
|
incoming_piece(front_request, recv_buffer.begin());
|
2008-12-30 04:54:07 +01:00
|
|
|
if (associated_torrent().expired()) return;
|
2010-10-31 04:05:11 +01:00
|
|
|
|
|
|
|
int size_to_cut = m_body_start + front_request.length;
|
2016-07-28 04:31:08 +02:00
|
|
|
TORRENT_ASSERT(int(m_recv_buffer.get().size()) < size_to_cut + 1
|
2014-11-23 07:14:47 +01:00
|
|
|
|| m_recv_buffer.get()[size_to_cut] == 'H'
|
|
|
|
|| (m_parser.chunked_encoding() && m_recv_buffer.get()[size_to_cut] == '\r'));
|
2010-10-31 04:05:11 +01:00
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
m_recv_buffer.cut(size_to_cut, t->block_size() + 1024);
|
2010-10-31 04:05:11 +01:00
|
|
|
if (m_response_left == 0) m_chunk_pos = 0;
|
|
|
|
else m_chunk_pos -= front_request.length;
|
2008-12-30 04:54:07 +01:00
|
|
|
bytes_transferred -= payload;
|
|
|
|
m_body_start = 0;
|
|
|
|
if (m_response_left > 0) continue;
|
|
|
|
TORRENT_ASSERT(m_response_left == 0);
|
|
|
|
m_parser.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_seed_connection::get_specific_peer_info(peer_info& p) const
|
|
|
|
{
|
2010-10-10 20:43:58 +02:00
|
|
|
web_connection_base::get_specific_peer_info(p);
|
2008-12-30 04:54:07 +01:00
|
|
|
p.flags |= peer_info::local_connection;
|
|
|
|
p.connection_type = peer_info::http_seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|