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-09-10 08:12:41 +02:00
|
|
|
#include <fstream>
|
2011-03-14 03:59:46 +01:00
|
|
|
#include <deque>
|
2007-09-10 08:12:41 +02:00
|
|
|
|
2013-06-23 19:36:00 +02:00
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
2009-12-06 00:41:33 +01:00
|
|
|
#include "libtorrent/http_parser.hpp"
|
2009-12-17 09:50:35 +01:00
|
|
|
#include "libtorrent/thread.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
|
2009-11-02 03:00:41 +01:00
|
|
|
#include "libtorrent/thread.hpp"
|
2006-05-28 21:03:54 +02:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2009-12-06 00:41:33 +01:00
|
|
|
#include <boost/bind.hpp>
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
#include "test.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2008-01-02 04:18:29 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2008-05-14 07:29:42 +02:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2009-12-15 14:11:07 +01:00
|
|
|
#include "libtorrent/socket_io.hpp" // print_endpoint
|
2010-10-12 10:57:43 +02:00
|
|
|
#include "libtorrent/socket_type.hpp"
|
|
|
|
#include "libtorrent/instantiate_connection.hpp"
|
2013-07-28 17:06:28 +02:00
|
|
|
#include "setup_transfer.hpp"
|
2010-10-12 10:57:43 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
#include <boost/asio/ssl/stream.hpp>
|
|
|
|
#include <boost/asio/ssl/context.hpp>
|
|
|
|
#endif
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2013-06-12 09:57:13 +02:00
|
|
|
#include <boost/detail/atomic_count.hpp>
|
|
|
|
|
2011-09-10 07:36:38 +02:00
|
|
|
#define DEBUG_WEB_SERVER 0
|
|
|
|
|
|
|
|
#define DLOG if (DEBUG_WEB_SERVER) fprintf
|
|
|
|
|
2008-01-02 04:18:29 +01:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
2009-04-10 10:40:13 +02:00
|
|
|
bool tests_failure = false;
|
|
|
|
|
|
|
|
void report_failure(char const* err, char const* file, int line)
|
|
|
|
{
|
2013-06-14 18:38:26 +02:00
|
|
|
fprintf(stderr, "\n***** %s:%d \"%s\" *****\n\n", file, line, err);
|
2009-04-10 10:40:13 +02:00
|
|
|
tests_failure = true;
|
|
|
|
}
|
|
|
|
|
2013-06-18 09:33:49 +02:00
|
|
|
std::auto_ptr<alert> wait_for_alert(session& ses, int type)
|
|
|
|
{
|
|
|
|
std::auto_ptr<alert> ret;
|
|
|
|
while (!ret.get())
|
|
|
|
{
|
|
|
|
ses.wait_for_alert(milliseconds(5000));
|
|
|
|
std::deque<alert*> alerts;
|
|
|
|
ses.pop_alerts(&alerts);
|
|
|
|
for (std::deque<alert*>::iterator i = alerts.begin()
|
|
|
|
, end(alerts.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (!ret.get() && (*i)->type() == type)
|
|
|
|
{
|
|
|
|
ret = std::auto_ptr<alert>(*i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
delete *i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-19 23:41:33 +02:00
|
|
|
int load_file(std::string const& filename, std::vector<char>& v, libtorrent::error_code& ec, int limit)
|
|
|
|
{
|
|
|
|
ec.clear();
|
|
|
|
FILE* f = fopen(filename.c_str(), "rb");
|
|
|
|
if (f == NULL)
|
|
|
|
{
|
|
|
|
ec.assign(errno, boost::system::get_generic_category());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r = fseek(f, 0, SEEK_END);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, boost::system::get_generic_category());
|
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
long s = ftell(f);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, boost::system::get_generic_category());
|
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s > limit)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = fseek(f, 0, SEEK_SET);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, boost::system::get_generic_category());
|
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
v.resize(s);
|
|
|
|
if (s == 0)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = fread(&v[0], 1, v.size(), f);
|
|
|
|
if (r < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, boost::system::get_generic_category());
|
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (r != s) return -3;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-02 10:42:51 +01:00
|
|
|
bool print_alerts(libtorrent::session& ses, char const* name
|
|
|
|
, bool allow_disconnects, bool allow_no_torrents, bool allow_failed_fastresume
|
2011-04-10 03:36:07 +02:00
|
|
|
, bool (*predicate)(libtorrent::alert*), bool no_output)
|
2008-01-02 04:18:29 +01:00
|
|
|
{
|
2009-01-02 10:42:51 +01:00
|
|
|
bool ret = false;
|
2008-01-02 04:18:29 +01:00
|
|
|
std::vector<torrent_handle> handles = ses.get_torrents();
|
|
|
|
TEST_CHECK(!handles.empty() || allow_no_torrents);
|
|
|
|
torrent_handle h;
|
|
|
|
if (!handles.empty()) h = handles[0];
|
2011-03-14 03:59:46 +01:00
|
|
|
std::deque<alert*> alerts;
|
|
|
|
ses.pop_alerts(&alerts);
|
|
|
|
for (std::deque<alert*>::iterator i = alerts.begin(); i != alerts.end(); ++i)
|
2008-01-02 04:18:29 +01:00
|
|
|
{
|
2011-03-14 03:59:46 +01:00
|
|
|
if (predicate && predicate(*i)) ret = true;
|
|
|
|
if (peer_disconnected_alert* p = alert_cast<peer_disconnected_alert>(*i))
|
2008-05-12 09:01:25 +02:00
|
|
|
{
|
2013-07-28 17:06:28 +02:00
|
|
|
fprintf(stderr, "%s: %s(%s): %s\n", time_now_string(), name, print_endpoint(p->ip).c_str(), p->message().c_str());
|
2008-05-12 09:01:25 +02:00
|
|
|
}
|
2011-03-14 03:59:46 +01:00
|
|
|
else if ((*i)->message() != "block downloading"
|
|
|
|
&& (*i)->message() != "block finished"
|
2011-04-10 03:36:07 +02:00
|
|
|
&& (*i)->message() != "piece finished"
|
|
|
|
&& !no_output)
|
2008-05-12 09:01:25 +02:00
|
|
|
{
|
2013-07-28 17:06:28 +02:00
|
|
|
fprintf(stderr, "%s: %s: %s\n", time_now_string(), name, (*i)->message().c_str());
|
2008-05-12 09:01:25 +02:00
|
|
|
}
|
2011-04-10 03:36:07 +02:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
TEST_CHECK(alert_cast<fastresume_rejected_alert>(*i) == 0 || allow_failed_fastresume);
|
2008-10-01 01:37:42 +02:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
peer_error_alert* pea = alert_cast<peer_error_alert>(*i);
|
2012-03-23 05:35:35 +01:00
|
|
|
if (pea)
|
|
|
|
{
|
2013-07-28 17:06:28 +02:00
|
|
|
fprintf(stderr, "%s: peer error: %s\n", time_now_string(), pea->error.message().c_str());
|
2012-03-23 05:35:35 +01:00
|
|
|
TEST_CHECK((!handles.empty() && h.status().is_seeding)
|
|
|
|
|| pea->error.message() == "connecting to peer"
|
|
|
|
|| pea->error.message() == "closing connection to ourself"
|
|
|
|
|| pea->error.message() == "duplicate connection"
|
|
|
|
|| pea->error.message() == "duplicate peer-id"
|
|
|
|
|| pea->error.message() == "upload to upload connection"
|
|
|
|
|| pea->error.message() == "stopping torrent"
|
|
|
|
|| (allow_disconnects && pea->error.message() == "Broken pipe")
|
|
|
|
|| (allow_disconnects && pea->error.message() == "Connection reset by peer")
|
2013-08-02 03:12:27 +02:00
|
|
|
|| (allow_disconnects && pea->error.message() == "no shared cipher")
|
2012-03-23 05:35:35 +01:00
|
|
|
|| (allow_disconnects && pea->error.message() == "End of file."));
|
|
|
|
}
|
2011-03-14 03:59:46 +01:00
|
|
|
delete *i;
|
2008-01-02 04:18:29 +01:00
|
|
|
}
|
2009-01-02 10:42:51 +01:00
|
|
|
return ret;
|
2008-01-02 04:18:29 +01:00
|
|
|
}
|
2006-10-11 16:02:21 +02:00
|
|
|
|
2010-12-24 01:37:01 +01:00
|
|
|
bool listen_done = false;
|
|
|
|
bool listen_alert(libtorrent::alert* a)
|
|
|
|
{
|
|
|
|
if (alert_cast<listen_failed_alert>(a)
|
|
|
|
|| alert_cast<listen_succeeded_alert>(a))
|
|
|
|
listen_done = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait_for_listen(libtorrent::session& ses, char const* name)
|
|
|
|
{
|
|
|
|
listen_done = false;
|
|
|
|
alert const* a = 0;
|
|
|
|
do
|
|
|
|
{
|
2011-04-10 03:36:07 +02:00
|
|
|
print_alerts(ses, name, true, true, true, &listen_alert, false);
|
2010-12-24 01:37:01 +01:00
|
|
|
if (listen_done) break;
|
|
|
|
a = ses.wait_for_alert(milliseconds(500));
|
|
|
|
} while (a);
|
|
|
|
}
|
|
|
|
|
2013-06-20 07:28:42 +02:00
|
|
|
void print_ses_rate(float time
|
|
|
|
, libtorrent::torrent_status const* st1
|
2013-06-14 18:38:26 +02:00
|
|
|
, libtorrent::torrent_status const* st2
|
|
|
|
, libtorrent::torrent_status const* st3)
|
|
|
|
{
|
2013-06-21 03:06:40 +02:00
|
|
|
fprintf(stderr, "%3.1fs | %dkB/s %dkB/s %d%% %d", time
|
|
|
|
, int(st1->download_payload_rate / 1000)
|
|
|
|
, int(st1->upload_payload_rate / 1000)
|
|
|
|
, int(st1->progress * 100)
|
|
|
|
, st1->num_peers);
|
2013-06-14 18:38:26 +02:00
|
|
|
if (st2)
|
|
|
|
std::cerr << " : "
|
|
|
|
<< int(st2->download_payload_rate / 1000.f) << "kB/s "
|
|
|
|
<< int(st2->upload_payload_rate / 1000.f) << "kB/s "
|
|
|
|
<< int(st2->progress * 100) << "% "
|
|
|
|
<< st2->num_peers
|
|
|
|
<< " cc: " << st2->connect_candidates;
|
|
|
|
if (st3)
|
|
|
|
std::cerr << " : "
|
|
|
|
<< int(st3->download_payload_rate / 1000.f) << "kB/s "
|
|
|
|
<< int(st3->upload_payload_rate / 1000.f) << "kB/s "
|
|
|
|
<< int(st3->progress * 100) << "% "
|
|
|
|
<< st3->num_peers
|
|
|
|
<< " cc: " << st3->connect_candidates;
|
|
|
|
|
2013-06-21 03:06:40 +02:00
|
|
|
fprintf(stderr, "\n");
|
2013-06-14 18:38:26 +02:00
|
|
|
}
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void test_sleep(int millisec)
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
2009-10-20 04:49:56 +02:00
|
|
|
libtorrent::sleep(millisec);
|
2006-05-28 21:03:54 +02:00
|
|
|
}
|
|
|
|
|
2013-08-04 01:30:37 +02:00
|
|
|
static std::set<int> running_proxies;
|
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
void stop_proxy(int port)
|
2007-11-27 01:06:59 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "delegated -P%d -Fkill", port);
|
2011-09-10 07:36:38 +02:00
|
|
|
int ret = system(buf);
|
|
|
|
if (ret == 0)
|
|
|
|
perror("system");
|
2013-08-04 01:30:37 +02:00
|
|
|
else
|
|
|
|
running_proxies.erase(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop_all_proxies()
|
|
|
|
{
|
|
|
|
std::set<int> proxies = running_proxies;
|
|
|
|
for (std::set<int>::iterator i = proxies.begin()
|
|
|
|
, end(proxies.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
stop_proxy(*i);
|
2011-09-10 07:36:38 +02:00
|
|
|
}
|
2007-11-28 01:16:14 +01:00
|
|
|
}
|
|
|
|
|
2012-06-09 18:58:16 +02:00
|
|
|
int start_proxy(int proxy_type)
|
2007-11-28 01:16:14 +01:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2012-06-09 18:58:16 +02:00
|
|
|
int port = 10000 + (rand() % 50000);
|
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
stop_proxy(port);
|
2009-10-26 02:29:39 +01:00
|
|
|
|
|
|
|
char const* type = "";
|
|
|
|
char const* auth = "";
|
|
|
|
|
2007-11-28 01:16:14 +01:00
|
|
|
switch (proxy_type)
|
|
|
|
{
|
|
|
|
case proxy_settings::socks4:
|
2009-10-26 02:29:39 +01:00
|
|
|
type = "socks4";
|
2007-11-28 01:16:14 +01:00
|
|
|
break;
|
|
|
|
case proxy_settings::socks5:
|
2009-10-26 02:29:39 +01:00
|
|
|
type = "socks5";
|
2007-11-28 01:16:14 +01:00
|
|
|
break;
|
|
|
|
case proxy_settings::socks5_pw:
|
2009-10-26 02:29:39 +01:00
|
|
|
type = "socks5";
|
|
|
|
auth = "AUTHORIZER=-list{testuser:testpass}";
|
2007-11-28 01:16:14 +01:00
|
|
|
break;
|
|
|
|
case proxy_settings::http:
|
2009-10-26 02:29:39 +01:00
|
|
|
type = "http";
|
2007-11-28 01:16:14 +01:00
|
|
|
break;
|
|
|
|
case proxy_settings::http_pw:
|
2009-10-26 02:29:39 +01:00
|
|
|
type = "http";
|
|
|
|
auth = "AUTHORIZER=-list{testuser:testpass}";
|
2007-11-28 01:16:14 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-10-26 02:29:39 +01:00
|
|
|
|
|
|
|
char buf[512];
|
|
|
|
// we need to echo n since dg will ask us to configure it
|
|
|
|
snprintf(buf, sizeof(buf), "echo n | delegated -P%d ADMIN=test@test.com "
|
2010-12-27 23:54:14 +01:00
|
|
|
"PERMIT=\"*:*:localhost\" REMITTABLE=\"*\" RELAY=proxy,delegate "
|
2009-10-26 02:29:39 +01:00
|
|
|
"SERVER=%s %s"
|
|
|
|
, port, type, auth);
|
|
|
|
|
2013-08-04 01:30:37 +02:00
|
|
|
fprintf(stderr, "%s starting delegated proxy on port %d (%s %s)...\n", time_now_string(), port, type, auth);
|
2012-10-11 04:20:16 +02:00
|
|
|
int r = system(buf);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed (%d) %s\n", errno, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-08-04 01:30:37 +02:00
|
|
|
running_proxies.insert(port);
|
|
|
|
fprintf(stderr, "%s launched\n", time_now_string());
|
2009-12-25 23:18:14 +01:00
|
|
|
// apparently delegate takes a while to open its listen port
|
2010-12-27 23:54:14 +01:00
|
|
|
test_sleep(500);
|
2012-06-09 18:58:16 +02:00
|
|
|
return port;
|
2007-11-27 01:06:59 +01:00
|
|
|
}
|
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
2007-09-01 06:43:13 +02:00
|
|
|
template <class T>
|
|
|
|
boost::intrusive_ptr<T> clone_ptr(boost::intrusive_ptr<T> const& ptr)
|
|
|
|
{
|
|
|
|
return boost::intrusive_ptr<T>(new T(*ptr));
|
|
|
|
}
|
|
|
|
|
2013-01-06 05:53:26 +01:00
|
|
|
void create_random_files(std::string const& path, const int file_sizes[], int num_files)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
char* random_data = (char*)malloc(300000);
|
|
|
|
for (int i = 0; i != num_files; ++i)
|
|
|
|
{
|
|
|
|
std::generate(random_data, random_data + 300000, &std::rand);
|
|
|
|
char filename[200];
|
2013-01-07 01:55:15 +01:00
|
|
|
snprintf(filename, sizeof(filename), "test%d", i);
|
|
|
|
std::string full_path = combine_path(path, filename);
|
2013-01-06 05:53:26 +01:00
|
|
|
int to_write = file_sizes[i];
|
2013-01-07 01:55:15 +01:00
|
|
|
file f(full_path, file::write_only, ec);
|
2013-01-06 19:38:33 +01:00
|
|
|
if (ec) fprintf(stderr, "failed to create file \"%s\": (%d) %s\n"
|
2013-01-07 01:55:15 +01:00
|
|
|
, full_path.c_str(), ec.value(), ec.message().c_str());
|
2013-01-06 05:53:26 +01:00
|
|
|
size_type offset = 0;
|
|
|
|
while (to_write > 0)
|
|
|
|
{
|
|
|
|
int s = (std::min)(to_write, 300000);
|
2013-02-24 11:02:26 +01:00
|
|
|
file::iovec_t b = { random_data, size_t(s)};
|
2013-01-06 05:53:26 +01:00
|
|
|
f.writev(offset, &b, 1, ec);
|
2013-01-06 19:38:33 +01:00
|
|
|
if (ec) fprintf(stderr, "failed to write file \"%s\": (%d) %s\n"
|
2013-01-07 01:55:15 +01:00
|
|
|
, full_path.c_str(), ec.value(), ec.message().c_str());
|
2013-01-06 05:53:26 +01:00
|
|
|
offset += s;
|
|
|
|
to_write -= s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(random_data);
|
|
|
|
}
|
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
boost::intrusive_ptr<torrent_info> create_torrent(std::ostream* file, int piece_size
|
2013-07-28 17:06:28 +02:00
|
|
|
, int num_pieces, bool add_tracker, std::string ssl_certificate)
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
|
|
|
char const* tracker_url = "http://non-existent-name.com/announce";
|
2009-05-30 20:50:38 +02:00
|
|
|
// excercise the path when encountering invalid urls
|
|
|
|
char const* invalid_tracker_url = "http:";
|
2009-06-23 03:52:44 +02:00
|
|
|
char const* invalid_tracker_protocol = "foo://non/existent-name.com/announce";
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage fs;
|
2008-08-19 17:04:14 +02:00
|
|
|
int total_size = piece_size * num_pieces;
|
2009-10-26 02:29:39 +01:00
|
|
|
fs.add_file("temporary", total_size);
|
2008-06-18 14:34:39 +02:00
|
|
|
libtorrent::create_torrent t(fs, piece_size);
|
2010-02-18 05:37:02 +01:00
|
|
|
if (add_tracker)
|
|
|
|
{
|
|
|
|
t.add_tracker(tracker_url);
|
|
|
|
t.add_tracker(invalid_tracker_url);
|
|
|
|
t.add_tracker(invalid_tracker_protocol);
|
|
|
|
}
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2013-07-28 17:06:28 +02:00
|
|
|
if (!ssl_certificate.empty())
|
|
|
|
{
|
|
|
|
std::vector<char> file_buf;
|
|
|
|
error_code ec;
|
|
|
|
int res = load_file(ssl_certificate, file_buf, ec);
|
|
|
|
if (ec || res < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to load SSL certificate: %s\n", ec.message().c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string pem;
|
|
|
|
std::copy(file_buf.begin(), file_buf.end(), std::back_inserter(pem));
|
|
|
|
t.set_root_cert(pem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-18 14:34:39 +02:00
|
|
|
std::vector<char> piece(piece_size);
|
2007-07-03 01:48:06 +02:00
|
|
|
for (int i = 0; i < int(piece.size()); ++i)
|
2007-06-20 20:41:53 +02:00
|
|
|
piece[i] = (i % 26) + 'A';
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
2008-05-14 07:29:42 +02:00
|
|
|
int num = t.num_pieces();
|
2007-06-09 01:02:31 +02:00
|
|
|
sha1_hash ph = hasher(&piece[0], piece.size()).final();
|
2006-05-28 21:03:54 +02:00
|
|
|
for (int i = 0; i < num; ++i)
|
2008-05-14 07:29:42 +02:00
|
|
|
t.set_hash(i, ph);
|
2007-10-04 11:32:09 +02:00
|
|
|
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
while (total_size > 0)
|
|
|
|
{
|
|
|
|
file->write(&piece[0], (std::min)(int(piece.size()), total_size));
|
|
|
|
total_size -= piece.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
std::vector<char> tmp;
|
|
|
|
std::back_insert_iterator<std::vector<char> > out(tmp);
|
2009-10-26 02:29:39 +01:00
|
|
|
|
2011-09-05 07:50:41 +02:00
|
|
|
entry tor = t.generate();
|
|
|
|
|
|
|
|
bencode(out, tor);
|
2009-12-15 14:11:07 +01:00
|
|
|
error_code ec;
|
|
|
|
return boost::intrusive_ptr<torrent_info>(new torrent_info(
|
|
|
|
&tmp[0], tmp.size(), ec));
|
2007-10-04 11:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
boost::tuple<torrent_handle, torrent_handle, torrent_handle>
|
|
|
|
setup_transfer(session* ses1, session* ses2, session* ses3
|
2007-12-30 10:36:01 +01:00
|
|
|
, bool clear_files, bool use_metadata_transfer, bool connect_peers
|
2008-08-18 22:02:50 +02:00
|
|
|
, std::string suffix, int piece_size
|
2009-02-03 08:46:24 +01:00
|
|
|
, boost::intrusive_ptr<torrent_info>* torrent, bool super_seeding
|
2013-07-28 17:06:28 +02:00
|
|
|
, add_torrent_params const* p, bool stop_lsd, bool use_ssl_ports)
|
2007-10-04 11:32:09 +02:00
|
|
|
{
|
|
|
|
assert(ses1);
|
|
|
|
assert(ses2);
|
|
|
|
|
2011-02-16 11:16:52 +01:00
|
|
|
if (stop_lsd)
|
|
|
|
{
|
|
|
|
ses1->stop_lsd();
|
|
|
|
ses2->stop_lsd();
|
|
|
|
if (ses3) ses3->stop_lsd();
|
|
|
|
}
|
2010-12-24 01:37:01 +01:00
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
session_settings sess_set = ses1->settings();
|
2010-12-24 01:37:01 +01:00
|
|
|
if (ses3) sess_set.allow_multiple_connections_per_ip = true;
|
2009-04-10 10:40:13 +02:00
|
|
|
sess_set.ignore_limits_on_local_network = false;
|
2010-12-27 23:54:14 +01:00
|
|
|
sess_set.mixed_mode_algorithm = session_settings::prefer_tcp;
|
2010-12-24 01:37:01 +01:00
|
|
|
sess_set.max_failcount = 1;
|
2009-04-10 10:40:13 +02:00
|
|
|
ses1->set_settings(sess_set);
|
|
|
|
ses2->set_settings(sess_set);
|
|
|
|
if (ses3) ses3->set_settings(sess_set);
|
2010-11-29 02:33:05 +01:00
|
|
|
ses1->set_alert_mask(~(alert::progress_notification | alert::stats_notification));
|
|
|
|
ses2->set_alert_mask(~(alert::progress_notification | alert::stats_notification));
|
|
|
|
if (ses3) ses3->set_alert_mask(~(alert::progress_notification | alert::stats_notification));
|
2009-04-10 10:40:13 +02:00
|
|
|
|
2011-02-21 06:24:41 +01:00
|
|
|
std::srand((unsigned int)time(0));
|
2008-10-30 06:31:26 +01:00
|
|
|
peer_id pid;
|
|
|
|
std::generate(&pid[0], &pid[0] + 20, std::rand);
|
|
|
|
ses1->set_peer_id(pid);
|
|
|
|
std::generate(&pid[0], &pid[0] + 20, std::rand);
|
|
|
|
ses2->set_peer_id(pid);
|
2007-10-04 11:32:09 +02:00
|
|
|
assert(ses1->id() != ses2->id());
|
|
|
|
if (ses3)
|
2008-10-30 06:31:26 +01:00
|
|
|
{
|
|
|
|
std::generate(&pid[0], &pid[0] + 20, std::rand);
|
|
|
|
ses3->set_peer_id(pid);
|
2007-10-04 11:32:09 +02:00
|
|
|
assert(ses3->id() != ses2->id());
|
2008-10-30 06:31:26 +01:00
|
|
|
}
|
2007-10-04 11:32:09 +02:00
|
|
|
|
2008-08-18 22:02:50 +02:00
|
|
|
boost::intrusive_ptr<torrent_info> t;
|
|
|
|
if (torrent == 0)
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2012-06-17 01:23:36 +02:00
|
|
|
create_directory("tmp1" + suffix, ec);
|
|
|
|
std::ofstream file(combine_path("tmp1" + suffix, "temporary").c_str());
|
2013-07-28 17:06:28 +02:00
|
|
|
t = ::create_torrent(&file, piece_size, 19, true);
|
2008-08-18 22:02:50 +02:00
|
|
|
file.close();
|
|
|
|
if (clear_files)
|
|
|
|
{
|
2012-06-17 01:23:36 +02:00
|
|
|
remove_all(combine_path("tmp2" + suffix, "temporary"), ec);
|
|
|
|
remove_all(combine_path("tmp3" + suffix, "temporary"), ec);
|
2008-08-18 22:02:50 +02:00
|
|
|
}
|
2009-04-04 23:44:04 +02:00
|
|
|
char ih_hex[41];
|
|
|
|
to_hex((char const*)&t->info_hash()[0], 20, ih_hex);
|
2012-06-17 01:23:36 +02:00
|
|
|
fprintf(stderr, "generated torrent: %s tmp1%s/temporary\n", ih_hex, suffix.c_str());
|
2008-08-18 22:02:50 +02:00
|
|
|
}
|
|
|
|
else
|
2007-12-30 10:36:01 +01:00
|
|
|
{
|
2008-08-18 22:02:50 +02:00
|
|
|
t = *torrent;
|
2007-12-30 10:36:01 +01:00
|
|
|
}
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
// they should not use the same save dir, because the
|
|
|
|
// file pool will complain if two torrents are trying to
|
|
|
|
// use the same files
|
2009-02-03 08:46:24 +01:00
|
|
|
add_torrent_params param;
|
2012-03-19 05:02:13 +01:00
|
|
|
param.flags &= ~add_torrent_params::flag_paused;
|
|
|
|
param.flags &= ~add_torrent_params::flag_auto_managed;
|
2009-02-03 08:46:24 +01:00
|
|
|
if (p) param = *p;
|
|
|
|
param.ti = clone_ptr(t);
|
2012-06-17 01:23:36 +02:00
|
|
|
param.save_path = "tmp1" + suffix;
|
2012-03-19 05:02:13 +01:00
|
|
|
param.flags |= add_torrent_params::flag_seed_mode;
|
2009-12-15 14:11:07 +01:00
|
|
|
error_code ec;
|
|
|
|
torrent_handle tor1 = ses1->add_torrent(param, ec);
|
2008-12-08 07:36:22 +01:00
|
|
|
tor1.super_seeding(super_seeding);
|
2012-03-19 05:02:13 +01:00
|
|
|
|
|
|
|
// the downloader cannot use seed_mode
|
|
|
|
param.flags &= ~add_torrent_params::flag_seed_mode;
|
|
|
|
|
2008-01-02 04:18:29 +01:00
|
|
|
TEST_CHECK(!ses1->get_torrents().empty());
|
2007-06-06 02:41:20 +02:00
|
|
|
torrent_handle tor2;
|
2007-06-09 01:02:31 +02:00
|
|
|
torrent_handle tor3;
|
2009-02-03 08:46:24 +01:00
|
|
|
|
2008-01-02 04:18:29 +01:00
|
|
|
if (ses3)
|
|
|
|
{
|
2009-02-03 08:46:24 +01:00
|
|
|
param.ti = clone_ptr(t);
|
2012-06-17 01:23:36 +02:00
|
|
|
param.save_path = "tmp3" + suffix;
|
2009-12-15 14:11:07 +01:00
|
|
|
tor3 = ses3->add_torrent(param, ec);
|
2008-01-02 04:18:29 +01:00
|
|
|
TEST_CHECK(!ses3->get_torrents().empty());
|
|
|
|
}
|
2007-06-09 01:02:31 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
if (use_metadata_transfer)
|
2009-02-03 08:46:24 +01:00
|
|
|
{
|
|
|
|
param.ti = 0;
|
|
|
|
param.info_hash = t->info_hash();
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
else
|
2009-02-03 08:46:24 +01:00
|
|
|
{
|
|
|
|
param.ti = clone_ptr(t);
|
|
|
|
}
|
2012-06-17 01:23:36 +02:00
|
|
|
param.save_path = "tmp2" + suffix;
|
2009-02-03 08:46:24 +01:00
|
|
|
|
2009-12-15 14:11:07 +01:00
|
|
|
tor2 = ses2->add_torrent(param, ec);
|
2008-01-02 04:18:29 +01:00
|
|
|
TEST_CHECK(!ses2->get_torrents().empty());
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
assert(ses1->get_torrents().size() == 1);
|
|
|
|
assert(ses2->get_torrents().size() == 1);
|
|
|
|
|
2010-12-24 01:37:01 +01:00
|
|
|
// test_sleep(100);
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-10-01 04:09:12 +02:00
|
|
|
if (connect_peers)
|
2007-06-09 01:02:31 +02:00
|
|
|
{
|
2008-11-05 04:17:41 +01:00
|
|
|
error_code ec;
|
2013-07-28 17:06:28 +02:00
|
|
|
if (use_ssl_ports)
|
|
|
|
{
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: ses1: connecting peer port: %d\n", time_now_string(), int(ses2->ssl_listen_port()));
|
2013-07-28 17:06:28 +02:00
|
|
|
tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec)
|
|
|
|
, ses2->ssl_listen_port()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: ses1: connecting peer port: %d\n", time_now_string(), int(ses2->listen_port()));
|
2013-07-28 17:06:28 +02:00
|
|
|
tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec)
|
|
|
|
, ses2->listen_port()));
|
|
|
|
}
|
2007-10-01 04:09:12 +02:00
|
|
|
|
|
|
|
if (ses3)
|
|
|
|
{
|
|
|
|
// give the other peers some time to get an initial
|
|
|
|
// set of pieces before they start sharing with each-other
|
2013-07-28 17:06:28 +02:00
|
|
|
|
|
|
|
if (use_ssl_ports)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ses3: connecting peer port: %d\n", int(ses2->ssl_listen_port()));
|
|
|
|
tor3.connect_peer(tcp::endpoint(
|
|
|
|
address::from_string("127.0.0.1", ec)
|
|
|
|
, ses2->ssl_listen_port()));
|
|
|
|
fprintf(stderr, "ses3: connecting peer port: %d\n", int(ses1->ssl_listen_port()));
|
|
|
|
tor3.connect_peer(tcp::endpoint(
|
|
|
|
address::from_string("127.0.0.1", ec)
|
|
|
|
, ses1->ssl_listen_port()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ses3: connecting peer port: %d\n", int(ses2->listen_port()));
|
|
|
|
tor3.connect_peer(tcp::endpoint(
|
|
|
|
address::from_string("127.0.0.1", ec)
|
|
|
|
, ses2->listen_port()));
|
|
|
|
fprintf(stderr, "ses3: connecting peer port: %d\n", int(ses1->listen_port()));
|
|
|
|
tor3.connect_peer(tcp::endpoint(
|
|
|
|
address::from_string("127.0.0.1", ec)
|
|
|
|
, ses1->listen_port()));
|
|
|
|
}
|
2007-10-01 04:09:12 +02:00
|
|
|
}
|
2007-06-09 01:02:31 +02:00
|
|
|
}
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
return boost::make_tuple(tor1, tor2, tor3);
|
2006-05-28 21:03:54 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
boost::asio::io_service* tracker_ios = 0;
|
|
|
|
boost::shared_ptr<libtorrent::thread> tracker_server;
|
|
|
|
libtorrent::mutex tracker_lock;
|
2010-08-26 19:00:24 +02:00
|
|
|
libtorrent::event tracker_initialized;
|
2010-02-18 05:37:02 +01:00
|
|
|
|
|
|
|
bool udp_failed = false;
|
|
|
|
|
|
|
|
void stop_tracker()
|
|
|
|
{
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: stop_tracker()\n", time_now_string());
|
2010-02-18 05:37:02 +01:00
|
|
|
if (tracker_server && tracker_ios)
|
|
|
|
{
|
|
|
|
tracker_ios->stop();
|
|
|
|
tracker_server->join();
|
|
|
|
tracker_server.reset();
|
2013-03-10 11:45:22 +01:00
|
|
|
delete tracker_ios;
|
2010-02-18 05:37:02 +01:00
|
|
|
tracker_ios = 0;
|
|
|
|
}
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: stop_tracker() done\n", time_now_string());
|
2010-02-18 05:37:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void udp_tracker_thread(int* port);
|
|
|
|
|
|
|
|
int start_tracker()
|
|
|
|
{
|
|
|
|
stop_tracker();
|
|
|
|
|
|
|
|
{
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(tracker_lock);
|
2010-02-18 05:37:02 +01:00
|
|
|
tracker_initialized.clear(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
int port = 0;
|
|
|
|
|
|
|
|
tracker_server.reset(new libtorrent::thread(boost::bind(&udp_tracker_thread, &port)));
|
|
|
|
|
|
|
|
{
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(tracker_lock);
|
2010-02-18 05:37:02 +01:00
|
|
|
tracker_initialized.wait(l);
|
|
|
|
}
|
2010-12-24 01:37:01 +01:00
|
|
|
// test_sleep(100);
|
2010-02-18 05:37:02 +01:00
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2013-06-12 09:57:13 +02:00
|
|
|
boost::detail::atomic_count g_udp_tracker_requests(0);
|
2013-06-23 19:36:00 +02:00
|
|
|
boost::detail::atomic_count g_http_tracker_requests(0);
|
2011-09-17 23:15:42 +02:00
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
void on_udp_receive(error_code const& ec, size_t bytes_transferred, udp::endpoint const* from, char* buffer, udp::socket* sock)
|
|
|
|
{
|
|
|
|
if (ec)
|
|
|
|
{
|
2013-08-01 05:50:57 +02:00
|
|
|
fprintf(stderr, "%s: UDP tracker, read failed: %s\n", time_now_string(), ec.message().c_str());
|
2010-02-18 05:37:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
udp_failed = false;
|
|
|
|
|
|
|
|
if (bytes_transferred < 16)
|
|
|
|
{
|
2013-08-04 02:49:41 +02:00
|
|
|
fprintf(stderr, "%s: UDP message too short (from: %s)\n", time_now_string(), print_endpoint(*from).c_str());
|
2010-02-18 05:37:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-06-12 09:57:13 +02:00
|
|
|
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: UDP message %d bytes\n", time_now_string(), int(bytes_transferred));
|
2013-06-12 09:57:13 +02:00
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
char* ptr = buffer;
|
2010-03-20 23:00:13 +01:00
|
|
|
detail::read_uint64(ptr);
|
2010-02-18 05:37:02 +01:00
|
|
|
boost::uint32_t action = detail::read_uint32(ptr);
|
|
|
|
boost::uint32_t transaction_id = detail::read_uint32(ptr);
|
|
|
|
|
|
|
|
error_code e;
|
|
|
|
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case 0: // connect
|
|
|
|
|
2013-08-04 02:49:41 +02:00
|
|
|
fprintf(stderr, "%s: UDP connect from %s\n", time_now_string(), print_endpoint(*from).c_str());
|
2010-02-18 05:37:02 +01:00
|
|
|
ptr = buffer;
|
|
|
|
detail::write_uint32(0, ptr); // action = connect
|
|
|
|
detail::write_uint32(transaction_id, ptr); // transaction_id
|
|
|
|
detail::write_uint64(10, ptr); // connection_id
|
|
|
|
sock->send_to(asio::buffer(buffer, 16), *from, 0, e);
|
2013-08-04 02:49:41 +02:00
|
|
|
if (e) fprintf(stderr, "%s: send_to failed. ERROR: %s\n", time_now_string(), e.message().c_str());
|
2010-02-18 05:37:02 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // announce
|
|
|
|
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: UDP announce\n", time_now_string());
|
2010-02-18 05:37:02 +01:00
|
|
|
ptr = buffer;
|
|
|
|
detail::write_uint32(1, ptr); // action = announce
|
|
|
|
detail::write_uint32(transaction_id, ptr); // transaction_id
|
|
|
|
detail::write_uint32(1800, ptr); // interval
|
|
|
|
detail::write_uint32(1, ptr); // incomplete
|
|
|
|
detail::write_uint32(1, ptr); // complete
|
2011-09-17 23:15:42 +02:00
|
|
|
++g_udp_tracker_requests;
|
2010-02-18 05:37:02 +01:00
|
|
|
// 0 peers
|
|
|
|
sock->send_to(asio::buffer(buffer, 20), *from, 0, e);
|
2013-08-04 02:49:41 +02:00
|
|
|
if (e) fprintf(stderr, "%s: send_to failed. ERROR: %s\n", time_now_string(), e.message().c_str());
|
2010-02-18 05:37:02 +01:00
|
|
|
break;
|
2013-06-12 09:57:13 +02:00
|
|
|
case 2:
|
|
|
|
// ignore scrapes
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: UDP scrape\n", time_now_string());
|
2013-06-12 09:57:13 +02:00
|
|
|
break;
|
|
|
|
default:
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: UDP unknown message: %d\n", time_now_string(), action);
|
2010-02-18 05:37:02 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void udp_tracker_thread(int* port)
|
|
|
|
{
|
2013-03-10 11:45:22 +01:00
|
|
|
tracker_ios = new io_service;
|
|
|
|
|
|
|
|
udp::socket acceptor(*tracker_ios);
|
2010-02-18 05:37:02 +01:00
|
|
|
error_code ec;
|
|
|
|
acceptor.open(udp::v4(), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error opening listen UDP socket: %s\n", ec.message().c_str());
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(tracker_lock);
|
2010-02-18 05:37:02 +01:00
|
|
|
tracker_initialized.signal(l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
acceptor.bind(udp::endpoint(address_v4::any(), 0), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error binding UDP socket to port 0: %s\n", ec.message().c_str());
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(tracker_lock);
|
2010-02-18 05:37:02 +01:00
|
|
|
tracker_initialized.signal(l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*port = acceptor.local_endpoint().port();
|
|
|
|
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: UDP tracker initialized on port %d\n", time_now_string(), *port);
|
2010-02-18 05:37:02 +01:00
|
|
|
|
|
|
|
{
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(tracker_lock);
|
2010-02-18 05:37:02 +01:00
|
|
|
tracker_initialized.signal(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[2000];
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
udp::endpoint from;
|
|
|
|
udp_failed = true;
|
|
|
|
acceptor.async_receive_from(
|
|
|
|
asio::buffer(buffer, sizeof(buffer)), from, boost::bind(
|
|
|
|
&on_udp_receive, _1, _2, &from, &buffer[0], &acceptor));
|
2013-03-10 11:45:22 +01:00
|
|
|
tracker_ios->run_one(ec);
|
2010-02-18 05:37:02 +01:00
|
|
|
if (udp_failed) return;
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: Error receiving on UDP socket: %s\n", time_now_string(), ec.message().c_str());
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(tracker_lock);
|
2010-02-18 05:37:02 +01:00
|
|
|
tracker_initialized.signal(l);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-10 11:45:22 +01:00
|
|
|
tracker_ios->reset();
|
2010-02-18 05:37:02 +01:00
|
|
|
}
|
|
|
|
}
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2009-12-17 09:50:35 +01:00
|
|
|
boost::asio::io_service* web_ios = 0;
|
2009-12-06 00:41:33 +01:00
|
|
|
boost::shared_ptr<libtorrent::thread> web_server;
|
2009-12-17 09:50:35 +01:00
|
|
|
libtorrent::mutex web_lock;
|
2010-08-26 19:00:24 +02:00
|
|
|
libtorrent::event web_initialized;
|
2011-09-10 07:36:38 +02:00
|
|
|
bool stop_thread = false;
|
|
|
|
|
|
|
|
static void terminate_web_thread()
|
|
|
|
{
|
|
|
|
stop_thread = true;
|
|
|
|
web_ios->stop();
|
|
|
|
web_ios = 0;
|
|
|
|
}
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2010-01-24 00:57:11 +01:00
|
|
|
void stop_web_server()
|
2009-12-06 00:41:33 +01:00
|
|
|
{
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: stop_web_server()\n", time_now_string());
|
2009-12-17 09:50:35 +01:00
|
|
|
if (web_server && web_ios)
|
2009-12-06 00:41:33 +01:00
|
|
|
{
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: stopping web server thread\n", time_now_string());
|
2011-09-10 07:36:38 +02:00
|
|
|
web_ios->post(&terminate_web_thread);
|
2009-12-06 00:41:33 +01:00
|
|
|
web_server->join();
|
|
|
|
web_server.reset();
|
|
|
|
}
|
2013-06-17 02:45:17 +02:00
|
|
|
remove("server.pem");
|
2013-07-30 08:04:16 +02:00
|
|
|
fprintf(stderr, "%s: stop_web_server() done\n", time_now_string());
|
2009-12-06 00:41:33 +01:00
|
|
|
}
|
|
|
|
|
2010-10-27 08:39:18 +02:00
|
|
|
void web_server_thread(int* port, bool ssl, bool chunked);
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2010-10-27 08:39:18 +02:00
|
|
|
int start_web_server(bool ssl, bool chunked_encoding)
|
2009-12-06 00:41:33 +01:00
|
|
|
{
|
2010-01-24 00:57:11 +01:00
|
|
|
stop_web_server();
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2011-09-10 07:36:38 +02:00
|
|
|
stop_thread = false;
|
|
|
|
|
2009-12-17 09:50:35 +01:00
|
|
|
{
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(web_lock);
|
2009-12-17 09:50:35 +01:00
|
|
|
web_initialized.clear(l);
|
|
|
|
}
|
|
|
|
|
2010-01-24 00:57:11 +01:00
|
|
|
int port = 0;
|
|
|
|
|
2010-10-27 08:39:18 +02:00
|
|
|
web_server.reset(new libtorrent::thread(boost::bind(
|
|
|
|
&web_server_thread, &port, ssl, chunked_encoding)));
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2009-12-17 09:50:35 +01:00
|
|
|
{
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(web_lock);
|
2009-12-17 09:50:35 +01:00
|
|
|
web_initialized.wait(l);
|
|
|
|
}
|
|
|
|
|
2009-12-06 00:41:33 +01:00
|
|
|
// create this directory so that the path
|
|
|
|
// "relative/../test_file" can resolve
|
|
|
|
error_code ec;
|
|
|
|
create_directory("relative", ec);
|
2010-12-24 01:37:01 +01:00
|
|
|
// test_sleep(100);
|
2010-01-24 00:57:11 +01:00
|
|
|
return port;
|
2009-12-06 00:41:33 +01:00
|
|
|
}
|
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
void send_response(socket_type& s, error_code& ec
|
2010-10-27 08:39:18 +02:00
|
|
|
, int code, char const* status_message, char const** extra_header
|
2009-12-06 00:41:33 +01:00
|
|
|
, int len)
|
|
|
|
{
|
2010-10-27 08:39:18 +02:00
|
|
|
char msg[600];
|
2011-09-10 07:36:38 +02:00
|
|
|
int pkt_len = snprintf(msg, sizeof(msg), "HTTP/1.1 %d %s\r\n"
|
2009-12-06 00:41:33 +01:00
|
|
|
"content-length: %d\r\n"
|
2009-12-25 17:00:15 +01:00
|
|
|
"%s"
|
2010-10-27 08:39:18 +02:00
|
|
|
"%s"
|
|
|
|
"%s"
|
|
|
|
"%s"
|
2009-12-06 00:41:33 +01:00
|
|
|
"\r\n"
|
|
|
|
, code, status_message, len
|
2010-10-27 08:39:18 +02:00
|
|
|
, extra_header[0]
|
|
|
|
, extra_header[1]
|
|
|
|
, extra_header[2]
|
|
|
|
, extra_header[3]);
|
2011-09-10 07:36:38 +02:00
|
|
|
DLOG(stderr, ">> %s\n", msg);
|
2009-12-06 00:41:33 +01:00
|
|
|
write(s, boost::asio::buffer(msg, pkt_len), boost::asio::transfer_all(), ec);
|
2011-09-10 07:36:38 +02:00
|
|
|
if (ec) fprintf(stderr, "*** send failed: %s\n", ec.message().c_str());
|
2009-12-06 00:41:33 +01:00
|
|
|
}
|
|
|
|
|
2011-09-10 07:36:38 +02:00
|
|
|
void on_accept(error_code& accept_ec, error_code const& ec, bool* done)
|
2009-12-17 09:50:35 +01:00
|
|
|
{
|
2011-09-10 07:36:38 +02:00
|
|
|
accept_ec = ec;
|
|
|
|
*done = true;
|
2009-12-17 09:50:35 +01:00
|
|
|
}
|
|
|
|
|
2010-10-27 08:39:18 +02:00
|
|
|
void send_content(socket_type& s, char const* file, int size, bool chunked)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
if (chunked)
|
|
|
|
{
|
|
|
|
int chunk_size = 13;
|
|
|
|
char head[20];
|
|
|
|
std::vector<boost::asio::const_buffer> bufs(3);
|
|
|
|
bufs[2] = asio::const_buffer("\r\n", 2);
|
|
|
|
while (chunk_size > 0)
|
|
|
|
{
|
|
|
|
chunk_size = std::min(chunk_size, size);
|
|
|
|
int len = snprintf(head, sizeof(head), "%x\r\n", chunk_size);
|
|
|
|
bufs[0] = asio::const_buffer(head, len);
|
|
|
|
if (chunk_size == 0)
|
|
|
|
{
|
|
|
|
// terminate
|
|
|
|
bufs.erase(bufs.begin()+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bufs[1] = asio::const_buffer(file, chunk_size);
|
|
|
|
}
|
|
|
|
write(s, bufs, boost::asio::transfer_all(), ec);
|
2011-09-10 07:36:38 +02:00
|
|
|
if (ec) fprintf(stderr, "*** send failed: %s\n", ec.message().c_str());
|
2010-10-27 08:39:18 +02:00
|
|
|
size -= chunk_size;
|
|
|
|
file += chunk_size;
|
|
|
|
chunk_size *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
write(s, boost::asio::buffer(file, size), boost::asio::transfer_all(), ec);
|
2011-09-10 07:36:38 +02:00
|
|
|
if (ec) fprintf(stderr, "*** send failed: %s\n", ec.message().c_str());
|
2010-10-27 08:39:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-10 07:36:38 +02:00
|
|
|
void on_read(error_code const& ec, size_t bytes_transferred, size_t* bt, error_code* e, bool* done)
|
|
|
|
{
|
2011-10-30 04:51:55 +01:00
|
|
|
DLOG(stderr, "on_read %d [ ec: %s ]\n", int(bytes_transferred), ec.message().c_str());
|
2011-09-10 07:36:38 +02:00
|
|
|
*bt = bytes_transferred;
|
|
|
|
*e = ec;
|
|
|
|
*done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_read_timeout(error_code const& ec, bool* timed_out)
|
|
|
|
{
|
|
|
|
if (ec) return;
|
2011-10-30 04:51:55 +01:00
|
|
|
fprintf(stderr, "read timed out\n");
|
2011-09-10 07:36:38 +02:00
|
|
|
*timed_out = true;
|
|
|
|
}
|
|
|
|
|
2010-10-27 08:39:18 +02:00
|
|
|
void web_server_thread(int* port, bool ssl, bool chunked)
|
2009-12-06 00:41:33 +01:00
|
|
|
{
|
|
|
|
io_service ios;
|
|
|
|
socket_acceptor acceptor(ios);
|
|
|
|
error_code ec;
|
|
|
|
acceptor.open(tcp::v4(), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error opening listen socket: %s\n", ec.message().c_str());
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(web_lock);
|
2009-12-17 09:50:35 +01:00
|
|
|
web_initialized.signal(l);
|
2009-12-06 00:41:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
acceptor.set_option(socket_acceptor::reuse_address(true), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error setting listen socket to reuse addr: %s\n", ec.message().c_str());
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(web_lock);
|
2009-12-17 09:50:35 +01:00
|
|
|
web_initialized.signal(l);
|
2009-12-06 00:41:33 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-24 00:57:11 +01:00
|
|
|
acceptor.bind(tcp::endpoint(address_v4::any(), 0), ec);
|
2009-12-06 00:41:33 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2010-01-24 00:57:11 +01:00
|
|
|
fprintf(stderr, "Error binding listen socket to port 0: %s\n", ec.message().c_str());
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(web_lock);
|
2009-12-17 09:50:35 +01:00
|
|
|
web_initialized.signal(l);
|
2009-12-06 00:41:33 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-24 00:57:11 +01:00
|
|
|
*port = acceptor.local_endpoint().port();
|
2009-12-06 00:41:33 +01:00
|
|
|
acceptor.listen(10, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error listening on socket: %s\n", ec.message().c_str());
|
2010-05-03 00:26:43 +02:00
|
|
|
libtorrent::mutex::scoped_lock l(web_lock);
|
2009-12-17 09:50:35 +01:00
|
|
|
web_initialized.signal(l);
|
2009-12-06 00:41:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-17 09:50:35 +01:00
|
|
|
web_ios = &ios;
|
|
|
|
|
2009-12-06 00:41:33 +01:00
|
|
|
char buf[10000];
|
|
|
|
int len = 0;
|
2009-12-25 17:00:15 +01:00
|
|
|
int offset = 0;
|
2010-06-22 20:09:04 +02:00
|
|
|
bool connection_close = false;
|
2010-10-12 10:57:43 +02:00
|
|
|
socket_type s(ios);
|
|
|
|
void* ctx = 0;
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2011-09-10 07:36:38 +02:00
|
|
|
boost::asio::ssl::context ssl_ctx(ios, boost::asio::ssl::context::sslv23_server);
|
2013-07-22 02:53:13 +02:00
|
|
|
if (ssl)
|
|
|
|
{
|
2013-07-29 11:27:46 +02:00
|
|
|
ssl_ctx.use_certificate_chain_file("../ssl/server.pem");
|
|
|
|
ssl_ctx.use_private_key_file("../ssl/server.pem", asio::ssl::context::pem);
|
2013-07-22 02:53:13 +02:00
|
|
|
ssl_ctx.set_verify_mode(boost::asio::ssl::context::verify_none);
|
2010-10-12 10:57:43 +02:00
|
|
|
|
2013-07-22 02:53:13 +02:00
|
|
|
ctx = &ssl_ctx;
|
|
|
|
}
|
2010-10-12 10:57:43 +02:00
|
|
|
#endif
|
2009-12-25 17:00:15 +01:00
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
proxy_settings p;
|
|
|
|
instantiate_connection(ios, p, s, ctx);
|
2013-06-12 09:57:13 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "web server initialized on port %d%s\n", *port, ssl ? " [SSL]" : "");
|
|
|
|
|
|
|
|
{
|
|
|
|
libtorrent::mutex::scoped_lock l(web_lock);
|
|
|
|
web_initialized.signal(l);
|
|
|
|
}
|
|
|
|
|
2009-12-06 00:41:33 +01:00
|
|
|
for (;;)
|
|
|
|
{
|
2010-06-22 20:09:04 +02:00
|
|
|
if (connection_close)
|
2009-12-06 00:41:33 +01:00
|
|
|
{
|
2011-09-10 07:36:38 +02:00
|
|
|
error_code ec;
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (ssl)
|
|
|
|
{
|
|
|
|
DLOG(stderr, "shutting down SSL connection\n");
|
|
|
|
s.get<ssl_stream<stream_socket> >()->shutdown(ec);
|
|
|
|
if (ec) fprintf(stderr, "SSL shutdown failed: %s\n", ec.message().c_str());
|
|
|
|
ec.clear();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
DLOG(stderr, "closing connection\n");
|
2010-06-22 20:09:04 +02:00
|
|
|
s.close(ec);
|
2011-09-10 07:36:38 +02:00
|
|
|
if (ec) fprintf(stderr, "close failed: %s\n", ec.message().c_str());
|
2010-06-22 20:09:04 +02:00
|
|
|
connection_close = false;
|
2009-12-06 00:41:33 +01:00
|
|
|
}
|
|
|
|
|
2010-06-22 20:09:04 +02:00
|
|
|
if (!s.is_open())
|
|
|
|
{
|
|
|
|
len = 0;
|
|
|
|
offset = 0;
|
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
error_code ec;
|
2011-09-10 07:36:38 +02:00
|
|
|
instantiate_connection(ios, p, s, ctx);
|
2010-10-12 10:57:43 +02:00
|
|
|
stream_socket* sock;
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2011-09-10 07:36:38 +02:00
|
|
|
if (ssl) sock = &s.get<ssl_stream<stream_socket> >()->next_layer();
|
2010-10-12 10:57:43 +02:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
sock = s.get<stream_socket>();
|
|
|
|
|
2011-09-10 07:36:38 +02:00
|
|
|
bool accept_done = false;
|
|
|
|
DLOG(stderr, "waiting for incoming connection\n");
|
|
|
|
acceptor.async_accept(*sock, boost::bind(&on_accept, boost::ref(ec), _1, &accept_done));
|
|
|
|
while (!accept_done)
|
|
|
|
{
|
|
|
|
error_code e;
|
|
|
|
ios.reset();
|
|
|
|
if (stop_thread || ios.run_one(e) == 0)
|
|
|
|
{
|
2013-08-01 05:50:57 +02:00
|
|
|
fprintf(stderr, "%s: io_service stopped: %s\n", time_now_string(), e.message().c_str());
|
2011-09-10 07:36:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stop_thread) break;
|
|
|
|
|
|
|
|
if (ec)
|
2010-06-22 20:09:04 +02:00
|
|
|
{
|
2013-08-01 05:50:57 +02:00
|
|
|
fprintf(stderr, "%s: accept failed: %s\n", time_now_string(), ec.message().c_str());
|
2010-06-22 20:09:04 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-08-01 05:50:57 +02:00
|
|
|
DLOG(stderr, "%s: accepting incoming connection\n", time_now_string());
|
2011-09-10 07:36:38 +02:00
|
|
|
if (!s.is_open())
|
|
|
|
{
|
2013-08-01 05:50:57 +02:00
|
|
|
fprintf(stderr, "%s: incoming connection closed\n", time_now_string());
|
2011-09-10 07:36:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-12 10:57:43 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (ssl)
|
2011-09-10 07:36:38 +02:00
|
|
|
{
|
2013-08-01 05:50:57 +02:00
|
|
|
DLOG(stderr, "%s: SSL handshake\n", time_now_string());
|
2011-09-10 07:36:38 +02:00
|
|
|
s.get<ssl_stream<stream_socket> >()->accept_handshake(ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "SSL handshake failed: %s\n", ec.message().c_str());
|
|
|
|
connection_close = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-10-12 10:57:43 +02:00
|
|
|
#endif
|
|
|
|
|
2010-06-22 20:09:04 +02:00
|
|
|
}
|
2009-12-17 09:50:35 +01:00
|
|
|
|
2009-12-06 00:41:33 +01:00
|
|
|
http_parser p;
|
|
|
|
bool failed = false;
|
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
do
|
2009-12-06 00:41:33 +01:00
|
|
|
{
|
2009-12-25 17:00:15 +01:00
|
|
|
p.reset();
|
|
|
|
bool error = false;
|
|
|
|
|
|
|
|
p.incoming(buffer::const_interval(buf + offset, buf + len), error);
|
|
|
|
|
2010-10-27 08:39:18 +02:00
|
|
|
char const* extra_header[4] = {"","","",""};
|
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
TEST_CHECK(error == false);
|
|
|
|
if (error)
|
2009-12-06 00:41:33 +01:00
|
|
|
{
|
2009-12-25 17:00:15 +01:00
|
|
|
fprintf(stderr, "parse failed\n");
|
2009-12-06 00:41:33 +01:00
|
|
|
failed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
while (!p.finished())
|
|
|
|
{
|
2010-11-29 02:33:05 +01:00
|
|
|
TORRENT_ASSERT(len < int(sizeof(buf)));
|
2011-09-10 07:36:38 +02:00
|
|
|
size_t received = 0;
|
|
|
|
bool done = false;
|
|
|
|
bool timed_out = false;
|
2011-10-30 04:51:55 +01:00
|
|
|
DLOG(stderr, "async_read_some %d bytes [ len: %d ]\n", int(sizeof(buf) - len), len);
|
2011-09-10 07:36:38 +02:00
|
|
|
s.async_read_some(boost::asio::buffer(&buf[len]
|
|
|
|
, sizeof(buf) - len), boost::bind(&on_read, _1, _2, &received, &ec, &done));
|
|
|
|
deadline_timer timer(ios);
|
2012-04-05 06:17:19 +02:00
|
|
|
timer.expires_at(time_now_hires() + seconds(2));
|
2011-09-10 07:36:38 +02:00
|
|
|
timer.async_wait(boost::bind(&on_read_timeout, _1, &timed_out));
|
|
|
|
|
|
|
|
while (!done && !timed_out)
|
|
|
|
{
|
|
|
|
error_code e;
|
|
|
|
ios.reset();
|
|
|
|
if (stop_thread || ios.run_one(e) == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "io_service stopped: %s\n", e.message().c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timed_out)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "read timed out, closing connection\n");
|
|
|
|
failed = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-10-12 10:57:43 +02:00
|
|
|
// fprintf(stderr, "read: %d\n", int(received));
|
2009-12-25 17:00:15 +01:00
|
|
|
|
|
|
|
if (ec || received <= 0)
|
|
|
|
{
|
2011-09-10 07:36:38 +02:00
|
|
|
fprintf(stderr, "read failed: \"%s\" (%s) received: %d\n"
|
|
|
|
, ec.message().c_str(), ec.category().name(), int(received));
|
2009-12-25 17:00:15 +01:00
|
|
|
failed = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-09-10 07:36:38 +02:00
|
|
|
|
|
|
|
timer.cancel(ec);
|
|
|
|
if (ec)
|
|
|
|
fprintf(stderr, "timer.cancel failed: %s\n", ec.message().c_str());
|
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
len += received;
|
2010-06-22 20:09:04 +02:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
p.incoming(buffer::const_interval(buf + offset, buf + len), error);
|
2010-06-22 20:09:04 +02:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
TEST_CHECK(error == false);
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "parse failed\n");
|
|
|
|
failed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-06-22 20:09:04 +02:00
|
|
|
|
|
|
|
std::string connection = p.header("connection");
|
|
|
|
std::string via = p.header("via");
|
|
|
|
|
|
|
|
// The delegate proxy doesn't say connection close, but it expects it to be closed
|
|
|
|
// the Via: header is an indicator of delegate making the request
|
|
|
|
if (connection == "close" || !via.empty())
|
|
|
|
{
|
2011-09-10 07:36:38 +02:00
|
|
|
DLOG(stderr, "*** got connection close\n");
|
|
|
|
connection_close = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.protocol() == "HTTP/1.0")
|
|
|
|
{
|
|
|
|
DLOG(stderr, "*** HTTP/1.0, closing connection when done\n");
|
2010-06-22 20:09:04 +02:00
|
|
|
connection_close = true;
|
|
|
|
}
|
|
|
|
|
2013-06-12 09:57:13 +02:00
|
|
|
DLOG(stderr, "REQ: %s", std::string(buf + offset, p.body_start()).c_str());
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2010-06-22 20:09:04 +02:00
|
|
|
if (failed)
|
|
|
|
{
|
2011-09-10 07:36:38 +02:00
|
|
|
fprintf(stderr, "*** connection failed\n");
|
2010-10-12 10:57:43 +02:00
|
|
|
connection_close = true;
|
2010-06-22 20:09:04 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2011-02-21 06:24:41 +01:00
|
|
|
offset += int(p.body_start() + p.content_length());
|
2010-06-22 20:09:04 +02:00
|
|
|
// fprintf(stderr, "offset: %d len: %d\n", offset, len);
|
2009-12-25 23:18:14 +01:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
if (p.method() != "get" && p.method() != "post")
|
|
|
|
{
|
2011-09-10 07:36:38 +02:00
|
|
|
fprintf(stderr, "*** incorrect method: %s\n", p.method().c_str());
|
|
|
|
connection_close = true;
|
2009-12-25 17:00:15 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
std::string path = p.path();
|
2013-08-01 05:50:57 +02:00
|
|
|
fprintf(stderr, "%s: %s\n", time_now_string(), path.c_str());
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
if (path == "/redirect")
|
|
|
|
{
|
2010-10-27 08:39:18 +02:00
|
|
|
extra_header[0] = "Location: /test_file\r\n";
|
|
|
|
send_response(s, ec, 301, "Moved Permanently", extra_header, 0);
|
2009-12-25 17:00:15 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
if (path == "/infinite_redirect")
|
|
|
|
{
|
2010-10-31 04:08:12 +01:00
|
|
|
extra_header[0] = "Location: /infinite_redirect\r\n";
|
2010-10-27 08:39:18 +02:00
|
|
|
send_response(s, ec, 301, "Moved Permanently", extra_header, 0);
|
2009-12-25 17:00:15 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
if (path == "/relative/redirect")
|
|
|
|
{
|
2010-10-27 08:39:18 +02:00
|
|
|
extra_header[0] = "Location: ../test_file\r\n";
|
|
|
|
send_response(s, ec, 301, "Moved Permanently", extra_header, 0);
|
2009-12-25 17:00:15 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
if (path.substr(0, 9) == "/announce")
|
|
|
|
{
|
2011-09-17 23:15:42 +02:00
|
|
|
fprintf(stderr, "%s\n", path.c_str());
|
2010-02-18 05:37:02 +01:00
|
|
|
entry announce;
|
|
|
|
announce["interval"] = 1800;
|
|
|
|
announce["complete"] = 1;
|
|
|
|
announce["incomplete"] = 1;
|
|
|
|
announce["peers"].string();
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), announce);
|
2011-09-17 23:15:42 +02:00
|
|
|
++g_http_tracker_requests;
|
2010-02-18 05:37:02 +01:00
|
|
|
|
2010-10-27 08:39:18 +02:00
|
|
|
send_response(s, ec, 200, "OK", extra_header, buf.size());
|
2010-02-18 05:37:02 +01:00
|
|
|
write(s, boost::asio::buffer(&buf[0], buf.size()), boost::asio::transfer_all(), ec);
|
2011-09-10 07:36:38 +02:00
|
|
|
if (ec)
|
|
|
|
fprintf(stderr, "*** send failed: %s\n", ec.message().c_str());
|
2010-02-18 05:37:02 +01:00
|
|
|
}
|
|
|
|
|
2010-10-17 18:15:32 +02:00
|
|
|
if (path.substr(0, 6) == "/seed?")
|
|
|
|
{
|
|
|
|
char const* piece = strstr(path.c_str(), "&piece=");
|
2011-09-10 07:36:38 +02:00
|
|
|
if (piece == 0) piece = strstr(path.c_str(), "?piece=");
|
2010-10-17 18:15:32 +02:00
|
|
|
if (piece == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "invalid web seed request: %s\n", path.c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
boost::uint64_t idx = atoi(piece + 7);
|
|
|
|
char const* range = strstr(path.c_str(), "&ranges=");
|
2011-09-10 07:36:38 +02:00
|
|
|
if (range == 0) range = strstr(path.c_str(), "?ranges=");
|
2010-10-17 18:15:32 +02:00
|
|
|
int range_end = 0;
|
|
|
|
int range_start = 0;
|
|
|
|
if (range)
|
|
|
|
{
|
|
|
|
range_start = atoi(range + 8);
|
|
|
|
range = strchr(range, '-');
|
|
|
|
if (range == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "invalid web seed request: %s\n", path.c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
range_end = atoi(range + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
range_start = 0;
|
2010-10-30 21:45:50 +02:00
|
|
|
// assume piece size of 64kiB
|
2011-09-10 07:36:38 +02:00
|
|
|
range_end = 64*1024+1;
|
2010-10-17 18:15:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int size = range_end - range_start + 1;
|
2010-10-30 21:45:50 +02:00
|
|
|
boost::uint64_t off = idx * 64 * 1024 + range_start;
|
2010-10-17 18:15:32 +02:00
|
|
|
std::vector<char> file_buf;
|
|
|
|
error_code ec;
|
2012-06-17 01:23:36 +02:00
|
|
|
int res = load_file(combine_path("tmp1_web_seed", "seed"), file_buf, ec);
|
2011-01-19 11:07:51 +01:00
|
|
|
|
2010-10-17 18:15:32 +02:00
|
|
|
if (res == -1 || file_buf.empty())
|
|
|
|
{
|
2010-10-27 08:39:18 +02:00
|
|
|
send_response(s, ec, 404, "Not Found", extra_header, 0);
|
2010-10-17 18:15:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-27 08:39:18 +02:00
|
|
|
send_response(s, ec, 200, "OK", extra_header, size);
|
2011-09-10 07:36:38 +02:00
|
|
|
DLOG(stderr, "sending %d bytes of payload [%d, %d) piece: %d\n"
|
|
|
|
, size, int(off), int(off + size), int(idx));
|
2010-10-17 18:15:32 +02:00
|
|
|
write(s, boost::asio::buffer(&file_buf[0] + off, size)
|
|
|
|
, boost::asio::transfer_all(), ec);
|
2011-09-10 07:36:38 +02:00
|
|
|
if (ec)
|
|
|
|
fprintf(stderr, "*** send failed: %s\n", ec.message().c_str());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DLOG(stderr, "*** done\n");
|
|
|
|
}
|
2010-10-17 18:15:32 +02:00
|
|
|
|
|
|
|
memmove(buf, buf + offset, len - offset);
|
|
|
|
len -= offset;
|
|
|
|
offset = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-25 23:18:14 +01:00
|
|
|
// fprintf(stderr, ">> serving file %s\n", path.c_str());
|
2009-12-25 17:00:15 +01:00
|
|
|
std::vector<char> file_buf;
|
|
|
|
// remove the / from the path
|
|
|
|
path = path.substr(1);
|
2011-01-19 11:07:51 +01:00
|
|
|
error_code ec;
|
|
|
|
int res = load_file(path, file_buf, ec);
|
2009-12-25 17:00:15 +01:00
|
|
|
if (res == -1)
|
|
|
|
{
|
2011-11-25 23:50:43 +01:00
|
|
|
fprintf(stderr, ">> file not found: %s\n", path.c_str());
|
2010-10-27 08:39:18 +02:00
|
|
|
send_response(s, ec, 404, "Not Found", extra_header, 0);
|
2009-12-25 17:00:15 +01:00
|
|
|
continue;
|
|
|
|
}
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
// this means the file was either too big or couldn't be read
|
2011-11-25 23:50:43 +01:00
|
|
|
fprintf(stderr, ">> file too big: %s\n", path.c_str());
|
2010-10-27 08:39:18 +02:00
|
|
|
send_response(s, ec, 503, "Internal Error", extra_header, 0);
|
2009-12-25 17:00:15 +01:00
|
|
|
continue;
|
|
|
|
}
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
// serve file
|
2009-12-06 00:41:33 +01:00
|
|
|
|
2009-12-25 17:00:15 +01:00
|
|
|
if (extension(path) == ".gz")
|
|
|
|
{
|
2010-10-27 08:39:18 +02:00
|
|
|
extra_header[0] = "Content-Encoding: gzip\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunked)
|
|
|
|
{
|
|
|
|
extra_header[2] = "Transfer-Encoding: chunked\r\n";
|
2009-12-25 17:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!p.header("range").empty())
|
|
|
|
{
|
|
|
|
std::string range = p.header("range");
|
|
|
|
int start, end;
|
|
|
|
sscanf(range.c_str(), "bytes=%d-%d", &start, &end);
|
2010-10-27 08:39:18 +02:00
|
|
|
char eh[400];
|
|
|
|
snprintf(eh, sizeof(eh), "Content-Range: bytes %d-%d\r\n", start, end);
|
|
|
|
extra_header[1] = eh;
|
2012-03-22 02:48:17 +01:00
|
|
|
if (end - start + 1 >= 1000)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "request size: %.2f kB\n", int(end - start + 1)/1000.f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "request size: %d Bytes\n", int(end - start + 1));
|
|
|
|
}
|
2010-10-27 08:39:18 +02:00
|
|
|
send_response(s, ec, 206, "Partial", extra_header, end - start + 1);
|
2010-01-24 00:57:11 +01:00
|
|
|
if (!file_buf.empty())
|
|
|
|
{
|
2010-10-27 08:39:18 +02:00
|
|
|
send_content(s, &file_buf[0] + start, end - start + 1, chunked);
|
2010-01-24 00:57:11 +01:00
|
|
|
}
|
2011-09-10 07:36:38 +02:00
|
|
|
DLOG(stderr, "send %d bytes of payload\n", end - start + 1);
|
2009-12-25 17:00:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
send_response(s, ec, 200, "OK", extra_header, file_buf.size());
|
2010-04-02 22:48:09 +02:00
|
|
|
if (!file_buf.empty())
|
2010-10-27 08:39:18 +02:00
|
|
|
send_content(s, &file_buf[0], file_buf.size(), chunked);
|
2009-12-25 17:00:15 +01:00
|
|
|
}
|
2011-09-10 07:36:38 +02:00
|
|
|
DLOG(stderr, "%d bytes left in receive buffer. offset: %d\n", len - offset, offset);
|
2010-06-22 20:09:04 +02:00
|
|
|
memmove(buf, buf + offset, len - offset);
|
|
|
|
len -= offset;
|
|
|
|
offset = 0;
|
2009-12-25 17:00:15 +01:00
|
|
|
} while (offset < len);
|
2009-12-06 00:41:33 +01:00
|
|
|
}
|
2011-09-10 07:36:38 +02:00
|
|
|
|
|
|
|
web_ios = 0;
|
2013-08-01 05:50:57 +02:00
|
|
|
fprintf(stderr, "%s: exiting web server thread\n", time_now_string());
|
2009-12-06 00:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|