673 lines
21 KiB
C++
673 lines
21 KiB
C++
// Spec: plans/01-runtime.md 6.2. Connection and request lifecycle.
|
|||
|
|
#include "connection.h"
|
||
|
|
|
||
|
|
#include <errno.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
#ifdef __linux__
|
||
|
|
#include <sys/sendfile.h>
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#include "runtime/observability_hooks.h"
|
||
|
|
#include "v1/serializer.h"
|
||
|
|
#include "v2/connection.h"
|
||
|
|
|
||
|
|
namespace molo::runtime::http {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::size_t SaturateToSize(uint64_t v) {
|
||
|
|
return v > (uint64_t)SIZE_MAX ? SIZE_MAX : (std::size_t)v;
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
std::shared_ptr<Connection> Connection::Create(
|
||
|
|
EventLoop& loop, std::unique_ptr<Transport> transport,
|
||
|
|
std::unique_ptr<tls::ITlsHandle> tls, Handler handler,
|
||
|
|
ConnectionConfig config) {
|
||
|
|
if (!transport || !handler) return nullptr;
|
||
|
|
if (config.body_cap > caps::kBodyMaxCeiling) {
|
||
|
|
config.body_cap = caps::kBodyMaxCeiling;
|
||
|
|
}
|
||
|
|
auto c = std::shared_ptr<Connection>(
|
||
|
|
new Connection(loop, std::move(transport), std::move(tls),
|
||
|
|
std::move(handler), config));
|
||
|
|
c->waiter_ = std::make_unique<FdWaiter>(loop, c->transport_->fd());
|
||
|
|
return c;
|
||
|
|
}
|
||
|
|
|
||
|
|
Connection::Connection(EventLoop& loop, std::unique_ptr<Transport> transport,
|
||
|
|
std::unique_ptr<tls::ITlsHandle> tls, Handler handler,
|
||
|
|
ConnectionConfig config)
|
||
|
|
: loop_(loop),
|
||
|
|
transport_(std::move(transport)),
|
||
|
|
tls_(std::move(tls)),
|
||
|
|
handler_(std::move(handler)),
|
||
|
|
config_(config) {}
|
||
|
|
|
||
|
|
Connection::~Connection() { close(); }
|
||
|
|
|
||
|
|
std::string Connection::protocol() const {
|
||
|
|
if (protocol_ == Protocol::H2PriorKnowledge) return "HTTP/2";
|
||
|
|
if (protocol_ == Protocol::Http1) return "HTTP/1.1";
|
||
|
|
return "detecting";
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Connection::is_tls() const { return tls_ && tls_->is_tls(); }
|
||
|
|
|
||
|
|
std::string Connection::peer() const {
|
||
|
|
return transport_ ? transport_->peer() : "";
|
||
|
|
}
|
||
|
|
|
||
|
|
void Connection::start() {
|
||
|
|
auto self = shared_from_this();
|
||
|
|
obs::OnAccept(transport_->peer());
|
||
|
|
MOLO_RUNTIME_COUNT("runtime.accept");
|
||
|
|
loop_.post(Runner(std::move(self)));
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<void> Connection::Runner(std::shared_ptr<Connection> self) {
|
||
|
|
co_await self->Run();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Connection::drain() {
|
||
|
|
// Thread-safe: the flag flip and waiter poke run on the loop thread so a
|
||
|
|
// parked coroutine never resumes on a foreign thread.
|
||
|
|
std::weak_ptr<Connection> weak = weak_from_this();
|
||
|
|
loop_.defer([weak] {
|
||
|
|
auto self = weak.lock();
|
||
|
|
if (!self || self->closed_) return;
|
||
|
|
self->draining_ = true;
|
||
|
|
if (self->active_h2_) {
|
||
|
|
self->active_h2_->drain();
|
||
|
|
} else if (self->waiter_) {
|
||
|
|
self->waiter_->Wake();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
void Connection::InterruptRead() {
|
||
|
|
if (waiter_) waiter_->Wake();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Connection::close() {
|
||
|
|
// Pin via weak_ptr: shared_from_this() would throw in the destructor.
|
||
|
|
// In the dtor the pin is null, which is safe because Run (the only parked
|
||
|
|
// coroutine) holds a shared ref and is therefore already finished.
|
||
|
|
std::shared_ptr<Connection> pin = weak_from_this().lock();
|
||
|
|
if (closed_) return;
|
||
|
|
closed_ = true;
|
||
|
|
if (waiter_) waiter_->cancel();
|
||
|
|
if (transport_) {
|
||
|
|
obs::OnClose(transport_->peer(), transport_->bytes_read(),
|
||
|
|
transport_->bytes_written());
|
||
|
|
transport_->close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<Connection::ReadResult> Connection::PumpRead(
|
||
|
|
std::chrono::milliseconds timeout) {
|
||
|
|
if (closed_) co_return ReadResult::Closed;
|
||
|
|
// Drain interrupts idle waits but lets the in-flight request finish.
|
||
|
|
if (draining_ && !in_request_) co_return ReadResult::Closed;
|
||
|
|
auto deadline = std::chrono::steady_clock::now() + timeout;
|
||
|
|
char tmp[caps::kReadChunkSize];
|
||
|
|
for (;;) {
|
||
|
|
if (closed_) co_return ReadResult::Closed;
|
||
|
|
if (tls_ && tls_->is_tls() && tls_done_ && !raw_.empty()) {
|
||
|
|
// Drain stashed ciphertext through decrypt first.
|
||
|
|
char plain[caps::kReadChunkSize];
|
||
|
|
std::size_t used = 0;
|
||
|
|
ssize_t p = tls_->decrypt(raw_.data(), raw_.size(), plain,
|
||
|
|
sizeof(plain), used);
|
||
|
|
if (p < 0) co_return ReadResult::Error;
|
||
|
|
if (p > 0 || used > 0) {
|
||
|
|
in_.append(plain, (std::size_t)(p > 0 ? p : 0));
|
||
|
|
raw_.erase(0, used);
|
||
|
|
if (p > 0) co_return ReadResult::Ok;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
ssize_t n = transport_->recv(tmp, sizeof(tmp));
|
||
|
|
if (n > 0) {
|
||
|
|
if (tls_ && tls_->is_tls() && tls_done_) {
|
||
|
|
raw_.append(tmp, (std::size_t)n);
|
||
|
|
continue; // loop back into the decrypt drain above
|
||
|
|
}
|
||
|
|
in_.append(tmp, (std::size_t)n);
|
||
|
|
co_return ReadResult::Ok;
|
||
|
|
}
|
||
|
|
if (n == 0) co_return ReadResult::Eof;
|
||
|
|
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
||
|
|
co_return ReadResult::Error;
|
||
|
|
}
|
||
|
|
auto now = std::chrono::steady_clock::now();
|
||
|
|
if (now >= deadline) co_return ReadResult::Timeout;
|
||
|
|
auto left =
|
||
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now);
|
||
|
|
bool fired = false;
|
||
|
|
TimerWheel::TimerId timer = loop_.schedule_after(left, [this, &fired] {
|
||
|
|
fired = true;
|
||
|
|
if (waiter_) waiter_->Wake();
|
||
|
|
});
|
||
|
|
bool alive = co_await waiter_->readable();
|
||
|
|
loop_.cancel_timer(timer);
|
||
|
|
if (!alive || closed_) co_return ReadResult::Closed;
|
||
|
|
if (draining_ && !in_request_) co_return ReadResult::Closed;
|
||
|
|
if (fired) {
|
||
|
|
// One last non-blocking attempt before declaring the timeout.
|
||
|
|
n = transport_->recv(tmp, sizeof(tmp));
|
||
|
|
if (n > 0) {
|
||
|
|
if (tls_ && tls_->is_tls() && tls_done_) {
|
||
|
|
raw_.append(tmp, (std::size_t)n);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
in_.append(tmp, (std::size_t)n);
|
||
|
|
co_return ReadResult::Ok;
|
||
|
|
}
|
||
|
|
co_return ReadResult::Timeout;
|
||
|
|
}
|
||
|
|
// Woken without data or timeout: drain poke or readiness race. Surface
|
||
|
|
// it so the caller re-checks exit conditions instead of sleeping past
|
||
|
|
// them. No data is lost: the next call tries recv first.
|
||
|
|
co_return ReadResult::Interrupted;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<bool> Connection::Flush(std::chrono::milliseconds timeout) {
|
||
|
|
std::string send_buf;
|
||
|
|
if (tls_ && tls_->is_tls() && tls_done_) {
|
||
|
|
send_buf.resize(out_.size() + 512);
|
||
|
|
ssize_t n = tls_->encrypt(out_.data(), out_.size(), send_buf.data(),
|
||
|
|
send_buf.size());
|
||
|
|
if (n < 0) co_return false;
|
||
|
|
send_buf.resize((std::size_t)n);
|
||
|
|
out_.clear();
|
||
|
|
} else {
|
||
|
|
send_buf.swap(out_);
|
||
|
|
}
|
||
|
|
std::size_t off = 0;
|
||
|
|
while (off < send_buf.size()) {
|
||
|
|
if (closed_) co_return false;
|
||
|
|
ssize_t n = transport_->send(send_buf.data() + off,
|
||
|
|
send_buf.size() - off);
|
||
|
|
if (n > 0) {
|
||
|
|
off += (std::size_t)n;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (n == 0) co_return false;
|
||
|
|
if (errno != EAGAIN && errno != EWOULDBLOCK) co_return false;
|
||
|
|
bool fired = false;
|
||
|
|
TimerWheel::TimerId timer = 0;
|
||
|
|
if (timeout.count() > 0) {
|
||
|
|
timer = loop_.schedule_after(timeout, [this, &fired] {
|
||
|
|
fired = true;
|
||
|
|
if (waiter_) waiter_->Wake();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
bool alive = co_await waiter_->writable();
|
||
|
|
if (timer != 0) loop_.cancel_timer(timer);
|
||
|
|
if (!alive || closed_) co_return false;
|
||
|
|
if (fired) co_return false;
|
||
|
|
}
|
||
|
|
co_return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<bool> Connection::AwaitWritable(std::chrono::milliseconds timeout) {
|
||
|
|
bool fired = false;
|
||
|
|
TimerWheel::TimerId timer = 0;
|
||
|
|
if (timeout.count() > 0) {
|
||
|
|
timer = loop_.schedule_after(timeout, [this, &fired] {
|
||
|
|
fired = true;
|
||
|
|
if (waiter_) waiter_->Wake();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
bool alive = co_await waiter_->writable();
|
||
|
|
if (timer != 0) loop_.cancel_timer(timer);
|
||
|
|
if (!alive || closed_) co_return false;
|
||
|
|
co_return !fired;
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<bool> Connection::SendFileBody(FileBody fb,
|
||
|
|
std::chrono::milliseconds timeout) {
|
||
|
|
struct Closer {
|
||
|
|
int fd;
|
||
|
|
~Closer() {
|
||
|
|
if (fd >= 0) ::close(fd);
|
||
|
|
}
|
||
|
|
} closer{fb.fd};
|
||
|
|
if (!ClampFileBody(fb)) co_return false;
|
||
|
|
uint64_t start = fb.offset;
|
||
|
|
uint64_t length = fb.length;
|
||
|
|
bool use_tls = tls_ && tls_->is_tls() && tls_done_;
|
||
|
|
#ifdef __linux__
|
||
|
|
if (!use_tls) {
|
||
|
|
// Zero-copy path: one sendfile call per 64 KB chunk with writable waits
|
||
|
|
// for backpressure. The socket is non-blocking, so EAGAIN just parks.
|
||
|
|
off_t off = (off_t)start;
|
||
|
|
uint64_t left = length;
|
||
|
|
while (left > 0) {
|
||
|
|
if (closed_) co_return false;
|
||
|
|
std::size_t n = left > caps::kFileChunkSize
|
||
|
|
? caps::kFileChunkSize
|
||
|
|
: (std::size_t)left;
|
||
|
|
ssize_t w = ::sendfile(transport_->fd(), fb.fd, &off, n);
|
||
|
|
if (w > 0) {
|
||
|
|
left -= (uint64_t)w;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (w == 0) co_return false; // truncated under us: fail closed
|
||
|
|
if (errno != EAGAIN && errno != EWOULDBLOCK) co_return false;
|
||
|
|
if (!co_await AwaitWritable(timeout)) co_return false;
|
||
|
|
}
|
||
|
|
co_return true;
|
||
|
|
}
|
||
|
|
#else
|
||
|
|
(void)use_tls;
|
||
|
|
#endif
|
||
|
|
// TLS or non-Linux: bounded pread streaming through the outbox, one Flush
|
||
|
|
// per chunk so the write ceiling always holds.
|
||
|
|
uint64_t pos = start;
|
||
|
|
uint64_t left = length;
|
||
|
|
char buf[caps::kFileChunkSize];
|
||
|
|
while (left > 0) {
|
||
|
|
if (closed_) co_return false;
|
||
|
|
std::size_t n =
|
||
|
|
left > sizeof(buf) ? sizeof(buf) : (std::size_t)left;
|
||
|
|
ssize_t r = ::pread(fb.fd, buf, n, (off_t)pos);
|
||
|
|
if (r <= 0) co_return false;
|
||
|
|
pos += (uint64_t)r;
|
||
|
|
left -= (uint64_t)r;
|
||
|
|
out_.append(buf, (std::size_t)r);
|
||
|
|
if (!co_await Flush(timeout)) co_return false;
|
||
|
|
}
|
||
|
|
co_return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<bool> Connection::DoHandshake() {
|
||
|
|
if (!tls_ || !tls_->is_tls()) {
|
||
|
|
tls_done_ = true;
|
||
|
|
co_return true;
|
||
|
|
}
|
||
|
|
auto deadline =
|
||
|
|
std::chrono::steady_clock::now() + config_.tls_handshake_timeout;
|
||
|
|
for (;;) {
|
||
|
|
if (closed_) co_return false;
|
||
|
|
const char* in_ptr = raw_.data();
|
||
|
|
std::size_t in_len = raw_.size();
|
||
|
|
std::size_t before = in_len;
|
||
|
|
std::string hs_out;
|
||
|
|
tls::HandshakeState st =
|
||
|
|
tls_->handshake_step(in_ptr, in_len, hs_out);
|
||
|
|
raw_.erase(0, before - in_len);
|
||
|
|
if (!hs_out.empty()) {
|
||
|
|
out_.append(hs_out);
|
||
|
|
auto now = std::chrono::steady_clock::now();
|
||
|
|
if (now >= deadline) co_return false;
|
||
|
|
auto left =
|
||
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now);
|
||
|
|
if (!co_await Flush(left)) co_return false;
|
||
|
|
}
|
||
|
|
if (st == tls::HandshakeState::Done) {
|
||
|
|
tls_done_ = true;
|
||
|
|
co_return true;
|
||
|
|
}
|
||
|
|
if (st == tls::HandshakeState::Failed) co_return false;
|
||
|
|
auto now = std::chrono::steady_clock::now();
|
||
|
|
if (now >= deadline) co_return false;
|
||
|
|
auto left =
|
||
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now);
|
||
|
|
// Handshake bytes bypass the plaintext buffer: read raw directly.
|
||
|
|
char tmp[caps::kReadChunkSize];
|
||
|
|
for (;;) {
|
||
|
|
ssize_t n = transport_->recv(tmp, sizeof(tmp));
|
||
|
|
if (n > 0) {
|
||
|
|
raw_.append(tmp, (std::size_t)n);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
if (n == 0) co_return false;
|
||
|
|
if (errno != EAGAIN && errno != EWOULDBLOCK) co_return false;
|
||
|
|
auto hs_now = std::chrono::steady_clock::now();
|
||
|
|
if (hs_now >= deadline) co_return false;
|
||
|
|
auto hs_left = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||
|
|
deadline - hs_now);
|
||
|
|
bool fired = false;
|
||
|
|
TimerWheel::TimerId timer =
|
||
|
|
loop_.schedule_after(hs_left, [this, &fired] {
|
||
|
|
fired = true;
|
||
|
|
if (waiter_) waiter_->Wake();
|
||
|
|
});
|
||
|
|
bool alive = co_await waiter_->readable();
|
||
|
|
loop_.cancel_timer(timer);
|
||
|
|
if (!alive || closed_ || fired) co_return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Connection::IsH2cUpgrade(const RequestHead& head) const {
|
||
|
|
if (!config_.allow_h2c_upgrade) return false;
|
||
|
|
if (head.version != "HTTP/1.1") return false;
|
||
|
|
std::string conn = ToLower(head.headers.get("connection"));
|
||
|
|
if (conn.find("upgrade") == std::string::npos) return false;
|
||
|
|
std::string up = ToLower(head.headers.get("upgrade"));
|
||
|
|
if (up.find("h2c") == std::string::npos) return false;
|
||
|
|
return head.headers.has("http2-settings");
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<void> Connection::SendError(int status, bool close_after) {
|
||
|
|
ResponseHead head;
|
||
|
|
head.status = status;
|
||
|
|
std::string body = std::to_string(status) + " " + StatusReason(status) + "\n";
|
||
|
|
head.headers.set("content-type", "text/plain; charset=utf-8");
|
||
|
|
out_.append(v1::Serializer::SerializeHead(head, "HTTP/1.1", false,
|
||
|
|
body.size(), false, close_after));
|
||
|
|
out_.append(body);
|
||
|
|
co_await Flush(config_.idle_timeout);
|
||
|
|
if (close_after) close();
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<void> Connection::Run() {
|
||
|
|
auto self = shared_from_this();
|
||
|
|
if (!co_await DoHandshake()) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (tls_ && tls_->is_tls()) {
|
||
|
|
protocol_ = ProtocolDetector::from_alpn(tls_->alpn_protocol());
|
||
|
|
} else {
|
||
|
|
ProtocolDetector detector;
|
||
|
|
std::size_t det_fed = 0;
|
||
|
|
auto start = std::chrono::steady_clock::now();
|
||
|
|
auto idle_deadline = start + config_.idle_timeout;
|
||
|
|
auto head_deadline = start + config_.header_timeout;
|
||
|
|
bool det_got_any = false;
|
||
|
|
for (;;) {
|
||
|
|
if (det_fed < in_.size()) {
|
||
|
|
// Feed only new bytes; the detector keeps match progress.
|
||
|
|
protocol_ =
|
||
|
|
detector.feed(in_.data() + det_fed, in_.size() - det_fed);
|
||
|
|
det_fed = in_.size();
|
||
|
|
if (protocol_ != Protocol::NeedMore) break;
|
||
|
|
}
|
||
|
|
// First byte races the idle timeout (silent close); completing
|
||
|
|
// detection races the header timeout (408).
|
||
|
|
auto limit = det_got_any ? head_deadline : idle_deadline;
|
||
|
|
auto now = std::chrono::steady_clock::now();
|
||
|
|
if (now >= limit) {
|
||
|
|
if (!det_got_any) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
co_await SendError(408, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
auto left =
|
||
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(limit - now);
|
||
|
|
ReadResult r = co_await PumpRead(left);
|
||
|
|
if (r == ReadResult::Ok) {
|
||
|
|
det_got_any = true;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (r == ReadResult::Interrupted) continue;
|
||
|
|
if (r == ReadResult::Timeout) {
|
||
|
|
if (!det_got_any) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
co_await SendError(408, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (protocol_ == Protocol::H2PriorKnowledge) {
|
||
|
|
bool expect_magic = !(tls_ && tls_->is_tls());
|
||
|
|
co_await RunH2(expect_magic, false, RequestHead{}, "");
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
co_await RunH1();
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<void> Connection::RunH1() {
|
||
|
|
auto self = shared_from_this();
|
||
|
|
v1::Parser parser(config_.body_cap);
|
||
|
|
while (!closed_ && requests_ < config_.max_requests) {
|
||
|
|
if (draining_) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
parser.reset();
|
||
|
|
auto head_deadline =
|
||
|
|
std::chrono::steady_clock::now() + config_.header_timeout;
|
||
|
|
auto body_deadline = head_deadline;
|
||
|
|
bool body_phase = false;
|
||
|
|
v1::ParseResult pr = v1::ParseResult::NeedMore;
|
||
|
|
bool got_any = false;
|
||
|
|
bool head_seen = false;
|
||
|
|
for (;;) {
|
||
|
|
if (!in_.empty()) {
|
||
|
|
// Bytes already here (detection or pipelining) count as activity:
|
||
|
|
// the idle timeout must not apply.
|
||
|
|
got_any = true;
|
||
|
|
in_request_ = true;
|
||
|
|
// The parser owns its pending buffer; hand over everything.
|
||
|
|
pr = parser.feed(in_.data(), in_.size());
|
||
|
|
in_.clear();
|
||
|
|
if (!head_seen && parser.head_done()) {
|
||
|
|
head_seen = true;
|
||
|
|
body_phase = true;
|
||
|
|
body_deadline =
|
||
|
|
std::chrono::steady_clock::now() + config_.body_timeout;
|
||
|
|
if (parser.expect_continue() &&
|
||
|
|
pr == v1::ParseResult::NeedMore) {
|
||
|
|
// Head is in, body still coming: release the client.
|
||
|
|
out_.append("HTTP/1.1 100 Continue\r\n\r\n");
|
||
|
|
if (!co_await Flush(config_.idle_timeout)) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (pr != v1::ParseResult::NeedMore) break;
|
||
|
|
}
|
||
|
|
auto now = std::chrono::steady_clock::now();
|
||
|
|
auto limit = body_phase ? body_deadline : head_deadline;
|
||
|
|
if (!got_any) limit = std::chrono::steady_clock::now() +
|
||
|
|
config_.idle_timeout;
|
||
|
|
if (now >= limit) {
|
||
|
|
if (!got_any) {
|
||
|
|
// Idle keep-alive expiry: silent close, no error page.
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
co_await SendError(408, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
auto left =
|
||
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(limit - now);
|
||
|
|
ReadResult r = co_await PumpRead(left);
|
||
|
|
if (r == ReadResult::Ok) {
|
||
|
|
got_any = true;
|
||
|
|
in_request_ = true;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (r == ReadResult::Interrupted) continue;
|
||
|
|
if (r == ReadResult::Eof || r == ReadResult::Closed) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (r == ReadResult::Timeout) {
|
||
|
|
if (!got_any) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
co_await SendError(408, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
// Pipelined bytes after this message return to the input buffer.
|
||
|
|
in_ = parser.take_pending();
|
||
|
|
if (pr == v1::ParseResult::Error400) {
|
||
|
|
obs::OnParseError(400);
|
||
|
|
co_await SendError(400, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (pr == v1::ParseResult::Error413) {
|
||
|
|
obs::OnParseError(413);
|
||
|
|
co_await SendError(413, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (pr == v1::ParseResult::Error431) {
|
||
|
|
obs::OnParseError(431);
|
||
|
|
co_await SendError(431, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (pr == v1::ParseResult::Error501) {
|
||
|
|
obs::OnParseError(501);
|
||
|
|
co_await SendError(501, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (pr != v1::ParseResult::Complete) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
const RequestHead& head = parser.head();
|
||
|
|
if (IsH2cUpgrade(head)) {
|
||
|
|
std::string body = parser.body();
|
||
|
|
out_.append(
|
||
|
|
"HTTP/1.1 101 Switching Protocols\r\nconnection: Upgrade\r\n"
|
||
|
|
"upgrade: h2c\r\ncontent-length: 0\r\n\r\n");
|
||
|
|
if (!co_await Flush(config_.idle_timeout)) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
++requests_;
|
||
|
|
protocol_ = Protocol::H2PriorKnowledge;
|
||
|
|
co_await RunH2(false, true, head, body);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
FullRequest req;
|
||
|
|
req.head = head;
|
||
|
|
req.body = parser.body();
|
||
|
|
req.trailers = parser.trailers();
|
||
|
|
req.is_tls = tls_ && tls_->is_tls();
|
||
|
|
req.peer = transport_->peer();
|
||
|
|
req.protocol = "HTTP/1.1";
|
||
|
|
bool keep = parser.keep_alive() && !draining_;
|
||
|
|
// Handler soft budget: the timer closes the transport so stuck handlers
|
||
|
|
// fail fast; the result is discarded when over budget.
|
||
|
|
budget_exceeded_ = false;
|
||
|
|
TimerWheel::TimerId budget = loop_.schedule_after(
|
||
|
|
config_.handler_budget, [self] {
|
||
|
|
self->budget_exceeded_ = true;
|
||
|
|
if (self->waiter_) self->waiter_->Wake();
|
||
|
|
if (self->transport_) self->transport_->close();
|
||
|
|
});
|
||
|
|
FullResponse resp;
|
||
|
|
bool handler_ok = true;
|
||
|
|
try {
|
||
|
|
resp = co_await handler_(req);
|
||
|
|
} catch (...) {
|
||
|
|
handler_ok = false;
|
||
|
|
}
|
||
|
|
loop_.cancel_timer(budget);
|
||
|
|
if (closed_ || budget_exceeded_) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (!handler_ok) {
|
||
|
|
co_await SendError(500, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
++requests_;
|
||
|
|
bool head_only = (head.method == "HEAD");
|
||
|
|
bool no_body_status = resp.head.status == 204 || resp.head.status == 304 ||
|
||
|
|
(resp.head.status >= 100 && resp.head.status < 200);
|
||
|
|
bool have_file = resp.file.has_value() && resp.file->fd >= 0;
|
||
|
|
if (have_file && !resp.body.empty()) {
|
||
|
|
// Contract violation: file and string body at once. Fail closed.
|
||
|
|
::close(resp.file->fd);
|
||
|
|
resp.file.reset();
|
||
|
|
co_await SendError(500, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
uint64_t file_len = 0;
|
||
|
|
bool send_file = false;
|
||
|
|
if (have_file) {
|
||
|
|
// Clamp before serializing so Content-Length always matches the bytes.
|
||
|
|
if (!ClampFileBody(*resp.file)) {
|
||
|
|
::close(resp.file->fd);
|
||
|
|
resp.file.reset();
|
||
|
|
co_await SendError(500, true);
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
file_len = resp.file->length;
|
||
|
|
if (head_only || no_body_status) {
|
||
|
|
::close(resp.file->fd); // HEAD keeps the length, drops the bytes
|
||
|
|
resp.file.reset();
|
||
|
|
} else {
|
||
|
|
send_file = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
std::size_t body_len =
|
||
|
|
have_file ? SaturateToSize(file_len) : resp.body.size();
|
||
|
|
out_.append(v1::Serializer::SerializeHead(resp.head, head.version, false,
|
||
|
|
body_len, head_only, !keep));
|
||
|
|
if (send_file) {
|
||
|
|
FileBody fb = *resp.file;
|
||
|
|
resp.file.reset();
|
||
|
|
if (out_.size() > config_.write_ceiling) {
|
||
|
|
::close(fb.fd);
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (!co_await Flush(config_.idle_timeout)) {
|
||
|
|
::close(fb.fd);
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (!co_await SendFileBody(fb, config_.idle_timeout)) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
if (!head_only) out_.append(resp.body);
|
||
|
|
if (out_.size() > config_.write_ceiling) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
if (!co_await Flush(config_.idle_timeout)) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
in_request_ = false;
|
||
|
|
if (!keep || requests_ >= config_.max_requests) {
|
||
|
|
close();
|
||
|
|
co_return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
close();
|
||
|
|
}
|
||
|
|
|
||
|
|
Task<void> Connection::RunH2(bool expect_magic, bool from_upgrade,
|
||
|
|
const RequestHead& upgrade_head,
|
||
|
|
const std::string& upgrade_body) {
|
||
|
|
auto self = shared_from_this();
|
||
|
|
v2::H2Config cfg;
|
||
|
|
cfg.body_cap = config_.body_cap;
|
||
|
|
cfg.idle_timeout = config_.idle_timeout;
|
||
|
|
cfg.handler_budget = config_.handler_budget;
|
||
|
|
auto h2 = std::make_shared<v2::H2Connection>(*this, cfg);
|
||
|
|
set_in_request(true);
|
||
|
|
set_active_h2(h2.get());
|
||
|
|
if (draining_) h2->drain();
|
||
|
|
co_await h2->Run(expect_magic, from_upgrade, upgrade_head, upgrade_body);
|
||
|
|
requests_ += h2->requests_served();
|
||
|
|
set_active_h2(nullptr);
|
||
|
|
set_in_request(false);
|
||
|
|
close();
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::runtime::http
|