2007-03-27 09:04:31 +02:00
|
|
|
/*
|
|
|
|
|
2015-06-03 07:18:48 +02:00
|
|
|
Copyright (c) 2007-2015, Arvid Norberg
|
2007-03-27 09:04:31 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/http_connection.hpp"
|
2015-03-15 00:10:20 +01:00
|
|
|
#include "libtorrent/aux_/escape_string.hpp"
|
2008-01-27 23:39:50 +01:00
|
|
|
#include "libtorrent/instantiate_connection.hpp"
|
2008-01-30 19:32:13 +01:00
|
|
|
#include "libtorrent/gzip.hpp"
|
2008-05-17 16:19:34 +02:00
|
|
|
#include "libtorrent/parse_url.hpp"
|
2008-05-03 18:05:42 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
2012-04-05 06:17:19 +02:00
|
|
|
#include "libtorrent/socket_type.hpp" // for async_shutdown
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/resolver_interface.hpp"
|
|
|
|
#include "libtorrent/settings_pack.hpp"
|
2015-03-12 05:34:54 +01:00
|
|
|
#include "libtorrent/aux_/time.hpp"
|
2015-11-11 07:17:36 +01:00
|
|
|
#include "libtorrent/random.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
#include "libtorrent/debug.hpp"
|
|
|
|
#endif
|
|
|
|
|
2015-08-20 01:33:20 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
|
|
|
|
2007-04-03 22:00:47 +02:00
|
|
|
#include <boost/bind.hpp>
|
2007-04-10 11:11:32 +02:00
|
|
|
#include <string>
|
2008-05-19 00:14:55 +02:00
|
|
|
#include <algorithm>
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2015-08-20 01:33:20 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
|
|
|
|
2008-01-30 19:32:13 +01:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
http_connection::http_connection(io_service& ios
|
|
|
|
, resolver_interface& resolver
|
2012-10-19 03:28:47 +02:00
|
|
|
, http_handler const& handler
|
|
|
|
, bool bottled
|
|
|
|
, int max_bottled_buffer_size
|
2011-08-28 23:06:15 +02:00
|
|
|
, http_connect_handler const& ch
|
|
|
|
, http_filter_handler const& fh
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2015-08-08 22:19:44 +02:00
|
|
|
, ssl::context* ssl_ctx
|
2011-08-28 23:06:15 +02:00
|
|
|
#endif
|
|
|
|
)
|
2015-11-07 23:18:45 +01:00
|
|
|
: m_next_ep(0)
|
|
|
|
, m_sock(ios)
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2015-02-08 17:03:09 +01:00
|
|
|
, m_ssl_ctx(ssl_ctx)
|
|
|
|
, m_own_ssl_context(false)
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2011-08-28 23:06:15 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2015-02-08 17:03:09 +01:00
|
|
|
, m_i2p_conn(0)
|
2011-08-28 23:06:15 +02:00
|
|
|
#endif
|
2015-02-08 17:03:09 +01:00
|
|
|
, m_resolver(resolver)
|
2011-08-28 23:06:15 +02:00
|
|
|
, m_handler(handler)
|
|
|
|
, m_connect_handler(ch)
|
|
|
|
, m_filter_handler(fh)
|
|
|
|
, m_timer(ios)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_limiter_timer(ios)
|
2015-03-12 05:34:54 +01:00
|
|
|
, m_last_receive(aux::time_now())
|
|
|
|
, m_start_time(aux::time_now())
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_read_pos(0)
|
|
|
|
, m_redirects(5)
|
2012-10-19 03:28:47 +02:00
|
|
|
, m_max_bottled_buffer_size(max_bottled_buffer_size)
|
2011-08-28 23:06:15 +02:00
|
|
|
, m_rate_limit(0)
|
|
|
|
, m_download_quota(0)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_priority(0)
|
|
|
|
, m_resolve_flags(0)
|
|
|
|
, m_port(0)
|
|
|
|
, m_bottled(bottled)
|
|
|
|
, m_called(false)
|
2011-08-28 23:06:15 +02:00
|
|
|
, m_limiter_timer_active(false)
|
|
|
|
, m_ssl(false)
|
|
|
|
, m_abort(false)
|
2014-10-16 07:33:09 +02:00
|
|
|
, m_connecting(false)
|
2011-08-28 23:06:15 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!m_handler.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
http_connection::~http_connection()
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (m_own_ssl_context) delete m_ssl_ctx;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-03-12 08:44:27 +01:00
|
|
|
void http_connection::get(std::string const& url, time_duration timeout, int prio
|
2015-08-25 04:18:10 +02:00
|
|
|
, aux::proxy_settings const* ps, int handle_redirects, std::string const& user_agent
|
2015-02-08 17:03:09 +01:00
|
|
|
, address const& bind_addr, int resolve_flags, std::string const& auth_
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
, i2p_connection* i2p_conn
|
|
|
|
#endif
|
|
|
|
)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2010-12-30 23:51:15 +01:00
|
|
|
m_user_agent = user_agent;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_resolve_flags = resolve_flags;
|
2010-12-30 23:51:15 +01:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
std::string protocol;
|
2007-05-22 22:44:18 +02:00
|
|
|
std::string auth;
|
2007-03-27 09:04:31 +02:00
|
|
|
std::string hostname;
|
|
|
|
std::string path;
|
2009-06-12 18:40:38 +02:00
|
|
|
error_code ec;
|
2007-03-27 09:04:31 +02:00
|
|
|
int port;
|
2008-05-17 16:19:34 +02:00
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
boost::tie(protocol, auth, hostname, port, path)
|
|
|
|
= parse_url_components(url, ec);
|
2008-05-17 16:19:34 +02:00
|
|
|
|
2015-02-08 17:03:09 +01:00
|
|
|
if (auth.empty()) auth = auth_;
|
|
|
|
|
|
|
|
m_auth = auth;
|
|
|
|
|
2009-02-27 10:03:05 +01:00
|
|
|
int default_port = protocol == "https" ? 443 : 80;
|
2013-02-26 06:57:29 +01:00
|
|
|
if (port == -1) port = default_port;
|
2009-02-27 10:03:05 +01:00
|
|
|
|
2009-06-30 05:31:58 +02:00
|
|
|
// keep ourselves alive even if the callback function
|
|
|
|
// deletes this object
|
|
|
|
boost::shared_ptr<http_connection> me(shared_from_this());
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
m_timer.get_io_service().post(boost::bind(&http_connection::callback
|
2015-08-08 08:33:54 +02:00
|
|
|
, me, ec, static_cast<char*>(NULL), 0));
|
2014-07-06 21:18:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-30 20:50:38 +02:00
|
|
|
if (protocol != "http"
|
2009-06-09 17:43:56 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2009-05-30 20:50:38 +02:00
|
|
|
&& protocol != "https"
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
2015-08-09 04:53:11 +02:00
|
|
|
error_code err(errors::unsupported_url_protocol);
|
2014-07-06 21:18:00 +02:00
|
|
|
m_timer.get_io_service().post(boost::bind(&http_connection::callback
|
2015-08-09 04:53:11 +02:00
|
|
|
, me, err, static_cast<char*>(NULL), 0));
|
2008-05-17 16:19:34 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2011-09-17 23:15:42 +02:00
|
|
|
TORRENT_ASSERT(prio >= 0 && prio < 3);
|
2008-03-14 11:17:27 +01:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
bool ssl = false;
|
|
|
|
if (protocol == "https") ssl = true;
|
2015-08-08 08:33:54 +02:00
|
|
|
|
2014-03-03 01:09:06 +01:00
|
|
|
char request[4096];
|
2009-04-12 11:00:16 +02:00
|
|
|
char* end = request + sizeof(request);
|
|
|
|
char* ptr = request;
|
|
|
|
|
|
|
|
#define APPEND_FMT(fmt) ptr += snprintf(ptr, end - ptr, fmt)
|
|
|
|
#define APPEND_FMT1(fmt, arg) ptr += snprintf(ptr, end - ptr, fmt, arg)
|
|
|
|
#define APPEND_FMT2(fmt, arg1, arg2) ptr += snprintf(ptr, end - ptr, fmt, arg1, arg2)
|
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
// exclude ssl here, because SSL assumes CONNECT support in the
|
|
|
|
// proxy and is handled at the lower layer
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ps && (ps->type == settings_pack::http
|
|
|
|
|| ps->type == settings_pack::http_pw)
|
2008-01-27 23:39:50 +01:00
|
|
|
&& !ssl)
|
|
|
|
{
|
|
|
|
// if we're using an http proxy and not an ssl
|
|
|
|
// connection, just do a regular http proxy request
|
2011-01-18 04:41:54 +01:00
|
|
|
APPEND_FMT1("GET %s HTTP/1.1\r\n", url.c_str());
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ps->type == settings_pack::http_pw)
|
2009-04-12 11:00:16 +02:00
|
|
|
APPEND_FMT1("Proxy-Authorization: Basic %s\r\n", base64encode(
|
|
|
|
ps->username + ":" + ps->password).c_str());
|
2013-11-01 10:05:41 +01:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
hostname = ps->hostname;
|
|
|
|
port = ps->port;
|
2013-11-01 10:05:41 +01:00
|
|
|
|
|
|
|
APPEND_FMT1("Host: %s", hostname.c_str());
|
|
|
|
if (port != default_port) APPEND_FMT1(":%d\r\n", port);
|
|
|
|
else APPEND_FMT("\r\n");
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-18 04:41:54 +01:00
|
|
|
APPEND_FMT2("GET %s HTTP/1.1\r\n"
|
2009-04-12 11:00:16 +02:00
|
|
|
"Host: %s", path.c_str(), hostname.c_str());
|
|
|
|
if (port != default_port) APPEND_FMT1(":%d\r\n", port);
|
|
|
|
else APPEND_FMT("\r\n");
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
|
|
|
|
2010-12-30 23:51:15 +01:00
|
|
|
// APPEND_FMT("Accept: */*\r\n");
|
|
|
|
|
|
|
|
if (!m_user_agent.empty())
|
|
|
|
APPEND_FMT1("User-Agent: %s\r\n", m_user_agent.c_str());
|
2015-08-08 08:33:54 +02:00
|
|
|
|
2008-12-29 08:31:33 +01:00
|
|
|
if (m_bottled)
|
2009-04-12 11:00:16 +02:00
|
|
|
APPEND_FMT("Accept-Encoding: gzip\r\n");
|
2008-12-29 08:31:33 +01:00
|
|
|
|
2013-02-23 23:13:25 +01:00
|
|
|
if (!auth.empty())
|
|
|
|
APPEND_FMT1("Authorization: Basic %s\r\n", base64encode(auth).c_str());
|
|
|
|
|
2009-04-12 11:00:16 +02:00
|
|
|
APPEND_FMT("Connection: close\r\n\r\n");
|
2008-01-30 19:32:13 +01:00
|
|
|
|
2015-02-08 17:03:09 +01:00
|
|
|
m_sendbuffer.assign(request);
|
2008-05-18 06:48:06 +02:00
|
|
|
m_url = url;
|
2014-07-06 21:18:00 +02:00
|
|
|
start(hostname, port, timeout, prio
|
|
|
|
, ps, ssl, handle_redirects, bind_addr, m_resolve_flags
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
, i2p_conn
|
|
|
|
#endif
|
|
|
|
);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void http_connection::start(std::string const& hostname, int port
|
2015-08-25 04:18:10 +02:00
|
|
|
, time_duration timeout, int prio, aux::proxy_settings const* ps, bool ssl
|
2014-07-06 21:18:00 +02:00
|
|
|
, int handle_redirects
|
2009-08-20 05:19:12 +02:00
|
|
|
, address const& bind_addr
|
2014-07-06 21:18:00 +02:00
|
|
|
, int resolve_flags
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
, i2p_connection* i2p_conn
|
|
|
|
#endif
|
|
|
|
)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2011-09-17 23:15:42 +02:00
|
|
|
TORRENT_ASSERT(prio >= 0 && prio < 3);
|
2008-03-14 11:17:27 +01:00
|
|
|
|
2008-01-12 02:41:56 +01:00
|
|
|
m_redirects = handle_redirects;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_resolve_flags = resolve_flags;
|
2008-01-27 23:39:50 +01:00
|
|
|
if (ps) m_proxy = *ps;
|
|
|
|
|
2010-12-31 01:55:54 +01:00
|
|
|
// keep ourselves alive even if the callback function
|
|
|
|
// deletes this object
|
|
|
|
boost::shared_ptr<http_connection> me(shared_from_this());
|
|
|
|
|
2012-01-21 09:18:41 +01:00
|
|
|
m_completion_timeout = timeout;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_read_timeout = seconds(5);
|
|
|
|
if (m_read_timeout < timeout / 5) m_read_timeout = timeout / 5;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2014-10-03 22:56:57 +02:00
|
|
|
m_timer.expires_from_now((std::min)(
|
|
|
|
m_read_timeout, m_completion_timeout), ec);
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_timeout");
|
|
|
|
#endif
|
2010-04-30 21:08:16 +02:00
|
|
|
m_timer.async_wait(boost::bind(&http_connection::on_timeout
|
2010-12-31 01:55:54 +01:00
|
|
|
, boost::weak_ptr<http_connection>(me), _1));
|
2007-04-01 17:39:08 +02:00
|
|
|
m_called = false;
|
2008-01-12 02:41:56 +01:00
|
|
|
m_parser.reset();
|
|
|
|
m_recvbuffer.clear();
|
|
|
|
m_read_pos = 0;
|
2008-03-12 08:44:27 +01:00
|
|
|
m_priority = prio;
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2008-01-31 09:24:01 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
m_timer.get_io_service().post(boost::bind(&http_connection::callback
|
2015-08-08 08:33:54 +02:00
|
|
|
, me, ec, static_cast<char*>(NULL), 0));
|
2008-01-31 09:24:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_sock.is_open() && m_hostname == hostname && m_port == port
|
|
|
|
&& m_ssl == ssl && m_bind_addr == bind_addr)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_write");
|
|
|
|
#endif
|
2015-06-06 07:22:53 +02:00
|
|
|
async_write(m_sock, boost::asio::buffer(m_sendbuffer)
|
2010-12-31 01:55:54 +01:00
|
|
|
, boost::bind(&http_connection::on_write, me, _1));
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
m_ssl = ssl;
|
|
|
|
m_bind_addr = bind_addr;
|
2015-08-09 04:53:11 +02:00
|
|
|
error_code err;
|
|
|
|
if (m_sock.is_open()) m_sock.close(err);
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
bool is_i2p = false;
|
|
|
|
char const* top_domain = strrchr(hostname.c_str(), '.');
|
|
|
|
if (top_domain && strcmp(top_domain, ".i2p") == 0 && i2p_conn)
|
|
|
|
{
|
|
|
|
// this is an i2p name, we need to use the sam connection
|
|
|
|
// to do the name lookup
|
|
|
|
is_i2p = true;
|
|
|
|
m_i2p_conn = i2p_conn;
|
|
|
|
// quadruple the timeout for i2p destinations
|
|
|
|
// because i2p is sloooooow
|
2012-01-21 09:18:41 +01:00
|
|
|
m_completion_timeout *= 4;
|
|
|
|
m_read_timeout *= 4;
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TORRENT_USE_I2P
|
2014-07-06 21:18:00 +02:00
|
|
|
if (is_i2p && i2p_conn->proxy().type != settings_pack::i2p_proxy)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
m_timer.get_io_service().post(boost::bind(&http_connection::callback
|
2015-08-08 08:33:54 +02:00
|
|
|
, me, error_code(errors::no_i2p_router, get_libtorrent_category()), static_cast<char*>(NULL), 0));
|
2009-08-20 05:19:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-08-25 04:18:10 +02:00
|
|
|
aux::proxy_settings const* proxy = ps;
|
2010-10-22 07:02:42 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2015-08-25 04:18:10 +02:00
|
|
|
aux::proxy_settings i2p_proxy;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (is_i2p)
|
|
|
|
{
|
|
|
|
i2p_proxy = i2p_conn->proxy();
|
|
|
|
proxy = &i2p_proxy;
|
|
|
|
}
|
2010-10-22 07:02:42 +02:00
|
|
|
#endif
|
|
|
|
|
2010-03-02 22:55:34 +01:00
|
|
|
// in this case, the upper layer is assumed to have taken
|
|
|
|
// care of the proxying already. Don't instantiate the socket
|
|
|
|
// with this proxy
|
2014-07-06 21:18:00 +02:00
|
|
|
if (proxy && (proxy->type == settings_pack::http
|
|
|
|
|| proxy->type == settings_pack::http_pw)
|
2010-03-02 22:55:34 +01:00
|
|
|
&& !ssl)
|
|
|
|
{
|
2010-10-22 07:02:42 +02:00
|
|
|
proxy = 0;
|
2010-03-02 22:55:34 +01:00
|
|
|
}
|
2015-08-25 04:18:10 +02:00
|
|
|
aux::proxy_settings null_proxy;
|
2010-03-02 22:55:34 +01:00
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
void* userdata = 0;
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2011-08-28 23:06:15 +02:00
|
|
|
if (m_ssl)
|
|
|
|
{
|
|
|
|
if (m_ssl_ctx == 0)
|
|
|
|
{
|
2015-08-08 22:19:44 +02:00
|
|
|
m_ssl_ctx = new (std::nothrow) ssl::context(
|
|
|
|
m_timer.get_io_service(), ssl::context::sslv23_client);
|
2011-08-28 23:06:15 +02:00
|
|
|
if (m_ssl_ctx)
|
|
|
|
{
|
|
|
|
m_own_ssl_context = true;
|
2015-08-08 22:19:44 +02:00
|
|
|
m_ssl_ctx->set_verify_mode(ssl::context::verify_none, ec);
|
2015-09-02 07:30:40 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
m_timer.get_io_service().post(boost::bind(&http_connection::callback
|
|
|
|
, me, ec, static_cast<char*>(NULL), 0));
|
|
|
|
return;
|
|
|
|
}
|
2011-08-28 23:06:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
userdata = m_ssl_ctx;
|
|
|
|
}
|
2009-08-20 05:19:12 +02:00
|
|
|
#endif
|
2015-08-25 04:18:10 +02:00
|
|
|
// assume this is not a tracker connection. Tracker connections that
|
|
|
|
// shouldn't be subject to the proxy should pass in NULL as the proxy
|
|
|
|
// pointer.
|
2014-07-06 21:18:00 +02:00
|
|
|
instantiate_connection(m_timer.get_io_service()
|
2015-08-25 04:18:10 +02:00
|
|
|
, proxy ? *proxy : null_proxy, m_sock, userdata, NULL, false, false);
|
2010-10-12 10:57:43 +02:00
|
|
|
|
2008-01-31 09:24:01 +01:00
|
|
|
if (m_bind_addr != address_v4::any())
|
|
|
|
{
|
2009-01-19 23:17:48 +01:00
|
|
|
m_sock.open(m_bind_addr.is_v4()?tcp::v4():tcp::v6(), ec);
|
2008-01-31 09:24:01 +01:00
|
|
|
m_sock.bind(tcp::endpoint(m_bind_addr, 0), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
m_timer.get_io_service().post(boost::bind(&http_connection::callback
|
2015-08-08 08:33:54 +02:00
|
|
|
, me, ec, static_cast<char*>(NULL), 0));
|
2008-01-31 09:24:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2012-04-05 06:17:19 +02:00
|
|
|
setup_ssl_hostname(m_sock, hostname, ec);
|
2012-01-29 03:27:55 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
m_timer.get_io_service().post(boost::bind(&http_connection::callback
|
2015-08-08 08:33:54 +02:00
|
|
|
, me, ec, static_cast<char*>(NULL), 0));
|
2012-01-29 03:27:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
if (is_i2p)
|
|
|
|
{
|
2015-06-28 00:03:03 +02:00
|
|
|
if (hostname.length() < 516) // Base64 encoded destination with optional .i2p
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
2015-06-28 00:03:03 +02:00
|
|
|
add_outstanding_async("http_connection::on_i2p_resolve");
|
2010-11-28 02:47:30 +01:00
|
|
|
#endif
|
2015-06-28 00:03:03 +02:00
|
|
|
i2p_conn->async_name_lookup(hostname.c_str(), boost::bind(&http_connection::on_i2p_resolve
|
|
|
|
, me, _1, _2));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
connect_i2p_tracker(hostname.c_str());
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2015-11-22 03:43:08 +01:00
|
|
|
|
|
|
|
// TODO: 3 if hostname is in fact an IP address (v4 or v6), we should go
|
|
|
|
// straight to connecting, regardless of proxy_hostname is enabled or not
|
2010-08-03 11:08:37 +02:00
|
|
|
if (ps && ps->proxy_hostnames
|
2014-07-06 21:18:00 +02:00
|
|
|
&& (ps->type == settings_pack::socks5
|
|
|
|
|| ps->type == settings_pack::socks5_pw))
|
2010-08-03 11:08:37 +02:00
|
|
|
{
|
|
|
|
m_hostname = hostname;
|
|
|
|
m_port = port;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_endpoints.push_back(tcp::endpoint(address(), port));
|
2014-10-03 22:56:57 +02:00
|
|
|
connect();
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
else
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_resolve");
|
|
|
|
#endif
|
2011-03-21 03:11:39 +01:00
|
|
|
m_endpoints.clear();
|
2015-11-07 23:18:45 +01:00
|
|
|
m_next_ep = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_resolver.async_resolve(hostname, m_resolve_flags
|
|
|
|
, boost::bind(&http_connection::on_resolve
|
2010-12-31 01:55:54 +01:00
|
|
|
, me, _1, _2));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
m_hostname = hostname;
|
|
|
|
m_port = port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-01 17:39:08 +02:00
|
|
|
void http_connection::on_timeout(boost::weak_ptr<http_connection> p
|
2008-05-03 18:05:42 +02:00
|
|
|
, error_code const& e)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("http_connection::on_timeout");
|
|
|
|
#endif
|
2007-04-01 17:39:08 +02:00
|
|
|
boost::shared_ptr<http_connection> c = p.lock();
|
|
|
|
if (!c) return;
|
2007-05-05 02:29:33 +02:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
if (e == boost::asio::error::operation_aborted) return;
|
2007-05-05 02:29:33 +02:00
|
|
|
|
2014-10-06 05:19:29 +02:00
|
|
|
if (c->m_abort) return;
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point now = clock_type::now();
|
2012-01-21 09:18:41 +01:00
|
|
|
|
2015-09-10 05:57:14 +02:00
|
|
|
if (c->m_start_time + c->m_completion_timeout <= now
|
|
|
|
|| c->m_last_receive + c->m_read_timeout <= now)
|
2007-04-02 20:51:35 +02:00
|
|
|
{
|
2014-10-06 10:39:05 +02:00
|
|
|
// the connection timed out. If we have more endpoints to try, just
|
|
|
|
// close this connection. The on_connect handler will try the next
|
|
|
|
// endpoint in the list.
|
2015-11-07 23:18:45 +01:00
|
|
|
if (c->m_next_ep < c->m_endpoints.size())
|
2008-05-19 00:14:55 +02:00
|
|
|
{
|
|
|
|
error_code ec;
|
2014-10-06 10:39:05 +02:00
|
|
|
c->m_sock.close(ec);
|
2014-10-16 07:33:09 +02:00
|
|
|
if (!c->m_connecting) c->connect();
|
2008-05-19 00:14:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
c->callback(boost::asio::error::timed_out);
|
2008-05-19 00:14:55 +02:00
|
|
|
}
|
2015-11-07 23:18:45 +01:00
|
|
|
return;
|
2014-10-03 22:56:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!c->m_sock.is_open()) return;
|
2007-04-02 20:51:35 +02:00
|
|
|
}
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_timeout");
|
|
|
|
#endif
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2012-01-21 09:18:41 +01:00
|
|
|
c->m_timer.expires_at((std::min)(
|
|
|
|
c->m_last_receive + c->m_read_timeout
|
|
|
|
, c->m_start_time + c->m_completion_timeout), ec);
|
2010-04-30 21:08:16 +02:00
|
|
|
c->m_timer.async_wait(boost::bind(&http_connection::on_timeout, p, _1));
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2013-08-12 02:51:49 +02:00
|
|
|
void http_connection::close(bool force)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2012-04-05 06:17:19 +02:00
|
|
|
if (m_abort) return;
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2013-08-12 02:51:49 +02:00
|
|
|
if (force)
|
|
|
|
m_sock.close(ec);
|
|
|
|
else
|
|
|
|
async_shutdown(m_sock, shared_from_this());
|
2012-04-05 06:17:19 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
m_timer.cancel(ec);
|
|
|
|
m_limiter_timer.cancel(ec);
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
m_hostname.clear();
|
2014-07-06 21:18:00 +02:00
|
|
|
m_port = 0;
|
2007-11-18 05:12:35 +01:00
|
|
|
m_handler.clear();
|
2008-05-19 00:14:55 +02:00
|
|
|
m_abort = true;
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2015-06-28 00:03:03 +02:00
|
|
|
void http_connection::connect_i2p_tracker(char const* destination)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
TORRENT_ASSERT(m_ssl == false);
|
|
|
|
TORRENT_ASSERT(m_sock.get<socket_type>());
|
|
|
|
TORRENT_ASSERT(m_sock.get<socket_type>()->get<i2p_stream>());
|
|
|
|
m_sock.get<socket_type>()->get<i2p_stream>()->set_destination(destination);
|
|
|
|
m_sock.get<socket_type>()->get<i2p_stream>()->set_command(i2p_stream::cmd_connect);
|
|
|
|
m_sock.get<socket_type>()->get<i2p_stream>()->set_session_id(m_i2p_conn->session_id());
|
|
|
|
#else
|
|
|
|
m_sock.get<i2p_stream>()->set_destination(destination);
|
|
|
|
m_sock.get<i2p_stream>()->set_command(i2p_stream::cmd_connect);
|
|
|
|
m_sock.get<i2p_stream>()->set_session_id(m_i2p_conn->session_id());
|
2010-11-28 02:47:30 +01:00
|
|
|
#endif
|
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_connect");
|
2009-08-20 05:19:12 +02:00
|
|
|
#endif
|
|
|
|
m_sock.async_connect(tcp::endpoint(), boost::bind(&http_connection::on_connect
|
|
|
|
, shared_from_this(), _1));
|
|
|
|
}
|
2015-06-28 00:03:03 +02:00
|
|
|
|
|
|
|
void http_connection::on_i2p_resolve(error_code const& e
|
|
|
|
, char const* destination)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("http_connection::on_i2p_resolve");
|
|
|
|
#endif
|
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
callback(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
connect_i2p_tracker(destination);
|
|
|
|
}
|
2009-08-20 05:19:12 +02:00
|
|
|
#endif
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void http_connection::on_resolve(error_code const& e
|
2014-07-06 21:18:00 +02:00
|
|
|
, std::vector<address> const& addresses)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("http_connection::on_resolve");
|
|
|
|
#endif
|
2007-03-27 09:04:31 +02:00
|
|
|
if (e)
|
|
|
|
{
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(!addresses.empty());
|
2008-01-31 09:24:01 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<address>::const_iterator i = addresses.begin()
|
|
|
|
, end(addresses.end()); i != end; ++i)
|
|
|
|
m_endpoints.push_back(tcp::endpoint(*i, m_port));
|
2008-01-31 09:24:01 +01:00
|
|
|
|
2008-10-22 21:40:32 +02:00
|
|
|
if (m_filter_handler) m_filter_handler(*this, m_endpoints);
|
|
|
|
if (m_endpoints.empty())
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:17:36 +01:00
|
|
|
std::random_shuffle(m_endpoints.begin(), m_endpoints.end(), randint);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2008-07-03 12:05:19 +02:00
|
|
|
// The following statement causes msvc to crash (ICE). Since it's not
|
|
|
|
// necessary in the vast majority of cases, just ignore the endpoint
|
|
|
|
// order for windows
|
|
|
|
#if !defined _MSC_VER || _MSC_VER > 1310
|
2008-05-19 00:14:55 +02:00
|
|
|
// sort the endpoints so that the ones with the same IP version as our
|
|
|
|
// bound listen socket are first. So that when contacting a tracker,
|
|
|
|
// we'll talk to it from the same IP that we're listening on
|
2009-05-15 23:23:41 +02:00
|
|
|
if (m_bind_addr != address_v4::any())
|
|
|
|
std::partition(m_endpoints.begin(), m_endpoints.end()
|
|
|
|
, boost::bind(&address::is_v4, boost::bind(&tcp::endpoint::address, _1))
|
|
|
|
== m_bind_addr.is_v4());
|
2008-07-03 12:05:19 +02:00
|
|
|
#endif
|
2008-05-19 00:14:55 +02:00
|
|
|
|
2014-10-03 22:56:57 +02:00
|
|
|
connect();
|
2008-05-19 00:14:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-03 22:56:57 +02:00
|
|
|
void http_connection::connect()
|
2008-05-19 00:14:55 +02:00
|
|
|
{
|
2015-11-07 23:18:45 +01:00
|
|
|
TORRENT_ASSERT(m_next_ep < m_endpoints.size());
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
boost::shared_ptr<http_connection> me(shared_from_this());
|
|
|
|
|
2010-08-03 11:08:37 +02:00
|
|
|
if (m_proxy.proxy_hostnames
|
2014-07-06 21:18:00 +02:00
|
|
|
&& (m_proxy.type == settings_pack::socks5
|
|
|
|
|| m_proxy.type == settings_pack::socks5_pw))
|
2010-08-03 11:08:37 +02:00
|
|
|
{
|
|
|
|
// we're using a socks proxy and we're resolving
|
|
|
|
// hostnames through it
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2010-10-12 10:57:43 +02:00
|
|
|
if (m_ssl)
|
2010-08-03 11:08:37 +02:00
|
|
|
{
|
2010-10-12 10:57:43 +02:00
|
|
|
TORRENT_ASSERT(m_sock.get<ssl_stream<socks5_stream> >());
|
2011-09-10 07:52:07 +02:00
|
|
|
m_sock.get<ssl_stream<socks5_stream> >()->next_layer().set_dst_name(m_hostname);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
2010-10-12 10:57:43 +02:00
|
|
|
else
|
2010-08-03 11:08:37 +02:00
|
|
|
#endif
|
2010-10-12 10:57:43 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_sock.get<socks5_stream>());
|
|
|
|
m_sock.get<socks5_stream>()->set_dst_name(m_hostname);
|
|
|
|
}
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
2014-10-06 10:39:05 +02:00
|
|
|
|
2015-11-07 23:18:45 +01:00
|
|
|
TORRENT_ASSERT(m_next_ep < m_endpoints.size());
|
|
|
|
if (m_next_ep >= m_endpoints.size()) return;
|
2014-10-06 10:39:05 +02:00
|
|
|
|
2015-11-07 23:18:45 +01:00
|
|
|
tcp::endpoint target_address = m_endpoints[m_next_ep];
|
|
|
|
++m_next_ep;
|
2014-10-06 10:39:05 +02:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_connect");
|
|
|
|
#endif
|
2014-10-16 07:33:09 +02:00
|
|
|
TORRENT_ASSERT(!m_connecting);
|
|
|
|
m_connecting = true;
|
2007-05-05 02:29:33 +02:00
|
|
|
m_sock.async_connect(target_address, boost::bind(&http_connection::on_connect
|
2008-05-19 00:14:55 +02:00
|
|
|
, shared_from_this(), _1));
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2008-05-19 00:14:55 +02:00
|
|
|
void http_connection::on_connect(error_code const& e)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("http_connection::on_connect");
|
|
|
|
#endif
|
2014-10-16 07:33:09 +02:00
|
|
|
TORRENT_ASSERT(m_connecting);
|
|
|
|
m_connecting = false;
|
2008-05-19 00:14:55 +02:00
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
m_last_receive = clock_type::now();
|
2012-01-21 09:18:41 +01:00
|
|
|
m_start_time = m_last_receive;
|
2007-03-27 09:04:31 +02:00
|
|
|
if (!e)
|
2015-06-06 07:22:53 +02:00
|
|
|
{
|
2007-09-10 01:52:34 +02:00
|
|
|
if (m_connect_handler) m_connect_handler(*this);
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_write");
|
|
|
|
#endif
|
2015-06-06 07:22:53 +02:00
|
|
|
async_write(m_sock, boost::asio::buffer(m_sendbuffer)
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&http_connection::on_write, shared_from_this(), _1));
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
2015-11-07 23:18:45 +01:00
|
|
|
else if (m_next_ep < m_endpoints.size() && !m_abort)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
|
|
|
// The connection failed. Try the next endpoint in the list.
|
2008-10-21 10:45:42 +02:00
|
|
|
error_code ec;
|
|
|
|
m_sock.close(ec);
|
2014-10-03 22:56:57 +02:00
|
|
|
connect();
|
2015-06-06 07:22:53 +02:00
|
|
|
}
|
2008-05-19 00:14:55 +02:00
|
|
|
else
|
2015-06-06 07:22:53 +02:00
|
|
|
{
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 04:10:48 +02:00
|
|
|
void http_connection::callback(error_code e, char* data, int size)
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
2009-02-11 09:16:14 +01:00
|
|
|
if (m_bottled && m_called) return;
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
2011-02-13 10:13:05 +01:00
|
|
|
if (data && m_bottled && m_parser.header_finished())
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
2015-08-09 04:53:11 +02:00
|
|
|
size = m_parser.collapse_chunk_headers(data, size);
|
2011-01-06 05:08:57 +01:00
|
|
|
|
2009-02-11 09:16:14 +01:00
|
|
|
std::string const& encoding = m_parser.header("content-encoding");
|
2009-12-06 03:38:10 +01:00
|
|
|
if ((encoding == "gzip" || encoding == "x-gzip") && size > 0 && data)
|
2008-01-30 19:32:13 +01:00
|
|
|
{
|
2014-07-20 22:49:56 +02:00
|
|
|
error_code ec;
|
|
|
|
inflate_gzip(data, size, buf, m_max_bottled_buffer_size, ec);
|
|
|
|
|
|
|
|
if (ec)
|
2008-01-30 19:32:13 +01:00
|
|
|
{
|
2014-07-20 22:49:56 +02:00
|
|
|
if (m_handler) m_handler(ec, m_parser, data, size, *this);
|
2009-02-11 09:16:14 +01:00
|
|
|
return;
|
2008-01-30 19:32:13 +01:00
|
|
|
}
|
2009-02-11 09:16:14 +01:00
|
|
|
size = int(buf.size());
|
2009-09-18 21:55:41 +02:00
|
|
|
data = size == 0 ? 0 : &buf[0];
|
2008-01-30 19:32:13 +01:00
|
|
|
}
|
2011-01-06 05:08:57 +01:00
|
|
|
|
|
|
|
// if we completed the whole response, no need
|
|
|
|
// to tell the user that the connection was closed by
|
|
|
|
// the server or by us. Just clear any error
|
|
|
|
if (m_parser.finished()) e.clear();
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
2009-02-11 09:16:14 +01:00
|
|
|
m_called = true;
|
|
|
|
error_code ec;
|
|
|
|
m_timer.cancel(ec);
|
|
|
|
if (m_handler) m_handler(e, m_parser, data, size, *this);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void http_connection::on_write(error_code const& e)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("http_connection::on_write");
|
|
|
|
#endif
|
2012-04-01 02:42:04 +02:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
if (e == boost::asio::error::operation_aborted) return;
|
2012-04-01 02:42:04 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
if (e)
|
|
|
|
{
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-22 08:59:54 +02:00
|
|
|
if (m_abort) return;
|
|
|
|
|
2015-02-08 17:03:09 +01:00
|
|
|
std::string().swap(m_sendbuffer);
|
2007-03-27 09:04:31 +02:00
|
|
|
m_recvbuffer.resize(4096);
|
2007-04-03 22:00:47 +02:00
|
|
|
|
|
|
|
int amount_to_read = m_recvbuffer.size() - m_read_pos;
|
|
|
|
if (m_rate_limit > 0 && amount_to_read > m_download_quota)
|
|
|
|
{
|
|
|
|
amount_to_read = m_download_quota;
|
|
|
|
if (m_download_quota == 0)
|
|
|
|
{
|
|
|
|
if (!m_limiter_timer_active)
|
2010-11-28 02:47:30 +01:00
|
|
|
{
|
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_assign_bandwidth");
|
|
|
|
#endif
|
2008-05-03 18:05:42 +02:00
|
|
|
on_assign_bandwidth(error_code());
|
2010-11-28 02:47:30 +01:00
|
|
|
}
|
2007-04-03 22:00:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_read");
|
|
|
|
#endif
|
2015-06-06 07:22:53 +02:00
|
|
|
m_sock.async_read_some(boost::asio::buffer(&m_recvbuffer[0] + m_read_pos
|
2007-04-03 22:00:47 +02:00
|
|
|
, amount_to_read)
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&http_connection::on_read
|
|
|
|
, shared_from_this(), _1, _2));
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void http_connection::on_read(error_code const& e
|
2007-03-27 09:04:31 +02:00
|
|
|
, std::size_t bytes_transferred)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("http_connection::on_read");
|
|
|
|
#endif
|
2013-10-22 08:59:54 +02:00
|
|
|
|
2007-04-03 22:00:47 +02:00
|
|
|
if (m_rate_limit)
|
|
|
|
{
|
|
|
|
m_download_quota -= bytes_transferred;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_download_quota >= 0);
|
2007-04-03 22:00:47 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
if (e == boost::asio::error::operation_aborted) return;
|
2012-04-01 02:42:04 +02:00
|
|
|
|
2013-10-22 08:59:54 +02:00
|
|
|
if (m_abort) return;
|
|
|
|
|
2009-06-30 05:31:58 +02:00
|
|
|
// keep ourselves alive even if the callback function
|
|
|
|
// deletes this object
|
|
|
|
boost::shared_ptr<http_connection> me(shared_from_this());
|
|
|
|
|
2009-02-11 08:54:16 +01:00
|
|
|
// when using the asio SSL wrapper, it seems like
|
|
|
|
// we get the shut_down error instead of EOF
|
2015-06-06 07:22:53 +02:00
|
|
|
if (e == boost::asio::error::eof || e == boost::asio::error::shut_down)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
error_code ec = boost::asio::error::eof;
|
2008-01-12 02:41:56 +01:00
|
|
|
TORRENT_ASSERT(bytes_transferred == 0);
|
2014-09-17 04:10:48 +02:00
|
|
|
char* data = 0;
|
2007-05-02 00:44:54 +02:00
|
|
|
std::size_t size = 0;
|
2007-10-04 00:23:18 +02:00
|
|
|
if (m_bottled && m_parser.header_finished())
|
2007-05-02 00:44:54 +02:00
|
|
|
{
|
2014-09-17 04:10:48 +02:00
|
|
|
data = &m_recvbuffer[0] + m_parser.body_start();
|
2007-05-02 00:44:54 +02:00
|
|
|
size = m_parser.get_body().left();
|
|
|
|
}
|
2009-02-11 08:54:16 +01:00
|
|
|
callback(ec, data, size);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e)
|
|
|
|
{
|
2008-01-12 02:41:56 +01:00
|
|
|
TORRENT_ASSERT(bytes_transferred == 0);
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_read_pos += bytes_transferred;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_read_pos <= int(m_recvbuffer.size()));
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
if (m_bottled || !m_parser.header_finished())
|
|
|
|
{
|
|
|
|
libtorrent::buffer::const_interval rcv_buf(&m_recvbuffer[0]
|
|
|
|
, &m_recvbuffer[0] + m_read_pos);
|
2007-12-29 19:24:50 +01:00
|
|
|
bool error = false;
|
|
|
|
m_parser.incoming(rcv_buf, error);
|
|
|
|
if (error)
|
2007-12-12 20:29:19 +01:00
|
|
|
{
|
2007-12-29 19:24:50 +01:00
|
|
|
// HTTP parse error
|
2009-12-06 00:03:48 +01:00
|
|
|
error_code ec = errors::http_parse_error;
|
2007-12-29 19:24:50 +01:00
|
|
|
callback(ec, 0, 0);
|
2007-12-12 20:29:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-01-12 02:41:56 +01:00
|
|
|
|
|
|
|
// having a nonempty path means we should handle redirects
|
|
|
|
if (m_redirects && m_parser.header_finished())
|
|
|
|
{
|
|
|
|
int code = m_parser.status_code();
|
|
|
|
|
2014-07-09 22:53:39 +02:00
|
|
|
if (is_redirect(code))
|
2008-01-12 02:41:56 +01:00
|
|
|
{
|
|
|
|
// attempt a redirect
|
2008-05-18 06:48:06 +02:00
|
|
|
std::string const& location = m_parser.header("location");
|
|
|
|
if (location.empty())
|
2008-01-12 02:41:56 +01:00
|
|
|
{
|
|
|
|
// missing location header
|
2009-12-06 00:03:48 +01:00
|
|
|
callback(error_code(errors::http_missing_location));
|
2008-01-12 02:41:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2012-04-05 06:17:19 +02:00
|
|
|
// it would be nice to gracefully shut down SSL here
|
|
|
|
// but then we'd have to do all the reconnect logic
|
|
|
|
// in its handler. For now, just kill the connection.
|
|
|
|
// async_shutdown(m_sock, shared_from_this());
|
2008-01-12 02:41:56 +01:00
|
|
|
m_sock.close(ec);
|
2014-07-09 22:53:39 +02:00
|
|
|
|
|
|
|
std::string url = resolve_redirect_location(m_url, location);
|
|
|
|
get(url, m_completion_timeout, m_priority, &m_proxy, m_redirects - 1
|
2015-02-08 17:03:09 +01:00
|
|
|
, m_user_agent, m_bind_addr, m_resolve_flags, m_auth
|
2010-12-30 23:51:15 +01:00
|
|
|
#if TORRENT_USE_I2P
|
2014-07-09 22:53:39 +02:00
|
|
|
, m_i2p_conn
|
2010-12-30 23:51:15 +01:00
|
|
|
#endif
|
2014-07-09 22:53:39 +02:00
|
|
|
);
|
2008-01-12 02:41:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-11-22 03:43:08 +01:00
|
|
|
|
2008-01-12 02:41:56 +01:00
|
|
|
m_redirects = 0;
|
|
|
|
}
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
if (!m_bottled && m_parser.header_finished())
|
|
|
|
{
|
|
|
|
if (m_read_pos > m_parser.body_start())
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e, &m_recvbuffer[0] + m_parser.body_start()
|
2007-03-27 09:04:31 +02:00
|
|
|
, m_read_pos - m_parser.body_start());
|
|
|
|
m_read_pos = 0;
|
2015-03-12 05:34:54 +01:00
|
|
|
m_last_receive = clock_type::now();
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
else if (m_bottled && m_parser.finished())
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-30 00:37:50 +01:00
|
|
|
m_timer.cancel(ec);
|
2014-09-17 04:10:48 +02:00
|
|
|
callback(e, &m_recvbuffer[0] + m_parser.body_start(), m_parser.get_body().left());
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_bottled);
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e, &m_recvbuffer[0], m_read_pos);
|
2007-03-27 09:04:31 +02:00
|
|
|
m_read_pos = 0;
|
2015-03-12 05:34:54 +01:00
|
|
|
m_last_receive = clock_type::now();
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2014-03-03 01:09:06 +01:00
|
|
|
// if we've hit the limit, double the buffer size
|
2007-03-27 09:04:31 +02:00
|
|
|
if (int(m_recvbuffer.size()) == m_read_pos)
|
2014-03-03 01:09:06 +01:00
|
|
|
m_recvbuffer.resize((std::min)(m_read_pos * 2, m_max_bottled_buffer_size));
|
|
|
|
|
2012-10-19 03:28:47 +02:00
|
|
|
if (m_read_pos == m_max_bottled_buffer_size)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2014-03-03 01:09:06 +01:00
|
|
|
// if we've reached the size limit, terminate the connection and
|
|
|
|
// report the error
|
|
|
|
callback(error_code(boost::system::errc::file_too_large, generic_category()));
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-04-03 22:00:47 +02:00
|
|
|
int amount_to_read = m_recvbuffer.size() - m_read_pos;
|
|
|
|
if (m_rate_limit > 0 && amount_to_read > m_download_quota)
|
|
|
|
{
|
|
|
|
amount_to_read = m_download_quota;
|
|
|
|
if (m_download_quota == 0)
|
|
|
|
{
|
|
|
|
if (!m_limiter_timer_active)
|
2010-11-28 02:47:30 +01:00
|
|
|
{
|
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_assign_bandwidth");
|
|
|
|
#endif
|
2008-05-03 18:05:42 +02:00
|
|
|
on_assign_bandwidth(error_code());
|
2010-11-28 02:47:30 +01:00
|
|
|
}
|
2007-04-03 22:00:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_read");
|
|
|
|
#endif
|
2015-06-06 07:22:53 +02:00
|
|
|
m_sock.async_read_some(boost::asio::buffer(&m_recvbuffer[0] + m_read_pos
|
2007-04-03 22:00:47 +02:00
|
|
|
, amount_to_read)
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&http_connection::on_read
|
|
|
|
, me, _1, _2));
|
2007-04-03 22:00:47 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void http_connection::on_assign_bandwidth(error_code const& e)
|
2007-04-03 22:00:47 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("http_connection::on_assign_bandwidth");
|
|
|
|
#endif
|
2015-06-06 07:22:53 +02:00
|
|
|
if ((e == boost::asio::error::operation_aborted
|
2007-04-07 01:01:27 +02:00
|
|
|
&& m_limiter_timer_active)
|
2007-04-10 20:53:12 +02:00
|
|
|
|| !m_sock.is_open())
|
2007-04-07 01:01:27 +02:00
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
callback(boost::asio::error::eof);
|
2007-04-10 21:22:08 +02:00
|
|
|
return;
|
2007-04-07 01:01:27 +02:00
|
|
|
}
|
2007-04-03 22:00:47 +02:00
|
|
|
m_limiter_timer_active = false;
|
|
|
|
if (e) return;
|
|
|
|
|
|
|
|
if (m_download_quota > 0) return;
|
|
|
|
|
|
|
|
m_download_quota = m_rate_limit / 4;
|
|
|
|
|
|
|
|
int amount_to_read = m_recvbuffer.size() - m_read_pos;
|
|
|
|
if (amount_to_read > m_download_quota)
|
|
|
|
amount_to_read = m_download_quota;
|
|
|
|
|
2007-11-16 00:55:30 +01:00
|
|
|
if (!m_sock.is_open()) return;
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_read");
|
|
|
|
#endif
|
2015-06-06 07:22:53 +02:00
|
|
|
m_sock.async_read_some(boost::asio::buffer(&m_recvbuffer[0] + m_read_pos
|
2007-04-03 22:00:47 +02:00
|
|
|
, amount_to_read)
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&http_connection::on_read
|
|
|
|
, shared_from_this(), _1, _2));
|
2007-04-03 22:00:47 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-04-03 22:00:47 +02:00
|
|
|
m_limiter_timer_active = true;
|
2007-12-30 00:37:50 +01:00
|
|
|
m_limiter_timer.expires_from_now(milliseconds(250), ec);
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_assign_bandwidth");
|
|
|
|
#endif
|
2010-04-30 21:08:16 +02:00
|
|
|
m_limiter_timer.async_wait(boost::bind(&http_connection::on_assign_bandwidth
|
2007-04-03 22:00:47 +02:00
|
|
|
, shared_from_this(), _1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::rate_limit(int limit)
|
|
|
|
{
|
2007-11-16 00:55:30 +01:00
|
|
|
if (!m_sock.is_open()) return;
|
|
|
|
|
2007-04-03 22:00:47 +02:00
|
|
|
if (!m_limiter_timer_active)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-04-03 22:00:47 +02:00
|
|
|
m_limiter_timer_active = true;
|
2007-12-30 00:37:50 +01:00
|
|
|
m_limiter_timer.expires_from_now(milliseconds(250), ec);
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("http_connection::on_assign_bandwidth");
|
|
|
|
#endif
|
2010-04-30 21:08:16 +02:00
|
|
|
m_limiter_timer.async_wait(boost::bind(&http_connection::on_assign_bandwidth
|
2007-04-03 22:00:47 +02:00
|
|
|
, shared_from_this(), _1));
|
|
|
|
}
|
|
|
|
m_rate_limit = limit;
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|