From 02c870da94c654212a4e21e17379d02cbd32bb79 Mon Sep 17 00:00:00 2001 From: Andrei Kurushin Date: Wed, 8 Feb 2017 15:58:04 +0300 Subject: [PATCH] fix disabled "close_redundant_connections" web_seed disconnect exception (#1676) fix disabled "close_redundant_connections" web_seed disconnect --- simulation/test_web_seed.cpp | 46 ++++++++++++++++++++++++++++++++++++ src/web_peer_connection.cpp | 9 ++++--- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/simulation/test_web_seed.cpp b/simulation/test_web_seed.cpp index 7c80fd373..b69d4536e 100644 --- a/simulation/test_web_seed.cpp +++ b/simulation/test_web_seed.cpp @@ -483,3 +483,49 @@ TORRENT_TEST(urlseed_timeout) TEST_EQUAL(timeout, true); } +// check for correct handle of unexpected http status response. +// with disabled "close_redundant_connections" alive web server connection +// may be closed in such manner. +TORRENT_TEST(no_close_redudant_webseed) +{ + using namespace libtorrent; + + file_storage fs; + fs.add_file("file1", 1); + lt::add_torrent_params params = ::create_torrent(fs); + params.url_seeds.push_back("http://2.2.2.2:8080/"); + + bool expected = false; + run_test( + [¶ms](lt::session& ses) + { + lt::settings_pack pack; + pack.set_bool(settings_pack::close_redundant_connections, false); + ses.apply_settings(pack); + ses.async_add_torrent(params); + }, + [](lt::session& ses, lt::alert const* alert) {}, + [&expected](sim::simulation& sim, lt::session& ses) + { + sim::asio::io_service web_server(sim, address_v4::from_string("2.2.2.2")); + // listen on port 8080 + sim::http_server http(web_server, 8080); + + http.register_handler("/file1" + , [&expected](std::string method, std::string req + , std::map&) + { + expected = true; + char const* extra_headers[4] = { "Content-Range: bytes 0-0/1\r\n", "", "", ""}; + + return sim::send_response(206, "Partial Content", 1, extra_headers). + append("A"). + append(sim::send_response(408, "REQUEST TIMEOUT", 0)); + }); + + sim.run(); + } + ); + + TEST_CHECK(expected); +} diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index 555c994be..3c602dbdd 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -789,9 +789,12 @@ void web_peer_connection::on_receive(error_code const& error // if the status code is not one of the accepted ones, abort if (!is_ok_status(m_parser.status_code())) { - file_request_t const& file_req = m_file_requests.front(); - m_web->have_files.resize(t->torrent_file().num_files(), true); - m_web->have_files.clear_bit(file_req.file_index); + if (!m_file_requests.empty()) + { + file_request_t const& file_req = m_file_requests.front(); + m_web->have_files.resize(t->torrent_file().num_files(), true); + m_web->have_files.clear_bit(file_req.file_index); + } handle_error(int(recv_buffer.size())); return; }