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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/http_connection.hpp"
|
2007-12-02 19:34:37 +01:00
|
|
|
#include "libtorrent/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"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
2007-04-03 22:00:47 +02:00
|
|
|
#include <boost/bind.hpp>
|
2007-04-10 11:11:32 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2007-03-27 09:04:31 +02:00
|
|
|
#include <asio/ip/tcp.hpp>
|
2007-04-10 11:11:32 +02:00
|
|
|
#include <string>
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-04-03 22:00:47 +02:00
|
|
|
using boost::bind;
|
2007-04-02 20:51:35 +02:00
|
|
|
|
2008-01-30 19:32:13 +01:00
|
|
|
namespace libtorrent {
|
|
|
|
|
|
|
|
namespace
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2008-01-30 19:32:13 +01:00
|
|
|
char to_lower(char c) { return std::tolower(c); }
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { max_bottled_buffer = 1024 * 1024 };
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-09-11 19:45:20 +02:00
|
|
|
|
2007-04-19 22:52:29 +02:00
|
|
|
void http_connection::get(std::string const& url, time_duration timeout
|
2008-01-30 19:32:13 +01:00
|
|
|
, proxy_settings const* ps, int handle_redirects, std::string const& user_agent)
|
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;
|
|
|
|
int port;
|
2007-05-22 22:44:18 +02:00
|
|
|
boost::tie(protocol, auth, hostname, port, path) = parse_url_components(url);
|
2008-01-27 23:39:50 +01:00
|
|
|
|
|
|
|
bool ssl = false;
|
|
|
|
if (protocol == "https") ssl = true;
|
|
|
|
#ifndef TORRENT_USE_OPENSSL
|
|
|
|
if (ssl)
|
|
|
|
{
|
|
|
|
callback(asio::error::not_supported);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
std::stringstream headers;
|
2008-01-27 23:39:50 +01:00
|
|
|
if (ps && (ps->type == proxy_settings::http
|
|
|
|
|| ps->type == proxy_settings::http_pw)
|
|
|
|
&& !ssl)
|
|
|
|
{
|
|
|
|
// if we're using an http proxy and not an ssl
|
|
|
|
// connection, just do a regular http proxy request
|
2008-01-30 19:32:13 +01:00
|
|
|
headers << "GET " << url << " HTTP/1.0\r\n";
|
2008-01-27 23:39:50 +01:00
|
|
|
if (ps->type == proxy_settings::http_pw)
|
|
|
|
headers << "Proxy-Authorization: Basic " << base64encode(
|
|
|
|
ps->username + ":" + ps->password) << "\r\n";
|
|
|
|
hostname = ps->hostname;
|
|
|
|
port = ps->port;
|
|
|
|
ps = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
headers << "GET " << path << " HTTP/1.0\r\n"
|
2008-01-30 19:32:13 +01:00
|
|
|
"Host:" << hostname << "\r\n";
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
|
|
|
|
2007-05-22 22:44:18 +02:00
|
|
|
if (!auth.empty())
|
|
|
|
headers << "Authorization: Basic " << base64encode(auth) << "\r\n";
|
2008-01-30 19:32:13 +01:00
|
|
|
|
|
|
|
if (!user_agent.empty())
|
|
|
|
headers << "User-Agent: " << user_agent << "\r\n";
|
|
|
|
|
|
|
|
headers <<
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Accept-Encoding: gzip\r\n"
|
|
|
|
"\r\n";
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
sendbuffer = headers.str();
|
2008-01-27 23:39:50 +01:00
|
|
|
start(hostname, boost::lexical_cast<std::string>(port), timeout, ps
|
|
|
|
, ssl, handle_redirects);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::start(std::string const& hostname, std::string const& port
|
2008-01-27 23:39:50 +01:00
|
|
|
, time_duration timeout, proxy_settings const* ps, bool ssl, int handle_redirects)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2008-01-12 02:41:56 +01:00
|
|
|
m_redirects = handle_redirects;
|
2008-01-27 23:39:50 +01:00
|
|
|
if (ps) m_proxy = *ps;
|
|
|
|
|
|
|
|
m_ssl = ssl;
|
2007-03-27 09:04:31 +02:00
|
|
|
m_timeout = timeout;
|
2007-12-30 00:37:50 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
m_timer.expires_from_now(m_timeout, ec);
|
2007-04-01 17:39:08 +02:00
|
|
|
m_timer.async_wait(bind(&http_connection::on_timeout
|
|
|
|
, boost::weak_ptr<http_connection>(shared_from_this()), _1));
|
|
|
|
m_called = false;
|
2008-01-12 02:41:56 +01:00
|
|
|
m_parser.reset();
|
|
|
|
m_recvbuffer.clear();
|
|
|
|
m_read_pos = 0;
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2007-03-29 18:23:11 +02:00
|
|
|
if (m_sock.is_open() && m_hostname == hostname && m_port == port)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
|
|
|
asio::async_write(m_sock, asio::buffer(sendbuffer)
|
|
|
|
, bind(&http_connection::on_write, shared_from_this(), _1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-12-30 00:37:50 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
m_sock.close(ec);
|
2008-01-27 23:39:50 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (m_ssl)
|
|
|
|
{
|
|
|
|
m_sock.instantiate<ssl_stream<socket_type> >(m_resolver.get_io_service());
|
|
|
|
ssl_stream<socket_type>& s = m_sock.get<ssl_stream<socket_type> >();
|
|
|
|
bool ret = instantiate_connection(m_resolver.get_io_service(), m_proxy, s.next_layer());
|
|
|
|
TORRENT_ASSERT(ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_sock.instantiate<socket_type>(m_resolver.get_io_service());
|
|
|
|
bool ret = instantiate_connection(m_resolver.get_io_service()
|
|
|
|
, m_proxy, m_sock.get<socket_type>());
|
|
|
|
TORRENT_ASSERT(ret);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
bool ret = instantiate_connection(m_resolver.get_io_service(), m_proxy, m_sock);
|
|
|
|
TORRENT_ASSERT(ret);
|
|
|
|
#endif
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
tcp::resolver::query query(hostname, port);
|
|
|
|
m_resolver.async_resolve(query, bind(&http_connection::on_resolve
|
|
|
|
, shared_from_this(), _1, _2));
|
|
|
|
m_hostname = hostname;
|
|
|
|
m_port = port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
void http_connection::on_connect_timeout()
|
|
|
|
{
|
|
|
|
if (m_connection_ticket > -1) m_cc.done(m_connection_ticket);
|
|
|
|
m_connection_ticket = -1;
|
|
|
|
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(asio::error::timed_out);
|
2007-05-05 02:29:33 +02:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2007-04-01 17:39:08 +02:00
|
|
|
void http_connection::on_timeout(boost::weak_ptr<http_connection> p
|
|
|
|
, asio::error_code const& e)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
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
|
|
|
if (c->m_connection_ticket > -1) c->m_cc.done(c->m_connection_ticket);
|
|
|
|
c->m_connection_ticket = -1;
|
|
|
|
|
|
|
|
if (e == asio::error::operation_aborted) return;
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
if (c->m_last_receive + c->m_timeout < time_now())
|
2007-04-02 20:51:35 +02:00
|
|
|
{
|
2007-11-18 05:12:35 +01:00
|
|
|
c->callback(asio::error::timed_out);
|
|
|
|
c->close();
|
2007-04-02 20:51:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-16 00:55:30 +01:00
|
|
|
if (!c->m_sock.is_open()) return;
|
2007-12-30 00:37:50 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
c->m_timer.expires_at(c->m_last_receive + c->m_timeout, ec);
|
2007-04-02 20:51:35 +02:00
|
|
|
c->m_timer.async_wait(bind(&http_connection::on_timeout, p, _1));
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::close()
|
|
|
|
{
|
2007-12-30 00:37:50 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
m_timer.cancel(ec);
|
|
|
|
m_limiter_timer.cancel(ec);
|
|
|
|
m_sock.close(ec);
|
2007-03-27 09:04:31 +02:00
|
|
|
m_hostname.clear();
|
|
|
|
m_port.clear();
|
2007-05-05 02:29:33 +02:00
|
|
|
|
|
|
|
if (m_connection_ticket > -1) m_cc.done(m_connection_ticket);
|
|
|
|
m_connection_ticket = -1;
|
2007-11-16 00:55:30 +01:00
|
|
|
|
2007-11-18 05:12:35 +01:00
|
|
|
m_handler.clear();
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::on_resolve(asio::error_code const& e
|
|
|
|
, tcp::resolver::iterator i)
|
|
|
|
{
|
|
|
|
if (e)
|
|
|
|
{
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e);
|
2007-03-27 09:04:31 +02:00
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i != tcp::resolver::iterator());
|
2007-05-05 02:29:33 +02:00
|
|
|
m_cc.enqueue(bind(&http_connection::connect, shared_from_this(), _1, *i)
|
|
|
|
, bind(&http_connection::on_connect_timeout, shared_from_this())
|
|
|
|
, m_timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::connect(int ticket, tcp::endpoint target_address)
|
|
|
|
{
|
|
|
|
m_connection_ticket = ticket;
|
|
|
|
m_sock.async_connect(target_address, boost::bind(&http_connection::on_connect
|
2007-03-27 09:04:31 +02:00
|
|
|
, shared_from_this(), _1/*, ++i*/));
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::on_connect(asio::error_code const& e
|
|
|
|
/*, tcp::resolver::iterator i*/)
|
|
|
|
{
|
|
|
|
if (!e)
|
|
|
|
{
|
2007-04-05 00:27:36 +02:00
|
|
|
m_last_receive = time_now();
|
2007-09-10 01:52:34 +02:00
|
|
|
if (m_connect_handler) m_connect_handler(*this);
|
2007-03-27 09:04:31 +02:00
|
|
|
asio::async_write(m_sock, asio::buffer(sendbuffer)
|
|
|
|
, bind(&http_connection::on_write, shared_from_this(), _1));
|
|
|
|
}
|
|
|
|
/* else if (i != tcp::resolver::iterator())
|
|
|
|
{
|
|
|
|
// The connection failed. Try the next endpoint in the list.
|
|
|
|
m_sock.close();
|
2007-05-05 02:29:33 +02:00
|
|
|
m_cc.enqueue(bind(&http_connection::connect, shared_from_this(), _1, *i)
|
|
|
|
, bind(&http_connection::on_connect_timeout, shared_from_this())
|
|
|
|
, m_timeout);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
*/ else
|
|
|
|
{
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e);
|
2007-03-27 09:04:31 +02:00
|
|
|
close();
|
2007-11-18 05:12:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::callback(asio::error_code const& e, char const* data, int size)
|
|
|
|
{
|
|
|
|
if (!m_bottled || !m_called)
|
|
|
|
{
|
2008-01-30 19:32:13 +01:00
|
|
|
std::vector<char> buf;
|
|
|
|
if (m_bottled && m_parser.finished())
|
|
|
|
{
|
|
|
|
std::string const& encoding = m_parser.header("content-encoding");
|
|
|
|
if (encoding == "gzip" || encoding == "x-gzip")
|
|
|
|
{
|
|
|
|
std::string error;
|
|
|
|
if (inflate_gzip(data, size, buf, max_bottled_buffer, error))
|
|
|
|
{
|
|
|
|
callback(asio::error::fault, data, size);
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data = &buf[0];
|
|
|
|
size = int(buf.size());
|
|
|
|
}
|
|
|
|
}
|
2007-04-01 17:39:08 +02:00
|
|
|
m_called = true;
|
2007-12-30 00:37:50 +01:00
|
|
|
m_timer.cancel();
|
2007-11-18 05:12:35 +01:00
|
|
|
if (m_handler) m_handler(e, m_parser, data, size);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::on_write(asio::error_code const& e)
|
|
|
|
{
|
|
|
|
if (e)
|
|
|
|
{
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e);
|
2007-03-27 09:04:31 +02:00
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string().swap(sendbuffer);
|
|
|
|
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)
|
|
|
|
on_assign_bandwidth(asio::error_code());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
m_sock.async_read_some(asio::buffer(&m_recvbuffer[0] + m_read_pos
|
2007-04-03 22:00:47 +02:00
|
|
|
, amount_to_read)
|
|
|
|
, bind(&http_connection::on_read
|
|
|
|
, shared_from_this(), _1, _2));
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::on_read(asio::error_code const& e
|
|
|
|
, std::size_t bytes_transferred)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
if (e == asio::error::eof)
|
|
|
|
{
|
2008-01-12 02:41:56 +01:00
|
|
|
TORRENT_ASSERT(bytes_transferred == 0);
|
2007-05-02 00:44:54 +02:00
|
|
|
char const* data = 0;
|
|
|
|
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
|
|
|
{
|
|
|
|
data = m_parser.get_body().begin;
|
|
|
|
size = m_parser.get_body().left();
|
|
|
|
}
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e, data, size);
|
|
|
|
close();
|
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
|
|
|
close();
|
|
|
|
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
|
|
|
|
asio::error_code ec = asio::error::fault;
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (code >= 300 && code < 400)
|
|
|
|
{
|
|
|
|
// attempt a redirect
|
|
|
|
std::string const& url = m_parser.header("location");
|
|
|
|
if (url.empty())
|
|
|
|
{
|
|
|
|
// missing location header
|
|
|
|
callback(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
asio::error_code ec;
|
|
|
|
m_sock.close(ec);
|
2008-01-27 23:39:50 +01:00
|
|
|
get(url, m_timeout, &m_proxy, m_redirects - 1);
|
2008-01-12 02:41:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-04-05 00:27:36 +02:00
|
|
|
m_last_receive = time_now();
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
else if (m_bottled && m_parser.finished())
|
|
|
|
{
|
2007-12-30 00:37:50 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
m_timer.cancel(ec);
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(e, m_parser.get_body().begin, 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;
|
2007-04-05 00:27:36 +02:00
|
|
|
m_last_receive = time_now();
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (int(m_recvbuffer.size()) == m_read_pos)
|
2007-09-11 19:45:20 +02:00
|
|
|
m_recvbuffer.resize((std::min)(m_read_pos + 2048, int(max_bottled_buffer)));
|
|
|
|
if (m_read_pos == max_bottled_buffer)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(asio::error::eof);
|
2007-03-27 09:04:31 +02:00
|
|
|
close();
|
|
|
|
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)
|
|
|
|
on_assign_bandwidth(asio::error_code());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_sock.async_read_some(asio::buffer(&m_recvbuffer[0] + m_read_pos
|
|
|
|
, amount_to_read)
|
|
|
|
, bind(&http_connection::on_read
|
|
|
|
, shared_from_this(), _1, _2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_connection::on_assign_bandwidth(asio::error_code const& e)
|
|
|
|
{
|
2007-04-10 20:53:12 +02:00
|
|
|
if ((e == 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
|
|
|
{
|
2007-11-18 05:12:35 +01:00
|
|
|
callback(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;
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
m_sock.async_read_some(asio::buffer(&m_recvbuffer[0] + m_read_pos
|
2007-04-03 22:00:47 +02:00
|
|
|
, amount_to_read)
|
2007-03-27 09:04:31 +02:00
|
|
|
, bind(&http_connection::on_read
|
|
|
|
, shared_from_this(), _1, _2));
|
2007-04-03 22:00:47 +02:00
|
|
|
|
2007-12-30 00:37:50 +01:00
|
|
|
asio::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);
|
2007-04-03 22:00:47 +02:00
|
|
|
m_limiter_timer.async_wait(bind(&http_connection::on_assign_bandwidth
|
|
|
|
, 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)
|
|
|
|
{
|
2007-12-30 00:37:50 +01:00
|
|
|
asio::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);
|
2007-04-03 22:00:47 +02:00
|
|
|
m_limiter_timer.async_wait(bind(&http_connection::on_assign_bandwidth
|
|
|
|
, shared_from_this(), _1));
|
|
|
|
}
|
|
|
|
m_rate_limit = limit;
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|