2007-01-25 07:40:21 +01:00
|
|
|
/*
|
2007-12-03 17:59:25 +01:00
|
|
|
* charybdis: an advanced ircd.
|
2007-01-25 07:40:21 +01:00
|
|
|
* client.c: Controls clients.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
|
|
|
|
* Copyright (C) 1996-2002 Hybrid Development Team
|
|
|
|
* Copyright (C) 2002-2005 ircd-ratbox development team
|
2007-12-03 17:59:25 +01:00
|
|
|
* Copyright (C) 2007 William Pitcock
|
2016-09-10 21:57:33 +02:00
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
2007-01-25 07:40:21 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA
|
|
|
|
*/
|
|
|
|
|
2017-09-21 04:31:54 +02:00
|
|
|
#include <ircd/asio.h>
|
2016-09-12 23:07:46 +02:00
|
|
|
|
2016-09-23 08:59:24 +02:00
|
|
|
namespace ircd {
|
|
|
|
|
2017-08-23 23:50:37 +02:00
|
|
|
// Default time limit for how long a client connection can be in "async mode"
|
|
|
|
// (or idle mode) after which it is disconnected.
|
2017-03-14 02:39:54 +01:00
|
|
|
const auto async_timeout
|
|
|
|
{
|
2017-09-26 03:43:33 +02:00
|
|
|
300s
|
2017-03-14 02:39:54 +01:00
|
|
|
};
|
|
|
|
|
2017-08-23 23:50:37 +02:00
|
|
|
// Time limit for how long a connected client can be in "request mode." This
|
|
|
|
// should never be hit unless there's an error in the handling code.
|
2017-03-14 02:39:54 +01:00
|
|
|
const auto request_timeout
|
|
|
|
{
|
2017-09-26 03:43:33 +02:00
|
|
|
60s
|
2017-03-14 02:39:54 +01:00
|
|
|
};
|
|
|
|
|
2017-08-23 23:50:37 +02: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.
|
2016-11-29 16:23:38 +01:00
|
|
|
ctx::pool request
|
2016-09-11 08:05:38 +02:00
|
|
|
{
|
2017-10-12 03:16:10 +02:00
|
|
|
"request", 256_KiB
|
2016-09-23 08:59:24 +02:00
|
|
|
};
|
2016-09-11 08:05:38 +02:00
|
|
|
|
2017-08-23 23:50:37 +02:00
|
|
|
// Container for all active clients (connections) for iteration purposes.
|
|
|
|
client::list client::clients;
|
2016-09-23 08:59:24 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
bool handle_ec_timeout(client &);
|
2016-09-23 08:59:24 +02:00
|
|
|
bool handle_ec_eof(client &);
|
|
|
|
bool handle_ec_success(client &);
|
2017-09-30 08:04:41 +02:00
|
|
|
bool handle_ec(client &, const net::error_code &);
|
2016-09-23 08:59:24 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
void async_recv_next(std::shared_ptr<client>, const milliseconds &timeout);
|
|
|
|
void async_recv_next(std::shared_ptr<client>);
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2017-08-23 23:50:37 +02:00
|
|
|
void disconnect(client &, const socket::dc & = socket::dc::RST);
|
2016-11-29 16:23:38 +01:00
|
|
|
void disconnect_all();
|
2016-08-24 01:08:40 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class... args> std::shared_ptr<client> make_client(args&&...);
|
2016-09-23 08:59:24 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
} // namespace ircd
|
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
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
request.add(2);
|
2016-09-11 08:05:38 +02:00
|
|
|
}
|
2016-08-22 03:57:43 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::client::init::~init()
|
2016-09-11 08:05:38 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2017-09-21 04:31:54 +02:00
|
|
|
log::debug("Interrupting %zu requests; dropping %zu requests; disconnecting %zu clients.",
|
|
|
|
request.active(),
|
|
|
|
request.pending(),
|
|
|
|
client::clients.size());
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
request.interrupt();
|
|
|
|
disconnect_all();
|
2017-09-20 07:23:49 +02:00
|
|
|
request.join();
|
2016-09-11 08:05:38 +02:00
|
|
|
}
|
2016-08-22 03:57:43 +02:00
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::hostport
|
|
|
|
ircd::local(const client &client)
|
2017-03-11 02:46:25 +01:00
|
|
|
{
|
|
|
|
if(!client.sock)
|
|
|
|
return { "0.0.0.0"s, 0 };
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
return net::local_hostport(*client.sock);
|
2017-03-11 02:46:25 +01:00
|
|
|
}
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::hostport
|
|
|
|
ircd::remote(const client &client)
|
2017-03-11 02:46:25 +01:00
|
|
|
{
|
|
|
|
if(!client.sock)
|
|
|
|
return { "0.0.0.0"s, 0 };
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
return net::remote_hostport(*client.sock);
|
2017-03-11 02:46:25 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 02:39:54 +01:00
|
|
|
ircd::http::response::write_closure
|
|
|
|
ircd::write_closure(client &client)
|
|
|
|
{
|
2017-03-31 00:46:40 +02:00
|
|
|
// returns a function that can be called to send an iovector of data to a client
|
2017-09-12 18:28:41 +02:00
|
|
|
return [&client](const ilist<const_buffer> &iov)
|
2017-03-14 02:39:54 +01:00
|
|
|
{
|
2017-08-23 23:13:57 +02:00
|
|
|
//std::cout << "<<<<" << std::endl;
|
|
|
|
//std::cout << iov << std::endl;
|
|
|
|
//std::cout << "----" << std::endl;
|
|
|
|
const auto written
|
|
|
|
{
|
|
|
|
write(*client.sock, iov)
|
|
|
|
};
|
2017-03-14 02:39:54 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::parse::read_closure
|
|
|
|
ircd::read_closure(client &client)
|
|
|
|
{
|
|
|
|
static const auto handle_error([]
|
|
|
|
(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
using namespace boost::system::errc;
|
|
|
|
|
|
|
|
switch(e.code().value())
|
|
|
|
{
|
|
|
|
case operation_canceled: throw http::error(http::REQUEST_TIMEOUT);
|
|
|
|
default: throw boost::system::system_error(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-03-21 03:30:07 +01:00
|
|
|
const char *const got(start);
|
2017-03-14 02:39:54 +01:00
|
|
|
read(client, start, stop);
|
2017-08-23 23:13:57 +02:00
|
|
|
//std::cout << ">>>>" << std::endl;
|
|
|
|
//std::cout << string_view{got, start} << std::endl;
|
|
|
|
//std::cout << "----" << std::endl;
|
2017-03-14 02:39:54 +01:00
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
handle_error(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:44:00 +02:00
|
|
|
char *
|
|
|
|
ircd::read(client &client,
|
|
|
|
char *&start,
|
|
|
|
char *const &stop)
|
|
|
|
{
|
|
|
|
auto &sock(*client.sock);
|
|
|
|
const std::array<mutable_buffer, 1> bufs
|
|
|
|
{{
|
|
|
|
{ start, stop }
|
|
|
|
}};
|
|
|
|
|
|
|
|
char *const base(start);
|
|
|
|
start += sock.read_some(bufs);
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ircd::write(client &client,
|
|
|
|
const char *&start,
|
|
|
|
const char *const &stop)
|
|
|
|
{
|
|
|
|
auto &sock(*client.sock);
|
|
|
|
const std::array<const_buffer, 1> bufs
|
|
|
|
{{
|
|
|
|
{ start, stop }
|
|
|
|
}};
|
|
|
|
|
|
|
|
const char *const base(start);
|
|
|
|
start += sock.write(bufs);
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// client
|
|
|
|
//
|
|
|
|
|
2017-03-11 02:46:25 +01:00
|
|
|
ircd::client::client()
|
|
|
|
:client{std::shared_ptr<socket>{}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-30 08:04:41 +02:00
|
|
|
ircd::client::client(const hostport &host_port,
|
2017-03-11 02:46:25 +01:00
|
|
|
const seconds &timeout)
|
|
|
|
:client
|
|
|
|
{
|
|
|
|
std::make_shared<socket>(host_port.first, host_port.second, timeout)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::client::client(std::shared_ptr<socket> sock)
|
2017-08-23 22:47:15 +02:00
|
|
|
:clit{clients, clients.emplace(end(clients), this)}
|
2017-03-11 02:46:25 +01:00
|
|
|
,sock{std::move(sock)}
|
2017-09-24 04:51:06 +02:00
|
|
|
,request_timer{ircd::timer::nostart}
|
2017-03-11 02:46:25 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::client::~client()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-12 19:04:40 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
void handle_request(client &client, parse::capstan &pc, const http::request::head &head);
|
|
|
|
bool handle_request(client &client, parse::capstan &pc);
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:46:25 +01:00
|
|
|
bool
|
|
|
|
ircd::client::main()
|
|
|
|
noexcept try
|
|
|
|
{
|
2017-10-12 03:16:10 +02:00
|
|
|
const auto header_max{8192};
|
|
|
|
const auto content_max{65536};
|
|
|
|
unique_buffer<mutable_buffer> buffer
|
|
|
|
{
|
|
|
|
header_max + content_max
|
|
|
|
};
|
|
|
|
|
|
|
|
parse::buffer pb{buffer};
|
2017-09-12 19:04:40 +02:00
|
|
|
parse::capstan pc{pb, read_closure(*this)}; do
|
|
|
|
{
|
|
|
|
if(!handle_request(*this, pc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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:50:37 +02:00
|
|
|
using boost::asio::error::eof;
|
2017-08-23 22:47:15 +02:00
|
|
|
using boost::asio::error::broken_pipe;
|
|
|
|
using boost::asio::error::connection_reset;
|
2017-08-23 23:13:57 +02:00
|
|
|
using namespace boost::system::errc;
|
|
|
|
|
|
|
|
switch(e.code().value())
|
|
|
|
{
|
|
|
|
case success:
|
|
|
|
assert(0);
|
|
|
|
return true;
|
|
|
|
|
2017-08-23 23:50:37 +02:00
|
|
|
case eof:
|
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:
|
2017-08-23 23:50:37 +02:00
|
|
|
case operation_canceled:
|
2017-08-23 23:13:57 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
log::critical("(unexpected) system_error: %s", e.what());
|
|
|
|
if(ircd::debugmode)
|
|
|
|
std::terminate();
|
|
|
|
|
2016-09-12 23:07:46 +02:00
|
|
|
return false;
|
2016-09-11 08:05:38 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
catch(const std::exception &e)
|
2017-03-14 00:11:30 +01:00
|
|
|
{
|
|
|
|
log::error("client[%s] [500 Internal Error]: %s",
|
2017-09-30 08:04:41 +02:00
|
|
|
string(remote(*this)),
|
2017-03-14 00:11:30 +01:00
|
|
|
e.what());
|
|
|
|
|
2017-08-23 23:13:57 +02:00
|
|
|
if(ircd::debugmode)
|
2017-09-12 19:04:00 +02:00
|
|
|
std::terminate();
|
2017-08-23 23:13:57 +02:00
|
|
|
|
2017-03-14 00:11:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::handle_request(client &client,
|
|
|
|
parse::capstan &pc)
|
|
|
|
try
|
|
|
|
{
|
2017-09-24 04:46:17 +02:00
|
|
|
client.request_timer = ircd::timer{};
|
2017-03-14 02:39:54 +01:00
|
|
|
client.sock->set_timeout(request_timeout, [&client]
|
2017-09-30 08:04:41 +02:00
|
|
|
(const net::error_code &ec)
|
2017-03-14 02:39:54 +01:00
|
|
|
{
|
|
|
|
if(!ec)
|
|
|
|
client.sock->cancel();
|
|
|
|
});
|
|
|
|
|
2017-03-13 22:07:58 +01:00
|
|
|
http::request
|
|
|
|
{
|
2017-03-14 00:11:30 +01:00
|
|
|
pc, nullptr, write_closure(client), [&client, &pc]
|
|
|
|
(const auto &head)
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2017-03-14 02:39:54 +01:00
|
|
|
client.sock->timer.cancel();
|
2017-03-14 00:11:30 +01:00
|
|
|
handle_request(client, pc, head);
|
2017-03-13 22:07:58 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const http::error &e)
|
|
|
|
{
|
2017-09-24 04:46:17 +02:00
|
|
|
log::debug("client[%s] HTTP %s in %ld$us %s",
|
2017-09-30 08:04:41 +02:00
|
|
|
string(remote(client)),
|
2017-08-23 23:13:57 +02:00
|
|
|
e.what(),
|
2017-09-24 04:46:17 +02:00
|
|
|
client.request_timer.at<microseconds>().count(),
|
2017-08-23 23:13:57 +02:00
|
|
|
e.content);
|
2017-03-13 22:07:58 +01:00
|
|
|
|
|
|
|
switch(e.code)
|
|
|
|
{
|
|
|
|
case http::BAD_REQUEST: return false;
|
|
|
|
case http::INTERNAL_SERVER_ERROR: return false;
|
2017-03-14 02:39:54 +01:00
|
|
|
case http::REQUEST_TIMEOUT: return false;
|
2017-03-13 22:07:58 +01:00
|
|
|
default: return true;
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 00:11:30 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
ircd::handle_request(client &client,
|
|
|
|
parse::capstan &pc,
|
|
|
|
const http::request::head &head)
|
2017-03-13 22:07:58 +01:00
|
|
|
{
|
2017-08-23 23:50:37 +02:00
|
|
|
log::debug("client[%s] HTTP %s `%s' (content-length: %zu)",
|
2017-09-30 08:04:41 +02:00
|
|
|
string(remote(client)),
|
2017-08-23 23:13:57 +02:00
|
|
|
head.method,
|
|
|
|
head.path,
|
|
|
|
head.content_length);
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2017-09-21 11:12:29 +02:00
|
|
|
auto &resource
|
|
|
|
{
|
|
|
|
ircd::resource::find(head.path)
|
|
|
|
};
|
|
|
|
|
2017-03-14 00:11:30 +01:00
|
|
|
resource(client, pc, head);
|
|
|
|
}
|
2017-03-13 22:07:58 +01:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
std::shared_ptr<ircd::client>
|
|
|
|
ircd::add_client(std::shared_ptr<socket> s)
|
2016-09-12 23:07:46 +02:00
|
|
|
{
|
2017-08-23 22:47:15 +02:00
|
|
|
const auto client
|
|
|
|
{
|
|
|
|
make_client(std::move(s))
|
|
|
|
};
|
|
|
|
|
2017-08-23 23:13:57 +02:00
|
|
|
log::debug("client[%s] CONNECTED local[%s]",
|
2017-09-30 08:04:41 +02:00
|
|
|
string(remote(*client)),
|
|
|
|
string(local(*client)));
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-14 02:39:54 +01:00
|
|
|
async_recv_next(client, async_timeout);
|
2016-11-29 16:23:38 +01:00
|
|
|
return client;
|
2016-09-12 23:07:46 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
template<class... args>
|
|
|
|
std::shared_ptr<ircd::client>
|
|
|
|
ircd::make_client(args&&... a)
|
2016-09-12 23:07:46 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
return std::make_shared<client>(std::forward<args>(a)...);
|
2016-09-11 08:05:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::disconnect_all()
|
2016-09-13 02:10:04 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
for(auto &client : client::clients) try
|
|
|
|
{
|
|
|
|
disconnect(*client, socket::dc::RST);
|
|
|
|
}
|
2017-08-23 23:13:57 +02:00
|
|
|
catch(const std::exception &e)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-08-23 23:13:57 +02:00
|
|
|
log::warning("Error disconnecting client @%p: %s", &client, e.what());
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2016-09-13 02:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::disconnect(client &client,
|
|
|
|
const socket::dc &type)
|
2015-12-13 00:22:21 +01:00
|
|
|
{
|
2016-09-12 23:07:46 +02:00
|
|
|
auto &sock(*client.sock);
|
2016-11-29 16:23:38 +01:00
|
|
|
sock.disconnect(type);
|
2015-12-13 00:22:21 +01:00
|
|
|
}
|
|
|
|
|
2016-09-13 02:10:04 +02:00
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::async_recv_next(std::shared_ptr<client> client)
|
2016-09-13 02:10:04 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
async_recv_next(std::move(client), milliseconds(-1));
|
2016-09-13 02:10:04 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02: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.
|
2017-08-23 22:47:15 +02:00
|
|
|
//
|
2017-09-12 18:37:44 +02:00
|
|
|
/// 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.
|
2017-08-23 22:47:15 +02:00
|
|
|
//
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Developers: Pay close attention to the comments to know exactly where you
|
|
|
|
/// are and what you can do at any given point in this sequence.
|
|
|
|
///
|
2016-09-11 08:05:38 +02:00
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::async_recv_next(std::shared_ptr<client> client,
|
|
|
|
const milliseconds &timeout)
|
2016-09-11 08:05:38 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
auto &sock(*client->sock);
|
2017-08-23 22:47:15 +02: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.
|
2017-09-30 08:04:41 +02:00
|
|
|
sock(timeout, [client(std::move(client)), timeout](const net::error_code &ec)
|
2016-11-29 16:23:38 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2017-08-23 22:47:15 +02:00
|
|
|
// Right here this handler is executing on the main stack (not in any
|
|
|
|
// ircd::context). We handle any socket errors now, and if this function
|
|
|
|
// returns here the client's shared_ptr may expire and that will be the
|
|
|
|
// end of this client, socket, and connection...
|
2016-11-29 16:23:38 +01:00
|
|
|
if(!handle_ec(*client, ec))
|
|
|
|
return;
|
2016-09-13 02:10:04 +02:00
|
|
|
|
2017-08-23 22:47:15 +02:00
|
|
|
// This call returns immediately because we can never block the main stack outside
|
|
|
|
// of the ircd::context system. 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.
|
|
|
|
request([client(std::move(client)), timeout]
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-08-23 22:47:15 +02:00
|
|
|
// Right here this handler is executing on an ircd::context with its own
|
|
|
|
// stack dedicated to the lifetime of this request. If client::main()
|
|
|
|
// returns true, we bring the client back into async mode to wait for
|
|
|
|
// the next request.
|
2016-11-29 16:23:38 +01:00
|
|
|
if(client->main())
|
|
|
|
async_recv_next(client, timeout);
|
|
|
|
});
|
|
|
|
});
|
2016-09-11 08:05:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-09-13 02:10:04 +02:00
|
|
|
ircd::handle_ec(client &client,
|
2017-09-30 08:04:41 +02:00
|
|
|
const net::error_code &ec)
|
2016-09-11 08:05:38 +02:00
|
|
|
{
|
|
|
|
using namespace boost::system::errc;
|
|
|
|
using boost::asio::error::eof;
|
|
|
|
|
|
|
|
switch(ec.value())
|
|
|
|
{
|
2016-09-13 02:10:04 +02:00
|
|
|
case success: return handle_ec_success(client);
|
|
|
|
case eof: return handle_ec_eof(client);
|
2016-11-29 16:23:38 +01:00
|
|
|
case operation_canceled: return handle_ec_timeout(client);
|
2017-10-03 13:03:16 +02:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
log::debug("client(%p): %s", &client, ec.message());
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-13 02:10:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::handle_ec_success(client &client)
|
|
|
|
{
|
2016-09-13 00:10:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::handle_ec_eof(client &client)
|
2017-08-23 22:47:15 +02:00
|
|
|
try
|
2016-09-13 00:10:56 +02:00
|
|
|
{
|
2017-08-23 22:47:15 +02:00
|
|
|
log::debug("client[%s]: EOF",
|
2017-09-30 08:04:41 +02:00
|
|
|
string(remote(client)));
|
2017-08-23 22:47:15 +02:00
|
|
|
|
2017-08-23 23:13:57 +02:00
|
|
|
client.sock->disconnect(socket::FIN_RECV);
|
2016-09-13 00:10:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-23 22:47:15 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::warning("client(%p): EOF: %s",
|
|
|
|
&client,
|
|
|
|
e.what());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-13 00:10:56 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
bool
|
|
|
|
ircd::handle_ec_timeout(client &client)
|
2017-08-23 22:47:15 +02:00
|
|
|
try
|
2016-09-23 08:59:24 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
auto &sock(*client.sock);
|
2017-08-23 22:47:15 +02:00
|
|
|
log::debug("client[%s]: disconnecting after inactivity timeout",
|
2017-09-30 08:04:41 +02:00
|
|
|
string(remote(client)));
|
2017-08-23 22:47:15 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
sock.disconnect();
|
|
|
|
return false;
|
2016-09-11 08:05:38 +02:00
|
|
|
}
|
2017-08-23 22:47:15 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::warning("client(%p): timeout: %s",
|
|
|
|
&client,
|
|
|
|
e.what());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|