2008-07-01 20:59:13 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, 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-11-18 05:12:35 +01:00
|
|
|
#include "test.hpp"
|
2015-04-24 05:48:08 +02:00
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
#include "test_utils.hpp"
|
|
|
|
|
2007-11-18 05:12:35 +01:00
|
|
|
#include "libtorrent/socket.hpp"
|
2009-12-16 11:42:34 +01:00
|
|
|
#include "libtorrent/socket_io.hpp" // print_endpoint
|
2007-11-18 05:12:35 +01:00
|
|
|
#include "libtorrent/http_connection.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/resolver.hpp"
|
2019-03-24 22:14:35 +01:00
|
|
|
#include "libtorrent/random.hpp"
|
2007-11-27 01:06:59 +01:00
|
|
|
|
|
|
|
#include <fstream>
|
2009-12-15 14:11:07 +01:00
|
|
|
#include <iostream>
|
2007-11-27 01:06:59 +01:00
|
|
|
#include <boost/optional.hpp>
|
2007-11-18 05:12:35 +01:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2007-11-18 05:12:35 +01:00
|
|
|
|
2018-01-30 14:56:07 +01:00
|
|
|
namespace {
|
|
|
|
|
2007-11-18 05:12:35 +01:00
|
|
|
io_service ios;
|
2014-07-06 21:18:00 +02:00
|
|
|
resolver res(ios);
|
2007-11-18 05:12:35 +01:00
|
|
|
|
|
|
|
int connect_handler_called = 0;
|
|
|
|
int handler_called = 0;
|
|
|
|
int data_size = 0;
|
|
|
|
int http_status = 0;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code g_error_code;
|
2007-11-27 01:06:59 +01:00
|
|
|
char data_buffer[4000];
|
2007-11-18 05:12:35 +01:00
|
|
|
|
|
|
|
void print_http_header(http_parser const& p)
|
|
|
|
{
|
2016-10-09 23:00:20 +02:00
|
|
|
std::cout << time_now_string() << " < " << p.status_code() << " " << p.message() << std::endl;
|
2007-11-18 05:12:35 +01:00
|
|
|
|
2017-04-04 22:42:37 +02:00
|
|
|
for (auto const& i : p.headers())
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
2017-04-04 22:42:37 +02:00
|
|
|
std::cout << time_now_string() << " < " << i.first << ": " << i.second << std::endl;
|
2007-11-18 05:12:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 14:56:07 +01:00
|
|
|
void http_connect_handler_test(http_connection& c)
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
|
|
|
++connect_handler_called;
|
|
|
|
TEST_CHECK(c.socket().is_open());
|
2008-11-05 04:17:41 +01:00
|
|
|
error_code ec;
|
2016-11-12 19:50:49 +01:00
|
|
|
std::cout << time_now_string() << " connected to: "
|
|
|
|
<< print_endpoint(c.socket().remote_endpoint(ec)) << std::endl;
|
2010-08-03 11:08:37 +02:00
|
|
|
// this is not necessarily true when using a proxy and proxying hostnames
|
|
|
|
// TEST_CHECK(c.socket().remote_endpoint(ec).address() == address::from_string("127.0.0.1", ec));
|
2007-11-18 05:12:35 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 14:56:07 +01:00
|
|
|
void http_handler_test(error_code const& ec, http_parser const& parser
|
2018-01-29 15:58:22 +01:00
|
|
|
, span<char const> data, http_connection&)
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
|
|
|
++handler_called;
|
2018-01-29 15:58:22 +01:00
|
|
|
data_size = int(data.size());
|
2008-05-03 18:05:42 +02:00
|
|
|
g_error_code = ec;
|
2017-09-04 00:13:08 +02:00
|
|
|
TORRENT_ASSERT(data.empty() || parser.finished());
|
2007-11-18 05:12:35 +01:00
|
|
|
|
2007-11-27 01:06:59 +01:00
|
|
|
if (parser.header_finished())
|
|
|
|
{
|
|
|
|
http_status = parser.status_code();
|
|
|
|
if (http_status == 200)
|
|
|
|
{
|
2018-11-05 13:08:55 +01:00
|
|
|
TEST_CHECK(span<char>(data_buffer, data.size()) == data);
|
2007-11-27 01:06:59 +01:00
|
|
|
}
|
|
|
|
}
|
2007-11-18 05:12:35 +01:00
|
|
|
print_http_header(parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_globals()
|
|
|
|
{
|
|
|
|
connect_handler_called = 0;
|
|
|
|
handler_called = 0;
|
|
|
|
data_size = 0;
|
|
|
|
http_status = 0;
|
2008-05-03 18:05:42 +02:00
|
|
|
g_error_code = error_code();
|
2007-11-18 05:12:35 +01:00
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
void run_test(std::string const& url, int size, int status, int connected
|
2015-08-25 04:18:10 +02:00
|
|
|
, boost::optional<error_code> ec, aux::proxy_settings const& ps
|
2015-02-08 17:03:09 +01:00
|
|
|
, std::string const& auth = std::string())
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
|
|
|
reset_globals();
|
|
|
|
|
2016-10-09 23:00:20 +02:00
|
|
|
std::cout << " ===== TESTING: " << url << " =====" << std::endl;
|
2007-11-18 05:12:35 +01:00
|
|
|
|
2016-10-09 23:00:20 +02:00
|
|
|
std::cout << time_now_string()
|
2013-08-04 01:30:37 +02:00
|
|
|
<< " expecting: size: " << size
|
2009-07-26 04:17:21 +02:00
|
|
|
<< " status: " << status
|
|
|
|
<< " connected: " << connected
|
|
|
|
<< " error: " << (ec?ec->message():"no error") << std::endl;
|
|
|
|
|
2020-03-13 12:15:39 +01:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
ssl::context ssl_ctx(ssl::context::sslv23_client);
|
|
|
|
ssl_ctx.set_verify_mode(ssl::context::verify_none);
|
|
|
|
#endif
|
|
|
|
|
2016-08-31 18:45:45 +02:00
|
|
|
std::shared_ptr<http_connection> h = std::make_shared<http_connection>(ios
|
2020-03-13 12:15:39 +01:00
|
|
|
, res, &::http_handler_test, true, 1024*1024, &::http_connect_handler_test
|
|
|
|
, http_filter_handler()
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
, &ssl_ctx
|
|
|
|
#endif
|
|
|
|
);
|
2018-05-20 12:08:46 +02:00
|
|
|
h->get(url, seconds(5), 0, &ps, 5, "test/user-agent", boost::none, resolver_flags{}, auth);
|
2007-11-18 05:12:35 +01:00
|
|
|
ios.reset();
|
2008-11-05 04:17:41 +01:00
|
|
|
error_code e;
|
|
|
|
ios.run(e);
|
2016-10-09 23:00:20 +02:00
|
|
|
if (e) std::cout << time_now_string() << " run failed: " << e.message() << std::endl;
|
|
|
|
|
2016-11-12 19:50:49 +01:00
|
|
|
std::string const n = time_now_string();
|
|
|
|
std::cout << n << " connect_handler_called: " << connect_handler_called << std::endl;
|
|
|
|
std::cout << n << " handler_called: " << handler_called << std::endl;
|
|
|
|
std::cout << n << " status: " << http_status << std::endl;
|
|
|
|
std::cout << n << " size: " << data_size << std::endl;
|
|
|
|
std::cout << n << " expected-size: " << size << std::endl;
|
|
|
|
std::cout << n << " error_code: " << g_error_code.message() << std::endl;
|
2007-11-19 04:48:45 +01:00
|
|
|
TEST_CHECK(connect_handler_called == connected);
|
2015-06-20 22:36:23 +02:00
|
|
|
TEST_CHECK(handler_called == 1);
|
2007-11-18 05:12:35 +01:00
|
|
|
TEST_CHECK(data_size == size || size == -1);
|
2008-05-03 18:05:42 +02:00
|
|
|
TEST_CHECK(!ec || g_error_code == *ec);
|
2007-11-19 04:48:45 +01:00
|
|
|
TEST_CHECK(http_status == status || status == -1);
|
2007-11-18 05:12:35 +01:00
|
|
|
}
|
|
|
|
|
2015-06-21 18:41:50 +02:00
|
|
|
void write_test_file()
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
2019-03-24 22:14:35 +01:00
|
|
|
aux::random_bytes(data_buffer);
|
2015-06-21 18:41:50 +02:00
|
|
|
error_code ec;
|
2017-07-15 02:59:20 +02:00
|
|
|
file test_file("test_file", open_mode::write_only, ec);
|
2015-06-21 18:41:50 +02:00
|
|
|
TEST_CHECK(!ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("file error: %s\n", ec.message().c_str());
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t b = { data_buffer, 3216};
|
2016-10-27 02:40:56 +02:00
|
|
|
test_file.writev(0, b, ec);
|
2015-06-21 18:41:50 +02:00
|
|
|
TEST_CHECK(!ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("file error: %s\n", ec.message().c_str());
|
2015-06-21 18:41:50 +02:00
|
|
|
test_file.close();
|
|
|
|
}
|
|
|
|
|
2015-06-22 05:06:08 +02:00
|
|
|
enum suite_flags_t
|
|
|
|
{
|
|
|
|
flag_chunked_encoding = 1,
|
|
|
|
flag_keepalive = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
void run_suite(std::string const& protocol
|
|
|
|
, settings_pack::proxy_type_t proxy_type
|
|
|
|
, int flags = flag_keepalive)
|
2015-06-21 18:41:50 +02:00
|
|
|
{
|
|
|
|
write_test_file();
|
|
|
|
|
|
|
|
// starting the web server will also generate test_file.gz (from test_file)
|
|
|
|
// so it has to happen after we write test_file
|
2015-06-22 05:06:08 +02:00
|
|
|
int port = start_web_server(protocol == "https"
|
2016-04-20 06:45:32 +02:00
|
|
|
, (flags & flag_chunked_encoding) != 0
|
|
|
|
, (flags & flag_keepalive) != 0);
|
2015-06-21 18:41:50 +02:00
|
|
|
|
2015-08-25 04:18:10 +02:00
|
|
|
aux::proxy_settings ps;
|
2015-06-21 18:41:50 +02:00
|
|
|
ps.hostname = "127.0.0.1";
|
|
|
|
ps.username = "testuser";
|
|
|
|
ps.password = "testpass";
|
|
|
|
ps.type = proxy_type;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ps.type != settings_pack::none)
|
2018-01-29 15:58:22 +01:00
|
|
|
ps.port = aux::numeric_cast<std::uint16_t>(start_proxy(ps.type));
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2018-03-22 17:01:38 +01:00
|
|
|
using err = boost::optional<error_code>;
|
2008-05-19 00:14:55 +02:00
|
|
|
|
2010-01-24 00:57:11 +01:00
|
|
|
char url[256];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(url, sizeof(url), "%s://127.0.0.1:%d/", protocol.c_str(), port);
|
2010-01-24 00:57:11 +01:00
|
|
|
std::string url_base(url);
|
|
|
|
|
|
|
|
run_test(url_base + "relative/redirect", 3216, 200, 2, error_code(), ps);
|
|
|
|
run_test(url_base + "redirect", 3216, 200, 2, error_code(), ps);
|
2013-11-22 04:05:38 +01:00
|
|
|
// the actual error code for an abort caused by too many
|
|
|
|
// redirects is a bit unpredictable. under SSL for instance,
|
|
|
|
// it will be an ungraceful shutdown
|
|
|
|
run_test(url_base + "infinite_redirect", 0, 301, 6, err(), ps);
|
2010-01-24 00:57:11 +01:00
|
|
|
run_test(url_base + "test_file", 3216, 200, 1, error_code(), ps);
|
|
|
|
run_test(url_base + "test_file.gz", 3216, 200, 1, error_code(), ps);
|
|
|
|
run_test(url_base + "non-existing-file", -1, 404, 1, err(), ps);
|
2015-02-08 17:03:09 +01:00
|
|
|
run_test(url_base + "password_protected", 3216, 200, 1, error_code(), ps
|
|
|
|
, "testuser:testpass");
|
2010-08-28 22:32:58 +02:00
|
|
|
|
2017-08-10 11:15:46 +02:00
|
|
|
// try a very long path
|
|
|
|
std::string path;
|
|
|
|
for (int i = 0; i < 6000; ++i)
|
|
|
|
{
|
|
|
|
path += static_cast<char>(i % 26) + 'a';
|
|
|
|
}
|
|
|
|
run_test(url_base + path, 0, 404, 1, err(), ps);
|
|
|
|
|
2010-08-28 22:32:58 +02:00
|
|
|
// only run the tests to handle NX_DOMAIN if we have a proper internet
|
|
|
|
// connection that doesn't inject false DNS responses (like Comcast does)
|
|
|
|
hostent* h = gethostbyname("non-existent-domain.se");
|
2016-06-19 01:24:27 +02:00
|
|
|
std::printf("gethostbyname(\"non-existent-domain.se\") = %p. h_errno = %d\n"
|
|
|
|
, static_cast<void*>(h), h_errno);
|
2016-07-09 22:26:26 +02:00
|
|
|
if (h == nullptr && h_errno == HOST_NOT_FOUND)
|
2010-08-28 22:32:58 +02:00
|
|
|
{
|
2019-11-19 09:39:19 +01:00
|
|
|
// if we have a proxy, we'll be able to connect to it, we will just get an
|
|
|
|
// error from the proxy saying it failed to connect to the final target
|
|
|
|
if (protocol == "http" && (ps.type == settings_pack::http || ps.type == settings_pack::http_pw))
|
|
|
|
run_test(protocol + "://non-existent-domain.se/non-existing-file", -1, -1, 1, err(), ps);
|
|
|
|
else
|
|
|
|
run_test(protocol + "://non-existent-domain.se/non-existing-file", -1, -1, 0, err(), ps);
|
2010-08-28 22:32:58 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ps.type != settings_pack::none)
|
2008-01-27 23:39:50 +01:00
|
|
|
stop_proxy(ps.port);
|
2015-06-21 18:41:50 +02:00
|
|
|
stop_web_server();
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 14:56:07 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2015-06-21 18:41:50 +02:00
|
|
|
TORRENT_TEST(no_proxy_ssl) { run_suite("https", settings_pack::none); }
|
|
|
|
TORRENT_TEST(http_ssl) { run_suite("https", settings_pack::http); }
|
|
|
|
TORRENT_TEST(http_pw_ssl) { run_suite("https", settings_pack::http_pw); }
|
2019-11-19 09:39:19 +01:00
|
|
|
TORRENT_TEST(socks5_proxy_ssl) { run_suite("https", settings_pack::socks5); }
|
|
|
|
TORRENT_TEST(socks5_pw_proxy_ssl) { run_suite("https", settings_pack::socks5_pw); }
|
2015-06-21 18:41:50 +02:00
|
|
|
#endif // USE_OPENSSL
|
|
|
|
|
2019-11-19 09:39:19 +01:00
|
|
|
TORRENT_TEST(http_proxy) { run_suite("http", settings_pack::http); }
|
|
|
|
TORRENT_TEST(http__pwproxy) { run_suite("http", settings_pack::http_pw); }
|
|
|
|
TORRENT_TEST(socks5_proxy) { run_suite("http", settings_pack::socks5); }
|
|
|
|
TORRENT_TEST(socks5_pw_proxy) { run_suite("http", settings_pack::socks5_pw); }
|
|
|
|
|
2015-06-22 05:06:08 +02:00
|
|
|
TORRENT_TEST(no_keepalive)
|
|
|
|
{
|
|
|
|
run_suite("http", settings_pack::none, 0);
|
|
|
|
}
|