2007-03-27 09:04:31 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2007-2016, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_HTTP_CONNECTION
|
|
|
|
#define TORRENT_HTTP_CONNECTION
|
|
|
|
|
2015-04-18 04:33:39 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2017-10-09 01:21:19 +02:00
|
|
|
// there is no forward declaration header for asio
|
|
|
|
namespace boost {
|
|
|
|
namespace asio {
|
|
|
|
namespace ssl {
|
2017-11-23 19:02:13 +01:00
|
|
|
class context;
|
2017-10-09 01:21:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-18 04:33:39 +02:00
|
|
|
#endif
|
|
|
|
|
2016-08-13 03:31:55 +02:00
|
|
|
#include <functional>
|
2016-08-02 18:13:05 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2008-01-31 09:24:01 +01:00
|
|
|
#include "libtorrent/http_parser.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/deadline_timer.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2008-01-27 23:39:50 +01:00
|
|
|
#include "libtorrent/socket_type.hpp"
|
2010-10-12 10:57:43 +02:00
|
|
|
#include "libtorrent/i2p_stream.hpp"
|
2017-01-15 00:53:25 +01:00
|
|
|
#include "libtorrent/aux_/vector.hpp"
|
2017-05-28 21:01:46 +02:00
|
|
|
#include "libtorrent/resolver_interface.hpp"
|
2017-11-23 18:01:32 +01:00
|
|
|
#include "libtorrent/optional.hpp"
|
2010-10-12 10:57:43 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
struct http_connection;
|
2014-07-06 21:18:00 +02:00
|
|
|
struct resolver_interface;
|
2012-10-19 03:28:47 +02:00
|
|
|
|
2017-10-14 22:52:07 +02:00
|
|
|
constexpr int default_max_bottled_buffer_size = 2 * 1024 * 1024;
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2016-08-13 03:31:55 +02:00
|
|
|
typedef std::function<void(error_code const&
|
2017-09-04 00:13:08 +02:00
|
|
|
, http_parser const&, span<char const> data, http_connection&)> http_handler;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2016-08-13 03:31:55 +02:00
|
|
|
typedef std::function<void(http_connection&)> http_connect_handler;
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2016-08-13 03:31:55 +02:00
|
|
|
typedef std::function<void(http_connection&, std::vector<tcp::endpoint>&)> http_filter_handler;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
// when bottled, the last two arguments to the handler
|
|
|
|
// will always be 0
|
2012-10-19 03:28:47 +02:00
|
|
|
struct TORRENT_EXTRA_EXPORT http_connection
|
2016-08-31 18:45:45 +02:00
|
|
|
: std::enable_shared_from_this<http_connection>
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
http_connection(io_service& ios
|
|
|
|
, resolver_interface& resolver
|
|
|
|
, http_handler const& handler
|
|
|
|
, bool bottled = true
|
2012-10-19 03:28:47 +02:00
|
|
|
, int max_bottled_buffer_size = default_max_bottled_buffer_size
|
2008-10-22 21:40:32 +02:00
|
|
|
, http_connect_handler const& ch = http_connect_handler()
|
2011-08-28 23:06:15 +02:00
|
|
|
, http_filter_handler const& fh = http_filter_handler()
|
2010-10-12 10:57:43 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2017-10-14 22:52:07 +02:00
|
|
|
, ssl::context* ssl_ctx = nullptr
|
2010-10-12 10:57:43 +02:00
|
|
|
#endif
|
2011-08-28 23:06:15 +02:00
|
|
|
);
|
|
|
|
|
2017-11-23 18:01:32 +01:00
|
|
|
// non-copyable
|
|
|
|
http_connection(http_connection const&) = delete;
|
|
|
|
http_connection& operator=(http_connection const&) = delete;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual ~http_connection();
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-04-03 22:00:47 +02:00
|
|
|
void rate_limit(int limit);
|
|
|
|
|
|
|
|
int rate_limit() const
|
|
|
|
{ return m_rate_limit; }
|
|
|
|
|
2015-02-08 17:03:09 +01:00
|
|
|
std::string m_sendbuffer;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-04-19 22:52:29 +02:00
|
|
|
void get(std::string const& url, time_duration timeout = seconds(30)
|
2017-08-07 19:48:39 +02:00
|
|
|
, int prio = 0, aux::proxy_settings const* ps = nullptr, int handle_redirects = 5
|
2015-02-08 17:03:09 +01:00
|
|
|
, std::string const& user_agent = std::string()
|
2017-04-28 05:34:39 +02:00
|
|
|
, boost::optional<address> const& bind_addr = boost::optional<address>()
|
2017-07-15 02:59:20 +02:00
|
|
|
, resolver_flags resolve_flags = resolver_flags{}, std::string const& auth_ = std::string()
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2017-09-28 10:11:20 +02:00
|
|
|
, i2p_connection* i2p_conn = nullptr
|
2009-08-20 05:19:12 +02:00
|
|
|
#endif
|
|
|
|
);
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void start(std::string const& hostname, int port
|
2017-08-07 19:48:39 +02:00
|
|
|
, time_duration timeout, int prio = 0, aux::proxy_settings const* ps = nullptr
|
2008-03-12 08:44:27 +01:00
|
|
|
, bool ssl = false, int handle_redirect = 5
|
2017-04-28 05:34:39 +02:00
|
|
|
, boost::optional<address> const& bind_addr = boost::optional<address>()
|
2017-07-15 02:59:20 +02:00
|
|
|
, resolver_flags resolve_flags = resolver_flags{}
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2017-09-28 10:11:20 +02:00
|
|
|
, i2p_connection* i2p_conn = nullptr
|
2009-08-20 05:19:12 +02:00
|
|
|
#endif
|
|
|
|
);
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2013-08-12 02:51:49 +02:00
|
|
|
void close(bool force = false);
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
socket_type const& socket() const { return m_sock; }
|
2009-05-15 23:23:41 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<tcp::endpoint> const& endpoints() const { return m_endpoints; }
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
private:
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2015-06-28 00:03:03 +02:00
|
|
|
void connect_i2p_tracker(char const* destination);
|
2009-08-20 05:19:12 +02:00
|
|
|
void on_i2p_resolve(error_code const& e
|
|
|
|
, char const* destination);
|
|
|
|
#endif
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_resolve(error_code const& e
|
2014-07-06 21:18:00 +02:00
|
|
|
, std::vector<address> const& addresses);
|
2014-10-03 22:56:57 +02:00
|
|
|
void connect();
|
2008-05-19 00:14:55 +02:00
|
|
|
void on_connect(error_code const& e);
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_write(error_code const& e);
|
|
|
|
void on_read(error_code const& e, std::size_t bytes_transferred);
|
2016-08-31 18:45:45 +02:00
|
|
|
static void on_timeout(std::weak_ptr<http_connection> p
|
2008-05-03 18:05:42 +02:00
|
|
|
, error_code const& e);
|
|
|
|
void on_assign_bandwidth(error_code const& e);
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2017-09-04 00:13:08 +02:00
|
|
|
void callback(error_code e, span<char> data = {});
|
2007-11-18 05:12:35 +01:00
|
|
|
|
2017-01-17 03:51:49 +01:00
|
|
|
aux::vector<char> m_recvbuffer;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
std::string m_hostname;
|
|
|
|
std::string m_url;
|
|
|
|
std::string m_user_agent;
|
|
|
|
|
2017-01-15 00:53:25 +01:00
|
|
|
aux::vector<tcp::endpoint> m_endpoints;
|
2015-11-07 23:18:45 +01:00
|
|
|
|
|
|
|
// if the current connection attempt fails, we'll connect to the
|
|
|
|
// endpoint with this index (in m_endpoints) next
|
|
|
|
int m_next_ep;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-02-08 17:03:09 +01:00
|
|
|
socket_type m_sock;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2015-08-08 22:19:44 +02:00
|
|
|
ssl::context* m_ssl_ctx;
|
2014-07-06 21:18:00 +02:00
|
|
|
bool m_own_ssl_context;
|
|
|
|
#endif
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
i2p_connection* m_i2p_conn;
|
2008-01-27 23:39:50 +01:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
resolver_interface& m_resolver;
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
http_parser m_parser;
|
|
|
|
http_handler m_handler;
|
2007-09-10 01:52:34 +02:00
|
|
|
http_connect_handler m_connect_handler;
|
2008-10-22 21:40:32 +02:00
|
|
|
http_filter_handler m_filter_handler;
|
2007-03-27 09:04:31 +02:00
|
|
|
deadline_timer m_timer;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2012-01-21 09:18:41 +01:00
|
|
|
time_duration m_read_timeout;
|
|
|
|
time_duration m_completion_timeout;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// the timer fires every 250 millisecond as long
|
|
|
|
// as all the quota was used.
|
|
|
|
deadline_timer m_limiter_timer;
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point m_last_receive;
|
|
|
|
time_point m_start_time;
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// specifies whether or not the connection is
|
|
|
|
// configured to use a proxy
|
2015-08-25 04:18:10 +02:00
|
|
|
aux::proxy_settings m_proxy;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2017-11-06 01:55:15 +01:00
|
|
|
// the address to bind to. unset means do not bind
|
2017-04-28 05:34:39 +02:00
|
|
|
boost::optional<address> m_bind_addr;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-02-08 17:03:09 +01:00
|
|
|
// if username password was passed in, remember it in case we need to
|
|
|
|
// re-issue the request for a redirect
|
|
|
|
std::string m_auth;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int m_read_pos;
|
|
|
|
|
|
|
|
// the number of redirects to follow (in sequence)
|
|
|
|
int m_redirects;
|
|
|
|
|
2012-10-19 03:28:47 +02:00
|
|
|
// maximum size of bottled buffer
|
|
|
|
int m_max_bottled_buffer_size;
|
2016-04-08 03:44:24 +02:00
|
|
|
|
2007-04-03 22:00:47 +02:00
|
|
|
// the current download limit, in bytes per second
|
|
|
|
// 0 is unlimited.
|
|
|
|
int m_rate_limit;
|
|
|
|
|
|
|
|
// the number of bytes we are allowed to receive
|
|
|
|
int m_download_quota;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the priority we have in the connection queue.
|
|
|
|
// 0 is normal, 1 is high
|
|
|
|
int m_priority;
|
|
|
|
|
|
|
|
// used for DNS lookups
|
2017-05-28 21:01:46 +02:00
|
|
|
resolver_flags m_resolve_flags;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint16_t m_port;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// bottled means that the handler is called once, when
|
|
|
|
// everything is received (and buffered in memory).
|
|
|
|
// non bottled means that once the headers have been
|
|
|
|
// received, data is streamed to the handler
|
|
|
|
bool m_bottled;
|
|
|
|
|
|
|
|
// set to true the first time the handler is called
|
|
|
|
bool m_called;
|
|
|
|
|
2007-04-03 22:00:47 +02:00
|
|
|
// only hand out new quota 4 times a second if the
|
|
|
|
// quota is 0. If it isn't 0 wait for it to reach
|
|
|
|
// 0 and continue to hand out quota at that time.
|
|
|
|
bool m_limiter_timer_active;
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
// true if the connection is using ssl
|
|
|
|
bool m_ssl;
|
2008-01-31 09:24:01 +01:00
|
|
|
|
2008-05-19 00:14:55 +02:00
|
|
|
bool m_abort;
|
2014-10-16 07:33:09 +02:00
|
|
|
|
|
|
|
// true while waiting for an async_connect
|
|
|
|
bool m_connecting;
|
2007-03-27 09:04:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|