diff --git a/include/libtorrent/http_connection.hpp b/include/libtorrent/http_connection.hpp index 2a663fa04..ae090c0a3 100644 --- a/include/libtorrent/http_connection.hpp +++ b/include/libtorrent/http_connection.hpp @@ -60,7 +60,7 @@ struct http_connection; class connection_queue; typedef boost::function http_handler; + , http_parser const&, char const* data, int size, http_connection&)> http_handler; typedef boost::function http_connect_handler; diff --git a/include/libtorrent/upnp.hpp b/include/libtorrent/upnp.hpp index 7c69ffd5a..4b597071b 100644 --- a/include/libtorrent/upnp.hpp +++ b/include/libtorrent/upnp.hpp @@ -103,13 +103,14 @@ private: void on_upnp_xml(error_code const& e - , libtorrent::http_parser const& p, rootdevice& d); + , libtorrent::http_parser const& p, rootdevice& d + , http_connection& c); void on_upnp_map_response(error_code const& e , libtorrent::http_parser const& p, rootdevice& d - , int mapping); + , int mapping, http_connection& c); void on_upnp_unmap_response(error_code const& e , libtorrent::http_parser const& p, rootdevice& d - , int mapping); + , int mapping, http_connection& c); void on_expire(error_code const& e); void disable(char const* msg); diff --git a/src/http_connection.cpp b/src/http_connection.cpp index 54a8ad481..12fb2195f 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -307,7 +307,7 @@ void http_connection::callback(error_code const& e, char const* data, int size) std::string error; if (inflate_gzip(data, size, buf, max_bottled_buffer, error)) { - callback(asio::error::fault, data, size); + if (m_handler) m_handler(asio::error::fault, m_parser, data, size, *this); close(); return; } @@ -317,7 +317,7 @@ void http_connection::callback(error_code const& e, char const* data, int size) } m_called = true; m_timer.cancel(); - if (m_handler) m_handler(e, m_parser, data, size); + if (m_handler) m_handler(e, m_parser, data, size, *this); } } diff --git a/src/upnp.cpp b/src/upnp.cpp index 73a34b6ae..055605cf4 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -248,9 +248,10 @@ void upnp::resend_request(error_code const& e) m_log << time_now_string() << " ==> connecting to " << d.url << std::endl; #endif + if (d.upnp_connection) d.upnp_connection->close(); d.upnp_connection.reset(new http_connection(m_io_service , m_cc, bind(&upnp::on_upnp_xml, self(), _1, _2 - , boost::ref(d)))); + , boost::ref(d), _5))); d.upnp_connection->get(d.url, seconds(30), 1); } catch (std::exception& e) @@ -492,9 +493,10 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer m_log << time_now_string() << " ==> connecting to " << d.url << std::endl; #endif + if (d.upnp_connection) d.upnp_connection->close(); d.upnp_connection.reset(new http_connection(m_io_service , m_cc, bind(&upnp::on_upnp_xml, self(), _1, _2 - , boost::ref(d)))); + , boost::ref(d), _5))); d.upnp_connection->get(d.url, seconds(30), 1); #ifndef BOOST_NO_EXCEPTIONS } @@ -631,9 +633,10 @@ void upnp::update_map(rootdevice& d, int i) return; } + if (d.upnp_connection) d.upnp_connection->close(); d.upnp_connection.reset(new http_connection(m_io_service , m_cc, bind(&upnp::on_upnp_map_response, self(), _1, _2 - , boost::ref(d), i), true + , boost::ref(d), i, _5), true , bind(&upnp::create_port_mapping, self(), _1, boost::ref(d), i))); d.upnp_connection->start(d.hostname, boost::lexical_cast(d.port) @@ -641,9 +644,10 @@ void upnp::update_map(rootdevice& d, int i) } else if (m.action == mapping_t::action_delete) { + if (d.upnp_connection) d.upnp_connection->close(); d.upnp_connection.reset(new http_connection(m_io_service , m_cc, bind(&upnp::on_upnp_unmap_response, self(), _1, _2 - , boost::ref(d), i), true + , boost::ref(d), i, _5), true , bind(&upnp::delete_port_mapping, self(), boost::ref(d), i))); d.upnp_connection->start(d.hostname, boost::lexical_cast(d.port) , seconds(10), 1); @@ -752,12 +756,13 @@ namespace } void upnp::on_upnp_xml(error_code const& e - , libtorrent::http_parser const& p, rootdevice& d) + , libtorrent::http_parser const& p, rootdevice& d + , http_connection& c) { mutex_t::scoped_lock l(m_mutex); TORRENT_ASSERT(d.magic == 1337); - if (d.upnp_connection) + if (d.upnp_connection && d.upnp_connection.get() == &c) { d.upnp_connection->close(); d.upnp_connection.reset(); @@ -911,12 +916,13 @@ namespace } void upnp::on_upnp_map_response(error_code const& e - , libtorrent::http_parser const& p, rootdevice& d, int mapping) + , libtorrent::http_parser const& p, rootdevice& d, int mapping + , http_connection& c) { mutex_t::scoped_lock l(m_mutex); TORRENT_ASSERT(d.magic == 1337); - if (d.upnp_connection) + if (d.upnp_connection && d.upnp_connection.get() == &c) { d.upnp_connection->close(); d.upnp_connection.reset(); @@ -1066,12 +1072,13 @@ void upnp::return_error(int mapping, int code) } void upnp::on_upnp_unmap_response(error_code const& e - , libtorrent::http_parser const& p, rootdevice& d, int mapping) + , libtorrent::http_parser const& p, rootdevice& d, int mapping + , http_connection& c) { mutex_t::scoped_lock l(m_mutex); TORRENT_ASSERT(d.magic == 1337); - if (d.upnp_connection) + if (d.upnp_connection && d.upnp_connection.get() == &c) { d.upnp_connection->close(); d.upnp_connection.reset(); diff --git a/test/test_http_connection.cpp b/test/test_http_connection.cpp index 3b0d401e0..781d7a3d8 100644 --- a/test/test_http_connection.cpp +++ b/test/test_http_connection.cpp @@ -38,7 +38,8 @@ void http_connect_handler(http_connection& c) TEST_CHECK(c.socket().remote_endpoint().address() == address::from_string("127.0.0.1")); } -void http_handler(error_code const& ec, http_parser const& parser, char const* data, int size) +void http_handler(error_code const& ec, http_parser const& parser + , char const* data, int size, http_connection& c) { ++handler_called; data_size = size;