2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-13 03:57:58 +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-13 03:57:58 +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.
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2017-10-25 18:39:58 +02:00
|
|
|
#include <ircd/asio.h>
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
namespace ircd::server
|
|
|
|
{
|
2018-03-04 06:55:59 +01:00
|
|
|
// Internal state
|
|
|
|
ctx::dock dock; // internal semaphore
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-03-04 06:55:59 +01:00
|
|
|
// Internal util
|
2018-03-05 15:59:10 +01:00
|
|
|
template<class F> size_t accumulate_peers(F&&);
|
2018-01-16 03:04:23 +01:00
|
|
|
template<class F> size_t accumulate_links(F&&);
|
|
|
|
template<class F> size_t accumulate_tags(F&&);
|
2018-01-15 09:11:32 +01:00
|
|
|
|
2018-03-04 06:55:59 +01:00
|
|
|
// Internal control
|
2019-03-23 05:00:34 +01:00
|
|
|
static decltype(ircd::server::peers)::iterator
|
|
|
|
create(const net::hostport &, decltype(peers)::iterator &);
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::server::log)
|
|
|
|
ircd::server::log
|
|
|
|
{
|
|
|
|
"server", 'S'
|
|
|
|
};
|
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
ircd::conf::item<ircd::seconds>
|
|
|
|
close_all_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.server.close_all_timeout" },
|
|
|
|
{ "default", 2L },
|
|
|
|
};
|
|
|
|
|
2018-08-28 20:48:09 +02:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::server::init::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-09-18 01:47:36 +02:00
|
|
|
interrupt_all();
|
|
|
|
close_all();
|
|
|
|
wait_all();
|
2018-08-28 20:48:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "All server peers, connections, and requests are clear."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// server
|
|
|
|
//
|
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
void
|
|
|
|
ircd::server::wait_all()
|
|
|
|
{
|
2019-07-26 00:11:57 +02:00
|
|
|
static const auto finished
|
2018-03-10 16:50:19 +01:00
|
|
|
{
|
2019-07-26 00:11:57 +02:00
|
|
|
[] { return !peer_unfinished(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
while(!dock.wait_for(seconds(4), finished))
|
|
|
|
{
|
|
|
|
for(const auto &[name, peer] : peers)
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "Waiting for peer %s tags:%zu links:%zu err:%b op[r:%b f:%b] %s",
|
|
|
|
name,
|
|
|
|
peer->tag_count(),
|
|
|
|
peer->link_count(),
|
|
|
|
peer->err_has(),
|
|
|
|
peer->op_resolve,
|
|
|
|
peer->op_fini,
|
|
|
|
};
|
2018-03-10 16:50:19 +01:00
|
|
|
|
2018-08-28 20:48:09 +02:00
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "Waiting for %zu tags on %zu links on %zu of %zu peers to close...",
|
|
|
|
tag_count(),
|
|
|
|
link_count(),
|
|
|
|
peer_unfinished(),
|
|
|
|
peer_count()
|
|
|
|
};
|
2018-03-10 16:50:19 +01:00
|
|
|
}
|
2018-10-24 23:28:15 +02:00
|
|
|
|
|
|
|
peers.clear();
|
2018-03-10 16:50:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::close_all()
|
|
|
|
{
|
2018-08-28 20:48:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Closing all %zu peers",
|
|
|
|
peer_count()
|
|
|
|
};
|
2018-03-10 16:50:19 +01:00
|
|
|
|
|
|
|
net::close_opts opts;
|
|
|
|
opts.timeout = seconds(close_all_timeout);
|
|
|
|
for(auto &peer : peers)
|
|
|
|
peer.second->close(opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::interrupt_all()
|
|
|
|
{
|
2018-08-28 20:48:09 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Interrupting %zu tags on %zu links on %zu peers",
|
|
|
|
tag_count(),
|
|
|
|
link_count(),
|
|
|
|
peer_count()
|
|
|
|
};
|
2018-03-10 16:50:19 +01:00
|
|
|
|
|
|
|
for(auto &peer : peers)
|
2018-05-29 20:53:37 +02:00
|
|
|
peer.second->cancel();
|
2018-03-10 16:50:19 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer &
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::get(const net::hostport &hostport)
|
|
|
|
{
|
2019-04-22 21:13:34 +02:00
|
|
|
thread_local char canonbuf[512];
|
2018-05-20 03:09:55 +02:00
|
|
|
const auto canonized
|
|
|
|
{
|
|
|
|
net::canonize(canonbuf, hostport)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it(peers.lower_bound(canonized));
|
|
|
|
if(it == peers.end() || it->first != canonized)
|
2019-03-23 05:00:34 +01:00
|
|
|
it = create(hostport, it);
|
2018-01-13 03:57:58 +01:00
|
|
|
|
|
|
|
return *it->second;
|
|
|
|
}
|
|
|
|
|
2019-03-23 05:00:34 +01:00
|
|
|
decltype(ircd::server::peers)::iterator
|
|
|
|
ircd::server::create(const net::hostport &hostport,
|
|
|
|
decltype(peers)::iterator &it)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-05-19 04:42:41 +02:00
|
|
|
auto peer
|
|
|
|
{
|
2018-12-06 04:38:40 +01:00
|
|
|
std::make_unique<server::peer>(hostport)
|
2018-03-14 00:58:58 +01:00
|
|
|
};
|
|
|
|
|
2019-03-23 05:00:34 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "peer(%p) for %s created; adding...",
|
|
|
|
peer.get(),
|
|
|
|
peer->hostcanon,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(bool(peer));
|
|
|
|
assert(!empty(peer->hostcanon));
|
|
|
|
const string_view key{peer->hostcanon};
|
|
|
|
it = peers.emplace_hint(it, key, std::move(peer));
|
|
|
|
|
|
|
|
assert(it->second->hostcanon.data() == it->first.data());
|
|
|
|
it->second->resolve(it->second->open_opts.hostport);
|
|
|
|
return it;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer &
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::find(const net::hostport &hostport)
|
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return *peers.at(host(hostport));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::exists(const net::hostport &hostport)
|
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return peers.find(host(hostport)) != end(peers);
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-03-12 22:06:12 +01:00
|
|
|
bool
|
|
|
|
ircd::server::errclear(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
peers.find(host(hostport))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == end(peers))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto &peer(*it->second);
|
|
|
|
return peer.err_clear();
|
|
|
|
}
|
|
|
|
|
2018-03-03 07:37:52 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::server::errmsg(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
peers.find(host(hostport))
|
2018-03-03 07:37:52 +01:00
|
|
|
};
|
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
if(it == end(peers))
|
2018-03-03 07:37:52 +01:00
|
|
|
return {};
|
|
|
|
|
|
|
|
return it->second->err_msg();
|
|
|
|
}
|
|
|
|
|
2019-04-12 06:40:59 +02:00
|
|
|
bool
|
|
|
|
ircd::server::for_each(const request::each_closure &closure)
|
|
|
|
{
|
|
|
|
for(const auto &[name, peer] : peers)
|
|
|
|
if(!for_each(*peer, closure))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::for_each(const peer &peer,
|
|
|
|
const request::each_closure &closure)
|
|
|
|
{
|
|
|
|
for(const auto &link : peer.links)
|
|
|
|
if(!for_each(link, closure))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::for_each(const link &link,
|
|
|
|
const request::each_closure &closure)
|
|
|
|
{
|
|
|
|
for(const auto &tag : link.queue)
|
|
|
|
{
|
|
|
|
assert(link.peer);
|
|
|
|
if(!tag.request)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!closure(*link.peer, link, *tag.request))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::peer_unfinished()
|
|
|
|
{
|
|
|
|
return accumulate_peers([]
|
|
|
|
(const auto &peer)
|
|
|
|
{
|
|
|
|
return !peer.finished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer_count()
|
2018-01-16 05:04:45 +01:00
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return peers.size();
|
2018-01-16 05:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link_count()
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return accumulate_peers([]
|
|
|
|
(const auto &peer)
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return peer.link_count();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2018-01-16 05:04:45 +01:00
|
|
|
ircd::server::tag_count()
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return accumulate_peers([]
|
|
|
|
(const auto &peer)
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return peer.tag_count();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
size_t
|
|
|
|
ircd::server::accumulate_tags(F&& closure)
|
|
|
|
{
|
|
|
|
return accumulate_links([&closure]
|
|
|
|
(const auto &link)
|
|
|
|
{
|
|
|
|
return link.accumulate_tags(std::forward<F>(closure));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
size_t
|
|
|
|
ircd::server::accumulate_links(F&& closure)
|
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return accumulate_peers([&closure]
|
|
|
|
(const auto &peer)
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return peer.accumulate_links(std::forward<F>(closure));
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::accumulate_peers(F&& closure)
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
return std::accumulate(begin(peers), end(peers), size_t(0), [&closure]
|
2018-01-16 03:04:23 +01:00
|
|
|
(auto ret, const auto &pair)
|
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
const auto &peer{*pair.second};
|
|
|
|
return ret += closure(peer);
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
///
|
|
|
|
// request
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-17 10:31:34 +01:00
|
|
|
decltype(ircd::server::request::opts_default)
|
|
|
|
ircd::server::request::opts_default
|
|
|
|
{};
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
/// Canceling a request is tricky. This allows a robust way to let the user's
|
|
|
|
/// request go out of scope at virtually any time without disrupting the
|
|
|
|
/// pipeline and other requests.
|
|
|
|
bool
|
2019-04-11 04:25:27 +02:00
|
|
|
__attribute__((stack_protect))
|
2018-01-17 06:23:15 +01:00
|
|
|
ircd::server::cancel(request &request)
|
|
|
|
{
|
|
|
|
if(!request.tag)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(request.tag->canceled())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(request.tag->abandoned())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto &tag
|
|
|
|
{
|
|
|
|
*request.tag
|
|
|
|
};
|
|
|
|
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-07-26 08:05:15 +02:00
|
|
|
request::log, "%s cancel commit:%d w:%zu hr:%zu cr:%zu",
|
2019-04-12 23:26:10 +02:00
|
|
|
loghead(request),
|
2018-09-19 00:10:10 +02:00
|
|
|
tag.committed(),
|
|
|
|
tag.state.written,
|
|
|
|
tag.state.head_read,
|
|
|
|
tag.state.content_read
|
|
|
|
};
|
2018-01-17 06:23:15 +01:00
|
|
|
|
2019-03-16 23:01:46 +01:00
|
|
|
tag.set_exception<canceled>("Request canceled");
|
2018-04-07 13:02:55 +02:00
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
// We got off easy... The link's write loop won't start an abandoned
|
|
|
|
// request. All that has to be done is indicate a full cancellation
|
|
|
|
// immediately and the user will know nothing was revealed to the remote.
|
|
|
|
if(!tag.committed())
|
2019-04-15 20:43:53 +02:00
|
|
|
{
|
|
|
|
disassociate(request, tag);
|
2018-01-17 06:23:15 +01:00
|
|
|
return true;
|
2019-04-15 20:43:53 +02:00
|
|
|
}
|
2018-01-17 06:23:15 +01:00
|
|
|
|
|
|
|
// Now things aren't so easy. More complicated logic happens inside...
|
|
|
|
cancel(request, tag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
2019-04-11 04:25:27 +02:00
|
|
|
__attribute__((stack_protect))
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::submit(const hostport &hostport,
|
|
|
|
request &request)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2019-01-18 17:55:06 +01:00
|
|
|
if(unlikely(ircd::run::level != ircd::run::level::RUN))
|
2018-03-04 06:55:59 +01:00
|
|
|
throw unavailable
|
|
|
|
{
|
|
|
|
"Unable to fulfill requests at this time."
|
|
|
|
};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(request.tag == nullptr);
|
2018-03-05 15:59:10 +01:00
|
|
|
auto &peer(server::get(hostport));
|
|
|
|
peer.submit(request);
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2019-04-12 07:19:36 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::server::loghead(const link &link,
|
|
|
|
const request &request)
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
thread_local char buf[512];
|
2019-04-12 07:19:36 +02:00
|
|
|
return loghead(buf, link, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::server::loghead(const mutable_buffer &buf,
|
|
|
|
const link &link,
|
|
|
|
const request &request)
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
buf, "%s %s",
|
|
|
|
loghead(link),
|
2019-04-12 07:19:36 +02:00
|
|
|
loghead(request)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::server::loghead(const request &request)
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
thread_local char buf[512];
|
2019-04-12 07:19:36 +02:00
|
|
|
return loghead(buf, request);
|
|
|
|
}
|
|
|
|
|
2019-03-24 21:49:15 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::server::loghead(const mutable_buffer &buf,
|
|
|
|
const request &request)
|
|
|
|
try
|
|
|
|
{
|
2019-03-26 02:43:00 +01:00
|
|
|
if(empty(request.out.head))
|
|
|
|
return "<no head>";
|
2019-03-24 21:49:15 +01:00
|
|
|
|
2019-04-12 05:49:29 +02:00
|
|
|
if(request.tag && request.tag->cancellation)
|
|
|
|
return "<canceled; out data is gone>";
|
|
|
|
|
2019-04-12 06:13:42 +02:00
|
|
|
const http::request::head head
|
2019-03-26 02:43:00 +01:00
|
|
|
{
|
2019-04-12 06:13:42 +02:00
|
|
|
request.out.gethead(request)
|
|
|
|
};
|
2019-03-26 02:43:00 +01:00
|
|
|
|
|
|
|
if(!head.method || !head.path)
|
|
|
|
return "<no head data>";
|
|
|
|
|
2019-03-24 21:49:15 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2019-04-25 00:25:23 +02:00
|
|
|
buf, "tag:%lu %s %s",
|
2019-04-12 07:19:36 +02:00
|
|
|
id(request),
|
|
|
|
head.method,
|
|
|
|
head.path
|
2019-03-24 21:49:15 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "server::loghead(): %s", e.what()
|
|
|
|
};
|
|
|
|
|
2019-04-12 06:13:42 +02:00
|
|
|
return "<critical error>";
|
2019-03-24 21:49:15 +01:00
|
|
|
}
|
|
|
|
|
2019-04-12 07:18:47 +02:00
|
|
|
uint64_t
|
|
|
|
ircd::server::id(const request &request)
|
2019-03-24 21:49:15 +01:00
|
|
|
{
|
2019-04-12 07:18:47 +02:00
|
|
|
return request.tag?
|
|
|
|
request.tag->state.id:
|
|
|
|
0UL;
|
2019-03-24 21:49:15 +01:00
|
|
|
}
|
|
|
|
|
2019-04-12 06:13:42 +02:00
|
|
|
//
|
|
|
|
// server::in
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::http::response::head
|
|
|
|
ircd::server::in::gethead(const request &request)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(empty(request.in.head))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if(request.tag && request.tag->cancellation)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
parse::buffer pb{request.in.head};
|
|
|
|
parse::capstan pc{pb, [](char *&read, char *stop)
|
|
|
|
{
|
|
|
|
read = stop;
|
|
|
|
}};
|
|
|
|
|
|
|
|
pc.read += size(request.in.head);
|
|
|
|
return http::response::head{pc};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "server::in::gethead(): %s", e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// server::out
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::http::request::head
|
|
|
|
ircd::server::out::gethead(const request &request)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(empty(request.out.head))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if(request.tag && request.tag->cancellation)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
parse::buffer pb{request.out.head};
|
|
|
|
parse::capstan pc{pb, [](char *&read, char *stop)
|
|
|
|
{
|
|
|
|
read = stop;
|
|
|
|
}};
|
|
|
|
|
|
|
|
pc.read += size(request.out.head);
|
|
|
|
return http::request::head{pc};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "server::out::gethead(): %s", e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-05-19 04:42:41 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-16 03:04:23 +01:00
|
|
|
//
|
2018-05-19 04:42:41 +02:00
|
|
|
// server/peer.h
|
2018-01-16 03:04:23 +01:00
|
|
|
//
|
|
|
|
|
2018-03-14 00:58:58 +01:00
|
|
|
decltype(ircd::server::peers)
|
|
|
|
ircd::server::peers
|
|
|
|
{};
|
|
|
|
|
2019-03-25 22:55:05 +01:00
|
|
|
decltype(ircd::server::peer::enable_ipv6)
|
|
|
|
ircd::server::peer::enable_ipv6
|
|
|
|
{
|
|
|
|
{ "name", "ircd.server.peer.enable_ipv6" },
|
|
|
|
{ "default", true }
|
|
|
|
};
|
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
decltype(ircd::server::peer::link_min_default)
|
|
|
|
ircd::server::peer::link_min_default
|
2018-03-04 06:54:42 +01:00
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
{ "name", "ircd.server.peer.link_min" },
|
2018-03-04 06:54:42 +01:00
|
|
|
{ "default", 1L }
|
|
|
|
};
|
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
decltype(ircd::server::peer::link_max_default)
|
|
|
|
ircd::server::peer::link_max_default
|
2018-03-04 06:54:42 +01:00
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
{ "name", "ircd.server.peer.link_max" },
|
2018-03-04 06:54:42 +01:00
|
|
|
{ "default", 2L }
|
|
|
|
};
|
|
|
|
|
2019-04-12 07:18:47 +02:00
|
|
|
decltype(ircd::server::peer::ids)
|
|
|
|
ircd::server::peer::ids;
|
|
|
|
|
2018-03-04 06:54:42 +01:00
|
|
|
//
|
2018-03-05 15:59:10 +01:00
|
|
|
// peer::peer
|
2018-03-04 06:54:42 +01:00
|
|
|
//
|
|
|
|
|
2018-12-06 04:38:40 +01:00
|
|
|
ircd::server::peer::peer(const net::hostport &hostport,
|
|
|
|
const net::open_opts &open_opts)
|
|
|
|
:hostcanon
|
|
|
|
{
|
|
|
|
net::canonize(hostport)
|
|
|
|
}
|
|
|
|
,service
|
|
|
|
{
|
|
|
|
net::service(hostport)
|
|
|
|
}
|
|
|
|
,open_opts
|
|
|
|
{
|
|
|
|
std::move(open_opts)
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2019-04-15 22:16:00 +02:00
|
|
|
const net::hostport &canon{this->hostcanon};
|
|
|
|
|
|
|
|
this->open_opts.hostport.host = this->hostcanon; // Resolve SRV on this name.
|
|
|
|
this->open_opts.hostport.port = net::port(hostport);
|
|
|
|
this->open_opts.hostport.service = this->service; // Resolve SRV on this protocol.
|
|
|
|
|
|
|
|
this->open_opts.server_name = this->hostcanon; // Send SNI for this name.
|
|
|
|
this->open_opts.common_name = this->hostcanon; // Cert verify this name.
|
|
|
|
|
|
|
|
if(rfc3986::valid(std::nothrow, rfc3986::parser::ip_address, host(this->open_opts.hostport)))
|
|
|
|
this->remote =
|
|
|
|
{
|
|
|
|
host(this->open_opts.hostport), port(this->open_opts.hostport)
|
|
|
|
};
|
|
|
|
|
2018-12-06 04:38:40 +01:00
|
|
|
this->open_opts.ipport = this->remote;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::~peer()
|
2018-01-13 03:57:58 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(links.empty());
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::close(const net::close_opts &opts)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-03-10 16:50:19 +01:00
|
|
|
op_fini = true;
|
2018-05-09 00:50:48 +02:00
|
|
|
link *links[LINK_MAX];
|
|
|
|
const auto end(pointers(this->links, links));
|
|
|
|
for(link **link(links); link != end; ++link)
|
|
|
|
(*link)->close(opts);
|
2018-03-10 16:50:19 +01:00
|
|
|
|
|
|
|
if(finished())
|
|
|
|
return handle_finished();
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
2018-05-29 20:53:37 +02:00
|
|
|
ircd::server::peer::cancel()
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-04 06:55:59 +01:00
|
|
|
for(auto &link : this->links)
|
2019-03-16 23:01:46 +01:00
|
|
|
link.cancel_all(make_exception_ptr<canceled>
|
|
|
|
(
|
2018-03-04 06:55:59 +01:00
|
|
|
"Request was aborted due to interruption."
|
2019-03-16 23:01:46 +01:00
|
|
|
));
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-03-12 22:06:12 +01:00
|
|
|
bool
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::err_clear()
|
2018-03-03 07:37:52 +01:00
|
|
|
{
|
2018-03-12 22:06:12 +01:00
|
|
|
const auto ret{bool(e)};
|
2018-03-08 17:24:19 +01:00
|
|
|
e.reset(nullptr);
|
2018-04-11 06:02:34 +02:00
|
|
|
op_fini = false;
|
2018-03-12 22:06:12 +01:00
|
|
|
return ret;
|
2018-03-08 17:24:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class... A>
|
|
|
|
void
|
|
|
|
ircd::server::peer::err_set(A&&... args)
|
|
|
|
{
|
|
|
|
this->e = std::make_unique<err>(std::forward<A>(args)...);
|
2018-03-03 07:37:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::err_msg()
|
2018-03-03 07:37:52 +01:00
|
|
|
const
|
|
|
|
{
|
2018-03-08 17:24:19 +01:00
|
|
|
return bool(e)? what(e->eptr) : string_view{};
|
2018-03-03 07:37:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::err_has()
|
2018-03-03 07:37:52 +01:00
|
|
|
const
|
|
|
|
{
|
2018-03-08 17:24:19 +01:00
|
|
|
return bool(e);
|
2018-03-03 07:37:52 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 16:42:28 +01:00
|
|
|
decltype(ircd::server::peer::error_clear_default)
|
|
|
|
ircd::server::peer::error_clear_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.server.peer.error.clear_default" },
|
|
|
|
{ "default", 305L }
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::peer::err_check()
|
|
|
|
{
|
2018-03-10 16:50:19 +01:00
|
|
|
if(op_fini)
|
2018-03-09 16:42:28 +01:00
|
|
|
return false;
|
|
|
|
|
2018-04-11 06:02:34 +02:00
|
|
|
if(!err_has())
|
2018-03-09 16:42:28 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
//TODO: The specific error type should be switched and finer
|
|
|
|
//TODO: timeouts should be used depending on the error: i.e
|
|
|
|
//TODO: NXDOMAIN vs. temporary conn timeout, etc.
|
2018-04-14 05:01:46 +02:00
|
|
|
if(e->etime + seconds(error_clear_default) > now<system_point>())
|
2018-03-09 16:42:28 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
err_clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::submit(request &request)
|
2018-01-17 14:58:44 +01:00
|
|
|
try
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2019-01-18 17:55:06 +01:00
|
|
|
if(!err_check() || unlikely(ircd::run::level != ircd::run::level::RUN))
|
2018-03-06 13:51:20 +01:00
|
|
|
throw unavailable
|
|
|
|
{
|
2019-04-24 02:44:18 +02:00
|
|
|
"Peer is unable to take any requests :%s", err_msg()
|
2018-03-06 13:51:20 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 13:01:26 +01:00
|
|
|
link *const ret
|
|
|
|
{
|
|
|
|
link_get(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(likely(ret))
|
|
|
|
{
|
|
|
|
ret->submit(request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!request.tag)
|
|
|
|
throw unavailable
|
|
|
|
{
|
2018-12-06 04:38:40 +01:00
|
|
|
"No link to peer %s available", hostcanon
|
2018-01-16 13:01:26 +01:00
|
|
|
};
|
|
|
|
else
|
2019-03-16 23:01:46 +01:00
|
|
|
request.tag->set_exception<unavailable>
|
|
|
|
(
|
2018-12-06 04:38:40 +01:00
|
|
|
"No link to peer %s available", hostcanon
|
2019-03-16 23:01:46 +01:00
|
|
|
);
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
2018-01-17 14:58:44 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-04-07 19:30:02 +02:00
|
|
|
if(!request.tag)
|
2018-01-17 14:58:44 +01:00
|
|
|
throw;
|
2018-04-07 19:30:02 +02:00
|
|
|
|
|
|
|
const auto eptr(std::current_exception());
|
|
|
|
const ctx::exception_handler eh;
|
|
|
|
request.tag->set_exception(eptr);
|
2018-01-17 14:58:44 +01:00
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
/// Dispatch algorithm here; finds the best link to place this request on,
|
|
|
|
/// or creates a new link entirely. There are a number of factors: foremost
|
|
|
|
/// if any special needs are indicated,
|
|
|
|
//
|
2018-01-16 13:01:26 +01:00
|
|
|
ircd::server::link *
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::link_get(const request &request)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-05-10 02:07:37 +02:00
|
|
|
assert(request.opt);
|
|
|
|
const auto &prio(request.opt->priority);
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
if(links.empty())
|
2018-01-16 13:01:26 +01:00
|
|
|
return &link_add(1);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
// Indicates that we can't add anymore links for this peer and the rest
|
2018-01-16 04:02:37 +01:00
|
|
|
// of the algorithm should consider this.
|
|
|
|
const bool links_maxed
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 04:02:37 +01:00
|
|
|
links.size() >= link_max()
|
|
|
|
};
|
|
|
|
|
|
|
|
link *best{nullptr};
|
2018-01-16 13:01:26 +01:00
|
|
|
for(auto &cand : links)
|
2018-01-16 04:02:37 +01:00
|
|
|
{
|
2018-01-16 09:45:51 +01:00
|
|
|
// Don't want a link that's shutting down or marked for exclusion
|
2018-03-09 16:29:37 +01:00
|
|
|
if(cand.op_fini || cand.exclude)
|
2018-01-16 09:45:51 +01:00
|
|
|
continue;
|
|
|
|
|
2018-01-16 13:01:26 +01:00
|
|
|
if(!best)
|
2018-01-16 09:45:51 +01:00
|
|
|
{
|
|
|
|
best = &cand;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-16 04:02:37 +01:00
|
|
|
// Indicates that the best candidate has its pipe saturated which can
|
|
|
|
// be factored into the comparisons here.
|
|
|
|
const bool best_maxed
|
|
|
|
{
|
|
|
|
best->tag_committed() >= best->tag_commit_max()
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool cand_maxed
|
|
|
|
{
|
|
|
|
cand.tag_committed() >= cand.tag_commit_max()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(best_maxed && !cand_maxed)
|
|
|
|
{
|
|
|
|
best = &cand;
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
2018-01-16 04:02:37 +01:00
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 04:02:37 +01:00
|
|
|
if(!best_maxed && cand_maxed)
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Candidates's queue has less or same backlog of unsent requests, but
|
|
|
|
// now measure if candidate will take longer to process at least the
|
|
|
|
// write-side of those requests.
|
2018-01-16 04:02:37 +01:00
|
|
|
if(cand.write_remaining() > best->write_remaining())
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Candidate might be working through really large content; though
|
|
|
|
// this is a very sketchy measurement right now since we only *might*
|
|
|
|
// know about content-length for the *one* active tag occupying the
|
|
|
|
// socket.
|
2018-01-16 04:02:37 +01:00
|
|
|
if(cand.read_remaining() > best->read_remaining())
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Coarse distribution based on who has more work; this is weak, should
|
|
|
|
// be replaced.
|
2018-01-16 05:04:45 +01:00
|
|
|
if(cand.tag_count() > best->tag_count())
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
|
|
|
|
2018-01-16 04:02:37 +01:00
|
|
|
best = &cand;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 02:07:37 +02:00
|
|
|
// Even though the prio is set to the super special value we allow the
|
|
|
|
// normal loop to first come up with a best link which already is open
|
|
|
|
// rather than unconditionally opening a new connection.
|
|
|
|
if(prio == std::numeric_limits<std::remove_reference<decltype(prio)>::type>::min())
|
|
|
|
{
|
|
|
|
if(!best)
|
|
|
|
return &link_add(1);
|
|
|
|
|
|
|
|
if(best->tag_committed())
|
|
|
|
return &link_add(1);
|
|
|
|
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2018-01-16 04:02:37 +01:00
|
|
|
if(links_maxed)
|
2018-01-16 13:01:26 +01:00
|
|
|
return best;
|
2018-01-16 04:02:37 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
// best might not be good enough, we could try another connection. If best
|
|
|
|
// has a backlog or is working on a large download or slow request.
|
2018-01-17 12:14:14 +01:00
|
|
|
if(!best)
|
|
|
|
{
|
|
|
|
best = &link_add();
|
|
|
|
return best;
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
if(best->tag_uncommitted() < best->tag_commit_max())
|
2018-01-16 13:01:26 +01:00
|
|
|
return best;
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
best = &link_add();
|
2018-01-16 13:01:26 +01:00
|
|
|
return best;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::link &
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::link_add(const size_t &num)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-03-11 19:45:24 +01:00
|
|
|
assert(!finished());
|
|
|
|
|
2018-03-08 17:24:19 +01:00
|
|
|
if(e)
|
2018-04-07 15:01:38 +02:00
|
|
|
{
|
2018-03-08 17:24:19 +01:00
|
|
|
std::rethrow_exception(e->eptr);
|
2018-04-07 15:01:38 +02:00
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
2018-01-17 14:58:44 +01:00
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
assert(!op_fini);
|
2018-01-16 03:04:23 +01:00
|
|
|
links.emplace_back(*this);
|
|
|
|
auto &link{links.back()};
|
|
|
|
|
2018-03-14 00:58:58 +01:00
|
|
|
if(remote)
|
|
|
|
link.open(open_opts);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
return link;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::handle_open(link &link,
|
2018-01-16 03:04:23 +01:00
|
|
|
std::exception_ptr eptr)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
if(eptr)
|
2018-03-10 16:50:19 +01:00
|
|
|
{
|
|
|
|
if(links.size() == 1)
|
|
|
|
err_set(eptr);
|
2018-01-17 11:34:22 +01:00
|
|
|
|
2019-04-17 05:57:06 +02:00
|
|
|
thread_local char rembuf[64];
|
2018-09-19 00:10:10 +02:00
|
|
|
log::derror
|
|
|
|
{
|
2019-04-24 05:06:34 +02:00
|
|
|
log, "%s [%s]: open :%s",
|
2019-04-22 20:47:29 +02:00
|
|
|
loghead(link),
|
2019-04-17 05:57:06 +02:00
|
|
|
string(rembuf, remote),
|
2018-09-19 00:10:10 +02:00
|
|
|
what(eptr)
|
|
|
|
};
|
2018-03-10 16:50:19 +01:00
|
|
|
|
2018-03-13 02:59:38 +01:00
|
|
|
if(op_fini)
|
|
|
|
{
|
2018-03-14 01:45:02 +01:00
|
|
|
if(link.finished())
|
|
|
|
handle_finished(link);
|
|
|
|
|
2018-03-13 02:59:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
link.close(net::dc::RST);
|
|
|
|
return;
|
|
|
|
}
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::handle_close(link &link,
|
2018-01-16 03:04:23 +01:00
|
|
|
std::exception_ptr eptr)
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
2019-04-17 05:57:06 +02:00
|
|
|
thread_local char rembuf[64];
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
if(eptr)
|
2018-09-19 00:10:10 +02:00
|
|
|
log::derror
|
|
|
|
{
|
2019-04-24 05:06:34 +02:00
|
|
|
log, "%s [%s]: close :%s",
|
2019-04-22 20:47:29 +02:00
|
|
|
loghead(link),
|
2019-04-17 05:57:06 +02:00
|
|
|
string(rembuf, remote),
|
2018-09-19 00:10:10 +02:00
|
|
|
what(eptr)
|
|
|
|
};
|
2018-03-09 16:29:37 +01:00
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
if(link.finished())
|
|
|
|
handle_finished(link);
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 09:45:51 +01:00
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::handle_error(link &link,
|
2018-01-17 10:05:31 +01:00
|
|
|
std::exception_ptr eptr)
|
|
|
|
{
|
2018-03-09 16:29:37 +01:00
|
|
|
assert(bool(eptr));
|
2018-03-04 06:55:59 +01:00
|
|
|
link.cancel_committed(eptr);
|
2018-09-19 00:10:10 +02:00
|
|
|
log::derror
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s :%s",
|
|
|
|
loghead(link),
|
2018-09-19 00:10:10 +02:00
|
|
|
what(eptr)
|
|
|
|
};
|
2018-03-09 16:29:37 +01:00
|
|
|
|
|
|
|
link.close(net::dc::RST);
|
2018-01-16 09:45:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::handle_error(link &link,
|
2018-11-09 06:18:39 +01:00
|
|
|
const std::system_error &e)
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2018-01-16 05:04:45 +01:00
|
|
|
using boost::asio::error::get_misc_category;
|
2019-04-17 05:57:06 +02:00
|
|
|
thread_local char rembuf[64];
|
2018-01-15 09:11:32 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
const auto &ec{e.code()};
|
2018-11-09 06:18:39 +01:00
|
|
|
if(system_category(ec)) switch(ec.value())
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
case 0:
|
2018-01-16 05:04:45 +01:00
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(ec.category() == get_misc_category()) switch(ec.value())
|
|
|
|
{
|
|
|
|
case asio::error::eof:
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s [%s]: %s",
|
|
|
|
loghead(link),
|
2019-04-17 05:57:06 +02:00
|
|
|
string(rembuf, remote),
|
2018-09-19 00:10:10 +02:00
|
|
|
e.what()
|
|
|
|
};
|
2018-01-16 05:04:45 +01:00
|
|
|
|
|
|
|
link.close(net::close_opts_default);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
2018-01-16 05:04:45 +01:00
|
|
|
|
2018-09-19 00:10:10 +02:00
|
|
|
log::derror
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s [%s]: %s",
|
|
|
|
loghead(link),
|
2019-04-17 05:57:06 +02:00
|
|
|
string(rembuf, remote),
|
2018-09-19 00:10:10 +02:00
|
|
|
e.what()
|
|
|
|
};
|
2018-01-16 05:04:45 +01:00
|
|
|
|
2018-03-04 06:55:59 +01:00
|
|
|
link.cancel_committed(std::make_exception_ptr(e));
|
2018-01-16 05:04:45 +01:00
|
|
|
link.close(net::dc::RST);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:38:38 +01:00
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::handle_finished(link &link)
|
2018-01-23 20:38:38 +01:00
|
|
|
{
|
2018-03-10 16:50:19 +01:00
|
|
|
assert(link.finished());
|
|
|
|
del(link);
|
|
|
|
|
|
|
|
if(finished())
|
|
|
|
handle_finished();
|
2018-01-23 20:38:38 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
/// This is where we're notified a tag has been completed either to start the
|
|
|
|
/// next request when the link has too many requests in flight or perhaps to
|
|
|
|
/// reschedule the queues in various links to diffuse the pending requests.
|
|
|
|
/// This can't throw because the link still has to remove this tag from its
|
|
|
|
/// queue.
|
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::handle_tag_done(link &link,
|
2018-01-16 05:04:45 +01:00
|
|
|
tag &tag)
|
|
|
|
noexcept try
|
|
|
|
{
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-07-26 08:05:15 +02:00
|
|
|
log, "%s [%s] => [%u] done wt:%zu rt:%zu %zu more in queue",
|
2019-04-22 20:47:29 +02:00
|
|
|
loghead(link),
|
2019-03-24 21:49:15 +01:00
|
|
|
tag.request?
|
|
|
|
loghead(*tag.request):
|
|
|
|
"<no request>"_sv,
|
2019-03-25 17:40:18 +01:00
|
|
|
uint(tag.state.status),
|
2018-09-19 00:10:10 +02:00
|
|
|
tag.write_size(),
|
|
|
|
tag.read_size(),
|
|
|
|
link.tag_count() - 1
|
|
|
|
};
|
2018-04-06 10:52:22 +02:00
|
|
|
|
2019-07-26 08:05:15 +02:00
|
|
|
if(tag.request)
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
request::log, "%s [%u] wt:%zu rt:%zu hr:%zu cr:%zu cl:%zu chunks:%zu",
|
|
|
|
loghead(*tag.request),
|
|
|
|
uint(tag.state.status),
|
|
|
|
tag.write_size(),
|
|
|
|
tag.read_size(),
|
|
|
|
tag.state.head_read,
|
|
|
|
tag.state.content_read,
|
|
|
|
tag.state.content_length,
|
|
|
|
tag.request->in.chunks.size(),
|
|
|
|
};
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
if(link.tag_committed() >= link.tag_commit_max())
|
|
|
|
link.wait_writable();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-19 00:10:10 +02:00
|
|
|
log::critical
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s tag:%lu done; %s",
|
|
|
|
loghead(link),
|
|
|
|
tag.state.id,
|
2018-09-19 00:10:10 +02:00
|
|
|
e.what()
|
|
|
|
};
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 06:13:48 +01:00
|
|
|
/// This is where we're notified a link has processed its queue and has no
|
|
|
|
/// more work. We can choose whether to close the link or keep it open and
|
|
|
|
/// reinstate the read poll; reschedule other work to this link, etc.
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::handle_link_done(link &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 06:13:48 +01:00
|
|
|
assert(link.tag_count() == 0);
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 06:13:48 +01:00
|
|
|
if(link_ready() > link_min())
|
|
|
|
{
|
|
|
|
link.close();
|
|
|
|
return;
|
|
|
|
}
|
2018-02-27 08:30:08 +01:00
|
|
|
|
|
|
|
link.wait_readable();
|
2018-01-16 06:13:48 +01:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:15:57 +01:00
|
|
|
/// This is called when a tag on a link receives an HTTP response head.
|
|
|
|
/// We can use this to learn information from the tag's request and the
|
|
|
|
/// response head etc.
|
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::handle_head_recv(const link &link,
|
2018-01-24 18:15:57 +01:00
|
|
|
const tag &tag,
|
|
|
|
const http::response::head &head)
|
|
|
|
{
|
2018-03-05 15:59:10 +01:00
|
|
|
// Learn the software version of the remote peer so we can shape
|
2018-01-24 18:15:57 +01:00
|
|
|
// requests more effectively.
|
2019-04-15 22:10:41 +02:00
|
|
|
if(!server_version && head.server)
|
2018-01-24 18:15:57 +01:00
|
|
|
{
|
2019-04-17 05:57:06 +02:00
|
|
|
thread_local char rembuf[64];
|
2019-04-15 22:10:41 +02:00
|
|
|
server_version = std::string{head.server};
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s learned %s is '%s'",
|
|
|
|
loghead(link),
|
2019-04-17 05:57:06 +02:00
|
|
|
string(rembuf, remote),
|
2019-04-15 22:10:41 +02:00
|
|
|
server_version,
|
2018-09-19 00:10:10 +02:00
|
|
|
};
|
2018-01-24 18:15:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 09:45:51 +01:00
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::disperse(link &link)
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
|
|
|
disperse_uncommitted(link);
|
2019-03-16 23:01:46 +01:00
|
|
|
link.cancel_committed(make_exception_ptr<canceled>
|
|
|
|
(
|
2018-01-20 16:03:09 +01:00
|
|
|
"Request was aborted; though it was partially completed"
|
2019-03-16 23:01:46 +01:00
|
|
|
));
|
2018-01-16 13:01:26 +01:00
|
|
|
|
|
|
|
assert(link.queue.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::disperse_uncommitted(link &link)
|
2018-01-16 09:45:51 +01:00
|
|
|
{
|
|
|
|
auto &queue(link.queue);
|
2018-01-17 14:58:44 +01:00
|
|
|
auto it(begin(queue));
|
2018-03-04 06:55:59 +01:00
|
|
|
while(it != end(queue)) try
|
2018-01-16 09:45:51 +01:00
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
auto &tag{*it};
|
|
|
|
if(!tag.request || tag.committed())
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
++it;
|
|
|
|
continue;
|
2018-01-16 13:01:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:58:44 +01:00
|
|
|
submit(*tag.request);
|
|
|
|
it = queue.erase(it);
|
|
|
|
}
|
2018-03-04 06:55:59 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
const auto &tag{*it};
|
2018-09-19 00:10:10 +02:00
|
|
|
log::warning
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s failed to resubmit tag:%lu :%s",
|
|
|
|
loghead(link),
|
|
|
|
tag.state.id,
|
2018-09-19 00:10:10 +02:00
|
|
|
e.what()
|
|
|
|
};
|
2018-03-04 06:55:59 +01:00
|
|
|
|
|
|
|
it = queue.erase(it);
|
|
|
|
}
|
2018-01-16 09:45:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 06:13:48 +01:00
|
|
|
/// This *cannot* be called unless a link's socket is closed and its queue
|
|
|
|
/// is empty. It is usually only called by a disconnect handler because
|
|
|
|
/// the proper way to remove a link is asynchronously through link.close();
|
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::del(link &link)
|
2018-01-16 06:13:48 +01:00
|
|
|
{
|
|
|
|
assert(!link.tag_count());
|
2018-02-27 07:49:44 +01:00
|
|
|
assert(!link.opened());
|
2018-01-16 03:04:23 +01:00
|
|
|
const auto it(std::find_if(begin(links), end(links), [&link]
|
|
|
|
(const auto &link_)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return &link_ == &link;
|
|
|
|
}));
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(it != end(links));
|
2019-04-17 05:57:06 +02:00
|
|
|
thread_local char rembuf[64];
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s removing from peer(%p) %zu of %zu to %s",
|
|
|
|
loghead(link),
|
2018-09-19 00:10:10 +02:00
|
|
|
this,
|
|
|
|
std::distance(begin(links), it),
|
|
|
|
links.size(),
|
2019-04-17 05:57:06 +02:00
|
|
|
string(rembuf, remote)
|
2018-09-19 00:10:10 +02:00
|
|
|
};
|
2018-01-16 06:13:48 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
links.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::resolve(const hostport &hostport)
|
2019-03-24 23:28:07 +01:00
|
|
|
{
|
|
|
|
net::dns::opts opts;
|
|
|
|
opts.qtype = 33; // start with SRV.
|
|
|
|
opts.nxdomain_exceptions = false;
|
|
|
|
resolve(hostport, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::peer::resolve(const hostport &hostport,
|
|
|
|
const net::dns::opts &opts)
|
2019-06-27 09:36:59 +02:00
|
|
|
try
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-10 16:50:19 +01:00
|
|
|
if(op_resolve || op_fini)
|
|
|
|
return;
|
|
|
|
|
2019-03-25 04:47:15 +01:00
|
|
|
// Skip DNS resolution for IP literals
|
|
|
|
if(rfc3986::valid(std::nothrow, rfc3986::parser::ip_address, host(hostport)))
|
|
|
|
{
|
|
|
|
this->remote = {host(hostport), port(hostport)};
|
|
|
|
open_opts.ipport = this->remote;
|
|
|
|
open_links();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:37:01 +01:00
|
|
|
if(unlikely(opts.qtype != 33 && opts.qtype != 28 && opts.qtype != 1))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Unsupported DNS question type '%u' for resolve", opts.qtype
|
|
|
|
};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
auto handler
|
2017-11-30 20:05:04 +01:00
|
|
|
{
|
2019-03-24 23:28:07 +01:00
|
|
|
opts.qtype == 33?
|
|
|
|
net::dns::callback(std::bind(&peer::handle_resolve_SRV, this, ph::_1, ph::_2)):
|
2019-03-25 22:37:01 +01:00
|
|
|
opts.qtype == 28?
|
|
|
|
net::dns::callback(std::bind(&peer::handle_resolve_AAAA, this, ph::_1, ph::_2)):
|
2019-03-24 23:28:07 +01:00
|
|
|
net::dns::callback(std::bind(&peer::handle_resolve_A, this, ph::_1, ph::_2))
|
2017-11-30 20:05:04 +01:00
|
|
|
};
|
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
op_resolve = true;
|
2019-03-24 23:28:07 +01:00
|
|
|
assert(ctx::current); // sorry, ircd::ctx required for now.
|
2019-03-22 02:24:36 +01:00
|
|
|
net::dns::resolve(hostport, opts, std::move(handler));
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2019-06-27 09:36:59 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
thread_local char buf[256];
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "peer(%p) DNS resolution for '%s' :%s",
|
|
|
|
this,
|
|
|
|
string(buf, hostport),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
op_resolve = false;
|
|
|
|
err_set(std::current_exception());
|
|
|
|
if(unlikely(ircd::run::level != ircd::run::level::RUN))
|
|
|
|
op_fini = true;
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
2019-03-24 23:28:07 +01:00
|
|
|
ircd::server::peer::handle_resolve_SRV(const hostport &hp,
|
|
|
|
const json::array &rrs)
|
2018-01-16 03:04:23 +01:00
|
|
|
try
|
|
|
|
{
|
2018-04-07 15:01:38 +02:00
|
|
|
assert(op_resolve);
|
2018-03-10 16:50:19 +01:00
|
|
|
op_resolve = false;
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2019-03-24 23:28:07 +01:00
|
|
|
if(unlikely(ircd::run::level != ircd::run::level::RUN))
|
|
|
|
op_fini = true;
|
|
|
|
|
|
|
|
if(unlikely(finished()))
|
|
|
|
return handle_finished();
|
|
|
|
|
|
|
|
if(op_fini)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const json::object &rr
|
2018-03-03 07:37:52 +01:00
|
|
|
{
|
2019-03-24 23:28:07 +01:00
|
|
|
net::dns::random_choice(rrs)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(net::dns::is_error(rr))
|
|
|
|
{
|
|
|
|
const json::string &error(rr.get("error"));
|
|
|
|
err_set(make_exception_ptr<rfc1035::error>("%s", error));
|
|
|
|
assert(this->e && this->e->eptr);
|
|
|
|
std::rethrow_exception(this->e->eptr);
|
2018-04-07 15:01:38 +02:00
|
|
|
__builtin_unreachable();
|
2018-03-03 07:37:52 +01:00
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2019-03-24 23:28:07 +01:00
|
|
|
// Target for the next address record query.
|
|
|
|
const hostport &target
|
|
|
|
{
|
|
|
|
rr.has("tgt")?
|
|
|
|
rstrip(unquote(rr.at("tgt")), '.'):
|
|
|
|
host(hp),
|
2018-05-02 20:42:46 +02:00
|
|
|
|
2019-03-24 23:28:07 +01:00
|
|
|
rr.has("port")?
|
|
|
|
rr.get<uint16_t>("port"):
|
|
|
|
port(hp)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Save the port from the SRV record to a class member because it won't
|
|
|
|
// get carried through the next A/AAAA query.
|
|
|
|
port(remote) = port(target);
|
2019-03-25 22:37:01 +01:00
|
|
|
port(open_opts.hostport) = port(target);
|
2019-03-24 23:28:07 +01:00
|
|
|
|
|
|
|
// Setup the address record query off this SRV response.
|
|
|
|
net::dns::opts opts;
|
2019-03-25 23:19:27 +01:00
|
|
|
opts.qtype = bool(peer::enable_ipv6) && bool(net::enable_ipv6)? 28: 1;
|
2019-03-24 23:28:07 +01:00
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "peer(%p) resolved %s SRV rrs:%zu resolving %s %s",
|
|
|
|
this,
|
2019-03-25 22:37:01 +01:00
|
|
|
hostcanon,
|
2019-03-24 23:28:07 +01:00
|
|
|
rrs.size(),
|
|
|
|
host(target),
|
|
|
|
rfc1035::rqtype.at(opts.qtype)
|
|
|
|
};
|
|
|
|
|
|
|
|
resolve(target, opts);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2019-04-24 05:06:34 +02:00
|
|
|
log, "peer(%p) resolve SRV :%s",
|
2019-03-24 23:28:07 +01:00
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
const ctx::exception_handler eh;
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:37:01 +01:00
|
|
|
void
|
|
|
|
ircd::server::peer::handle_resolve_AAAA(const hostport &target,
|
|
|
|
const json::array &rrs)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(op_resolve);
|
|
|
|
op_resolve = false;
|
|
|
|
|
|
|
|
if(unlikely(ircd::run::level != ircd::run::level::RUN))
|
|
|
|
op_fini = true;
|
|
|
|
|
|
|
|
if(unlikely(finished()))
|
|
|
|
return handle_finished();
|
|
|
|
|
|
|
|
if(op_fini)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(net::dns::is_empty(rrs) || net::dns::is_error(rrs))
|
|
|
|
{
|
|
|
|
// Setup the address record query off this SRV response.
|
|
|
|
net::dns::opts opts;
|
|
|
|
opts.qtype = 1;
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "peer(%p) resolved %s AAAA rrs:%zu resolving %s %s",
|
|
|
|
this,
|
|
|
|
hostcanon,
|
|
|
|
rrs.size(),
|
|
|
|
host(target),
|
|
|
|
rfc1035::rqtype.at(opts.qtype)
|
|
|
|
};
|
|
|
|
|
|
|
|
resolve(target, opts);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const json::object &rr
|
|
|
|
{
|
|
|
|
net::dns::random_choice(rrs)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(!net::dns::is_error(rr));
|
|
|
|
this->remote = net::ipport
|
|
|
|
{
|
|
|
|
unquote(rr.at("ip")), net::port(this->remote)
|
|
|
|
};
|
|
|
|
|
|
|
|
open_opts.ipport = this->remote;
|
|
|
|
open_links();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "peer(%p) resolve AAAA: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
const ctx::exception_handler eh;
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2019-03-24 23:28:07 +01:00
|
|
|
void
|
|
|
|
ircd::server::peer::handle_resolve_A(const hostport &,
|
|
|
|
const json::array &rrs)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const ctx::critical_assertion ca;
|
|
|
|
assert(op_resolve);
|
|
|
|
op_resolve = false;
|
2018-03-09 17:01:21 +01:00
|
|
|
|
2019-01-18 17:55:06 +01:00
|
|
|
if(unlikely(ircd::run::level != ircd::run::level::RUN))
|
2018-09-18 06:05:55 +02:00
|
|
|
op_fini = true;
|
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
if(unlikely(finished()))
|
|
|
|
return handle_finished();
|
2018-03-09 17:01:21 +01:00
|
|
|
|
2018-04-07 15:02:12 +02:00
|
|
|
if(op_fini)
|
|
|
|
return;
|
|
|
|
|
2019-03-25 04:12:15 +01:00
|
|
|
if(net::dns::is_empty(rrs))
|
2019-03-24 23:28:07 +01:00
|
|
|
{
|
|
|
|
err_set(make_exception_ptr<unavailable>("Host has no address record."));
|
|
|
|
assert(this->e && this->e->eptr);
|
|
|
|
std::rethrow_exception(this->e->eptr);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
const json::object &rr
|
|
|
|
{
|
|
|
|
net::dns::random_choice(rrs)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(net::dns::is_error(rr))
|
|
|
|
{
|
|
|
|
const json::string &error(rr.get("error"));
|
|
|
|
err_set(make_exception_ptr<rfc1035::error>("%s", error));
|
|
|
|
assert(this->e && this->e->eptr);
|
|
|
|
std::rethrow_exception(this->e->eptr);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the results of the query to this object instance.
|
|
|
|
this->remote = net::ipport
|
|
|
|
{
|
|
|
|
unquote(rr.at("ip")), net::port(this->remote)
|
|
|
|
};
|
|
|
|
|
|
|
|
open_opts.ipport = this->remote;
|
2019-03-25 04:46:58 +01:00
|
|
|
open_links();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2019-04-24 05:06:34 +02:00
|
|
|
log, "peer(%p) resolve A :%s",
|
2019-03-25 04:46:58 +01:00
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2019-03-24 23:28:07 +01:00
|
|
|
|
2019-03-25 04:46:58 +01:00
|
|
|
const ctx::exception_handler eh;
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::peer::open_links()
|
|
|
|
try
|
|
|
|
{
|
2019-03-24 23:28:07 +01:00
|
|
|
// The hostname in open_opts should still reference this object's string.
|
|
|
|
assert(host(open_opts.hostport).data() == this->hostcanon.data());
|
|
|
|
|
2018-05-09 00:50:48 +02:00
|
|
|
link *links[LINK_MAX];
|
|
|
|
const auto end(pointers(this->links, links));
|
|
|
|
for(link **link(links); link != end; ++link)
|
|
|
|
(*link)->open(open_opts);
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2018-01-16 13:01:26 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-19 00:10:10 +02:00
|
|
|
log::derror
|
|
|
|
{
|
2019-04-24 05:06:34 +02:00
|
|
|
log, "peer(%p) open links :%s",
|
2018-09-19 00:10:10 +02:00
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
2019-03-22 20:47:14 +01:00
|
|
|
const ctx::exception_handler eh;
|
2018-03-08 17:24:19 +01:00
|
|
|
close();
|
2018-01-16 13:01:26 +01:00
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
void
|
|
|
|
ircd::server::peer::handle_finished()
|
|
|
|
{
|
|
|
|
assert(finished());
|
|
|
|
|
|
|
|
// Right now this is what the server:: ~init sequence needs
|
|
|
|
// to wait for all links to close on IRCd shutdown.
|
|
|
|
server::dock.notify_all();
|
|
|
|
}
|
|
|
|
|
2018-03-27 02:21:24 +02:00
|
|
|
size_t
|
|
|
|
ircd::server::peer::read_total()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return read_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::peer::write_total()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return write_bytes;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::read_remaining()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.read_remaining();
|
|
|
|
});
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::read_completed()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.read_completed();
|
|
|
|
});
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-27 02:11:39 +02:00
|
|
|
ircd::server::peer::read_size()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-03-27 02:11:39 +02:00
|
|
|
return link.read_size();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::write_remaining()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.write_remaining();
|
|
|
|
});
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::write_completed()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.write_completed();
|
|
|
|
});
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-27 02:11:39 +02:00
|
|
|
ircd::server::peer::write_size()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-03-27 02:11:39 +02:00
|
|
|
return link.write_size();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::tag_uncommitted()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.tag_uncommitted();
|
|
|
|
});
|
|
|
|
}
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::tag_committed()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.tag_committed();
|
|
|
|
});
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::tag_count()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
return link.tag_count();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::link_ready()
|
2018-01-13 03:57:58 +01:00
|
|
|
const
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return accumulate_links([](const auto &link)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.ready();
|
|
|
|
});
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::link_busy()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.busy();
|
|
|
|
});
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::link_count()
|
2018-01-13 03:57:58 +01:00
|
|
|
const
|
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return links.size();
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 06:13:48 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::link_min()
|
2018-01-16 06:13:48 +01:00
|
|
|
const
|
|
|
|
{
|
2018-03-04 06:54:42 +01:00
|
|
|
return link_min_default;
|
2018-01-16 06:13:48 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::link_max()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
2018-03-04 06:54:42 +01:00
|
|
|
return link_max_default;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
bool
|
|
|
|
ircd::server::peer::finished()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return links.empty() && !op_resolve && op_fini;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
template<class F>
|
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::accumulate_tags(F&& closure)
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([&closure](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.accumulate([&closure](const auto &tag)
|
|
|
|
{
|
|
|
|
return closure(tag);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
template<class F>
|
|
|
|
size_t
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::peer::accumulate_links(F&& closure)
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(links), end(links), size_t(0), [&closure]
|
|
|
|
(auto ret, const auto &tag)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return ret += closure(tag);
|
|
|
|
});
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-10-21 14:33:45 +02:00
|
|
|
//
|
|
|
|
// peer::err
|
|
|
|
//
|
|
|
|
|
2019-04-17 04:04:23 +02:00
|
|
|
ircd::server::peer::err::err(const std::exception_ptr &eptr)
|
|
|
|
:eptr{eptr}
|
2018-10-21 14:33:45 +02:00
|
|
|
,etime{now<system_point>()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::peer::err::~err()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
2018-01-16 03:04:23 +01:00
|
|
|
// link
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
|
|
|
|
2018-03-04 06:54:42 +01:00
|
|
|
decltype(ircd::server::link::tag_max_default)
|
|
|
|
ircd::server::link::tag_max_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.server.link.tag_max" },
|
|
|
|
{ "default", -1L }
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::server::link::tag_commit_max_default)
|
|
|
|
ircd::server::link::tag_commit_max_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.server.link.tag_commit_max" },
|
|
|
|
{ "default", 3L }
|
|
|
|
};
|
|
|
|
|
2019-04-12 07:18:47 +02:00
|
|
|
decltype(ircd::server::link::ids)
|
|
|
|
ircd::server::link::ids;
|
|
|
|
|
2019-04-22 20:47:29 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::server::loghead(const link &link)
|
|
|
|
{
|
|
|
|
thread_local char buf[512];
|
|
|
|
return loghead(buf, link);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::server::loghead(const mutable_buffer &buf,
|
|
|
|
const link &link)
|
|
|
|
{
|
|
|
|
thread_local char rembuf[2][64];
|
2019-07-20 05:27:17 +02:00
|
|
|
|
|
|
|
const auto local
|
2019-04-22 20:47:29 +02:00
|
|
|
{
|
|
|
|
link.socket?
|
2019-07-20 05:27:17 +02:00
|
|
|
string(rembuf[0], local_ipport(*link.socket)):
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto remote
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
link.socket?
|
2019-07-20 05:27:17 +02:00
|
|
|
string(rembuf[1], remote_ipport(*link.socket)):
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "socket:%lu local:%s remote:%s link:%lu peer:%lu",
|
2019-04-22 20:47:29 +02:00
|
|
|
link.socket?
|
2019-07-20 05:27:17 +02:00
|
|
|
link.socket->id : 0UL,
|
|
|
|
local?
|
|
|
|
local : "0.0.0.0"_sv,
|
|
|
|
remote?
|
|
|
|
remote : "0.0.0.0"_sv,
|
2019-04-22 20:47:29 +02:00
|
|
|
link.id,
|
|
|
|
link.peer?
|
|
|
|
link.peer->id: 0UL,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-04 06:54:42 +01:00
|
|
|
//
|
|
|
|
// link::link
|
|
|
|
//
|
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
ircd::server::link::link(server::peer &peer)
|
2018-03-10 16:50:19 +01:00
|
|
|
:peer{&peer}
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::link::~link()
|
2018-01-13 03:57:58 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(!busy());
|
2018-02-27 07:49:44 +01:00
|
|
|
assert(!opened());
|
2018-01-15 09:11:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::link::submit(request &request)
|
2018-01-15 09:11:32 +01:00
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
assert(!request.tag || !request.tag->committed());
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
const auto it
|
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
request.tag? queue.emplace(end(queue), std::move(*request.tag)):
|
|
|
|
queue.emplace(end(queue), request)
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
2018-04-06 10:52:22 +02:00
|
|
|
/*
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "tag(%p) submitted to link(%p) queue: %zu",
|
|
|
|
&(*it),
|
|
|
|
this,
|
|
|
|
tag_count()
|
|
|
|
};
|
2018-04-06 10:52:22 +02:00
|
|
|
*/
|
2018-01-16 03:04:23 +01:00
|
|
|
if(ready())
|
|
|
|
wait_writable();
|
|
|
|
}
|
|
|
|
|
2018-03-04 06:55:59 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::cancel_all(std::exception_ptr eptr)
|
|
|
|
{
|
|
|
|
for(auto it(begin(queue)); it != end(queue); it = queue.erase(it))
|
|
|
|
{
|
|
|
|
auto &tag{*it};
|
|
|
|
if(!tag.request)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tag.set_exception(eptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::link::cancel_committed(std::exception_ptr eptr)
|
|
|
|
{
|
|
|
|
for(auto it(begin(queue)); it != end(queue); it = queue.erase(it))
|
|
|
|
{
|
|
|
|
auto &tag{*it};
|
|
|
|
if(!tag.request)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!tag.committed())
|
|
|
|
break;
|
|
|
|
|
|
|
|
tag.set_exception(eptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::link::cancel_uncommitted(std::exception_ptr eptr)
|
|
|
|
{
|
|
|
|
auto it(begin(queue));
|
|
|
|
while(it != end(queue))
|
|
|
|
{
|
|
|
|
auto &tag{*it};
|
|
|
|
if(!tag.request || tag.committed())
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag.set_exception(eptr);
|
|
|
|
it = queue.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
bool
|
|
|
|
ircd::server::link::open(const net::open_opts &open_opts)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2019-01-18 17:55:06 +01:00
|
|
|
assert(ircd::run::level == ircd::run::level::RUN);
|
2018-03-09 17:01:21 +01:00
|
|
|
|
2018-03-09 16:29:37 +01:00
|
|
|
if(op_init)
|
2018-01-16 03:04:23 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto handler
|
|
|
|
{
|
|
|
|
std::bind(&link::handle_open, this, ph::_1)
|
|
|
|
};
|
|
|
|
|
2018-03-09 16:29:37 +01:00
|
|
|
op_init = true;
|
2019-04-17 07:27:24 +02:00
|
|
|
op_open = true;
|
2019-03-23 02:54:33 +01:00
|
|
|
const unwind::exceptional unhandled{[this]
|
|
|
|
{
|
|
|
|
op_init = false;
|
2019-04-17 07:27:24 +02:00
|
|
|
op_open = false;
|
2019-03-23 02:54:33 +01:00
|
|
|
}};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
socket = net::open(open_opts, std::move(handler));
|
2019-04-17 07:27:24 +02:00
|
|
|
op_open = false;
|
|
|
|
|
|
|
|
if(finished())
|
|
|
|
{
|
|
|
|
assert(peer);
|
|
|
|
peer->handle_finished(*this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
return true;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::link::handle_open(std::exception_ptr eptr)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-03-09 16:29:37 +01:00
|
|
|
assert(op_init);
|
|
|
|
op_init = false;
|
2019-07-14 21:13:53 +02:00
|
|
|
synack_ts = time<seconds>();
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-03-09 16:29:37 +01:00
|
|
|
if(!eptr && !op_fini)
|
2018-01-16 03:04:23 +01:00
|
|
|
wait_writable();
|
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
if(peer)
|
|
|
|
peer->handle_open(*this, std::move(eptr));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
bool
|
|
|
|
ircd::server::link::close(const net::close_opts &close_opts)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-03-09 16:29:37 +01:00
|
|
|
if(op_fini)
|
2018-01-16 03:04:23 +01:00
|
|
|
return false;
|
|
|
|
|
2018-03-09 16:29:37 +01:00
|
|
|
op_fini = true;
|
2018-01-16 13:01:26 +01:00
|
|
|
|
2018-03-09 16:29:37 +01:00
|
|
|
// Tell the peer to ditch everything in the queue; op_fini has been set so
|
2018-01-16 13:01:26 +01:00
|
|
|
// the tags won't get assigned back to this link.
|
2018-03-05 15:59:10 +01:00
|
|
|
if(tag_count() && peer)
|
|
|
|
peer->disperse(*this);
|
2018-01-16 13:01:26 +01:00
|
|
|
|
2018-03-04 10:22:05 +01:00
|
|
|
auto handler
|
|
|
|
{
|
|
|
|
std::bind(&link::handle_close, this, ph::_1)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!socket)
|
|
|
|
{
|
|
|
|
handler(std::exception_ptr{});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
net::close(*socket, close_opts, std::move(handler));
|
|
|
|
return true;
|
2017-11-25 22:17:22 +01:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::handle_close(std::exception_ptr eptr)
|
|
|
|
{
|
2018-03-09 16:29:37 +01:00
|
|
|
assert(op_fini);
|
2018-03-04 10:16:55 +01:00
|
|
|
|
2018-03-09 22:53:23 +01:00
|
|
|
if(op_init)
|
|
|
|
{
|
|
|
|
assert(bool(eptr));
|
|
|
|
}
|
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
if(peer)
|
|
|
|
peer->handle_close(*this, std::move(eptr));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-14 06:16:49 +01:00
|
|
|
ircd::server::link::wait_writable()
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-03-10 16:50:19 +01:00
|
|
|
if(op_write || unlikely(op_fini))
|
2018-02-27 08:30:08 +01:00
|
|
|
return;
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
auto handler
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
std::bind(&link::handle_writable, this, ph::_1)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(ready());
|
2018-03-09 16:29:37 +01:00
|
|
|
op_write = true;
|
2019-03-23 02:54:33 +01:00
|
|
|
const unwind::exceptional unhandled{[this]
|
|
|
|
{
|
|
|
|
op_write = false;
|
|
|
|
}};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
net::wait(*socket, net::ready::WRITE, std::move(handler));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-11 04:25:27 +02:00
|
|
|
__attribute__((stack_protect))
|
2018-01-14 06:16:49 +01:00
|
|
|
ircd::server::link::handle_writable(const error_code &ec)
|
2019-05-02 11:24:13 +02:00
|
|
|
noexcept try
|
2018-01-14 06:16:49 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2018-01-15 05:51:39 +01:00
|
|
|
|
2018-03-09 16:29:37 +01:00
|
|
|
op_write = false;
|
2018-03-10 16:50:19 +01:00
|
|
|
|
|
|
|
if(unlikely(finished()))
|
|
|
|
{
|
|
|
|
assert(peer);
|
|
|
|
return peer->handle_finished(*this);
|
|
|
|
}
|
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
if(system_category(ec)) switch(ec.value())
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
case 0:
|
2018-01-15 09:11:32 +01:00
|
|
|
handle_writable_success();
|
2018-01-16 05:04:45 +01:00
|
|
|
return;
|
2018-01-15 05:51:39 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
case int(errc::operation_canceled):
|
2018-01-15 09:11:32 +01:00
|
|
|
return;
|
2018-01-15 05:51:39 +01:00
|
|
|
|
|
|
|
default:
|
2018-01-16 05:04:45 +01:00
|
|
|
break;
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
2018-01-16 05:04:45 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
throw std::system_error{ec};
|
2018-01-15 09:11:32 +01:00
|
|
|
}
|
2018-11-09 06:18:39 +01:00
|
|
|
catch(const std::system_error &e)
|
2018-01-23 20:38:17 +01:00
|
|
|
{
|
2018-03-10 16:50:19 +01:00
|
|
|
assert(peer);
|
|
|
|
peer->handle_error(*this, e);
|
2018-01-23 20:38:17 +01:00
|
|
|
}
|
2018-09-19 06:48:40 +02:00
|
|
|
catch(...)
|
2018-01-23 20:38:17 +01:00
|
|
|
{
|
2018-03-10 16:50:19 +01:00
|
|
|
assert(peer);
|
|
|
|
peer->handle_error(*this, std::current_exception());
|
2018-01-23 20:38:17 +01:00
|
|
|
}
|
2018-01-15 05:51:39 +01:00
|
|
|
|
2018-01-15 09:11:32 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::handle_writable_success()
|
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
auto it(begin(queue));
|
|
|
|
while(it != end(queue))
|
2018-01-15 09:11:32 +01:00
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
auto &tag{*it};
|
|
|
|
if((tag.abandoned() || tag.canceled()) && !tag.committed())
|
|
|
|
{
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s discarding canceled:%d abandoned:%d uncommitted tag %zu of %zu",
|
|
|
|
loghead(*this),
|
2018-09-19 00:10:10 +02:00
|
|
|
tag.canceled(),
|
|
|
|
tag.abandoned(),
|
|
|
|
tag_committed(),
|
|
|
|
tag_count()
|
|
|
|
};
|
2018-01-17 06:23:15 +01:00
|
|
|
|
|
|
|
it = queue.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-02 21:33:08 +02:00
|
|
|
if(tag.canceled() && tag.committed() && tag_committed() <= 1)
|
|
|
|
{
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s closing to interrupt canceled committed tag:%lu of %zu",
|
|
|
|
loghead(*this),
|
|
|
|
tag.state.id,
|
2018-09-19 00:10:10 +02:00
|
|
|
tag_count()
|
|
|
|
};
|
2018-05-02 21:33:08 +02:00
|
|
|
|
|
|
|
close();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-27 08:30:08 +01:00
|
|
|
if(tag_committed() == 0)
|
|
|
|
wait_readable();
|
|
|
|
|
2018-01-15 09:11:32 +01:00
|
|
|
if(!process_write(tag))
|
|
|
|
{
|
|
|
|
wait_writable();
|
|
|
|
break;
|
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
// Limits the amount of requests in the pipe.
|
|
|
|
if(tag_committed() >= tag_commit_max())
|
|
|
|
break;
|
2018-01-17 06:23:15 +01:00
|
|
|
|
|
|
|
++it;
|
2018-01-15 09:11:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::process_write(tag &tag)
|
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
if(!tag.committed())
|
2019-07-26 08:05:15 +02:00
|
|
|
{
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s starting on tag:%lu %zu of %zu: wt:%zu [%s]",
|
|
|
|
loghead(*this),
|
|
|
|
tag.state.id,
|
2018-09-19 00:10:10 +02:00
|
|
|
tag_committed(),
|
|
|
|
tag_count(),
|
2019-03-24 21:49:15 +01:00
|
|
|
tag.write_size(),
|
|
|
|
tag.request?
|
|
|
|
loghead(*tag.request):
|
|
|
|
"<no attached request>"_sv
|
2018-09-19 00:10:10 +02:00
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2019-07-26 08:05:15 +02:00
|
|
|
if(tag.request)
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
request::log, "%s wt:%zu on %s",
|
|
|
|
loghead(*tag.request),
|
|
|
|
tag.write_size(),
|
|
|
|
loghead(*this),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
while(tag.write_remaining())
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
|
|
|
const const_buffer buffer
|
|
|
|
{
|
|
|
|
tag.make_write_buffer()
|
|
|
|
};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(!empty(buffer));
|
2018-01-15 05:51:39 +01:00
|
|
|
const const_buffer written
|
|
|
|
{
|
2018-01-15 09:11:32 +01:00
|
|
|
process_write_next(buffer)
|
2018-01-15 05:51:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
tag.wrote_buffer(written);
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(tag_committed() <= tag_commit_max());
|
2018-01-15 09:11:32 +01:00
|
|
|
if(size(written) < size(buffer))
|
|
|
|
return false;
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
return true;
|
2018-01-14 06:16:49 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 09:11:32 +01:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::link::process_write_next(const const_buffer &buffer)
|
|
|
|
{
|
|
|
|
const size_t bytes
|
|
|
|
{
|
|
|
|
write_any(*socket, buffer)
|
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer written
|
|
|
|
{
|
|
|
|
data(buffer), bytes
|
|
|
|
};
|
|
|
|
|
2018-03-27 02:21:24 +02:00
|
|
|
assert(peer);
|
|
|
|
peer->write_bytes += bytes;
|
2018-01-15 09:11:32 +01:00
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::wait_readable()
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-05-02 21:33:08 +02:00
|
|
|
if(op_read || op_fini)
|
2018-02-27 08:30:08 +01:00
|
|
|
return;
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
assert(ready());
|
2018-03-09 16:29:37 +01:00
|
|
|
op_read = true;
|
2019-03-23 02:54:33 +01:00
|
|
|
const unwind::exceptional unhandled{[this]
|
|
|
|
{
|
|
|
|
op_read = false;
|
|
|
|
}};
|
|
|
|
|
2018-09-19 08:47:52 +02:00
|
|
|
auto handler
|
|
|
|
{
|
|
|
|
std::bind(&link::handle_readable, this, ph::_1)
|
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
net::wait(*socket, net::ready::READ, std::move(handler));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-11 04:25:27 +02:00
|
|
|
__attribute__((stack_protect))
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::link::handle_readable(const error_code &ec)
|
2019-05-02 11:24:13 +02:00
|
|
|
noexcept try
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
using std::errc;
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-03-09 16:29:37 +01:00
|
|
|
op_read = false;
|
2018-03-10 16:50:19 +01:00
|
|
|
|
|
|
|
if(unlikely(finished()))
|
|
|
|
{
|
|
|
|
assert(peer);
|
|
|
|
return peer->handle_finished(*this);
|
|
|
|
}
|
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
if(system_category(ec)) switch(ec.value())
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
case 0:
|
2018-01-13 03:57:58 +01:00
|
|
|
handle_readable_success();
|
2018-01-16 05:04:45 +01:00
|
|
|
return;
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
case int(errc::operation_canceled):
|
2018-01-15 09:11:32 +01:00
|
|
|
return;
|
2018-01-14 06:16:49 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
default:
|
2018-01-16 05:04:45 +01:00
|
|
|
break;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2018-01-16 05:04:45 +01:00
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
throw std::system_error{ec};
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2018-11-09 06:18:39 +01:00
|
|
|
catch(const std::system_error &e)
|
2018-01-14 06:16:49 +01:00
|
|
|
{
|
2018-03-10 16:50:19 +01:00
|
|
|
assert(peer);
|
|
|
|
peer->handle_error(*this, e);
|
2018-01-14 06:16:49 +01:00
|
|
|
}
|
2018-09-19 06:48:40 +02:00
|
|
|
catch(...)
|
2018-01-14 06:16:49 +01:00
|
|
|
{
|
2018-03-10 16:50:19 +01:00
|
|
|
assert(peer);
|
|
|
|
peer->handle_error(*this, std::current_exception());
|
2018-01-14 06:16:49 +01:00
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
/// Process as many read operations from as many tags as possible
|
2018-01-13 03:57:58 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::handle_readable_success()
|
|
|
|
{
|
|
|
|
if(queue.empty())
|
2018-03-18 07:20:15 +01:00
|
|
|
{
|
|
|
|
discard_read();
|
|
|
|
wait_readable();
|
|
|
|
return;
|
|
|
|
}
|
2018-01-14 06:16:49 +01:00
|
|
|
|
|
|
|
// Data pointed to by overrun will remain intact between iterations
|
|
|
|
// because this loop isn't executing in any ircd::ctx.
|
|
|
|
const_buffer overrun; do
|
|
|
|
{
|
|
|
|
if(!process_read(overrun))
|
2018-01-16 06:13:48 +01:00
|
|
|
{
|
|
|
|
wait_readable();
|
|
|
|
return;
|
|
|
|
}
|
2018-01-14 06:16:49 +01:00
|
|
|
}
|
|
|
|
while(!queue.empty());
|
2018-01-16 06:13:48 +01:00
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
assert(peer);
|
|
|
|
peer->handle_link_done(*this);
|
2018-01-14 06:16:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Process as many read operations for one tag as possible
|
|
|
|
bool
|
|
|
|
ircd::server::link::process_read(const_buffer &overrun)
|
|
|
|
try
|
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
auto &tag
|
|
|
|
{
|
|
|
|
queue.front()
|
|
|
|
};
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
if(!tag.committed())
|
|
|
|
{
|
|
|
|
// Tag hasn't sent its data yet, we shouldn't have anything for it
|
|
|
|
assert(empty(overrun));
|
2018-04-06 10:05:04 +02:00
|
|
|
discard_read(); // Should stumble on a socket error.
|
2018-01-17 06:23:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-02 21:33:08 +02:00
|
|
|
if(tag.canceled() && tag_committed() <= 1)
|
|
|
|
{
|
2018-09-19 00:10:10 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s closing to interrupt canceled committed tag:%lu of %zu",
|
|
|
|
loghead(*this),
|
|
|
|
tag.state.id,
|
2018-09-19 00:10:10 +02:00
|
|
|
tag_count()
|
|
|
|
};
|
2018-05-02 21:33:08 +02:00
|
|
|
|
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
bool done{false}; do
|
|
|
|
{
|
|
|
|
overrun = process_read_next(overrun, tag, done);
|
|
|
|
}
|
2019-04-10 05:22:06 +02:00
|
|
|
while(!done && !empty(overrun));
|
|
|
|
|
|
|
|
if(!done)
|
|
|
|
{
|
|
|
|
// This branch represents a read of -EAGAIN.
|
|
|
|
assert(empty(overrun));
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 06:16:49 +01:00
|
|
|
|
2018-03-05 15:59:10 +01:00
|
|
|
assert(peer);
|
2018-09-04 17:12:47 +02:00
|
|
|
peer->handle_tag_done(*this, tag);
|
|
|
|
assert(!queue.empty());
|
2018-01-14 06:16:49 +01:00
|
|
|
queue.pop_front();
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-16 09:45:51 +01:00
|
|
|
catch(const buffer_overrun &e)
|
|
|
|
{
|
|
|
|
queue.pop_front();
|
|
|
|
throw;
|
|
|
|
}
|
2018-01-14 06:16:49 +01:00
|
|
|
|
|
|
|
/// Process one read operation for one tag
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::link::process_read_next(const const_buffer &underrun,
|
2018-01-15 09:11:32 +01:00
|
|
|
tag &tag,
|
2018-01-14 06:16:49 +01:00
|
|
|
bool &done)
|
2018-01-16 09:45:51 +01:00
|
|
|
try
|
2018-01-14 06:16:49 +01:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
|
|
|
tag.make_read_buffer()
|
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const size_t copied
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
copy(buffer, underrun)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const mutable_buffer remaining
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
data(buffer) + copied, size(buffer) - copied
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const const_buffer view
|
|
|
|
{
|
2018-03-20 22:07:52 +01:00
|
|
|
read(remaining)
|
2018-01-14 06:16:49 +01:00
|
|
|
};
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const const_buffer overrun
|
|
|
|
{
|
2018-01-24 18:15:57 +01:00
|
|
|
tag.read_buffer(view, done, *this)
|
2018-01-14 06:16:49 +01:00
|
|
|
};
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(done || empty(overrun));
|
|
|
|
return overrun;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2019-03-16 23:01:46 +01:00
|
|
|
catch(const buffer_overrun &)
|
2018-01-16 09:45:51 +01:00
|
|
|
{
|
2019-03-16 23:01:46 +01:00
|
|
|
tag.set_exception(std::current_exception());
|
2018-01-16 09:45:51 +01:00
|
|
|
throw;
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-03-20 22:07:52 +01:00
|
|
|
/// Read directly off the link's socket into buf
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::link::read(const mutable_buffer &buf)
|
|
|
|
{
|
2018-05-02 20:42:46 +02:00
|
|
|
assert(!empty(buf));
|
2018-03-20 22:07:52 +01:00
|
|
|
const size_t received
|
|
|
|
{
|
|
|
|
read_one(*socket, buf)
|
|
|
|
};
|
|
|
|
|
2018-03-27 02:21:24 +02:00
|
|
|
assert(peer);
|
|
|
|
peer->read_bytes += received;
|
|
|
|
|
2018-03-20 22:07:52 +01:00
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), received
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-15 05:51:39 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::discard_read()
|
|
|
|
{
|
2019-03-01 19:07:56 +01:00
|
|
|
assert(socket);
|
2018-03-26 11:24:29 +02:00
|
|
|
ssize_t discard
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
2018-03-26 11:24:29 +02:00
|
|
|
SSL_pending(socket->ssl.native_handle())
|
2018-01-15 05:51:39 +01:00
|
|
|
};
|
|
|
|
|
2018-04-06 10:53:50 +02:00
|
|
|
if(discard <= 0 && queue.empty())
|
2018-03-26 11:24:29 +02:00
|
|
|
discard = available(*socket);
|
|
|
|
|
2018-04-06 10:53:50 +02:00
|
|
|
if(discard <= 0 && !queue.empty())
|
|
|
|
discard = 1;
|
|
|
|
|
2018-01-15 05:51:39 +01:00
|
|
|
const size_t discarded
|
|
|
|
{
|
2018-03-26 11:24:29 +02:00
|
|
|
discard_any(*socket, size_t(discard))
|
2018-01-15 05:51:39 +01:00
|
|
|
};
|
|
|
|
|
2018-03-27 02:21:24 +02:00
|
|
|
assert(peer);
|
|
|
|
peer->read_bytes += discarded;
|
|
|
|
|
2018-01-15 05:51:39 +01:00
|
|
|
// Shouldn't ever be hit because the read() within discard() throws
|
|
|
|
// the pending error like an eof.
|
2019-04-17 05:57:06 +02:00
|
|
|
thread_local char rembuf[64];
|
2019-03-01 19:07:56 +01:00
|
|
|
const fmt::snstringf msg
|
2018-09-19 00:10:10 +02:00
|
|
|
{
|
2019-03-01 19:07:56 +01:00
|
|
|
512, "peer(%p %s) link(%p q:%zu) socket(%s) discarded %zu of %zd unexpected bytes",
|
|
|
|
peer,
|
|
|
|
peer?
|
|
|
|
peer->hostcanon:
|
|
|
|
std::string{},
|
2018-09-19 00:10:10 +02:00
|
|
|
this,
|
2019-03-01 19:07:56 +01:00
|
|
|
queue.size(),
|
|
|
|
likely(peer)?
|
2019-04-17 05:57:06 +02:00
|
|
|
string(rembuf, peer->remote):
|
2019-03-01 19:07:56 +01:00
|
|
|
socket?
|
2019-04-17 05:57:06 +02:00
|
|
|
string(rembuf, remote_ipport(*socket)):
|
|
|
|
string_view{},
|
2018-09-19 00:10:10 +02:00
|
|
|
discarded,
|
|
|
|
discard
|
|
|
|
};
|
2018-01-15 05:51:39 +01:00
|
|
|
|
2019-03-01 19:07:56 +01:00
|
|
|
log::warning
|
|
|
|
{
|
2019-04-22 20:47:29 +02:00
|
|
|
log, "%s %s",
|
|
|
|
loghead(*this),
|
|
|
|
string_view{msg},
|
2019-03-01 19:07:56 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
// just in case so this doesn't get loopy with discarding zero with
|
|
|
|
// an empty queue...
|
2018-04-06 10:53:50 +02:00
|
|
|
if(unlikely(!discard && !discarded))
|
2019-01-14 00:50:04 +01:00
|
|
|
throw panic
|
2018-01-16 05:04:45 +01:00
|
|
|
{
|
2019-03-01 19:07:56 +01:00
|
|
|
"%s", string_view{msg}
|
2018-01-16 05:04:45 +01:00
|
|
|
};
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::link::tag_uncommitted()
|
2018-01-13 03:57:58 +01:00
|
|
|
const
|
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
return tag_count() - tag_committed();
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::link::tag_committed()
|
2018-01-13 03:57:58 +01:00
|
|
|
const
|
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
return tag.committed();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-01-16 05:04:45 +01:00
|
|
|
ircd::server::link::tag_count()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
2017-11-25 22:17:22 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return queue.size();
|
|
|
|
}
|
|
|
|
|
2018-03-27 02:21:24 +02:00
|
|
|
size_t
|
|
|
|
ircd::server::link::read_total()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return socket? socket->in.bytes : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::write_total()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return socket? socket->out.bytes : 0;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::link::read_remaining()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.read_remaining();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::read_completed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.read_completed();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2018-03-27 02:11:39 +02:00
|
|
|
ircd::server::link::read_size()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
2018-03-27 02:11:39 +02:00
|
|
|
return tag.read_size();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::write_remaining()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.write_remaining();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::write_completed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.write_completed();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2018-03-27 02:11:39 +02:00
|
|
|
ircd::server::link::write_size()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
2018-03-27 02:11:39 +02:00
|
|
|
return tag.write_size();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::busy()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !queue.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::ready()
|
|
|
|
const
|
|
|
|
{
|
2018-03-09 16:29:37 +01:00
|
|
|
return opened() && !op_init && !op_fini;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-27 07:49:44 +01:00
|
|
|
ircd::server::link::opened()
|
2018-01-16 03:04:23 +01:00
|
|
|
const noexcept
|
|
|
|
{
|
2018-02-27 07:49:44 +01:00
|
|
|
return bool(socket) && net::opened(*socket);
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:50:19 +01:00
|
|
|
bool
|
|
|
|
ircd::server::link::finished()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(!bool(socket))
|
2018-03-11 19:45:24 +01:00
|
|
|
return true;
|
2018-03-10 16:50:19 +01:00
|
|
|
|
2019-04-17 07:27:24 +02:00
|
|
|
return !opened() && op_fini && !op_init && !op_open && !op_write && !op_read;
|
2018-03-10 16:50:19 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::link::tag_commit_max()
|
|
|
|
const
|
|
|
|
{
|
2018-03-04 06:54:42 +01:00
|
|
|
return tag_commit_max_default;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::tag_max()
|
|
|
|
const
|
|
|
|
{
|
2018-03-04 06:54:42 +01:00
|
|
|
return tag_max_default;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
size_t
|
|
|
|
ircd::server::link::accumulate_tags(F&& closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(queue), end(queue), size_t(0), [&closure]
|
|
|
|
(auto ret, const auto &tag)
|
|
|
|
{
|
|
|
|
return ret += closure(tag);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-26 08:05:15 +02:00
|
|
|
//
|
|
|
|
// request
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::server::request::log)
|
|
|
|
ircd::server::request::log
|
|
|
|
{
|
|
|
|
"server.request"
|
|
|
|
};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
//
|
|
|
|
// tag
|
|
|
|
//
|
|
|
|
|
2019-04-12 07:18:47 +02:00
|
|
|
/// Monotonic counter for tags.
|
|
|
|
decltype(ircd::server::tag::state::ids)
|
|
|
|
ircd::server::tag::state::ids;
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
/// This is tricky. When a user cancels a request which has committed some
|
|
|
|
/// writes to the remote we have to continue to service it through to
|
|
|
|
/// completion without disrupting the linearity of the link's pipeline
|
|
|
|
/// and causing trouble with other requests. This all depends on what phase
|
|
|
|
/// the request is currently in.
|
|
|
|
///
|
|
|
|
/// In any case, the goal here is to swap out the user's request buffers
|
|
|
|
/// and replace them with cancellation buffers which will be transparent
|
|
|
|
/// to the link as it completes the request.
|
|
|
|
void
|
|
|
|
ircd::server::cancel(request &request,
|
|
|
|
tag &tag)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
// Must have a fully associated request/tag which has committed some
|
|
|
|
// data to the wire to enter this routine.
|
|
|
|
assert(tag.committed());
|
2018-09-19 06:24:19 +02:00
|
|
|
assert(!tag.canceled());
|
2018-01-17 06:23:15 +01:00
|
|
|
assert(request.tag == &tag);
|
|
|
|
assert(tag.request == &request);
|
2019-04-12 16:12:49 +02:00
|
|
|
assert(!tag.cancellation);
|
2018-01-17 06:23:15 +01:00
|
|
|
|
|
|
|
// Disassociate the user's request and add our dummy request in its place.
|
2018-04-07 13:01:09 +02:00
|
|
|
disassociate(request, tag);
|
2018-01-17 06:23:15 +01:00
|
|
|
|
2018-04-07 13:01:09 +02:00
|
|
|
assert(tag.request == nullptr);
|
2018-01-17 06:23:15 +01:00
|
|
|
tag.request = new server::request{};
|
|
|
|
tag.request->tag = &tag;
|
|
|
|
|
|
|
|
// Setup the cancellation buffers by mirroring the current state of the
|
|
|
|
// user's buffers.
|
|
|
|
|
|
|
|
const size_t cancellation_size
|
|
|
|
{
|
|
|
|
size(request.out) + size(request.in)
|
|
|
|
};
|
|
|
|
|
2019-04-12 16:12:49 +02:00
|
|
|
assert(!tag.cancellation);
|
2018-01-17 06:23:15 +01:00
|
|
|
tag.cancellation = std::make_unique<char[]>(cancellation_size);
|
|
|
|
char *ptr{tag.cancellation.get()};
|
|
|
|
|
|
|
|
const mutable_buffer out_head{ptr, size(request.out.head)};
|
2018-01-17 14:58:44 +01:00
|
|
|
tag.request->out.head = out_head;
|
2018-01-17 06:23:15 +01:00
|
|
|
ptr += size(out_head);
|
|
|
|
|
|
|
|
const mutable_buffer out_content{ptr, size(request.out.content)};
|
2018-01-17 14:58:44 +01:00
|
|
|
tag.request->out.content = out_content;
|
2018-01-17 06:23:15 +01:00
|
|
|
ptr += size(out_content);
|
|
|
|
|
|
|
|
const mutable_buffer in_head{ptr, size(request.in.head)};
|
2018-01-17 14:58:44 +01:00
|
|
|
tag.request->in.head = in_head;
|
2018-01-17 06:23:15 +01:00
|
|
|
ptr += size(in_head);
|
|
|
|
|
|
|
|
const mutable_buffer in_content{ptr, size(request.in.content)};
|
2018-04-27 06:09:49 +02:00
|
|
|
// The nullity (btw that's a real word) of in.content has to be preserved
|
|
|
|
// between the user's tag and the cancellation tag. This is important for
|
|
|
|
// a dynamic chunked encoded response which has null in.content until done.
|
|
|
|
if(!null(request.in.content))
|
|
|
|
{
|
|
|
|
tag.request->in.content = in_content;
|
|
|
|
ptr += size(in_content);
|
|
|
|
}
|
2018-05-02 21:00:32 +02:00
|
|
|
else tag.request->in.content = request.in.content;
|
2018-01-17 06:23:15 +01:00
|
|
|
|
|
|
|
assert(size_t(std::distance(tag.cancellation.get(), ptr)) == cancellation_size);
|
|
|
|
|
|
|
|
// If the head is not completely written we have to copy the remainder from where
|
|
|
|
// the socket left off.
|
2018-03-20 04:37:28 +01:00
|
|
|
if(tag.state.written < size(request.out.head))
|
2018-01-17 06:23:15 +01:00
|
|
|
{
|
|
|
|
const const_buffer src
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
data(request.out.head) + tag.state.written, size(request.out.head) - tag.state.written
|
2018-01-17 06:23:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer dst
|
|
|
|
{
|
2018-03-20 22:09:32 +01:00
|
|
|
out_head + tag.state.written
|
2018-01-17 06:23:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
copy(dst, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the content is not completely written we have to copy the remainder from where
|
|
|
|
// the socket left off.
|
|
|
|
const size_t content_written
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
tag.state.written > size(request.out.head)? tag.state.written - size(request.out.head) : 0
|
2018-01-17 06:23:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(content_written < size(request.out.content))
|
|
|
|
{
|
|
|
|
const const_buffer src
|
|
|
|
{
|
|
|
|
data(request.out.content) + content_written, size(request.out.content) - content_written
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer dst
|
|
|
|
{
|
2018-03-20 22:09:32 +01:00
|
|
|
out_content + content_written
|
2018-01-17 06:23:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
copy(dst, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the head is not completely read we have to copy what's been received so far so
|
|
|
|
// we can parse a coherent head.
|
2018-03-20 04:37:28 +01:00
|
|
|
if(tag.state.head_read > 0 && tag.state.head_read < size(request.in.head))
|
2018-01-17 06:23:15 +01:00
|
|
|
{
|
|
|
|
const const_buffer src
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
data(request.in.head), tag.state.head_read
|
2018-01-17 06:23:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer dst
|
|
|
|
{
|
2018-03-20 22:09:32 +01:00
|
|
|
data(in_head), size(in_head)
|
2018-01-17 06:23:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
copy(dst, src);
|
|
|
|
}
|
|
|
|
|
2018-03-20 22:10:26 +01:00
|
|
|
// Normally we have no reason to copy content, but there is one exception:
|
|
|
|
// If the content is chunked encoding and the tag is in the phase of
|
|
|
|
// receiving the chunk head we have to copy what's been received of that
|
|
|
|
// head so far so the grammar can parse a coherent head to continue.
|
2018-04-26 03:18:09 +02:00
|
|
|
if(tag.state.chunk_length == size_t(-1) && !null(request.in.content))
|
2018-03-20 22:10:26 +01:00
|
|
|
{
|
2019-06-29 04:17:54 +02:00
|
|
|
assert(tag.state.content_read >= tag.state.content_length);
|
2018-03-20 22:10:26 +01:00
|
|
|
const const_buffer src
|
|
|
|
{
|
|
|
|
data(request.in.content) + tag.state.content_length,
|
|
|
|
tag.state.content_read - tag.state.content_length
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer dst
|
|
|
|
{
|
|
|
|
in_content + tag.state.content_length
|
|
|
|
};
|
|
|
|
|
|
|
|
copy(dst, src);
|
|
|
|
}
|
2018-04-26 03:18:09 +02:00
|
|
|
|
|
|
|
// Moving the dynamic buffer should have no real effect because the
|
|
|
|
// cancellation buffer already took over for it. We could do it anyway
|
|
|
|
// to prevent regressions but at the cost of maintaining twice the memory
|
|
|
|
// allocated. For now it's commented to let it die with the user's req.
|
|
|
|
//tag.request->in.dynamic = std::move(request.in.dynamic);
|
|
|
|
|
|
|
|
// Moving the chunk vector is important to maintain the state of dynamic
|
|
|
|
// chunk transfers through this cancel. There is no condition here for if
|
|
|
|
// this is not a dynamic chunk transfer because it's trivial.
|
|
|
|
tag.request->in.chunks = std::move(request.in.chunks);
|
2018-01-17 06:23:15 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
|
|
|
ircd::server::associate(request &request,
|
|
|
|
tag &tag)
|
|
|
|
{
|
|
|
|
assert(request.tag == nullptr);
|
|
|
|
assert(tag.request == nullptr);
|
|
|
|
|
|
|
|
auto &future
|
|
|
|
{
|
|
|
|
static_cast<ctx::future<http::code> &>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
future = tag.p;
|
|
|
|
request.tag = &tag;
|
|
|
|
tag.request = &request;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::associate(request &request,
|
|
|
|
tag &cur,
|
|
|
|
tag &&old)
|
2018-01-16 13:01:26 +01:00
|
|
|
noexcept
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
assert(request.tag == &old); // ctor moved
|
|
|
|
assert(cur.request == &request); // ctor moved
|
|
|
|
assert(old.request == &request); // ctor didn't trash old
|
|
|
|
|
|
|
|
cur.request = &request;
|
|
|
|
old.request = nullptr;
|
|
|
|
request.tag = &cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::associate(request &cur,
|
|
|
|
tag &tag,
|
|
|
|
request &&old)
|
2018-01-16 13:01:26 +01:00
|
|
|
noexcept
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
assert(tag.request == &old); // ctor already moved
|
|
|
|
assert(cur.tag == &tag); // ctor already moved
|
|
|
|
assert(old.tag == &tag); // ctor didn't trash old
|
|
|
|
|
|
|
|
cur.tag = &tag;
|
|
|
|
tag.request = &cur;
|
|
|
|
old.tag = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::disassociate(request &request,
|
|
|
|
tag &tag)
|
|
|
|
{
|
|
|
|
assert(request.tag == &tag);
|
|
|
|
assert(tag.request == &request);
|
2018-04-07 13:01:09 +02:00
|
|
|
assert(tag.abandoned());
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
request.tag = nullptr;
|
|
|
|
tag.request = nullptr;
|
2018-01-17 06:23:15 +01:00
|
|
|
|
|
|
|
// If the original request was canceled a new request was attached in its
|
|
|
|
// place in addition to an cancellation buffer. The existence of this
|
|
|
|
// cancellation buffer indicates that we must delete the request here.
|
|
|
|
// This is a little hacky but it gets the job done.
|
|
|
|
if(bool(tag.cancellation))
|
|
|
|
delete &request;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::tag::wrote_buffer(const const_buffer &buffer)
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
2018-03-20 04:37:28 +01:00
|
|
|
state.written += size(buffer);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-03-20 04:37:28 +01:00
|
|
|
if(state.written <= size(req.out.head))
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-21 14:04:48 +01:00
|
|
|
assert(data(buffer) >= begin(req.out.head));
|
|
|
|
assert(data(buffer) < end(req.out.head));
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2018-03-20 04:37:28 +01:00
|
|
|
else if(state.written <= size(req.out.head) + size(req.out.content))
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-21 14:04:48 +01:00
|
|
|
assert(data(buffer) >= begin(req.out.content));
|
|
|
|
assert(data(buffer) < end(req.out.content));
|
2018-03-27 02:11:39 +02:00
|
|
|
assert(state.written <= write_size());
|
2018-01-21 14:01:25 +01:00
|
|
|
|
|
|
|
// Invoke the user's optional progress callback; this function
|
|
|
|
// should be marked noexcept and has no reason to throw yet.
|
|
|
|
if(req.out.progress)
|
2018-03-20 04:37:28 +01:00
|
|
|
req.out.progress(buffer, const_buffer{data(req.out.content), state.written});
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2018-01-16 05:04:45 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::make_write_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
return
|
2018-03-20 04:37:28 +01:00
|
|
|
state.written < size(req.out.head)?
|
2018-01-16 05:04:45 +01:00
|
|
|
make_write_head_buffer():
|
|
|
|
|
2018-03-20 04:37:28 +01:00
|
|
|
state.written < size(req.out.head) + size(req.out.content)?
|
2018-01-16 05:04:45 +01:00
|
|
|
make_write_content_buffer():
|
|
|
|
|
|
|
|
const_buffer{};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::make_write_head_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
|
|
|
|
|
|
|
const size_t remain
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
size(req.out.head) - state.written
|
2018-01-16 05:04:45 +01:00
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
const const_buffer window
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
data(req.out.head) + state.written, remain
|
2018-01-16 05:04:45 +01:00
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::make_write_content_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
2018-03-20 04:37:28 +01:00
|
|
|
assert(state.written >= size(req.out.head));
|
2018-01-16 05:04:45 +01:00
|
|
|
|
|
|
|
const size_t content_offset
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
state.written - size(req.out.head)
|
2018-01-16 05:04:45 +01:00
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
const size_t remain
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
size(req.out.head) + size(req.out.content) - state.written
|
2018-01-16 05:04:45 +01:00
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
const const_buffer window
|
|
|
|
{
|
|
|
|
data(req.out.content) + content_offset, remain
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
return window;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 00:07:42 +01:00
|
|
|
/// Called by the controller of the socket with a view of the data received by
|
|
|
|
/// the socket. The location and size of `buffer` is the same or smaller than
|
|
|
|
/// the buffer previously supplied by make_read_buffer().
|
|
|
|
///
|
|
|
|
/// Sometimes make_read_buffer() supplies a buffer that is too large, and some
|
|
|
|
/// data read off the socket does not belong to this tag. In that case, This
|
|
|
|
/// function returns a const_buffer viewing the portion of `buffer` which is
|
|
|
|
/// considered the "overrun," and the socket controller will copy that over to
|
|
|
|
/// the next tag.
|
|
|
|
///
|
|
|
|
/// The tag indicates it is entirely finished with receiving its data by
|
|
|
|
/// setting the value of `done` to true. Otherwise it is assumed false.
|
|
|
|
///
|
|
|
|
/// The link argument is not to be used to control/modify the link from the
|
|
|
|
/// tag; it's only a backreference to flash information to the link/peer
|
|
|
|
/// through specific callbacks so the peer can learn information.
|
|
|
|
///
|
|
|
|
ircd::const_buffer
|
2019-04-11 04:25:27 +02:00
|
|
|
__attribute__((stack_protect))
|
2018-03-20 00:07:42 +01:00
|
|
|
ircd::server::tag::read_buffer(const const_buffer &buffer,
|
|
|
|
bool &done,
|
|
|
|
link &link)
|
|
|
|
{
|
|
|
|
assert(request);
|
2019-04-25 01:29:29 +02:00
|
|
|
const bool chunk_header_mode
|
|
|
|
{
|
|
|
|
state.chunk_length == size_t(-1)
|
|
|
|
};
|
2018-03-20 00:07:42 +01:00
|
|
|
|
2019-04-10 05:22:06 +02:00
|
|
|
if(empty(buffer))
|
|
|
|
return buffer;
|
|
|
|
|
2018-03-20 09:24:42 +01:00
|
|
|
if(state.status == (http::code)0)
|
2018-03-20 04:01:56 +01:00
|
|
|
return read_head(buffer, done, link);
|
2018-03-20 00:07:42 +01:00
|
|
|
|
2019-04-25 01:29:29 +02:00
|
|
|
if(chunk_header_mode && null(request->in.content))
|
2018-04-26 03:18:09 +02:00
|
|
|
return read_chunk_dynamic_head(buffer, done);
|
|
|
|
|
2019-04-25 01:29:29 +02:00
|
|
|
if(chunk_header_mode)
|
2018-03-20 09:27:41 +01:00
|
|
|
return read_chunk_head(buffer, done);
|
|
|
|
|
2018-04-26 03:18:09 +02:00
|
|
|
if(state.chunk_length && null(request->in.content))
|
|
|
|
return read_chunk_dynamic_content(buffer, done);
|
|
|
|
|
2018-03-20 09:27:41 +01:00
|
|
|
if(state.chunk_length)
|
|
|
|
return read_chunk_content(buffer, done);
|
|
|
|
|
2018-03-20 04:01:56 +01:00
|
|
|
return read_content(buffer, done);
|
2018-03-20 00:07:42 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::read_head(const const_buffer &buffer,
|
2018-01-24 18:15:57 +01:00
|
|
|
bool &done,
|
|
|
|
link &link)
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
auto &req{*request};
|
|
|
|
|
|
|
|
// informal search for head terminator
|
|
|
|
const auto pos
|
|
|
|
{
|
2019-06-29 03:23:43 +02:00
|
|
|
string_view{buffer}.find(http::headers::terminator)
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// No terminator found; account for what was received in this buffer
|
|
|
|
// for the next call to make_head_buffer() preparing for the subsequent
|
|
|
|
// invocation of this function with more data.
|
|
|
|
if(pos == string_view::npos)
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
state.head_read += size(buffer);
|
2018-01-16 03:04:23 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// This indicates how much head was just received from this buffer only,
|
|
|
|
// including the terminator which is considered part of the dome.
|
2018-01-16 03:04:23 +01:00
|
|
|
const size_t addl_head_bytes
|
|
|
|
{
|
2019-06-29 03:23:43 +02:00
|
|
|
pos + size(http::headers::terminator)
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// The received buffer may go past the end of the head.
|
2019-06-29 04:17:54 +02:00
|
|
|
assert(size(buffer) >= addl_head_bytes);
|
2018-01-16 08:41:24 +01:00
|
|
|
const size_t beyond_head_len
|
|
|
|
{
|
|
|
|
size(buffer) - addl_head_bytes
|
|
|
|
};
|
|
|
|
|
|
|
|
// The final update for the confirmed length of the head.
|
2018-03-20 04:37:28 +01:00
|
|
|
state.head_read += addl_head_bytes;
|
|
|
|
const size_t &head_read{state.head_read};
|
2018-01-16 08:41:24 +01:00
|
|
|
assert(head_read + beyond_head_len <= size(req.in.head));
|
|
|
|
|
|
|
|
// Window on any data in the buffer after the head.
|
|
|
|
const const_buffer beyond_head
|
|
|
|
{
|
|
|
|
data(req.in.head) + head_read, beyond_head_len
|
|
|
|
};
|
|
|
|
|
2018-01-20 11:30:20 +01:00
|
|
|
// Before changing the user's head buffer, we branch for a feature that
|
|
|
|
// allows the user to receive head and content into a single contiguous
|
|
|
|
// buffer by assigning in.content = in.head.
|
|
|
|
const bool contiguous
|
|
|
|
{
|
|
|
|
data(req.in.content) == data(req.in.head)
|
|
|
|
};
|
|
|
|
|
2018-03-20 09:26:42 +01:00
|
|
|
// Alternatively branch for a feature that allows dynamic allocation of
|
|
|
|
// the content buffer if the user did not specify any buffer.
|
|
|
|
const bool dynamic
|
2018-01-20 11:30:20 +01:00
|
|
|
{
|
2018-03-20 09:26:42 +01:00
|
|
|
!contiguous && empty(req.in.content)
|
|
|
|
};
|
2018-01-20 11:30:20 +01:00
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// Resize the user's head buffer tight to the head; this is how we convey
|
|
|
|
// the size of the dome back to the user.
|
2018-04-26 03:18:09 +02:00
|
|
|
state.head_rem = size(req.in.head) - head_read;
|
2018-01-16 08:41:24 +01:00
|
|
|
req.in.head = mutable_buffer
|
|
|
|
{
|
|
|
|
data(req.in.head), head_read
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
// Setup the capstan and mark the end of the tape
|
2018-01-16 08:41:24 +01:00
|
|
|
parse::buffer pb{req.in.head};
|
2018-01-16 03:04:23 +01:00
|
|
|
parse::capstan pc{pb};
|
2018-01-16 08:41:24 +01:00
|
|
|
pc.read += size(req.in.head);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// Play the tape through the formal grammar.
|
|
|
|
const http::response::head head{pc};
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(pb.completed() == head_read);
|
2018-03-20 04:37:28 +01:00
|
|
|
state.status = http::status(head.status);
|
|
|
|
state.content_length = head.content_length;
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-03-20 00:07:42 +01:00
|
|
|
// Proffer the HTTP head to the peer instance which owns the link working
|
|
|
|
// this tag so it can learn from any header data.
|
2018-03-05 15:59:10 +01:00
|
|
|
assert(link.peer);
|
|
|
|
link.peer->handle_head_recv(link, *this, head);
|
2018-01-24 18:15:57 +01:00
|
|
|
|
2018-03-20 09:26:42 +01:00
|
|
|
if(contiguous)
|
|
|
|
{
|
|
|
|
const auto content_max
|
|
|
|
{
|
|
|
|
std::max(ssize_t(size(req.in.content) - head_read), ssize_t(0))
|
|
|
|
};
|
|
|
|
|
|
|
|
req.in.content = mutable_buffer
|
|
|
|
{
|
|
|
|
data(req.in.head) + head_read, size_t(content_max)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:27:41 +01:00
|
|
|
// Branch for starting chunked encoding. We feed it whatever we have from
|
|
|
|
// beyond the head as whole or part (or none) of the first chunk. Similar
|
|
|
|
// to the non-chunked routine below, beyond_head may include all of the
|
|
|
|
// chunk content and then part of the next message too: read_chunk_head
|
|
|
|
// will return anything beyond this message as overrun and indicate done.
|
|
|
|
if(head.transfer_encoding == "chunked")
|
|
|
|
{
|
2018-04-26 00:10:04 +02:00
|
|
|
if(dynamic)
|
|
|
|
{
|
|
|
|
assert(req.opt);
|
|
|
|
req.in.chunks.reserve(req.opt->chunks_reserve);
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:27:41 +01:00
|
|
|
const const_buffer chunk
|
|
|
|
{
|
2018-04-26 03:18:09 +02:00
|
|
|
!dynamic?
|
|
|
|
const_buffer{data(req.in.content), move(req.in.content, beyond_head)}:
|
|
|
|
beyond_head
|
2018-03-20 09:27:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
state.chunk_length = -1;
|
|
|
|
const const_buffer overrun
|
|
|
|
{
|
2018-04-26 03:18:09 +02:00
|
|
|
!dynamic?
|
|
|
|
read_chunk_head(chunk, done):
|
|
|
|
read_chunk_dynamic_head(chunk, done)
|
2018-03-20 09:27:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(empty(overrun) || done == true);
|
|
|
|
return overrun;
|
|
|
|
}
|
|
|
|
|
2018-03-20 00:07:42 +01:00
|
|
|
// If no branch taken the rest of this function expects a content length
|
|
|
|
// to be known from the received head.
|
|
|
|
if(head.transfer_encoding)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Unsupported transfer-encoding '%s'", head.transfer_encoding
|
|
|
|
};
|
|
|
|
|
2018-03-20 09:26:42 +01:00
|
|
|
if(dynamic)
|
|
|
|
{
|
|
|
|
assert(req.opt);
|
|
|
|
const size_t alloc_size
|
|
|
|
{
|
|
|
|
std::min(state.content_length, req.opt->content_length_maxalloc)
|
|
|
|
};
|
|
|
|
|
|
|
|
req.in.dynamic = unique_buffer<mutable_buffer>{alloc_size};
|
|
|
|
req.in.content = req.in.dynamic;
|
|
|
|
}
|
|
|
|
|
2018-03-20 00:07:42 +01:00
|
|
|
// Now we check how much content was received beyond the head
|
2018-01-16 08:41:24 +01:00
|
|
|
const size_t &content_read
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
std::min(state.content_length, beyond_head_len)
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-21 13:29:58 +01:00
|
|
|
// Now we know how much bleed into the next message was also received
|
|
|
|
assert(beyond_head_len >= content_read);
|
|
|
|
const size_t beyond_content_len
|
|
|
|
{
|
|
|
|
beyond_head_len - content_read
|
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
const const_buffer partial_content
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
data(req.in.head) + head_read, content_read
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-21 13:29:58 +01:00
|
|
|
// Anything remaining is not our response and must be given back.
|
|
|
|
const const_buffer overrun
|
|
|
|
{
|
|
|
|
data(beyond_head) + size(partial_content), beyond_content_len
|
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// Reduce the user's content buffer to the content-length. This is sort of
|
|
|
|
// how we convey the content-length back to the user. The buffer size will
|
|
|
|
// eventually reflect how much content was actually received; the user can
|
|
|
|
// find the given content-length by parsing the header.
|
|
|
|
req.in.content = mutable_buffer
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
data(req.in.content), std::min(state.content_length, size(req.in.content))
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Any partial content was written to the head buffer by accident,
|
2018-01-21 13:29:58 +01:00
|
|
|
// that may have to be copied over to the content buffer.
|
|
|
|
if(!empty(partial_content) && !contiguous)
|
|
|
|
copy(req.in.content, partial_content);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-21 13:29:58 +01:00
|
|
|
// Invoke the read_content() routine which will increment this->content_read
|
|
|
|
read_content(partial_content, done);
|
2018-03-20 04:37:28 +01:00
|
|
|
assert(state.content_read == size(partial_content));
|
|
|
|
assert(state.content_read == state.content_length || !done);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
return overrun;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::read_content(const const_buffer &buffer,
|
|
|
|
bool &done)
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
auto &req{*request};
|
2018-01-16 08:41:24 +01:00
|
|
|
const auto &content{req.in.content};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
// The amount of remaining content for the response sequence
|
2018-03-20 04:37:28 +01:00
|
|
|
assert(size(content) + content_overflow() >= state.content_read);
|
|
|
|
assert(size(content) + content_overflow() == state.content_length);
|
2018-01-16 03:04:23 +01:00
|
|
|
const size_t remaining
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
size(content) + content_overflow() - state.content_read
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// The amount of content read in this buffer only.
|
|
|
|
const size_t addl_content_read
|
|
|
|
{
|
|
|
|
std::min(size(buffer), remaining)
|
|
|
|
};
|
|
|
|
|
2018-03-20 04:37:28 +01:00
|
|
|
state.content_read += addl_content_read;
|
2018-01-16 08:41:24 +01:00
|
|
|
assert(size(buffer) - addl_content_read == 0);
|
2018-03-20 04:37:28 +01:00
|
|
|
assert(state.content_read <= size(content) + content_overflow());
|
|
|
|
assert(state.content_read <= state.content_length);
|
2018-01-21 13:37:24 +01:00
|
|
|
|
|
|
|
// Invoke the user's optional progress callback; this function
|
|
|
|
// should be marked noexcept for the time being.
|
|
|
|
if(req.in.progress)
|
2018-03-20 04:37:28 +01:00
|
|
|
req.in.progress(buffer, const_buffer{data(content), state.content_read});
|
2018-01-21 13:37:24 +01:00
|
|
|
|
2019-06-26 06:07:50 +02:00
|
|
|
// Not finished with content
|
|
|
|
if(likely(state.content_read != size(content) + content_overflow()))
|
|
|
|
return {};
|
2019-03-09 01:52:48 +01:00
|
|
|
|
2019-06-26 06:07:50 +02:00
|
|
|
assert(!done);
|
|
|
|
done = true;
|
|
|
|
|
|
|
|
assert(req.opt);
|
|
|
|
assert(state.content_read == state.content_length);
|
|
|
|
if(content_overflow() && !req.opt->truncate_content)
|
|
|
|
{
|
|
|
|
assert(state.content_read > size(content));
|
|
|
|
set_exception<buffer_overrun>
|
|
|
|
(
|
|
|
|
"buffer of %zu bytes too small for content-length %zu bytes by %zu bytes",
|
|
|
|
size(content),
|
|
|
|
state.content_length,
|
|
|
|
content_overflow()
|
|
|
|
);
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2019-06-26 06:07:50 +02:00
|
|
|
else set_value(state.status);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
return {};
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
2019-06-26 06:07:50 +02:00
|
|
|
//
|
|
|
|
// chunked encoding into fixed-size buffers
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::server
|
|
|
|
{
|
|
|
|
static void chunk_content_completed(tag &, bool &done);
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:27:41 +01:00
|
|
|
ircd::const_buffer
|
2019-04-11 04:25:27 +02:00
|
|
|
__attribute__((stack_protect))
|
2018-03-20 09:27:41 +01:00
|
|
|
ircd::server::tag::read_chunk_head(const const_buffer &buffer,
|
2018-04-26 05:50:24 +02:00
|
|
|
bool &done,
|
|
|
|
const uint8_t recursion_level)
|
2018-03-20 09:27:41 +01:00
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
auto &req{*request};
|
|
|
|
const auto &content{req.in.content};
|
|
|
|
|
|
|
|
// informal search for head terminator
|
|
|
|
const auto pos
|
|
|
|
{
|
2019-06-29 03:23:43 +02:00
|
|
|
string_view{buffer}.find(http::line::terminator)
|
2018-03-20 09:27:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(pos == string_view::npos)
|
|
|
|
{
|
|
|
|
state.content_read += size(buffer);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// This indicates how much head was just received from this buffer only.
|
|
|
|
const size_t addl_head_bytes
|
|
|
|
{
|
2019-06-29 03:23:43 +02:00
|
|
|
pos + size(http::line::terminator)
|
2018-03-20 09:27:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// The received buffer may go past the end of the head.
|
|
|
|
assert(addl_head_bytes <= size(buffer));
|
|
|
|
const size_t beyond_head_length
|
|
|
|
{
|
|
|
|
size(buffer) - addl_head_bytes
|
|
|
|
};
|
|
|
|
|
|
|
|
// The total head length is found from the end of the last chunk content
|
|
|
|
state.content_read += addl_head_bytes;
|
|
|
|
assert(state.content_read > state.content_length);
|
|
|
|
const size_t head_length
|
|
|
|
{
|
|
|
|
state.content_read - state.content_length
|
|
|
|
};
|
|
|
|
|
|
|
|
// Window on any data in the buffer after the head.
|
|
|
|
const const_buffer beyond_head
|
|
|
|
{
|
|
|
|
data(content) + state.content_length + head_length, beyond_head_length
|
|
|
|
};
|
|
|
|
|
|
|
|
// Setup the capstan and mark the end of the tape
|
|
|
|
parse::buffer pb
|
|
|
|
{
|
|
|
|
mutable_buffer
|
|
|
|
{
|
|
|
|
data(content) + state.content_length, head_length
|
|
|
|
}
|
|
|
|
};
|
|
|
|
parse::capstan pc{pb};
|
|
|
|
pc.read += head_length;
|
|
|
|
|
|
|
|
// Play the tape through the formal grammar.
|
|
|
|
const http::response::chunk chunk{pc};
|
2019-06-29 03:23:43 +02:00
|
|
|
state.chunk_length = chunk.size + size(http::line::terminator);
|
2018-03-20 09:27:41 +01:00
|
|
|
|
|
|
|
// Now we check how much chunk was received beyond the head
|
2018-04-26 00:10:04 +02:00
|
|
|
const auto &chunk_read
|
2018-03-20 09:27:41 +01:00
|
|
|
{
|
|
|
|
std::min(state.chunk_length, beyond_head_length)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Now we know how much bleed into the next message was also received
|
|
|
|
assert(beyond_head_length >= chunk_read);
|
|
|
|
const size_t beyond_chunk_length
|
|
|
|
{
|
|
|
|
beyond_head_length - chunk_read
|
|
|
|
};
|
|
|
|
|
|
|
|
// Finally we erase the chunk head by replacing it with everything received
|
|
|
|
// after it.
|
|
|
|
const mutable_buffer target
|
|
|
|
{
|
|
|
|
data(content) + state.content_length, beyond_head_length
|
|
|
|
};
|
|
|
|
|
|
|
|
move(target, beyond_head);
|
|
|
|
|
|
|
|
// Increment the content_length to now include this chunk
|
|
|
|
state.content_length += state.chunk_length;
|
|
|
|
|
|
|
|
// Adjust the content_read to erase the chunk head.
|
|
|
|
state.content_read -= head_length;
|
|
|
|
|
|
|
|
const const_buffer partial_chunk
|
|
|
|
{
|
|
|
|
data(target), chunk_read
|
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer overrun
|
|
|
|
{
|
|
|
|
data(target) + chunk_read, beyond_chunk_length
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(state.chunk_length >= 2);
|
|
|
|
read_chunk_content(partial_chunk, done);
|
|
|
|
|
2019-06-29 04:32:52 +02:00
|
|
|
if(done || empty(overrun))
|
2018-03-20 09:27:41 +01:00
|
|
|
return overrun;
|
|
|
|
|
2018-04-26 05:50:24 +02:00
|
|
|
// Prevent stack overflow from lots of tiny chunks nagled together.
|
|
|
|
if(unlikely(recursion_level >= 32))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Chunking recursion limit exceeded"
|
|
|
|
};
|
|
|
|
|
|
|
|
return read_chunk_head(overrun, done, recursion_level + 1);
|
2018-03-20 09:27:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::read_chunk_content(const const_buffer &buffer,
|
|
|
|
bool &done)
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
auto &req{*request};
|
|
|
|
const auto &content{req.in.content};
|
|
|
|
|
|
|
|
// The amount of remaining content for the response sequence
|
|
|
|
const size_t remaining
|
|
|
|
{
|
|
|
|
content_remaining()
|
|
|
|
};
|
|
|
|
|
|
|
|
// The amount of content read in this buffer only.
|
|
|
|
const size_t addl_content_read
|
|
|
|
{
|
|
|
|
std::min(size(buffer), remaining)
|
|
|
|
};
|
|
|
|
|
2018-04-26 00:10:04 +02:00
|
|
|
// Increment the read counters for this chunk and all chunks.
|
|
|
|
state.chunk_read += addl_content_read;
|
2018-03-20 09:27:41 +01:00
|
|
|
state.content_read += addl_content_read;
|
2018-04-26 00:10:04 +02:00
|
|
|
assert(state.chunk_read <= state.content_read);
|
|
|
|
|
2019-06-29 03:13:25 +02:00
|
|
|
// Invoke the user's optional progress callback; this function
|
|
|
|
// should be marked noexcept for the time being.
|
|
|
|
if(req.in.progress && !done)
|
|
|
|
req.in.progress(buffer, const_buffer{data(content), state.content_read});
|
|
|
|
|
2019-06-26 06:07:50 +02:00
|
|
|
// This branch is taken at the completion of a chunk. The size
|
|
|
|
// all the buffers is rolled back to hide the terminator so it's
|
|
|
|
// either ignored or overwritten so it doesn't leak to the user.
|
2018-03-20 09:27:41 +01:00
|
|
|
if(state.content_read == state.content_length)
|
2019-06-26 06:07:50 +02:00
|
|
|
chunk_content_completed(*this, done);
|
2018-03-20 09:27:41 +01:00
|
|
|
|
2019-06-26 06:07:50 +02:00
|
|
|
// Not finished
|
|
|
|
if(likely(state.content_read != state.content_length))
|
|
|
|
return {};
|
2018-03-20 09:27:41 +01:00
|
|
|
|
2019-06-26 06:07:50 +02:00
|
|
|
assert(state.chunk_read == state.chunk_length);
|
|
|
|
assert(state.chunk_read <= state.content_read);
|
|
|
|
state.chunk_length = size_t(-1);
|
|
|
|
state.chunk_read = 0;
|
2018-03-20 09:27:41 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-06-26 06:07:50 +02:00
|
|
|
void
|
|
|
|
ircd::server::chunk_content_completed(tag &tag,
|
|
|
|
bool &done)
|
|
|
|
{
|
|
|
|
assert(tag.request);
|
|
|
|
auto &req{*tag.request};
|
|
|
|
auto &state{tag.state};
|
2019-06-26 09:18:13 +02:00
|
|
|
|
|
|
|
// Remove the terminator from the total length state.
|
2019-06-29 03:23:43 +02:00
|
|
|
assert(state.content_length >= size(http::line::terminator));
|
|
|
|
state.content_length -= size(http::line::terminator);
|
|
|
|
state.content_read -= size(http::line::terminator);
|
2019-06-26 06:07:50 +02:00
|
|
|
|
2019-06-26 09:18:13 +02:00
|
|
|
// Remove the terminator from the chunk length state.
|
2019-06-26 06:07:50 +02:00
|
|
|
assert(state.chunk_length >= 2);
|
|
|
|
assert(state.chunk_read == state.chunk_length);
|
2019-06-29 03:23:43 +02:00
|
|
|
state.chunk_length -= size(http::line::terminator);
|
|
|
|
state.chunk_read -= size(http::line::terminator);
|
2019-06-26 06:07:50 +02:00
|
|
|
|
2019-06-26 09:18:13 +02:00
|
|
|
// State sanity tests
|
|
|
|
assert(state.content_length >= state.content_read);
|
|
|
|
assert(state.content_length >= state.chunk_length);
|
|
|
|
assert(state.content_length >= state.chunk_read);
|
|
|
|
assert(state.content_read >= state.chunk_length);
|
|
|
|
assert(state.content_read >= state.chunk_read);
|
|
|
|
assert(state.chunk_length >= state.chunk_read);
|
|
|
|
if(state.chunk_length > 0)
|
2019-06-26 06:07:50 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
assert(state.chunk_read == 0);
|
|
|
|
assert(!done);
|
|
|
|
done = true;
|
|
|
|
req.in.content = mutable_buffer{data(req.in.content), state.content_length};
|
|
|
|
tag.set_value(state.status);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// chunked encoding into dynamic memories
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::server
|
|
|
|
{
|
|
|
|
static void chunk_dynamic_contiguous_copy(struct tag::state &, request &);
|
|
|
|
static void chunk_dynamic_content_completed(tag &, bool &done);
|
|
|
|
}
|
|
|
|
|
2018-04-26 03:18:09 +02:00
|
|
|
ircd::const_buffer
|
2019-04-11 04:25:27 +02:00
|
|
|
__attribute__((stack_protect))
|
2018-04-26 03:18:09 +02:00
|
|
|
ircd::server::tag::read_chunk_dynamic_head(const const_buffer &buffer,
|
2018-04-26 05:50:24 +02:00
|
|
|
bool &done,
|
|
|
|
const uint8_t recursion_level)
|
2018-04-26 03:18:09 +02:00
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
auto &req{*request};
|
2019-06-29 04:17:54 +02:00
|
|
|
assert(null(req.in.content)); // dynamic chunk mode
|
|
|
|
assert(state.chunk_length == size_t(-1)); // chunk head mode
|
2018-04-26 03:18:09 +02:00
|
|
|
|
|
|
|
// informal search for head terminator
|
|
|
|
const auto pos
|
|
|
|
{
|
2019-06-29 03:23:43 +02:00
|
|
|
string_view{buffer}.find(http::line::terminator)
|
2018-04-26 03:18:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(pos == string_view::npos)
|
|
|
|
{
|
|
|
|
state.chunk_read += size(buffer);
|
|
|
|
state.content_read += size(buffer);
|
2019-06-29 04:17:54 +02:00
|
|
|
assert(state.content_read == state.content_length + state.chunk_read);
|
2018-04-26 03:18:09 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// This indicates how much head was just received from this buffer only.
|
|
|
|
const size_t addl_head_bytes
|
|
|
|
{
|
2019-06-29 03:23:43 +02:00
|
|
|
pos + size(http::line::terminator)
|
2018-04-26 03:18:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// The received buffer may go past the end of the head.
|
|
|
|
assert(addl_head_bytes <= size(buffer));
|
|
|
|
const size_t beyond_head_length
|
|
|
|
{
|
|
|
|
size(buffer) - addl_head_bytes
|
|
|
|
};
|
|
|
|
|
|
|
|
state.chunk_read += addl_head_bytes;
|
2019-08-09 03:39:20 +02:00
|
|
|
state.content_read += addl_head_bytes;
|
|
|
|
const auto head_length
|
|
|
|
{
|
|
|
|
state.chunk_read
|
|
|
|
};
|
|
|
|
|
2018-04-26 03:18:09 +02:00
|
|
|
state.chunk_read = 0;
|
2019-08-09 03:39:20 +02:00
|
|
|
assert(state.content_read >= head_length);
|
|
|
|
state.content_read -= head_length;
|
2018-04-26 03:18:09 +02:00
|
|
|
|
|
|
|
// Window on any data in the buffer after the head.
|
|
|
|
const const_buffer beyond_head
|
|
|
|
{
|
|
|
|
data(buffer) + addl_head_bytes, beyond_head_length
|
|
|
|
};
|
|
|
|
|
|
|
|
// Setup the capstan and mark the end of the tape
|
|
|
|
parse::buffer pb
|
|
|
|
{
|
|
|
|
mutable_buffer
|
|
|
|
{
|
|
|
|
data(req.in.head) + state.head_read, head_length
|
|
|
|
}
|
|
|
|
};
|
|
|
|
parse::capstan pc{pb};
|
|
|
|
pc.read += head_length;
|
|
|
|
|
|
|
|
// Play the tape through the formal grammar.
|
|
|
|
const http::response::chunk chunk{pc};
|
|
|
|
assert(state.chunk_length == size_t(-1));
|
2019-06-29 03:23:43 +02:00
|
|
|
state.chunk_length = chunk.size + size(http::line::terminator);
|
2018-04-26 03:18:09 +02:00
|
|
|
|
|
|
|
// Increment the content_length to now include this chunk
|
|
|
|
state.content_length += state.chunk_length;
|
|
|
|
|
|
|
|
// Allocate the chunk content on the vector.
|
|
|
|
//TODO: maxalloc
|
|
|
|
req.in.chunks.emplace_back(state.chunk_length);
|
2019-06-29 04:17:54 +02:00
|
|
|
assert(size_chunks(req.in) == state.content_length);
|
2018-04-26 03:18:09 +02:00
|
|
|
|
|
|
|
// Now we check how much chunk was received beyond the head
|
2019-06-29 04:17:54 +02:00
|
|
|
// state.chunk_read is still 0 here because that's only incremented
|
2018-04-26 03:18:09 +02:00
|
|
|
// in the content read function.
|
2019-06-29 04:17:54 +02:00
|
|
|
assert(state.chunk_read == 0);
|
2018-04-26 03:18:09 +02:00
|
|
|
const auto &chunk_read
|
|
|
|
{
|
|
|
|
std::min(state.chunk_length, beyond_head_length)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Now we know how much bleed into the next message was also received
|
|
|
|
assert(beyond_head_length >= chunk_read);
|
|
|
|
const size_t beyond_chunk_length
|
|
|
|
{
|
|
|
|
beyond_head_length - chunk_read
|
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer partial_chunk
|
|
|
|
{
|
|
|
|
data(beyond_head), chunk_read
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t copied
|
|
|
|
{
|
|
|
|
copy(req.in.chunks.back(), partial_chunk)
|
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer overrun
|
|
|
|
{
|
|
|
|
data(beyond_head) + chunk_read, beyond_chunk_length
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(state.chunk_length >= 2);
|
|
|
|
read_chunk_dynamic_content(partial_chunk, done);
|
|
|
|
|
2019-06-29 04:32:52 +02:00
|
|
|
if(done || empty(overrun))
|
2018-04-26 03:18:09 +02:00
|
|
|
return overrun;
|
|
|
|
|
2018-04-26 05:50:24 +02:00
|
|
|
// Prevent stack overflow from lots of tiny chunks nagled together.
|
|
|
|
if(unlikely(recursion_level >= 32))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Chunking recursion limit exceeded"
|
|
|
|
};
|
|
|
|
|
|
|
|
return read_chunk_dynamic_head(overrun, done, recursion_level + 1);
|
2018-04-26 03:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::read_chunk_dynamic_content(const const_buffer &buffer,
|
|
|
|
bool &done)
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
auto &req{*request};
|
|
|
|
|
2019-06-29 04:17:54 +02:00
|
|
|
assert(state.chunk_length != size_t(-1)); // content mode
|
|
|
|
assert(null(req.in.content)); // dynamic chunk mode
|
2018-04-26 03:18:09 +02:00
|
|
|
assert(!req.in.chunks.empty());
|
|
|
|
const auto &chunk
|
|
|
|
{
|
|
|
|
req.in.chunks.back()
|
|
|
|
};
|
|
|
|
|
|
|
|
// The amount of remaining content for the response sequence
|
2019-06-29 04:17:54 +02:00
|
|
|
assert(size(chunk) >= state.chunk_read);
|
2018-04-26 03:18:09 +02:00
|
|
|
const size_t remaining
|
|
|
|
{
|
|
|
|
size(chunk) - state.chunk_read
|
|
|
|
};
|
|
|
|
|
|
|
|
// The amount of content read in this buffer only.
|
|
|
|
const size_t addl_content_read
|
|
|
|
{
|
|
|
|
std::min(size(buffer), remaining)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Increment the read counters for this chunk and all chunks.
|
|
|
|
state.chunk_read += addl_content_read;
|
|
|
|
state.content_read += addl_content_read;
|
|
|
|
assert(state.chunk_read <= state.content_read);
|
2019-06-26 09:18:13 +02:00
|
|
|
assert(state.chunk_read <= state.chunk_length);
|
2019-07-14 20:54:13 +02:00
|
|
|
assert(state.content_length >= state.content_read);
|
2018-04-26 03:18:09 +02:00
|
|
|
|
|
|
|
// Invoke the user's optional progress callback; this function
|
|
|
|
// should be marked noexcept for the time being.
|
|
|
|
if(req.in.progress && !done)
|
|
|
|
req.in.progress(buffer, const_buffer{data(chunk), state.chunk_read});
|
|
|
|
|
2019-06-29 03:13:25 +02:00
|
|
|
if(state.chunk_read == state.chunk_length)
|
|
|
|
chunk_dynamic_content_completed(*this, done);
|
|
|
|
|
2019-06-26 09:18:13 +02:00
|
|
|
assert(state.chunk_read <= state.chunk_length);
|
|
|
|
if(likely(state.chunk_read != state.chunk_length))
|
|
|
|
return {};
|
2018-04-26 03:18:09 +02:00
|
|
|
|
2019-06-26 09:18:13 +02:00
|
|
|
assert(state.chunk_read <= state.content_read);
|
|
|
|
state.chunk_length = size_t(-1);
|
|
|
|
state.chunk_read = 0;
|
2018-04-26 03:18:09 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-06-26 06:07:50 +02:00
|
|
|
void
|
|
|
|
ircd::server::chunk_dynamic_content_completed(tag &tag,
|
|
|
|
bool &done)
|
|
|
|
{
|
|
|
|
assert(tag.request);
|
|
|
|
auto &req{*tag.request};
|
|
|
|
auto &state{tag.state};
|
2019-06-26 09:18:13 +02:00
|
|
|
assert(!req.in.chunks.empty());
|
|
|
|
auto &chunk{req.in.chunks.back()};
|
|
|
|
|
|
|
|
// Remove the terminator from the total length state.
|
2019-06-29 03:23:43 +02:00
|
|
|
assert(state.content_length >= size(http::line::terminator));
|
|
|
|
assert(state.content_read >= size(http::line::terminator));
|
|
|
|
state.content_length -= size(http::line::terminator);
|
|
|
|
state.content_read -= size(http::line::terminator);
|
2019-06-26 06:07:50 +02:00
|
|
|
|
2019-06-26 09:18:13 +02:00
|
|
|
// Remove the terminator from the chunk length state.
|
2019-06-29 03:23:43 +02:00
|
|
|
assert(state.chunk_length >= size(http::line::terminator));
|
|
|
|
assert(state.chunk_read >= size(http::line::terminator));
|
2019-06-26 06:07:50 +02:00
|
|
|
assert(state.chunk_read == state.chunk_length);
|
2019-06-29 03:23:43 +02:00
|
|
|
state.chunk_length -= size(http::line::terminator);
|
|
|
|
state.chunk_read -= size(http::line::terminator);
|
2019-06-26 06:07:50 +02:00
|
|
|
|
2019-06-26 09:18:13 +02:00
|
|
|
// Remove the terminator from the end of the chunk
|
2019-06-29 03:23:43 +02:00
|
|
|
std::get<1>(chunk) -= size(http::line::terminator);
|
2019-06-26 06:07:50 +02:00
|
|
|
assert(size(chunk) == state.chunk_length);
|
|
|
|
assert(std::get<0>(chunk) <= std::get<1>(chunk));
|
2019-06-26 09:18:13 +02:00
|
|
|
|
|
|
|
// State sanity tests
|
2019-06-29 04:17:54 +02:00
|
|
|
assert(state.content_length == size_chunks(req.in));
|
2019-06-26 09:18:13 +02:00
|
|
|
assert(state.content_length >= state.content_read);
|
|
|
|
assert(state.content_length >= state.chunk_length);
|
|
|
|
assert(state.content_length >= state.chunk_read);
|
|
|
|
assert(state.content_read >= state.chunk_length);
|
|
|
|
assert(state.content_read >= state.chunk_read);
|
|
|
|
assert(state.chunk_length >= state.chunk_read);
|
|
|
|
if(state.chunk_length > 0)
|
2019-06-26 06:07:50 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
assert(state.chunk_read == 0);
|
|
|
|
assert(!done);
|
|
|
|
done = true;
|
|
|
|
|
|
|
|
assert(req.opt);
|
|
|
|
if(req.opt->contiguous_content && !req.in.chunks.empty())
|
|
|
|
chunk_dynamic_contiguous_copy(state, req);
|
|
|
|
|
|
|
|
tag.set_value(state.status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::chunk_dynamic_contiguous_copy(struct tag::state &state,
|
|
|
|
request &req)
|
|
|
|
{
|
|
|
|
assert(state.content_length == size_chunks(req.in));
|
|
|
|
assert(req.in.chunks.size() >= 1);
|
|
|
|
assert(empty(req.in.chunks.back()));
|
|
|
|
req.in.chunks.pop_back();
|
|
|
|
|
|
|
|
if(req.in.chunks.size() > 1)
|
|
|
|
{
|
|
|
|
req.in.dynamic = size_chunks(req.in);
|
|
|
|
req.in.content = req.in.dynamic;
|
|
|
|
|
|
|
|
size_t copied{0};
|
|
|
|
for(const auto &buffer : req.in.chunks)
|
|
|
|
copied += copy(req.in.content + copied, buffer);
|
|
|
|
|
|
|
|
assert(copied == size(req.in.content));
|
|
|
|
assert(copied == state.content_length);
|
|
|
|
}
|
|
|
|
else if(req.in.chunks.size() == 1)
|
|
|
|
{
|
|
|
|
req.in.dynamic = std::move(req.in.chunks.front());
|
|
|
|
req.in.content = req.in.dynamic;
|
|
|
|
assert(size(req.in.content) == state.content_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
req.in.chunks.clear();
|
|
|
|
}
|
|
|
|
|
2018-03-20 00:07:42 +01:00
|
|
|
/// An idempotent operation that provides the location of where the socket
|
|
|
|
/// should place the next received data. The tag figures this out based on
|
|
|
|
/// whether it receiving HTTP head data or whether it is in content mode.
|
|
|
|
///
|
|
|
|
ircd::mutable_buffer
|
2019-04-11 04:25:27 +02:00
|
|
|
__attribute__((stack_protect))
|
2018-03-20 00:07:42 +01:00
|
|
|
ircd::server::tag::make_read_buffer()
|
|
|
|
const
|
|
|
|
{
|
2019-04-25 01:29:29 +02:00
|
|
|
const bool chunk_header_mode
|
|
|
|
{
|
|
|
|
state.chunk_length == size_t(-1)
|
|
|
|
};
|
|
|
|
|
2019-06-18 08:07:17 +02:00
|
|
|
const bool chunk_dynamic_header_mode
|
|
|
|
{
|
|
|
|
chunk_header_mode && null(request->in.content)
|
|
|
|
};
|
|
|
|
|
2018-03-20 00:07:42 +01:00
|
|
|
assert(request);
|
2018-03-20 04:37:28 +01:00
|
|
|
assert(state.head_read <= size(request->in.head));
|
2019-04-25 03:20:47 +02:00
|
|
|
assert(state.content_read <= state.content_length + state.chunk_read);
|
2019-06-18 08:07:17 +02:00
|
|
|
assert(state.content_read <= state.content_length || chunk_dynamic_header_mode);
|
2018-03-20 00:07:42 +01:00
|
|
|
|
2018-03-20 09:24:42 +01:00
|
|
|
if(state.status == (http::code)0)
|
2018-03-20 04:01:56 +01:00
|
|
|
return make_read_head_buffer();
|
2018-03-20 00:07:42 +01:00
|
|
|
|
2019-04-25 01:29:29 +02:00
|
|
|
if(chunk_header_mode && null(request->in.content))
|
2018-04-26 03:18:09 +02:00
|
|
|
return make_read_chunk_dynamic_head_buffer();
|
2018-03-20 00:07:42 +01:00
|
|
|
|
2019-04-25 01:29:29 +02:00
|
|
|
if(chunk_header_mode)
|
2018-03-26 11:24:29 +02:00
|
|
|
return make_read_chunk_head_buffer();
|
|
|
|
|
2018-04-26 03:18:09 +02:00
|
|
|
if(state.chunk_length && null(request->in.content))
|
|
|
|
return make_read_chunk_dynamic_content_buffer();
|
|
|
|
|
2018-03-20 09:27:41 +01:00
|
|
|
if(state.chunk_length)
|
|
|
|
return make_read_chunk_content_buffer();
|
|
|
|
|
2018-04-26 03:18:09 +02:00
|
|
|
if(state.content_read >= size(request->in.content))
|
|
|
|
return make_read_discard_buffer();
|
|
|
|
|
2018-03-20 04:01:56 +01:00
|
|
|
return make_read_content_buffer();
|
2018-03-20 00:07:42 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_head_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
2018-01-16 08:41:24 +01:00
|
|
|
const auto &head{req.in.head};
|
2018-03-20 09:27:41 +01:00
|
|
|
if(unlikely(size(req.in.head) <= state.head_read))
|
2018-03-20 09:23:42 +01:00
|
|
|
throw buffer_overrun
|
|
|
|
{
|
|
|
|
"Supplied buffer of %zu too small for HTTP head", size(req.in.head)
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
const size_t remaining
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
size(head) - state.head_read
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
data(head) + state.head_read, remaining
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-04-06 09:24:17 +02:00
|
|
|
assert(size(buffer) <= size(head));
|
|
|
|
assert(size(buffer) > 0);
|
2018-01-16 03:04:23 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_content_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
2018-01-16 08:41:24 +01:00
|
|
|
const auto &content{req.in.content};
|
2018-03-20 09:23:42 +01:00
|
|
|
if(unlikely(size(content) <= state.content_read))
|
|
|
|
throw buffer_overrun
|
|
|
|
{
|
|
|
|
"Content buffer of %zu bytes too small to read %zu bytes of content",
|
|
|
|
size(content),
|
|
|
|
state.content_length
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
// The amount of bytes we still have to read to for the response
|
|
|
|
const size_t remaining
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
size(content) - state.content_read
|
2018-01-16 08:41:24 +01:00
|
|
|
};
|
|
|
|
|
2018-04-06 09:24:17 +02:00
|
|
|
assert(remaining > 0);
|
2018-03-20 04:37:28 +01:00
|
|
|
return
|
|
|
|
{
|
|
|
|
data(content) + state.content_read, remaining
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:27:41 +01:00
|
|
|
/// The chunk head buffer starts after the last chunk ended and has a size of
|
|
|
|
/// the rest of the available content buffer (hopefully much less will be
|
|
|
|
/// needed). If only part of the chunk head was received previously this
|
|
|
|
/// function accounts for that by returning a buffer which starts at the
|
|
|
|
/// content_read offset (which is at the end of that previous read).
|
|
|
|
///
|
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_chunk_head_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
assert(state.chunk_length == size_t(-1));
|
|
|
|
assert(state.content_read >= state.content_length);
|
|
|
|
|
|
|
|
const auto &req{*request};
|
|
|
|
const auto &content{req.in.content};
|
2018-04-26 03:18:09 +02:00
|
|
|
|
2018-03-20 09:27:41 +01:00
|
|
|
if(unlikely(size(content) <= state.content_read))
|
|
|
|
throw buffer_overrun
|
|
|
|
{
|
|
|
|
"Content buffer of %zu bytes too small to read next chunk header",
|
|
|
|
size(content)
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t remaining
|
|
|
|
{
|
|
|
|
size(content) - state.content_read
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
|
|
|
data(content) + state.content_read, remaining
|
|
|
|
};
|
|
|
|
|
2018-04-06 09:24:17 +02:00
|
|
|
assert(size(buffer) > 0);
|
2018-03-20 09:27:41 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_chunk_content_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
assert(state.chunk_length > 0);
|
|
|
|
assert(state.content_read <= state.content_length);
|
|
|
|
|
|
|
|
const auto &req{*request};
|
|
|
|
const auto &content{req.in.content};
|
|
|
|
|
|
|
|
assert(size(content) >= state.content_read);
|
|
|
|
const size_t buffer_remaining
|
2018-01-16 08:41:24 +01:00
|
|
|
{
|
2018-03-20 09:27:41 +01:00
|
|
|
size(content) - state.content_read
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
2018-03-20 09:27:41 +01:00
|
|
|
|
|
|
|
const size_t chunk_remaining
|
|
|
|
{
|
|
|
|
content_remaining()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(chunk_remaining <= state.chunk_length);
|
|
|
|
assert(chunk_remaining == state.content_length - state.content_read);
|
|
|
|
const size_t buffer_size
|
|
|
|
{
|
|
|
|
std::min(buffer_remaining, chunk_remaining)
|
|
|
|
};
|
|
|
|
|
2018-04-06 09:24:17 +02:00
|
|
|
if(unlikely(buffer_size < chunk_remaining))
|
|
|
|
throw buffer_overrun
|
|
|
|
{
|
|
|
|
"Content buffer of %zu bytes too small to read remaining %zu of chunk",
|
|
|
|
size(content),
|
|
|
|
chunk_remaining
|
|
|
|
};
|
|
|
|
|
2018-03-20 09:27:41 +01:00
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
|
|
|
data(content) + state.content_read, buffer_size
|
|
|
|
};
|
|
|
|
|
2018-04-06 09:24:17 +02:00
|
|
|
assert(size(buffer) > 0);
|
2018-03-20 09:27:41 +01:00
|
|
|
return buffer;
|
2018-01-16 08:41:24 +01:00
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-04-26 03:18:09 +02:00
|
|
|
/// The dynamic chunk head buffer starts after the main head and has a size
|
|
|
|
/// of the remaining main head buffer. This area is overwritten for each
|
|
|
|
/// chunk head.
|
|
|
|
///
|
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_chunk_dynamic_head_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
|
|
|
|
|
|
|
assert(state.chunk_length == size_t(-1));
|
|
|
|
assert(null(req.in.content));
|
|
|
|
assert(size(req.in.head) >= state.head_read);
|
|
|
|
|
|
|
|
const size_t head_max
|
|
|
|
{
|
|
|
|
size(req.in.head) + state.head_rem
|
|
|
|
};
|
|
|
|
|
|
|
|
// The total offset in the head buffer is the message head plus the
|
|
|
|
// amount of chunk head received so far, which is kept in chunk_read.
|
|
|
|
const size_t head_offset
|
|
|
|
{
|
|
|
|
state.head_read + state.chunk_read
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(head_max >= head_offset);
|
|
|
|
if(unlikely(head_max - head_offset <= 16))
|
|
|
|
throw buffer_overrun
|
|
|
|
{
|
|
|
|
"Remaining head buffer of %zu bytes too small to read next chunk header",
|
|
|
|
head_max - state.head_read
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t remaining
|
|
|
|
{
|
|
|
|
head_max - head_offset
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
|
|
|
data(req.in.head) + state.head_read + state.chunk_read, remaining
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(size(buffer) > 0);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_chunk_dynamic_content_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
|
|
|
|
|
|
|
assert(state.chunk_length > 0);
|
|
|
|
assert(state.content_read <= state.content_length);
|
|
|
|
assert(null(req.in.content));
|
|
|
|
assert(!req.in.chunks.empty());
|
|
|
|
const auto &buffer
|
|
|
|
{
|
|
|
|
req.in.chunks.back()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(size(buffer) == state.chunk_length);
|
|
|
|
assert(state.chunk_read <= size(buffer));
|
|
|
|
const size_t buffer_remaining
|
|
|
|
{
|
|
|
|
size(buffer) - state.chunk_read
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer ret
|
|
|
|
{
|
|
|
|
data(buffer) + state.chunk_read, buffer_remaining
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(size(ret) > 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_discard_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
2018-03-20 04:22:58 +01:00
|
|
|
assert(content_overflow() > 0);
|
2019-03-09 01:25:38 +01:00
|
|
|
assert(content_overflow() <= state.content_length);
|
2018-03-20 04:37:28 +01:00
|
|
|
assert(state.content_read >= size(request->in.content));
|
2019-03-09 01:25:38 +01:00
|
|
|
assert(state.content_length >= state.content_read);
|
2018-01-16 08:41:24 +01:00
|
|
|
const size_t remaining
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2019-03-09 01:25:38 +01:00
|
|
|
state.content_length - state.content_read
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2019-03-09 01:25:38 +01:00
|
|
|
assert(remaining <= state.content_length);
|
2018-01-16 08:41:24 +01:00
|
|
|
static char buffer[512];
|
|
|
|
const size_t buffer_max
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
std::min(remaining, sizeof(buffer))
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
return
|
|
|
|
{
|
|
|
|
buffer, buffer_max
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 04:01:56 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::tag::content_remaining()
|
|
|
|
const
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
assert(state.content_length >= state.content_read);
|
|
|
|
return state.content_length - state.content_read;
|
2018-03-20 04:01:56 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 04:22:58 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::tag::content_overflow()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
2018-03-20 09:27:41 +01:00
|
|
|
const ssize_t diff(state.content_length - size(req.in.content));
|
2018-03-20 04:22:58 +01:00
|
|
|
return std::max(diff, ssize_t(0));
|
|
|
|
}
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
template<class... args>
|
2018-01-16 13:01:26 +01:00
|
|
|
void
|
2018-01-17 06:23:15 +01:00
|
|
|
ircd::server::tag::set_value(args&&... a)
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
if(abandoned())
|
|
|
|
return;
|
2018-01-16 13:01:26 +01:00
|
|
|
|
2018-01-17 10:31:34 +01:00
|
|
|
const http::code &code
|
|
|
|
{
|
|
|
|
std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
|
2018-03-07 16:21:00 +01:00
|
|
|
assert(request->opt);
|
|
|
|
if(request->opt->http_exceptions && code >= http::code(300))
|
2018-01-17 10:31:34 +01:00
|
|
|
{
|
2018-02-27 10:38:34 +01:00
|
|
|
const string_view content
|
|
|
|
{
|
|
|
|
data(request->in.content), size(request->in.content)
|
|
|
|
};
|
|
|
|
|
2019-03-16 23:01:46 +01:00
|
|
|
set_exception<http::error>(code, std::string{content});
|
2018-01-17 10:31:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.set_value(code);
|
2019-04-18 10:19:02 +02:00
|
|
|
assert(abandoned());
|
2018-01-17 06:23:15 +01:00
|
|
|
}
|
2018-01-16 13:01:26 +01:00
|
|
|
|
2019-03-16 23:01:46 +01:00
|
|
|
template<class E,
|
|
|
|
class... args>
|
2018-01-17 06:23:15 +01:00
|
|
|
void
|
|
|
|
ircd::server::tag::set_exception(args&&... a)
|
2019-03-16 23:01:46 +01:00
|
|
|
try
|
2018-01-17 06:23:15 +01:00
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
if(abandoned())
|
|
|
|
return;
|
|
|
|
|
2019-03-16 23:01:46 +01:00
|
|
|
throw E
|
|
|
|
{
|
|
|
|
std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
set_exception(std::current_exception());
|
2018-01-16 13:01:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-07 03:37:26 +01:00
|
|
|
ircd::server::tag::set_exception(std::exception_ptr eptr)
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
if(abandoned())
|
|
|
|
return;
|
|
|
|
|
2018-01-16 13:01:26 +01:00
|
|
|
p.set_exception(std::move(eptr));
|
2019-04-18 10:19:02 +02:00
|
|
|
assert(abandoned());
|
2018-01-16 13:01:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
bool
|
|
|
|
ircd::server::tag::abandoned()
|
|
|
|
const
|
|
|
|
{
|
2019-08-30 06:30:30 +02:00
|
|
|
if(!p.valid())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!ctx::is(p.state(), ctx::future_state::PENDING))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2018-01-17 06:23:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::tag::canceled()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return bool(cancellation);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::tag::committed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return write_completed() > 0;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::tag::read_remaining()
|
|
|
|
const
|
|
|
|
{
|
2018-03-27 02:11:39 +02:00
|
|
|
return read_size() - read_completed();
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::tag::read_completed()
|
|
|
|
const
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
return state.head_read + state.content_read;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2018-03-27 02:11:39 +02:00
|
|
|
ircd::server::tag::read_size()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
2018-04-06 11:19:35 +02:00
|
|
|
return state.head_read + state.content_length;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::tag::write_remaining()
|
|
|
|
const
|
|
|
|
{
|
2018-03-27 02:11:39 +02:00
|
|
|
return write_size() - write_completed();
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::tag::write_completed()
|
|
|
|
const
|
|
|
|
{
|
2018-03-20 04:37:28 +01:00
|
|
|
return state.written;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2018-03-27 02:11:39 +02:00
|
|
|
ircd::server::tag::write_size()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return request? size(request->out) : 0;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|