2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-14 02:55:21 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-14 02:55:21 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2017-09-21 04:31:54 +02:00
|
|
|
#include <ircd/asio.h>
|
2016-09-12 23:07:46 +02:00
|
|
|
|
2018-01-09 03:08:26 +01:00
|
|
|
namespace ircd
|
2017-03-14 02:39:54 +01:00
|
|
|
{
|
2018-03-12 21:30:24 +01:00
|
|
|
ctx::dock dock;
|
2018-01-14 02:55:21 +01:00
|
|
|
}
|
2016-09-11 08:05:38 +02:00
|
|
|
|
2018-04-20 01:35:59 +02:00
|
|
|
//
|
|
|
|
// client::settings conf::item's
|
|
|
|
//
|
|
|
|
|
2018-09-02 07:03:25 +02:00
|
|
|
ircd::conf::item<size_t>
|
|
|
|
ircd::client::settings::max_client
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.max_client" },
|
|
|
|
{ "default", 16384L },
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::conf::item<size_t>
|
|
|
|
ircd::client::settings::max_client_per_peer
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.max_client_per_peer" },
|
|
|
|
{ "default", 24L },
|
|
|
|
};
|
|
|
|
|
2018-04-20 01:35:59 +02:00
|
|
|
ircd::conf::item<size_t>
|
|
|
|
ircd::client::settings::stack_size
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.stack_size" },
|
|
|
|
{ "default", ssize_t(1_MiB) },
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::conf::item<size_t>
|
|
|
|
ircd::client::settings::pool_size
|
|
|
|
{
|
2018-08-22 23:11:55 +02:00
|
|
|
{
|
|
|
|
{ "name", "ircd.client.pool_size " },
|
|
|
|
{ "default", 64L },
|
|
|
|
}, []
|
|
|
|
{
|
|
|
|
using client = ircd::client;
|
2018-09-02 00:08:48 +02:00
|
|
|
client::pool.set(client::settings::pool_size);
|
2018-08-22 23:11:55 +02:00
|
|
|
}
|
2018-04-20 01:35:59 +02:00
|
|
|
};
|
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
/// Linkage for the default settings
|
|
|
|
decltype(ircd::client::settings)
|
|
|
|
ircd::client::settings
|
|
|
|
{};
|
2016-09-23 08:59:24 +02:00
|
|
|
|
2018-04-20 01:35:59 +02:00
|
|
|
//
|
|
|
|
// client::conf conf::item's
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::conf::item<ircd::seconds>
|
|
|
|
ircd::client::conf::async_timeout_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.conf.async_timeout" },
|
2018-04-22 08:45:25 +02:00
|
|
|
{ "default", 60L },
|
2018-04-20 01:35:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ircd::conf::item<ircd::seconds>
|
|
|
|
ircd::client::conf::request_timeout_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.conf.request_timeout" },
|
2018-05-02 21:44:12 +02:00
|
|
|
{ "default", 30L },
|
2018-04-20 01:35:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ircd::conf::item<size_t>
|
|
|
|
ircd::client::conf::header_max_size_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.conf.header_max_size" },
|
|
|
|
{ "default", ssize_t(8_KiB) },
|
|
|
|
};
|
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
/// Linkage for the default conf
|
|
|
|
decltype(ircd::client::default_conf)
|
|
|
|
ircd::client::default_conf
|
|
|
|
{};
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2018-04-20 01:35:59 +02:00
|
|
|
//
|
|
|
|
// linkages
|
|
|
|
//
|
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
/// The pool of request contexts. When a client makes a request it does so by acquiring
|
|
|
|
/// a stack from this pool. The request handling and response logic can then be written
|
|
|
|
/// in a synchronous manner as if each connection had its own thread.
|
|
|
|
ircd::ctx::pool
|
2018-09-02 00:08:48 +02:00
|
|
|
ircd::client::pool
|
2018-01-14 02:55:21 +01:00
|
|
|
{
|
2018-04-20 01:35:59 +02:00
|
|
|
"client", size_t(settings.stack_size)
|
2018-01-14 02:55:21 +01:00
|
|
|
};
|
2016-08-24 01:08:40 +02:00
|
|
|
|
2018-04-15 01:21:52 +02:00
|
|
|
decltype(ircd::client::ctr)
|
|
|
|
ircd::client::ctr
|
|
|
|
{};
|
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
// Linkage for the container of all active clients for iteration purposes.
|
|
|
|
template<>
|
2018-09-02 06:05:45 +02:00
|
|
|
decltype(ircd::util::instance_multimap<ircd::net::ipport, ircd::client>::map)
|
|
|
|
ircd::util::instance_multimap<ircd::net::ipport, ircd::client>::map
|
2018-01-14 02:55:21 +01:00
|
|
|
{};
|
2016-09-23 08:59:24 +02:00
|
|
|
|
2018-01-09 03:08:26 +01:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
2016-09-23 08:59:24 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::client::init::init()
|
2016-09-11 08:05:38 +02:00
|
|
|
{
|
2018-09-01 12:39:35 +02:00
|
|
|
spawn();
|
2016-09-11 08:05:38 +02:00
|
|
|
}
|
2016-08-22 03:57:43 +02:00
|
|
|
|
2018-03-12 21:30:24 +01:00
|
|
|
ircd::client::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-08-20 03:09:43 +02:00
|
|
|
const ctx::uninterruptible::nothrow ui;
|
|
|
|
|
2018-09-21 02:54:18 +02:00
|
|
|
terminate_all();
|
2018-09-25 09:21:07 +02:00
|
|
|
close_all();
|
2018-09-18 01:47:36 +02:00
|
|
|
wait_all();
|
2018-08-20 03:09:43 +02:00
|
|
|
|
2018-08-28 20:48:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"All client contexts, connections, and requests are clear.",
|
|
|
|
};
|
|
|
|
|
2018-09-02 06:05:45 +02:00
|
|
|
assert(client::map.empty());
|
2018-03-12 21:30:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// util
|
|
|
|
//
|
|
|
|
|
2018-09-01 12:39:35 +02:00
|
|
|
void
|
|
|
|
ircd::client::spawn()
|
|
|
|
{
|
2018-09-02 00:08:48 +02:00
|
|
|
pool.add(size_t(settings.pool_size));
|
2018-09-01 12:39:35 +02:00
|
|
|
}
|
|
|
|
|
2018-03-12 21:30:24 +01:00
|
|
|
void
|
2018-09-18 01:47:36 +02:00
|
|
|
ircd::client::wait_all()
|
2018-03-12 21:30:24 +01:00
|
|
|
{
|
2018-09-02 00:08:48 +02:00
|
|
|
if(pool.active())
|
2018-09-18 01:47:36 +02:00
|
|
|
log::dwarning
|
2018-03-12 21:30:24 +01:00
|
|
|
{
|
2018-09-18 01:47:36 +02:00
|
|
|
"Waiting on %zu active of %zu client request contexts; %zu pending; %zu queued.",
|
2018-09-02 00:08:48 +02:00
|
|
|
pool.active(),
|
|
|
|
pool.size(),
|
|
|
|
pool.pending(),
|
|
|
|
pool.queued()
|
2018-03-12 21:30:24 +01:00
|
|
|
};
|
2017-09-21 04:31:54 +02:00
|
|
|
|
2018-09-18 01:47:36 +02:00
|
|
|
while(!client::map.empty())
|
|
|
|
if(!dock.wait_for(seconds(2)) && !client::map.empty())
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"Waiting for %zu clients to close...", client::map.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Joining %zu active of %zu client request contexts; %zu pending; %zu queued",
|
|
|
|
pool.active(),
|
|
|
|
pool.size(),
|
|
|
|
pool.pending(),
|
|
|
|
pool.queued()
|
|
|
|
};
|
|
|
|
|
|
|
|
pool.join();
|
2017-11-06 21:14:50 +01:00
|
|
|
}
|
|
|
|
|
2018-03-12 21:30:24 +01:00
|
|
|
void
|
|
|
|
ircd::client::close_all()
|
2017-11-06 21:14:50 +01:00
|
|
|
{
|
2018-09-02 06:05:45 +02:00
|
|
|
if(!client::map.empty())
|
2018-03-12 21:30:24 +01:00
|
|
|
log::debug
|
|
|
|
{
|
2018-09-02 06:05:45 +02:00
|
|
|
"Closing %zu clients", client::map.size()
|
2018-03-12 21:30:24 +01:00
|
|
|
};
|
2017-11-06 21:14:50 +01:00
|
|
|
|
2018-09-02 06:05:45 +02:00
|
|
|
auto it(begin(client::map));
|
|
|
|
while(it != end(client::map))
|
2018-03-12 21:30:24 +01:00
|
|
|
{
|
2018-09-02 06:05:45 +02:00
|
|
|
auto c(shared_from(*it->second)); ++it; try
|
2018-03-12 21:30:24 +01:00
|
|
|
{
|
|
|
|
c->close(net::dc::RST, [c](const auto &e)
|
|
|
|
{
|
|
|
|
dock.notify_one();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-05-02 21:44:32 +02:00
|
|
|
log::derror
|
2018-03-12 21:30:24 +01:00
|
|
|
{
|
|
|
|
"Error disconnecting client @%p: %s", c.get(), e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-09-18 01:47:36 +02:00
|
|
|
ircd::client::interrupt_all()
|
2018-03-12 21:30:24 +01:00
|
|
|
{
|
2018-09-02 00:08:48 +02:00
|
|
|
if(pool.active())
|
2018-09-18 01:47:36 +02:00
|
|
|
log::warning
|
2018-03-12 21:30:24 +01:00
|
|
|
{
|
2018-09-18 01:47:36 +02:00
|
|
|
"Interrupting %zu active of %zu client request contexts; %zu pending; %zu queued",
|
2018-09-02 00:08:48 +02:00
|
|
|
pool.active(),
|
|
|
|
pool.size(),
|
|
|
|
pool.pending(),
|
|
|
|
pool.queued()
|
2018-03-12 21:30:24 +01:00
|
|
|
};
|
2017-11-06 21:14:50 +01:00
|
|
|
|
2018-09-18 01:47:36 +02:00
|
|
|
pool.interrupt();
|
|
|
|
}
|
2018-05-28 10:35:10 +02:00
|
|
|
|
2018-09-18 01:47:36 +02:00
|
|
|
void
|
|
|
|
ircd::client::terminate_all()
|
|
|
|
{
|
|
|
|
if(pool.active())
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"Terminating %zu active of %zu client request contexts; %zu pending; %zu queued",
|
|
|
|
pool.active(),
|
|
|
|
pool.size(),
|
|
|
|
pool.pending(),
|
|
|
|
pool.queued()
|
|
|
|
};
|
2018-05-28 10:35:10 +02:00
|
|
|
|
2018-09-18 01:47:36 +02:00
|
|
|
pool.terminate();
|
2016-09-11 08:05:38 +02:00
|
|
|
}
|
2016-08-22 03:57:43 +02:00
|
|
|
|
2018-09-02 07:31:58 +02:00
|
|
|
void
|
|
|
|
ircd::client::create(const std::shared_ptr<socket> &sock)
|
|
|
|
{
|
|
|
|
const auto client
|
|
|
|
{
|
|
|
|
std::make_shared<ircd::client>(sock)
|
|
|
|
};
|
|
|
|
|
|
|
|
client->async();
|
|
|
|
}
|
|
|
|
|
2018-09-02 06:19:15 +02:00
|
|
|
size_t
|
2018-09-02 07:03:25 +02:00
|
|
|
ircd::client::count(net::ipport remote)
|
2018-09-02 06:19:15 +02:00
|
|
|
{
|
2018-09-02 07:03:25 +02:00
|
|
|
std::get<remote.PORT>(remote) = 0;
|
2018-09-02 06:19:15 +02:00
|
|
|
return client::map.count(remote);
|
|
|
|
}
|
|
|
|
|
2017-03-14 02:39:54 +01:00
|
|
|
ircd::parse::read_closure
|
|
|
|
ircd::read_closure(client &client)
|
|
|
|
{
|
2017-03-31 00:46:40 +02:00
|
|
|
// Returns a function the parser can call when it wants more data
|
2017-03-14 02:39:54 +01:00
|
|
|
return [&client](char *&start, char *const &stop)
|
|
|
|
{
|
2018-01-15 08:26:47 +01:00
|
|
|
char *const got(start);
|
|
|
|
read(client, start, stop);
|
|
|
|
//std::cout << ">>>> " << std::distance(got, start) << std::endl;
|
|
|
|
//std::cout << string_view{got, start} << std::endl;
|
|
|
|
//std::cout << "----" << std::endl;
|
2017-03-14 02:39:54 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:44:00 +02:00
|
|
|
char *
|
|
|
|
ircd::read(client &client,
|
|
|
|
char *&start,
|
|
|
|
char *const &stop)
|
|
|
|
{
|
2018-01-07 06:36:17 +01:00
|
|
|
assert(client.sock);
|
2017-10-12 02:44:00 +02:00
|
|
|
auto &sock(*client.sock);
|
2018-01-07 06:36:17 +01:00
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
start, stop
|
|
|
|
};
|
2017-10-12 02:44:00 +02:00
|
|
|
|
|
|
|
char *const base(start);
|
2018-01-07 06:36:17 +01:00
|
|
|
start += net::read(sock, buf);
|
2017-10-12 02:44:00 +02:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2018-01-09 03:08:26 +01:00
|
|
|
ircd::ipport
|
|
|
|
ircd::local(const client &client)
|
|
|
|
{
|
|
|
|
if(!client.sock)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return net::local_ipport(*client.sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ipport
|
|
|
|
ircd::remote(const client &client)
|
|
|
|
{
|
|
|
|
if(!client.sock)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return net::remote_ipport(*client.sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// async loop
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
2018-03-08 18:27:32 +01:00
|
|
|
using error_code = boost::system::error_code;
|
|
|
|
|
2018-01-09 03:08:26 +01:00
|
|
|
static bool handle_ec_default(client &, const error_code &);
|
|
|
|
static bool handle_ec_timeout(client &);
|
|
|
|
static bool handle_ec_short_read(client &);
|
|
|
|
static bool handle_ec_eof(client &);
|
|
|
|
static bool handle_ec(client &, const error_code &);
|
|
|
|
|
2018-08-20 03:09:43 +02:00
|
|
|
static void handle_client_request(std::shared_ptr<client>);
|
2018-01-14 02:55:21 +01:00
|
|
|
static void handle_client_ready(std::shared_ptr<client>, const error_code &ec);
|
2017-12-24 08:34:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This function is the basis for the client's request loop. We still use
|
|
|
|
/// an asynchronous pattern until there is activity on the socket (a request)
|
|
|
|
/// in which case the switch to synchronous mode is made by jumping into an
|
|
|
|
/// ircd::context drawn from the request pool. When the request is finished,
|
|
|
|
/// the client exits back into asynchronous mode until the next request is
|
|
|
|
/// received and rinse and repeat.
|
|
|
|
//
|
|
|
|
/// This sequence exists to avoid any possible c10k-style limitation imposed by
|
|
|
|
/// dedicating a context and its stack space to the lifetime of a connection.
|
|
|
|
/// This is similar to the thread-per-request pattern before async was in vogue.
|
|
|
|
///
|
2018-01-09 03:08:26 +01:00
|
|
|
/// This call returns immediately so we no longer block the current context and
|
|
|
|
/// its stack while waiting for activity on idle connections between requests.
|
2018-03-15 21:44:29 +01:00
|
|
|
bool
|
2018-01-14 02:55:21 +01:00
|
|
|
ircd::client::async()
|
2017-12-24 08:34:11 +01:00
|
|
|
{
|
2018-01-14 02:55:21 +01:00
|
|
|
assert(bool(this->sock));
|
|
|
|
assert(bool(this->conf));
|
|
|
|
auto &sock(*this->sock);
|
2018-04-21 04:54:18 +02:00
|
|
|
const auto &timeout
|
2018-02-22 02:41:28 +01:00
|
|
|
{
|
2018-04-21 04:54:18 +02:00
|
|
|
conf->async_timeout
|
2018-02-22 02:41:28 +01:00
|
|
|
};
|
|
|
|
|
2018-01-08 22:25:13 +01:00
|
|
|
const net::wait_opts opts
|
|
|
|
{
|
2018-02-22 02:41:28 +01:00
|
|
|
net::ready::READ, timeout
|
2018-01-08 22:25:13 +01:00
|
|
|
};
|
|
|
|
|
2018-01-09 03:08:26 +01:00
|
|
|
auto handler
|
2017-12-24 08:34:11 +01:00
|
|
|
{
|
2018-01-14 02:55:21 +01:00
|
|
|
std::bind(ircd::handle_client_ready, shared_from(*this), ph::_1)
|
2018-01-09 03:08:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
sock(opts, std::move(handler));
|
2018-03-15 21:44:29 +01:00
|
|
|
return true;
|
2018-01-09 03:08:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The client's socket is ready for reading. This intermediate handler
|
|
|
|
/// intercepts any errors otherwise dispatches the client to the request
|
|
|
|
/// pool to be married with a stack. Right here this handler is executing on
|
|
|
|
/// the main stack (not in any ircd::context).
|
|
|
|
///
|
|
|
|
/// The context the closure ends up getting is the next available from the
|
|
|
|
/// request pool, which may not be available immediately so this handler might
|
|
|
|
/// be queued for some time after this call returns.
|
|
|
|
void
|
|
|
|
ircd::handle_client_ready(std::shared_ptr<client> client,
|
|
|
|
const error_code &ec)
|
|
|
|
{
|
|
|
|
if(!handle_ec(*client, ec))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto handler
|
|
|
|
{
|
2018-01-14 02:55:21 +01:00
|
|
|
std::bind(ircd::handle_client_request, std::move(client))
|
2018-01-09 03:08:26 +01:00
|
|
|
};
|
|
|
|
|
2018-09-02 00:08:48 +02:00
|
|
|
if(client::pool.avail() == 0)
|
2018-04-20 01:25:39 +02:00
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
"Client context pool exhausted. %zu requests queued.",
|
2018-09-02 00:08:48 +02:00
|
|
|
client::pool.queued()
|
2018-04-20 01:25:39 +02:00
|
|
|
};
|
|
|
|
|
2018-09-02 00:08:48 +02:00
|
|
|
client::pool(std::move(handler));
|
2018-01-09 03:08:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A request context has been dispatched and is now handling this client.
|
|
|
|
/// This function is executing on that ircd::ctx stack. client::main() will
|
|
|
|
/// now be called and synchronous programming is possible. Afterward, the
|
|
|
|
/// client will release this ctx and its stack and fall back to async mode
|
|
|
|
/// or die.
|
|
|
|
void
|
2018-01-14 02:55:21 +01:00
|
|
|
ircd::handle_client_request(std::shared_ptr<client> client)
|
2018-08-20 03:09:43 +02:00
|
|
|
try
|
2018-01-09 03:08:26 +01:00
|
|
|
{
|
2018-04-14 02:03:17 +02:00
|
|
|
// The ircd::ctx now handling this request is referenced and accessible
|
|
|
|
// in client for the duration of this handling.
|
2018-04-21 04:53:36 +02:00
|
|
|
assert(ctx::current);
|
|
|
|
assert(!client->reqctx);
|
2018-04-14 02:03:17 +02:00
|
|
|
client->reqctx = ctx::current;
|
|
|
|
const unwind reset{[&client]
|
|
|
|
{
|
|
|
|
assert(bool(client));
|
2018-04-21 04:53:36 +02:00
|
|
|
assert(client->reqctx);
|
2018-04-22 08:45:25 +02:00
|
|
|
assert(client->reqctx == ctx::current);
|
2018-04-14 02:03:17 +02:00
|
|
|
client->reqctx = nullptr;
|
|
|
|
}};
|
|
|
|
|
2018-08-28 20:48:09 +02:00
|
|
|
if(!client->main())
|
|
|
|
{
|
2018-01-14 02:55:21 +01:00
|
|
|
client->close(net::dc::SSL_NOTIFY).wait();
|
2018-08-28 20:48:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
client->async();
|
2018-03-17 04:41:40 +01:00
|
|
|
}
|
2018-08-20 03:09:43 +02:00
|
|
|
catch(const std::exception &e)
|
2018-03-17 04:41:40 +01:00
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2018-05-07 00:09:12 +02:00
|
|
|
"socket(%p) client(%p) (below main) :%s",
|
2018-03-17 04:41:40 +01:00
|
|
|
client->sock.get(),
|
|
|
|
client.get(),
|
2018-08-20 03:09:43 +02:00
|
|
|
e.what()
|
2018-03-17 04:41:40 +01:00
|
|
|
};
|
2018-01-09 03:08:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This error handling switch is one of two places client errors
|
|
|
|
/// are handled. This handles the errors when the client is in async
|
|
|
|
/// mode rather than during a request. This executes on the main/callback
|
|
|
|
/// stack, not in any ircd::ctx, and must be asynchronous.
|
|
|
|
///
|
|
|
|
bool
|
|
|
|
ircd::handle_ec(client &client,
|
|
|
|
const error_code &ec)
|
|
|
|
{
|
|
|
|
using namespace boost::system::errc;
|
|
|
|
using boost::system::system_category;
|
|
|
|
using boost::asio::error::get_ssl_category;
|
|
|
|
using boost::asio::error::get_misc_category;
|
|
|
|
|
2018-09-26 02:31:56 +02:00
|
|
|
if(unlikely(runlevel != runlevel::RUN && ec == success))
|
2018-09-25 09:21:07 +02:00
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
"socket(%p) local[%s] remote[%s] refusing client request in runlevel %s",
|
|
|
|
client.sock.get(),
|
|
|
|
string(local(client)),
|
|
|
|
string(remote(client)),
|
|
|
|
reflect(runlevel)
|
|
|
|
};
|
|
|
|
|
|
|
|
client.close(net::dc::RST, net::close_ignore);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-09 03:08:26 +01:00
|
|
|
if(ec.category() == system_category()) switch(ec.value())
|
|
|
|
{
|
|
|
|
case success: return true;
|
2018-01-24 23:12:38 +01:00
|
|
|
case operation_canceled: return false;
|
2018-01-20 12:14:14 +01:00
|
|
|
case timed_out: return handle_ec_timeout(client);
|
2018-01-09 03:08:26 +01:00
|
|
|
default: return handle_ec_default(client, ec);
|
|
|
|
}
|
|
|
|
else if(ec.category() == get_misc_category()) switch(ec.value())
|
|
|
|
{
|
|
|
|
case asio::error::eof: return handle_ec_eof(client);
|
|
|
|
default: return handle_ec_default(client, ec);
|
|
|
|
}
|
|
|
|
else if(ec.category() == get_ssl_category()) switch(uint8_t(ec.value()))
|
|
|
|
{
|
|
|
|
case SSL_R_SHORT_READ: return handle_ec_short_read(client);
|
|
|
|
default: return handle_ec_default(client, ec);
|
|
|
|
}
|
|
|
|
else return handle_ec_default(client, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The client indicated they will not be sending the data we have been
|
|
|
|
/// waiting for. The proper behavior now is to initiate a clean shutdown.
|
|
|
|
bool
|
|
|
|
ircd::handle_ec_eof(client &client)
|
|
|
|
try
|
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"socket(%p) local[%s] remote[%s] end of file",
|
|
|
|
client.sock.get(),
|
|
|
|
string(local(client)),
|
|
|
|
string(remote(client))
|
|
|
|
};
|
2018-01-09 03:08:26 +01:00
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
client.close(net::dc::SSL_NOTIFY, net::close_ignore);
|
2018-01-09 03:08:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-05-30 11:56:08 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"socket(%p) EOF: %s",
|
|
|
|
client.sock.get(),
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-09 03:08:26 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The client terminated the connection, likely improperly, and SSL
|
|
|
|
/// is informing us with an opportunity to prevent truncation attacks.
|
|
|
|
/// Best behavior here is to just close the sd.
|
|
|
|
bool
|
|
|
|
ircd::handle_ec_short_read(client &client)
|
|
|
|
try
|
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
"socket(%p) local[%s] remote[%s] short_read",
|
|
|
|
client.sock.get(),
|
|
|
|
string(local(client)),
|
|
|
|
string(remote(client))
|
|
|
|
};
|
2018-01-09 03:08:26 +01:00
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
client.close(net::dc::RST, net::close_ignore);
|
2018-01-09 03:08:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-05-30 11:56:08 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"socket(%p) short_read: %s",
|
|
|
|
client.sock.get(),
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-09 03:08:26 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The net:: system determined the client timed out because we set a timer
|
|
|
|
/// on the socket waiting for data which never arrived. The client may very
|
|
|
|
/// well still be there, so the best thing to do is to attempt a clean
|
|
|
|
/// disconnect.
|
|
|
|
bool
|
|
|
|
ircd::handle_ec_timeout(client &client)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(bool(client.sock));
|
2018-04-09 19:17:57 +02:00
|
|
|
log::debug
|
2018-01-24 23:32:05 +01:00
|
|
|
{
|
|
|
|
"socket(%p) local[%s] remote[%s] disconnecting after inactivity timeout",
|
|
|
|
client.sock.get(),
|
|
|
|
string(local(client)),
|
|
|
|
string(remote(client))
|
|
|
|
};
|
2018-01-09 03:08:26 +01:00
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
client.close(net::dc::SSL_NOTIFY, net::close_ignore);
|
2018-01-09 03:08:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
"socket(%p) timeout: %s",
|
|
|
|
client.sock.get(),
|
|
|
|
e.what()
|
|
|
|
};
|
2018-01-09 03:08:26 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unknown/untreated error. Probably not worth attempting a clean shutdown
|
|
|
|
/// so a hard / immediate disconnect given instead.
|
|
|
|
bool
|
|
|
|
ircd::handle_ec_default(client &client,
|
|
|
|
const error_code &ec)
|
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
"socket(%p) local[%s] remote[%s] %s",
|
|
|
|
client.sock.get(),
|
|
|
|
string(local(client)),
|
|
|
|
string(remote(client)),
|
|
|
|
string(ec)
|
|
|
|
};
|
2018-01-09 03:08:26 +01:00
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
client.close(net::dc::RST, net::close_ignore);
|
2018-01-09 03:08:26 +01:00
|
|
|
return false;
|
2017-12-24 08:34:11 +01:00
|
|
|
}
|
|
|
|
|
2017-10-12 02:44:00 +02:00
|
|
|
//
|
|
|
|
// client
|
|
|
|
//
|
|
|
|
|
2018-09-02 06:05:45 +02:00
|
|
|
ircd::client::client(std::shared_ptr<socket> sock)
|
2018-09-02 07:03:25 +02:00
|
|
|
:instance_multimap{[&sock]
|
|
|
|
() -> net::ipport
|
|
|
|
{
|
|
|
|
assert(bool(sock));
|
2018-09-15 15:42:52 +02:00
|
|
|
const auto &ep(sock->remote());
|
|
|
|
return { ep.address(), 0 };
|
2018-09-02 07:03:25 +02:00
|
|
|
}()}
|
2018-09-02 06:05:45 +02:00
|
|
|
,head_buffer
|
|
|
|
{
|
|
|
|
conf->header_max_size
|
|
|
|
}
|
|
|
|
,sock
|
|
|
|
{
|
|
|
|
std::move(sock)
|
2017-03-11 02:46:25 +01:00
|
|
|
}
|
|
|
|
{
|
2018-04-20 01:35:59 +02:00
|
|
|
assert(size(head_buffer) >= 8_KiB);
|
2017-03-11 02:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::client::~client()
|
2017-10-25 18:49:16 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
//assert(!sock || !connected(*sock));
|
2017-10-25 18:49:16 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2017-03-11 02:46:25 +01:00
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"socket(%p) ~client(%p): %s",
|
|
|
|
sock.get(),
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
2017-10-25 18:49:16 +02:00
|
|
|
return;
|
2017-03-11 02:46:25 +01:00
|
|
|
}
|
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
/// Client main loop.
|
2017-12-22 00:12:36 +01:00
|
|
|
///
|
|
|
|
/// Before main(), the client had been sitting in async mode waiting for
|
|
|
|
/// socket activity. Once activity with data was detected indicating a request,
|
|
|
|
/// the client was dispatched to the request pool where it is paired to an
|
|
|
|
/// ircd::ctx with a stack. main() is then invoked on that ircd::ctx stack.
|
|
|
|
/// Nothing from the socket has been read into userspace before main().
|
|
|
|
///
|
2018-01-07 06:36:17 +01:00
|
|
|
/// This function parses requests off the socket in a loop until there are no
|
|
|
|
/// more requests or there is a fatal error. The ctx will "block" to wait for
|
|
|
|
/// more data off the socket during the middle of a request until the request
|
|
|
|
/// timeout is reached. main() will not "block" to wait for more data after a
|
|
|
|
/// request; it will simply `return true` which puts this client back into
|
|
|
|
/// async mode and relinquishes this stack. returning false will disconnect
|
2018-01-09 03:08:26 +01:00
|
|
|
/// the client rather than putting it back into async mode.
|
|
|
|
///
|
2018-08-20 03:09:43 +02:00
|
|
|
/// Normal exceptions do not pass below main() therefor anything unhandled is an
|
2018-01-09 03:08:26 +01:00
|
|
|
/// internal server error and the client is disconnected. The exception handler
|
|
|
|
/// here though is executing on a request ctx stack, and we can choose to take
|
|
|
|
/// advantage of that; in contrast to the handle_ec() switch which handles
|
|
|
|
/// errors on the main/callback stack and must be asynchronous.
|
2018-01-07 06:36:17 +01:00
|
|
|
///
|
|
|
|
bool
|
2018-01-14 02:55:21 +01:00
|
|
|
ircd::client::main()
|
2018-08-20 03:09:43 +02:00
|
|
|
try
|
2018-01-07 06:36:17 +01:00
|
|
|
{
|
2018-02-17 23:28:06 +01:00
|
|
|
parse::buffer pb{head_buffer};
|
2017-09-12 19:04:40 +02:00
|
|
|
parse::capstan pc{pb, read_closure(*this)}; do
|
|
|
|
{
|
2018-01-14 02:55:21 +01:00
|
|
|
if(!handle_request(pc))
|
2017-09-12 19:04:40 +02:00
|
|
|
return false;
|
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
// After the request, the head and content has been read off the socket
|
|
|
|
// and the capstan has advanced to the end of the content. The catch is
|
|
|
|
// that reading off the socket could have read too much, bleeding into
|
|
|
|
// the next request. This is rare, but pb.remove() will memmove() the
|
|
|
|
// bleed back to the beginning of the head buffer for the next loop.
|
2017-09-12 19:04:40 +02:00
|
|
|
pb.remove();
|
|
|
|
}
|
|
|
|
while(pc.unparsed());
|
|
|
|
|
|
|
|
return true;
|
2017-03-11 02:46:25 +01:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
2016-09-12 23:07:46 +02:00
|
|
|
{
|
2017-08-23 23:13:57 +02:00
|
|
|
using namespace boost::system::errc;
|
2017-12-29 23:53:39 +01:00
|
|
|
using boost::system::system_category;
|
2017-10-27 00:36:31 +02:00
|
|
|
using boost::asio::error::get_ssl_category;
|
|
|
|
using boost::asio::error::get_misc_category;
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
"socket(%p) local[%s] remote[%s] error during request: %s",
|
|
|
|
sock.get(),
|
|
|
|
string(local(*this)),
|
|
|
|
string(remote(*this)),
|
|
|
|
string(e.code())
|
|
|
|
};
|
2018-01-08 12:07:20 +01:00
|
|
|
|
2017-11-17 07:02:57 +01:00
|
|
|
const error_code &ec{e.code()};
|
|
|
|
const int &value{ec.value()};
|
2017-12-29 23:53:39 +01:00
|
|
|
if(ec.category() == system_category()) switch(value)
|
2017-08-23 23:13:57 +02:00
|
|
|
{
|
|
|
|
case success:
|
2017-11-01 23:51:24 +01:00
|
|
|
assert(0);
|
2017-08-23 23:13:57 +02:00
|
|
|
return true;
|
|
|
|
|
2017-08-23 22:47:15 +02:00
|
|
|
case broken_pipe:
|
|
|
|
case connection_reset:
|
2017-08-23 23:13:57 +02:00
|
|
|
case not_connected:
|
2018-01-14 02:55:21 +01:00
|
|
|
close(net::dc::RST, net::close_ignore);
|
2017-11-01 23:51:24 +01:00
|
|
|
return false;
|
|
|
|
|
2017-08-23 23:50:37 +02:00
|
|
|
case operation_canceled:
|
2018-01-20 12:14:14 +01:00
|
|
|
case timed_out:
|
2017-11-01 23:51:24 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case bad_file_descriptor:
|
2017-08-23 23:13:57 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-11-17 07:02:57 +01:00
|
|
|
else if(ec.category() == get_ssl_category()) switch(uint8_t(value))
|
2017-10-27 00:36:31 +02:00
|
|
|
{
|
2017-11-17 07:02:57 +01:00
|
|
|
case SSL_R_SHORT_READ:
|
2018-01-14 02:55:21 +01:00
|
|
|
close(net::dc::RST, net::close_ignore);
|
2018-01-08 12:07:20 +01:00
|
|
|
return false;
|
|
|
|
|
2017-11-25 22:20:46 +01:00
|
|
|
case SSL_R_PROTOCOL_IS_SHUTDOWN:
|
2018-01-14 02:55:21 +01:00
|
|
|
close(net::dc::RST, net::close_ignore);
|
2017-10-27 00:36:31 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-11-17 07:02:57 +01:00
|
|
|
else if(ec.category() == get_misc_category()) switch(value)
|
2017-10-27 00:36:31 +02:00
|
|
|
{
|
2017-11-17 07:02:57 +01:00
|
|
|
case boost::asio::error::eof:
|
2017-10-27 00:36:31 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"socket(%p) (unexpected) %s: (%d) %s",
|
|
|
|
sock.get(),
|
|
|
|
ec.category().name(),
|
|
|
|
value,
|
|
|
|
ec.message()
|
|
|
|
};
|
2017-08-23 23:13:57 +02:00
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
close(net::dc::RST, net::close_ignore);
|
2016-09-12 23:07:46 +02:00
|
|
|
return false;
|
2016-09-11 08:05:38 +02:00
|
|
|
}
|
2018-05-07 00:09:12 +02:00
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"socket(%p) local[%s] remote[%s] Request interrupted: %s",
|
|
|
|
sock.get(),
|
|
|
|
string(local(*this)),
|
|
|
|
string(remote(*this)),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
close(net::dc::SSL_NOTIFY, net::close_ignore);
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 02:55:21 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::critical
|
2018-03-08 16:40:41 +01:00
|
|
|
{
|
2018-03-16 00:51:18 +01:00
|
|
|
"socket(%p) local[%s] remote[%s] %s",
|
2018-03-15 20:26:29 +01:00
|
|
|
sock.get(),
|
|
|
|
string(local(*this)),
|
2018-03-08 16:40:41 +01:00
|
|
|
string(remote(*this)),
|
2018-03-09 20:35:55 +01:00
|
|
|
e.what()
|
2018-03-08 16:40:41 +01:00
|
|
|
};
|
2018-01-14 02:55:21 +01:00
|
|
|
|
2018-03-08 16:40:41 +01:00
|
|
|
return false;
|
2018-01-14 02:55:21 +01:00
|
|
|
}
|
2018-05-07 00:09:12 +02:00
|
|
|
catch(const ctx::terminated &)
|
|
|
|
{
|
|
|
|
close(net::dc::RST, net::close_ignore);
|
2018-08-20 03:09:43 +02:00
|
|
|
throw;
|
2018-05-07 00:09:12 +02:00
|
|
|
}
|
2018-01-14 02:55:21 +01:00
|
|
|
|
2018-01-12 03:45:25 +01:00
|
|
|
/// Handle a single request within the client main() loop.
|
|
|
|
///
|
|
|
|
/// This function returns false if the main() loop should exit
|
|
|
|
/// and thus disconnect the client. It should return true in most
|
|
|
|
/// cases even for lightly erroneous requests that won't affect
|
|
|
|
/// the next requests on the tape.
|
|
|
|
///
|
|
|
|
/// This function is timed. The timeout will prevent a client from
|
|
|
|
/// sending a partial request and leave us waiting for the rest.
|
|
|
|
/// As of right now this timeout extends to our handling of the
|
|
|
|
/// request too.
|
|
|
|
bool
|
2018-01-14 02:55:21 +01:00
|
|
|
ircd::client::handle_request(parse::capstan &pc)
|
2018-01-12 03:45:25 +01:00
|
|
|
try
|
|
|
|
{
|
2018-04-16 01:42:13 +02:00
|
|
|
net::scope_timeout timeout
|
2018-01-12 03:45:25 +01:00
|
|
|
{
|
2018-01-14 02:55:21 +01:00
|
|
|
*sock, conf->request_timeout
|
2018-01-12 03:45:25 +01:00
|
|
|
};
|
|
|
|
|
2018-02-12 20:58:40 +01:00
|
|
|
// This is the first read off the wire. The headers are entirely read and
|
|
|
|
// the tape is advanced.
|
|
|
|
timer = ircd::timer{};
|
2018-02-18 00:44:53 +01:00
|
|
|
const http::request::head head{pc};
|
2018-02-17 23:28:06 +01:00
|
|
|
head_length = pc.parsed - data(head_buffer);
|
2018-02-12 20:58:40 +01:00
|
|
|
content_consumed = std::min(pc.unparsed(), head.content_length);
|
|
|
|
pc.parsed += content_consumed;
|
2018-01-14 02:55:21 +01:00
|
|
|
assert(pc.parsed <= pc.read);
|
2018-02-12 20:58:40 +01:00
|
|
|
|
2018-04-16 01:42:13 +02:00
|
|
|
// The resource being sought will have its own specific timeout, or none
|
|
|
|
// at all. This timeout is now canceled to not conflict. Note that the
|
|
|
|
// time spent so far is still being accumulated by client.timer.
|
|
|
|
timeout.cancel();
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"socket(%p) local[%s] remote[%s] HTTP %s `%s' content-length:%zu have:%zu",
|
|
|
|
sock.get(),
|
|
|
|
string(local(*this)),
|
|
|
|
string(remote(*this)),
|
|
|
|
head.method,
|
|
|
|
head.path,
|
|
|
|
head.content_length,
|
|
|
|
content_consumed
|
|
|
|
};
|
2018-01-14 02:55:21 +01:00
|
|
|
|
2018-01-12 03:45:25 +01:00
|
|
|
bool ret
|
|
|
|
{
|
2018-02-18 00:44:53 +01:00
|
|
|
resource_request(head)
|
2018-01-12 03:45:25 +01:00
|
|
|
};
|
|
|
|
|
2018-02-12 20:58:40 +01:00
|
|
|
if(ret && iequals(head.connection, "close"_sv))
|
2018-01-12 03:45:25 +01:00
|
|
|
ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-01-15 08:26:47 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
if(e.code().value() != boost::system::errc::operation_canceled)
|
|
|
|
throw;
|
|
|
|
|
2018-04-14 00:36:10 +02:00
|
|
|
if(!sock || sock->fini)
|
|
|
|
return false;
|
|
|
|
|
2018-03-25 01:41:57 +01:00
|
|
|
const ctx::exception_handler eh;
|
2018-01-15 08:26:47 +01:00
|
|
|
resource::response
|
|
|
|
{
|
2018-03-09 22:15:32 +01:00
|
|
|
*this, http::REQUEST_TIMEOUT, {}, 0L, {}
|
2018-01-15 08:26:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-14 00:36:10 +02:00
|
|
|
catch(const std::exception &e)
|
2018-01-12 03:45:25 +01:00
|
|
|
{
|
2018-04-14 00:36:10 +02:00
|
|
|
if(!sock || sock->fini)
|
|
|
|
return false;
|
2018-03-25 01:41:57 +01:00
|
|
|
|
2018-03-16 00:51:18 +01:00
|
|
|
log::error
|
|
|
|
{
|
2018-04-14 00:36:10 +02:00
|
|
|
"socket(%p) local[%s] remote[%s] HTTP 500 Internal Error: %s",
|
2018-03-16 00:51:18 +01:00
|
|
|
sock.get(),
|
|
|
|
string(local(*this)),
|
|
|
|
string(remote(*this)),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
2018-04-14 00:36:10 +02:00
|
|
|
const ctx::exception_handler eh;
|
2018-01-12 03:45:25 +01:00
|
|
|
resource::response
|
|
|
|
{
|
2018-03-09 20:35:55 +01:00
|
|
|
*this, e.what(), "text/html; charset=utf8", http::INTERNAL_SERVER_ERROR
|
2018-01-12 03:45:25 +01:00
|
|
|
};
|
|
|
|
|
2018-03-16 00:51:18 +01:00
|
|
|
return false;
|
2018-01-12 03:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-18 00:44:53 +01:00
|
|
|
ircd::client::resource_request(const http::request::head &head)
|
2018-01-12 03:45:25 +01:00
|
|
|
try
|
|
|
|
{
|
2018-02-12 20:58:40 +01:00
|
|
|
const string_view content_partial
|
|
|
|
{
|
2018-02-17 23:28:06 +01:00
|
|
|
data(head_buffer) + head_length, content_consumed
|
2018-02-12 20:58:40 +01:00
|
|
|
};
|
|
|
|
|
2018-01-12 03:45:25 +01:00
|
|
|
auto &resource
|
|
|
|
{
|
2018-02-12 20:58:40 +01:00
|
|
|
ircd::resource::find(head.path)
|
2018-01-12 03:45:25 +01:00
|
|
|
};
|
|
|
|
|
2018-02-12 20:58:40 +01:00
|
|
|
resource(*this, head, content_partial);
|
2018-02-18 00:44:53 +01:00
|
|
|
discard_unconsumed(head);
|
2018-01-12 03:45:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-16 00:51:18 +01:00
|
|
|
catch(const http::error &e)
|
2018-01-12 03:45:25 +01:00
|
|
|
{
|
2018-03-25 01:41:57 +01:00
|
|
|
const ctx::exception_handler eh;
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
log::derror
|
2018-02-24 06:57:00 +01:00
|
|
|
{
|
2018-03-11 18:23:06 +01:00
|
|
|
"socket(%p) local[%s] remote[%s] HTTP %u %s `%s' :%s",
|
2018-02-24 06:57:00 +01:00
|
|
|
sock.get(),
|
|
|
|
string(local(*this)),
|
|
|
|
string(remote(*this)),
|
2018-03-16 00:51:18 +01:00
|
|
|
uint(e.code),
|
|
|
|
http::status(e.code),
|
2018-02-24 06:57:00 +01:00
|
|
|
head.uri,
|
2018-03-16 00:51:18 +01:00
|
|
|
e.content
|
2018-02-24 06:57:00 +01:00
|
|
|
};
|
|
|
|
|
2018-01-12 03:45:25 +01:00
|
|
|
resource::response
|
|
|
|
{
|
2018-03-16 00:51:18 +01:00
|
|
|
*this, e.content, "text/html; charset=utf8", e.code, e.headers
|
2018-01-12 03:45:25 +01:00
|
|
|
};
|
|
|
|
|
2018-03-16 00:51:18 +01:00
|
|
|
switch(e.code)
|
2018-01-12 03:45:25 +01:00
|
|
|
{
|
|
|
|
// These codes are "unrecoverable" errors and no more HTTP can be
|
|
|
|
// conducted with this tape. The client must be disconnected.
|
|
|
|
case http::BAD_REQUEST:
|
|
|
|
case http::REQUEST_TIMEOUT:
|
2018-01-15 08:26:47 +01:00
|
|
|
case http::PAYLOAD_TOO_LARGE:
|
|
|
|
case http::INTERNAL_SERVER_ERROR:
|
2018-01-12 03:45:25 +01:00
|
|
|
return false;
|
|
|
|
|
2018-01-15 08:26:47 +01:00
|
|
|
// These codes are "recoverable" and allow the next HTTP request in
|
|
|
|
// a pipeline to take place.
|
|
|
|
default:
|
2018-02-18 00:44:53 +01:00
|
|
|
discard_unconsumed(head);
|
2018-01-15 08:26:47 +01:00
|
|
|
return true;
|
2018-01-12 03:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 02:55:21 +01:00
|
|
|
|
|
|
|
void
|
2018-02-18 00:44:53 +01:00
|
|
|
ircd::client::discard_unconsumed(const http::request::head &head)
|
2018-01-14 02:55:21 +01:00
|
|
|
{
|
|
|
|
if(unlikely(!sock))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const size_t unconsumed
|
|
|
|
{
|
2018-02-12 20:58:40 +01:00
|
|
|
head.content_length - content_consumed
|
2018-01-14 02:55:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!unconsumed)
|
|
|
|
return;
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"socket(%p) local[%s] remote[%s] discarding %zu unconsumed of %zu bytes content...",
|
|
|
|
sock.get(),
|
|
|
|
string(local(*this)),
|
|
|
|
string(remote(*this)),
|
|
|
|
unconsumed,
|
|
|
|
head.content_length
|
|
|
|
};
|
2018-01-14 02:55:21 +01:00
|
|
|
|
2018-02-12 20:58:40 +01:00
|
|
|
content_consumed += net::discard_all(*sock, unconsumed);
|
|
|
|
assert(content_consumed == head.content_length);
|
2018-01-14 02:55:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::future<void>
|
|
|
|
ircd::client::close(const net::close_opts &opts)
|
|
|
|
{
|
2018-03-11 19:29:31 +01:00
|
|
|
if(likely(sock) && !sock->fini)
|
2018-01-14 02:55:21 +01:00
|
|
|
return net::close(*sock, opts);
|
|
|
|
else
|
2018-03-11 19:29:31 +01:00
|
|
|
return ctx::future<void>::already;
|
2018-01-14 02:55:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::client::close(const net::close_opts &opts,
|
|
|
|
net::close_callback callback)
|
|
|
|
{
|
2018-03-11 19:29:31 +01:00
|
|
|
if(!sock)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(sock->fini)
|
|
|
|
return callback({});
|
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
net::close(*sock, opts, std::move(callback));
|
|
|
|
}
|
2018-03-11 18:48:58 +01:00
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::client::write_all(const const_buffer &buf)
|
|
|
|
{
|
|
|
|
if(unlikely(!sock))
|
2018-03-15 20:26:29 +01:00
|
|
|
throw error{"No socket to client."};
|
2018-03-11 18:48:58 +01:00
|
|
|
|
|
|
|
return net::write_all(*sock, buf);
|
|
|
|
}
|