2007-03-27 09:04:31 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Arvid Norberg
|
|
|
|
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
|
|
|
|
|
|
|
|
#include <boost/function.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#include <boost/enable_shared_from_this.hpp>
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <vector>
|
2008-05-19 00:14:55 +02:00
|
|
|
#include <list>
|
2007-03-27 09:04:31 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "libtorrent/socket.hpp"
|
2008-01-31 09:24:01 +01:00
|
|
|
#include "libtorrent/http_parser.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/time.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"
|
2008-01-31 09:24:01 +01:00
|
|
|
#include "libtorrent/session_settings.hpp"
|
2008-01-27 23:39:50 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
#include "libtorrent/ssl_stream.hpp"
|
|
|
|
#include "libtorrent/variant_stream.hpp"
|
|
|
|
#endif
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
struct http_connection;
|
2008-02-25 11:28:53 +01:00
|
|
|
class connection_queue;
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
typedef boost::function<void(error_code const&
|
2008-05-17 02:27:26 +02:00
|
|
|
, http_parser const&, char const* data, int size, http_connection&)> http_handler;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
typedef boost::function<void(http_connection&)> http_connect_handler;
|
|
|
|
|
2008-10-22 21:40:32 +02:00
|
|
|
typedef boost::function<void(http_connection&, std::list<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
|
|
|
|
struct http_connection : boost::enable_shared_from_this<http_connection>, boost::noncopyable
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
http_connection(io_service& ios, connection_queue& cc
|
2007-09-10 01:52:34 +02:00
|
|
|
, http_handler const& handler, bool bottled = true
|
2008-10-22 21:40:32 +02:00
|
|
|
, http_connect_handler const& ch = http_connect_handler()
|
|
|
|
, http_filter_handler const& fh = http_filter_handler())
|
2007-03-27 09:04:31 +02:00
|
|
|
: m_sock(ios)
|
|
|
|
, m_read_pos(0)
|
|
|
|
, m_resolver(ios)
|
|
|
|
, m_handler(handler)
|
2007-09-10 01:52:34 +02:00
|
|
|
, m_connect_handler(ch)
|
2008-10-22 21:40:32 +02:00
|
|
|
, m_filter_handler(fh)
|
2007-03-27 09:04:31 +02:00
|
|
|
, m_timer(ios)
|
2007-04-05 00:27:36 +02:00
|
|
|
, m_last_receive(time_now())
|
2007-03-27 09:04:31 +02:00
|
|
|
, m_bottled(bottled)
|
2007-04-01 17:39:08 +02:00
|
|
|
, m_called(false)
|
2007-04-03 22:00:47 +02:00
|
|
|
, m_rate_limit(0)
|
|
|
|
, m_download_quota(0)
|
|
|
|
, m_limiter_timer_active(false)
|
|
|
|
, m_limiter_timer(ios)
|
2008-01-12 02:41:56 +01:00
|
|
|
, m_redirects(5)
|
2007-05-05 02:29:33 +02:00
|
|
|
, m_connection_ticket(-1)
|
|
|
|
, m_cc(cc)
|
2008-01-27 23:39:50 +01:00
|
|
|
, m_ssl(false)
|
2008-03-12 08:44:27 +01:00
|
|
|
, m_priority(0)
|
2008-05-19 00:14:55 +02:00
|
|
|
, m_abort(false)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_handler.empty());
|
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; }
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
std::string sendbuffer;
|
|
|
|
|
2007-04-19 22:52:29 +02:00
|
|
|
void get(std::string const& url, time_duration timeout = seconds(30)
|
2008-03-12 08:44:27 +01:00
|
|
|
, int prio = 0, proxy_settings const* ps = 0, int handle_redirects = 5
|
2008-01-31 09:24:01 +01:00
|
|
|
, std::string const& user_agent = "", address const& bind_addr = address_v4::any());
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
void start(std::string const& hostname, std::string const& port
|
2008-03-12 08:44:27 +01:00
|
|
|
, time_duration timeout, int prio = 0, proxy_settings const* ps = 0
|
|
|
|
, bool ssl = false, int handle_redirect = 5
|
|
|
|
, address const& bind_addr = address_v4::any());
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
void close();
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
variant_stream<socket_type, ssl_stream<socket_type> > const& socket() const { return m_sock; }
|
|
|
|
#else
|
|
|
|
socket_type const& socket() const { return m_sock; }
|
|
|
|
#endif
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
private:
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_resolve(error_code const& e
|
2007-03-27 09:04:31 +02:00
|
|
|
, tcp::resolver::iterator i);
|
2008-05-19 00:14:55 +02:00
|
|
|
void queue_connect();
|
2007-05-05 02:29:33 +02:00
|
|
|
void connect(int ticket, tcp::endpoint target_address);
|
|
|
|
void on_connect_timeout();
|
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);
|
2007-04-01 17:39:08 +02:00
|
|
|
static void on_timeout(boost::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
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void callback(error_code const& e, char const* data = 0, int size = 0);
|
2007-11-18 05:12:35 +01:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
std::vector<char> m_recvbuffer;
|
2008-01-27 23:39:50 +01:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
variant_stream<socket_type, ssl_stream<socket_type> > m_sock;
|
|
|
|
#else
|
|
|
|
socket_type m_sock;
|
|
|
|
#endif
|
2007-03-27 09:04:31 +02:00
|
|
|
int m_read_pos;
|
|
|
|
tcp::resolver m_resolver;
|
|
|
|
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;
|
2007-04-05 00:27:36 +02:00
|
|
|
time_duration m_timeout;
|
|
|
|
ptime m_last_receive;
|
2007-03-27 09:04:31 +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;
|
2007-04-01 17:39:08 +02:00
|
|
|
// set to true the first time the handler is called
|
|
|
|
bool m_called;
|
2007-03-27 09:04:31 +02:00
|
|
|
std::string m_hostname;
|
|
|
|
std::string m_port;
|
2008-05-18 06:48:06 +02:00
|
|
|
std::string m_url;
|
2007-04-03 22:00:47 +02:00
|
|
|
|
2008-05-19 00:14:55 +02:00
|
|
|
std::list<tcp::endpoint> m_endpoints;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// the timer fires every 250 millisecond as long
|
|
|
|
// as all the quota was used.
|
|
|
|
deadline_timer m_limiter_timer;
|
2007-04-19 22:52:29 +02:00
|
|
|
|
2008-01-12 02:41:56 +01:00
|
|
|
// the number of redirects to follow (in sequence)
|
|
|
|
int m_redirects;
|
2007-05-05 02:29:33 +02:00
|
|
|
|
|
|
|
int m_connection_ticket;
|
|
|
|
connection_queue& m_cc;
|
2008-01-27 23:39:50 +01:00
|
|
|
|
|
|
|
// specifies whether or not the connection is
|
|
|
|
// configured to use a proxy
|
|
|
|
proxy_settings m_proxy;
|
|
|
|
|
|
|
|
// true if the connection is using ssl
|
|
|
|
bool m_ssl;
|
2008-01-31 09:24:01 +01:00
|
|
|
|
|
|
|
// the address to bind to. address_v4::any()
|
|
|
|
// means do not bind
|
|
|
|
address m_bind_addr;
|
2008-03-12 08:44:27 +01:00
|
|
|
|
|
|
|
// the priority we have in the connection queue.
|
|
|
|
// 0 is normal, 1 is high
|
|
|
|
int m_priority;
|
2008-05-19 00:14:55 +02:00
|
|
|
|
|
|
|
bool m_abort;
|
2007-03-27 09:04:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|