feat: implement MoloApp plans 01-15 with integration

Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
This commit is contained in:
2026-10-23 18:20:04 +02:00
parent 54068f1953
commit 3c4334cec9
1068 changed files with 100899 additions and 2 deletions
+92
View File
@@ -0,0 +1,92 @@
// Shared Base64/Base64url codec (RFC 4648). Used by cookie signing
// (v1.<payload>.<tag>) and the H1-to-h2c upgrade (HTTP2-Settings). One home
// so no feature reimplements it.
#pragma once
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
namespace molo::runtime {
class Base64Url {
public:
static std::string Encode(std::string_view in);
static std::optional<std::string> Decode(std::string_view in);
private:
static int Digit(char c);
};
inline std::string Base64Url::Encode(std::string_view in) {
static const char kAlphabet[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
std::string out;
out.reserve((in.size() * 4 + 2) / 3);
std::size_t i = 0;
while (i + 3 <= in.size()) {
uint32_t n = ((uint32_t)(unsigned char)in[i] << 16) |
((uint32_t)(unsigned char)in[i + 1] << 8) |
(uint32_t)(unsigned char)in[i + 2];
out.push_back(kAlphabet[(n >> 18) & 63]);
out.push_back(kAlphabet[(n >> 12) & 63]);
out.push_back(kAlphabet[(n >> 6) & 63]);
out.push_back(kAlphabet[n & 63]);
i += 3;
}
std::size_t rem = in.size() - i;
if (rem == 1) {
uint32_t n = (uint32_t)(unsigned char)in[i] << 16;
out.push_back(kAlphabet[(n >> 18) & 63]);
out.push_back(kAlphabet[(n >> 12) & 63]);
} else if (rem == 2) {
uint32_t n = ((uint32_t)(unsigned char)in[i] << 16) |
((uint32_t)(unsigned char)in[i + 1] << 8);
out.push_back(kAlphabet[(n >> 18) & 63]);
out.push_back(kAlphabet[(n >> 12) & 63]);
out.push_back(kAlphabet[(n >> 6) & 63]);
}
return out;
}
inline int Base64Url::Digit(char c) {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '-' || c == '+') return 62;
if (c == '_' || c == '/') return 63;
return -1;
}
inline std::optional<std::string> Base64Url::Decode(std::string_view in) {
// Tolerate padding; reject other garbage loudly.
std::string s(in);
while (!s.empty() && s.back() == '=') s.pop_back();
if (s.size() % 4 == 1) return std::nullopt;
std::string out;
out.reserve(s.size() * 3 / 4);
for (std::size_t i = 0; i < s.size();) {
uint32_t n = 0;
std::size_t group = 0;
for (; group < 4 && i < s.size(); ++group, ++i) {
int d = Digit(s[i]);
if (d < 0) return std::nullopt;
n = (n << 6) | (uint32_t)d;
}
if (group == 4) {
out.push_back((char)((n >> 16) & 0xff));
out.push_back((char)((n >> 8) & 0xff));
out.push_back((char)(n & 0xff));
} else if (group == 3) {
out.push_back((char)((n >> 10) & 0xff));
out.push_back((char)((n >> 2) & 0xff));
} else if (group == 2) {
out.push_back((char)((n >> 4) & 0xff));
}
}
return out;
}
} // namespace molo::runtime
+80
View File
@@ -0,0 +1,80 @@
// Spec: plans/01-runtime.md section 6. Static ceilings for every runtime knob.
// Every limit here is a compile-time default; runtime config may lower it and
// may raise it only up to the paired ceiling. No autotuning anywhere.
#pragma once
#include <chrono>
#include <cstddef>
#include <cstdint>
namespace molo::runtime::caps {
// 6.1 Event loop and threading.
inline constexpr std::size_t kLoopWorkersMax = 64;
inline constexpr std::size_t kThreadPoolThreadsDefault = 4;
inline constexpr std::size_t kThreadPoolThreadsCeiling = 64;
inline constexpr std::size_t kThreadPoolQueueCap = 1024;
inline constexpr int kTimerGranularityMs = 10;
inline constexpr std::size_t kTimerWheelSlots = 1024;
inline constexpr std::size_t kTimersPerLoopCap = 65536;
inline constexpr std::size_t kPollerFdsCeiling = 65536;
inline constexpr std::size_t kPollerBatchMax = 256;
inline constexpr std::size_t kChannelCapDefault = 4096;
inline constexpr std::size_t kChannelCapCeiling = 1048576;
inline constexpr std::size_t kDeferredQueueCap = 65536;
// 6.2 Connection and request lifecycle.
inline constexpr std::chrono::milliseconds kHeaderTimeout{10000};
inline constexpr std::chrono::milliseconds kBodyTimeout{30000};
inline constexpr std::chrono::milliseconds kIdleKeepAliveTimeout{75000};
inline constexpr std::chrono::milliseconds kHandlerBudget{30000};
inline constexpr std::chrono::milliseconds kShutdownDrainTimeout{30000};
inline constexpr std::chrono::milliseconds kShutdownForcedTimeout{5000};
inline constexpr std::chrono::milliseconds kTlsHandshakeTimeout{5000};
inline constexpr std::size_t kMaxConnectionsDefault = 16384;
inline constexpr std::size_t kMaxConnectionsCeiling = 65536;
inline constexpr std::size_t kMaxRequestsPerH1Connection = 1000;
inline constexpr std::size_t kH2MaxConcurrentStreamsDefault = 100;
inline constexpr std::size_t kH2MaxConcurrentStreamsCeiling = 1000;
inline constexpr std::size_t kHeaderBlockMax = 65536;
inline constexpr std::size_t kHeaderFieldMax = 100;
inline constexpr std::size_t kHeaderLineMax = 16384;
inline constexpr std::size_t kUriMax = 8192;
inline constexpr std::size_t kBodyMaxDefault = 10 * 1024 * 1024;
inline constexpr std::size_t kBodyMaxCeiling = 100 * 1024 * 1024;
inline constexpr std::size_t kReadChunkSize = 16384;
inline constexpr std::size_t kWriteHighWatermark = 256 * 1024;
inline constexpr std::size_t kWriteLowWatermark = 64 * 1024;
inline constexpr std::size_t kWriteBufferCeiling = 16 * 1024 * 1024;
// 6.7 Static: bounded streaming chunk for file-backed response bodies.
inline constexpr std::size_t kFileChunkSize = 64 * 1024;
// 6.4 HTTP/2.
inline constexpr std::size_t kH2InitialWindow = 65535;
inline constexpr std::size_t kH2MaxFrameDefault = 16384;
inline constexpr std::size_t kH2MaxFrameCeiling = 16777215;
inline constexpr std::size_t kH2HeaderTableDefault = 4096;
inline constexpr std::size_t kH2HeaderTableCeiling = 16384;
inline constexpr std::size_t kH2HeaderListMax = 65536;
inline constexpr std::size_t kH2MaxFrameHeaderListCap = 100;
// 6.5 Compression.
inline constexpr std::size_t kCompressThreshold = 1024;
inline constexpr std::size_t kDecompressMaxAbsolute = 10 * 1024 * 1024;
inline constexpr std::size_t kDecompressMaxRatio = 10;
inline constexpr std::size_t kCompressChunkSize = 65536;
inline constexpr int kGzipLevel = 6;
inline constexpr int kBrotliQualityDynamic = 4;
inline constexpr int kBrotliQualityStatic = 11;
// 6.6 Cookies.
inline constexpr std::size_t kCookieCountMax = 50;
inline constexpr std::size_t kCookieTotalMax = 8192;
inline constexpr std::size_t kSetCookieMax = 4096;
// 6.7 Static files.
inline constexpr std::size_t kStaticChunkSize = 65536;
inline constexpr std::size_t kStaticPathMax = 4096;
inline constexpr int kStaticSymlinkDepthMax = 8;
} // namespace molo::runtime::caps
+162
View File
@@ -0,0 +1,162 @@
// Spec: plans/01-runtime.md section 3. Bounded inter-loop (and inter-thread)
// message queue. Only the owning loop drains it; anyone may push.
#pragma once
#include <cstddef>
#include <deque>
#include <functional>
#include <mutex>
#include <utility>
#include "caps.h"
#include "event_loop.h"
#if defined(__linux__)
#include <sys/eventfd.h>
#include <unistd.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif
namespace molo::runtime {
template <typename T>
class Channel {
public:
explicit Channel(std::size_t cap = caps::kChannelCapDefault)
: cap_(cap == 0 ? 1 : cap) {
if (cap_ > caps::kChannelCapCeiling) cap_ = caps::kChannelCapCeiling;
#if defined(__linux__)
notify_fd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
#else
if (pipe(pipe_fds_) == 0) {
int flags = fcntl(pipe_fds_[0], F_GETFL, 0);
fcntl(pipe_fds_[0], F_SETFL, flags | O_NONBLOCK);
flags = fcntl(pipe_fds_[1], F_GETFL, 0);
fcntl(pipe_fds_[1], F_SETFL, flags | O_NONBLOCK);
notify_fd_ = pipe_fds_[0];
}
#endif
}
~Channel() { detach(); close_fds(); }
Channel(const Channel&) = delete;
Channel& operator=(const Channel&) = delete;
bool valid() const { return notify_fd_ >= 0; }
// False when full or closed.
bool try_push(T value) {
bool need_wake = false;
{
std::lock_guard<std::mutex> lock(mutex_);
if (closed_ || queue_.size() >= cap_) return false;
need_wake = queue_.empty();
queue_.push_back(std::move(value));
}
if (need_wake) Signal();
return true;
}
bool try_pop(T& out) {
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.empty()) return false;
out = std::move(queue_.front());
queue_.pop_front();
return true;
}
void close() {
{
std::lock_guard<std::mutex> lock(mutex_);
closed_ = true;
}
Signal();
}
bool closed() const {
std::lock_guard<std::mutex> lock(mutex_);
return closed_;
}
std::size_t size() const {
std::lock_guard<std::mutex> lock(mutex_);
return queue_.size();
}
int notify_fd() const { return notify_fd_; }
bool attach(EventLoop& loop, std::function<void()> on_readable) {
if (!valid() || !on_readable) return false;
attached_loop_ = &loop;
return loop.poller().add_fd(notify_fd_, IPoller::kRead,
[this, cb = std::move(on_readable)](int, uint32_t) {
DrainSignal();
cb();
});
}
void detach() {
if (attached_loop_) {
attached_loop_->poller().remove_fd(notify_fd_);
attached_loop_ = nullptr;
}
}
private:
void Signal() {
#if defined(__linux__)
if (notify_fd_ < 0) return;
uint64_t v = 1;
ssize_t r = write(notify_fd_, &v, sizeof(v));
(void)r;
#else
if (pipe_fds_[1] < 0) return;
char c = 1;
ssize_t r = write(pipe_fds_[1], &c, 1);
(void)r;
#endif
}
void DrainSignal() {
#if defined(__linux__)
uint64_t v = 0;
while (read(notify_fd_, &v, sizeof(v)) == (ssize_t)sizeof(v)) {
if (v == 0) break;
}
#else
char buf[64];
while (read(notify_fd_, buf, sizeof(buf)) > 0) {
}
#endif
}
void close_fds() {
#if defined(__linux__)
if (notify_fd_ >= 0) {
detach();
::close(notify_fd_);
notify_fd_ = -1;
}
#else
if (pipe_fds_[0] >= 0) ::close(pipe_fds_[0]);
if (pipe_fds_[1] >= 0) ::close(pipe_fds_[1]);
pipe_fds_[0] = pipe_fds_[1] = -1;
notify_fd_ = -1;
#endif
}
std::size_t cap_;
mutable std::mutex mutex_;
std::deque<T> queue_;
bool closed_ = false;
int notify_fd_ = -1;
#if !defined(__linux__)
int pipe_fds_[2] = {-1, -1};
#endif
EventLoop* attached_loop_ = nullptr;
};
} // namespace molo::runtime
+345
View File
@@ -0,0 +1,345 @@
// Spec: plans/01-runtime.md 6.5, RFC 7932 sections 9.1, 9.2. In-house
// Brotli, framing subset (see header for the documented v1 gap).
#include "brotli.h"
#include <cstring>
#include "runtime/caps.h"
namespace molo::runtime::compress {
// ----- Encoder -----
void BrotliEncoder::reset() {
header_done_ = false;
finished_ = false;
pending_in_.clear();
stage_.clear();
acc_ = 0;
have_ = 0;
}
void BrotliEncoder::PutBits(uint32_t bits, int count) {
acc_ |= bits << have_;
have_ += count;
while (have_ >= 8) {
stage_.push_back((char)(acc_ & 0xff));
acc_ >>= 8;
have_ -= 8;
}
}
void BrotliEncoder::PadToByte() {
if (have_ > 0) {
stage_.push_back((char)(acc_ & 0xff));
acc_ = 0;
have_ = 0;
}
}
CodecResult BrotliEncoder::feed(const char* in, std::size_t n, char* out,
std::size_t cap, bool finish) {
CodecResult r;
if (finished_) {
if (n != 0) {
r.status = CodecStatus::Error;
return r;
}
std::size_t k = stage_.size() < cap ? stage_.size() : cap;
if (k > 0) {
memcpy(out, stage_.data(), k);
stage_.erase(0, k);
}
r.out_used = k;
r.status = stage_.empty() ? CodecStatus::Done : CodecStatus::NeedOutput;
return r;
}
if (n > 0 && in != nullptr) pending_in_.append(in, n);
r.in_used = n;
if (!header_done_) {
PutBits(0, 1); // WBITS 16: single 0 bit
header_done_ = true;
}
// Emit full 64 KiB non-final blocks eagerly for streaming; the tail goes
// out at finish. MNIBBLES 4 covers MLEN up to 65536.
while (pending_in_.size() >= 65536 ||
(finish && !pending_in_.empty())) {
std::size_t chunk = pending_in_.size();
if (chunk > 65536) chunk = 65536;
PutBits(0, 1); // ISLAST = 0
PutBits(0, 2); // MNIBBLES = 4 ("00")
PutBits((uint32_t)(chunk - 1), 16); // MLEN - 1
PutBits(1, 1); // ISUNCOMPRESSED = 1
PadToByte(); // ignored bits must be zero: they are
stage_.append(pending_in_.data(), chunk);
pending_in_.erase(0, chunk);
}
if (finish) {
PutBits(1, 1); // ISLAST = 1
PutBits(1, 1); // ISLASTEMPTY = 1
PadToByte();
finished_ = true;
}
std::size_t k = stage_.size() < cap ? stage_.size() : cap;
if (k > 0) {
memcpy(out, stage_.data(), k);
stage_.erase(0, k);
}
r.out_used = k;
if (finished_ && stage_.empty()) {
r.status = CodecStatus::Done;
} else if (!stage_.empty()) {
r.status = CodecStatus::NeedOutput;
} else {
r.status = CodecStatus::Ok;
}
return r;
}
// ----- Decoder -----
void BrotliDecoder::reset() {
staging_.clear();
bitpos_ = 0;
header_done_ = false;
done_ = false;
unsupported_ = false;
total_out_ = 0;
total_in_ = 0;
copy_left_ = 0;
}
bool BrotliDecoder::Need(std::size_t k) {
return staging_.size() * 8 - bitpos_ >= k;
}
uint32_t BrotliDecoder::Take(std::size_t k) {
uint32_t v = 0;
for (std::size_t i = 0; i < k; ++i) {
std::size_t p = bitpos_ + i;
unsigned char c = (unsigned char)staging_[p / 8];
v |= (uint32_t)((c >> (p % 8)) & 1) << i;
}
bitpos_ += k;
return v;
}
bool BrotliDecoder::AlignZero() {
while (bitpos_ % 8 != 0) {
if (!Need(1)) return false;
if (Take(1) != 0) return false;
}
return true;
}
// Returns 1 block done, 0 need more input, -1 corrupt, -2 unsupported.
int BrotliDecoder::ParseBlock(char* out, std::size_t cap,
std::size_t& produced, bool& need_output) {
need_output = false;
if (copy_left_ > 0) {
// Resume a pending uncompressed copy (byte aligned by construction).
while (copy_left_ > 0) {
if (produced >= cap) {
need_output = true;
return 1;
}
if (total_out_ >= caps::kDecompressMaxAbsolute) return -1;
if (!Need(8)) return 0;
char b = (char)Take(8);
out[produced++] = b;
++total_out_;
--copy_left_;
}
return 1;
}
if (!Need(1)) return 0;
bool islast = Take(1) != 0;
if (islast) {
if (!Need(1)) {
bitpos_ -= 1;
return 0;
}
bool isempty = Take(1) != 0;
if (!isempty) {
// Final non-empty block: ISUNCOMPRESSED is absent, so the block is
// entropy-coded. Documented v1 gap.
return -2;
}
// Stream ends here: rest of the byte must be zero padding.
while (bitpos_ % 8 != 0) {
if (!Need(1)) return 0;
if (Take(1) != 0) return -1;
}
done_ = true;
return 1;
}
if (!Need(2)) {
bitpos_ -= 1;
return 0;
}
uint32_t mn = Take(2);
if (mn == 3) {
// Empty block: reserved + metadata skip.
if (!Need(3)) {
bitpos_ -= 3; // ISLAST + MNIBBLES
return 0;
}
if (Take(1) != 0) return -1; // reserved
uint32_t mskip = Take(2); // MSKIPBYTES
std::size_t mlen = 0;
if (mskip > 0) {
if (!Need(mskip * 8)) {
bitpos_ -= 6; // rewind to block start
return 0;
}
mlen = (std::size_t)Take(mskip * 8) + 1;
}
// Align (zeros) then skip metadata bytes.
std::size_t snap = bitpos_;
while (bitpos_ % 8 != 0) {
if (!Need(1)) {
bitpos_ = snap - 6;
return 0;
}
if (Take(1) != 0) return -1;
}
if (staging_.size() - bitpos_ / 8 < mlen) {
bitpos_ = snap - 6;
return 0;
}
bitpos_ += mlen * 8;
return 1;
}
uint32_t nibbles = 4 + mn; // 4, 5, 6
if (!Need(nibbles * 4)) {
bitpos_ -= 3;
return 0;
}
uint32_t mlen = Take(nibbles * 4) + 1;
if (nibbles > 4) {
// Last nibble must be non-zero.
uint32_t top = (mlen - 1) >> ((nibbles - 1) * 4);
if ((top & 0xf) == 0) return -1;
}
if (!Need(1)) {
bitpos_ -= 3 + nibbles * 4;
return 0;
}
bool uncompressed = Take(1) != 0;
if (!uncompressed) return -2; // entropy-coded: documented gap
std::size_t snap = bitpos_;
while (bitpos_ % 8 != 0) {
if (!Need(1)) {
bitpos_ = snap - (4 + nibbles * 4);
return 0;
}
if (Take(1) != 0) return -1;
}
copy_left_ = mlen;
while (copy_left_ > 0) {
if (produced >= cap) {
need_output = true;
return 1;
}
if (total_out_ >= caps::kDecompressMaxAbsolute) return -1;
if (!Need(8)) return 0;
char b = (char)Take(8);
out[produced++] = b;
++total_out_;
--copy_left_;
}
return 1;
}
CodecResult BrotliDecoder::feed(const char* in, std::size_t n, char* out,
std::size_t cap, bool finish) {
CodecResult r;
if (done_ || unsupported_) {
r.status = unsupported_ ? CodecStatus::Unsupported : CodecStatus::Error;
return r;
}
if (n > 0 && in != nullptr) {
if (staging_.size() + n > caps::kBodyMaxCeiling) {
r.status = CodecStatus::Error;
return r;
}
staging_.append(in, n);
total_in_ += n;
}
r.in_used = n;
if (!header_done_) {
// WBITS variable-length code, rewound on truncation.
std::size_t snap = bitpos_;
if (!Need(1)) {
r.status = finish ? CodecStatus::Error : CodecStatus::Ok;
return r;
}
if (Take(1) == 0) {
header_done_ = true; // WBITS 16
} else {
if (!Need(3)) {
bitpos_ = snap;
r.status = finish ? CodecStatus::Error : CodecStatus::Ok;
return r;
}
uint32_t v = Take(3);
if (v == 0) {
if (!Need(3)) {
bitpos_ = snap;
r.status = finish ? CodecStatus::Error : CodecStatus::Ok;
return r;
}
uint32_t w = Take(3);
if (w == 1) {
r.status = CodecStatus::Error; // 0010001 is invalid
return r;
}
header_done_ = true; // 17 or 10..15
} else {
header_done_ = true; // 18..24
}
}
}
bool need_output = false;
for (;;) {
if (done_) break;
std::size_t produced = 0;
int st = ParseBlock(out + r.out_used, cap - r.out_used, produced,
need_output);
r.out_used += produced;
if (st == 1) {
if (done_) break;
if (need_output) {
r.status = CodecStatus::NeedOutput;
return r;
}
continue; // next block
}
if (st == -2) {
unsupported_ = true;
r.status = CodecStatus::Unsupported;
return r;
}
if (st == -1 || finish) {
r.status = CodecStatus::Error;
return r;
}
r.status = CodecStatus::Ok;
return r;
}
// Trailing bytes after the final block are invalid.
if (bitpos_ / 8 != staging_.size()) {
r.status = CodecStatus::Error;
return r;
}
if (total_out_ > caps::kDecompressMaxAbsolute ||
(total_in_ > 0 &&
total_out_ > total_in_ * caps::kDecompressMaxRatio)) {
r.status = CodecStatus::Error;
return r;
}
r.status = CodecStatus::Done;
return r;
}
} // namespace molo::runtime::compress
+65
View File
@@ -0,0 +1,65 @@
// Spec: plans/01-runtime.md 6.5, RFC 7932. In-house Brotli, framing subset.
//
// The encoder emits window bits plus uncompressed meta-blocks (ISLASTEMPTY
// final block): always valid Brotli, streaming, bounded. The decoder parses
// the stream header and meta-block headers, decodes uncompressed blocks and
// skips metadata blocks; entropy-coded blocks return Unsupported, which the
// caller maps to 415/passthrough. This is a documented v1 gap: full
// entropy-code decoding is future work, tracked next to this file.
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include "compressor.h"
namespace molo::runtime::compress {
class BrotliEncoder : public IEncoder {
public:
BrotliEncoder() = default;
CodecResult feed(const char* in, std::size_t n, char* out, std::size_t cap,
bool finish) override;
void reset() override;
const char* name() const override { return "br"; }
private:
void PutBits(uint32_t bits, int count);
void PadToByte();
bool header_done_ = false;
bool finished_ = false;
std::string pending_in_;
std::string stage_;
uint32_t acc_ = 0;
int have_ = 0;
};
class BrotliDecoder : public IDecoder {
public:
BrotliDecoder() = default;
CodecResult feed(const char* in, std::size_t n, char* out, std::size_t cap,
bool finish) override;
void reset() override;
const char* name() const override { return "br"; }
private:
// Bit cursor over staging_. Returns false when truncated.
bool Need(std::size_t k);
uint32_t Take(std::size_t k); // caller ensures Need(k)
bool AlignZero(); // pad to byte, must be zeros
int ParseBlock(char* out, std::size_t cap, std::size_t& produced,
bool& need_output);
std::string staging_;
std::size_t bitpos_ = 0;
bool header_done_ = false;
bool done_ = false;
bool unsupported_ = false;
std::size_t total_out_ = 0;
std::size_t total_in_ = 0;
// Persistent uncompressed-block copy state.
std::size_t copy_left_ = 0;
};
} // namespace molo::runtime::compress
+46
View File
@@ -0,0 +1,46 @@
// Spec: plans/01-runtime.md 6.5. Streaming codec interface. Both codecs are
// implemented in-house, work in bounded chunks, and never buffer a full
// passthrough body.
#pragma once
#include <cstddef>
namespace molo::runtime::compress {
enum class CodecStatus {
Ok, // progress made; call again for more
NeedOutput, // output buffer full, input remains
Done, // stream complete
Error, // corrupt input, cap hit, or API misuse
Unsupported, // valid framing the v1 decoder does not implement
};
struct CodecResult {
CodecStatus status = CodecStatus::Ok;
std::size_t in_used = 0;
std::size_t out_used = 0;
};
class IEncoder {
public:
virtual ~IEncoder() = default;
// finish=true flushes the last block and the trailer. After Done only
// drain feeds (n=0) are allowed until reset().
virtual CodecResult feed(const char* in, std::size_t n, char* out,
std::size_t cap, bool finish) = 0;
virtual void reset() = 0;
virtual const char* name() const = 0;
};
class IDecoder {
public:
virtual ~IDecoder() = default;
// finish=true means no more input will ever arrive: an incomplete stream
// is an Error. After Done, feed is an error until reset().
virtual CodecResult feed(const char* in, std::size_t n, char* out,
std::size_t cap, bool finish) = 0;
virtual void reset() = 0;
virtual const char* name() const = 0;
};
} // namespace molo::runtime::compress
+773
View File
@@ -0,0 +1,773 @@
// Spec: plans/01-runtime.md 6.5, RFC 1952, RFC 1951 (DEFLATE). In-house
// gzip: stored-block encoder, full inflate decoder with bomb guard.
#include "gzip.h"
#include <cstring>
#include "runtime/caps.h"
namespace molo::runtime::compress {
namespace {
constexpr std::array<uint32_t, 256> kCrcTable = [] {
std::array<uint32_t, 256> t{};
for (uint32_t i = 0; i < 256; ++i) {
uint32_t c = i;
for (int k = 0; k < 8; ++k) {
c = (c & 1) ? (0xedb88320u ^ (c >> 1)) : (c >> 1);
}
t[i] = c;
}
return t;
}();
constexpr uint32_t kLenBase[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13,
15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
67, 83, 99, 115, 131, 163, 195, 227, 258};
constexpr int kLenExtra[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
constexpr uint32_t kDistBase[30] = {
1, 2, 3, 4, 5, 7, 9, 13, 17, 25,
33, 49, 65, 97, 129, 193, 257, 385, 513, 769,
1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
constexpr int kDistExtra[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
constexpr int kClOrder[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
11, 4, 12, 3, 13, 2, 14, 1, 15};
} // namespace
uint32_t Crc32Update(uint32_t crc, const char* data, std::size_t len) {
for (std::size_t i = 0; i < len; ++i) {
crc = kCrcTable[(crc ^ (unsigned char)data[i]) & 0xff] ^ (crc >> 8);
}
return crc;
}
// ----- Encoder -----
GzipEncoder::GzipEncoder() { reset(); }
void GzipEncoder::reset() {
header_done_ = false;
trailer_done_ = false;
finished_ = false;
final_emitted_ = false;
pending_in_.clear();
hist_.clear();
stage_.clear();
crc_ = 0xffffffffu;
isize_ = 0;
acc_ = 0;
have_ = 0;
head_.assign(4096, -1);
prev_.clear();
}
void GzipEncoder::PutBit(int bit) {
if (bit) acc_ |= (1u << have_);
if (++have_ >= 8) {
stage_.push_back((char)(acc_ & 0xff));
acc_ = 0;
have_ = 0;
}
}
void GzipEncoder::PutBitsLsb(uint32_t bits, int count) {
for (int i = 0; i < count; ++i) PutBit((bits >> i) & 1);
}
void GzipEncoder::PutCode(uint32_t code, int len) {
for (int i = len - 1; i >= 0; --i) PutBit((code >> i) & 1);
}
void GzipEncoder::PutLitLen(int sym) {
if (sym <= 143) {
PutCode((uint32_t)(0x30 + sym), 8);
} else if (sym <= 255) {
PutCode((uint32_t)(0x190 + sym - 144), 9);
} else if (sym <= 279) {
PutCode((uint32_t)(sym - 256), 7);
} else {
PutCode((uint32_t)(0xc0 + sym - 280), 8);
}
}
void GzipEncoder::PutDist(int sym) { PutCode((uint32_t)sym, 5); }
void GzipEncoder::FlushBits() {
if (have_ > 0) {
stage_.push_back((char)(acc_ & 0xff));
acc_ = 0;
have_ = 0;
}
}
int GzipEncoder::Hash3(const char* p) {
return (((unsigned char)p[0] << 10) ^ ((unsigned char)p[1] << 5) ^
(unsigned char)p[2]) &
4095;
}
void GzipEncoder::EncodeBlock(const char* data, std::size_t start,
std::size_t len, bool last) {
// Block header: BFINAL + BTYPE 01 (fixed Huffman), LSB-first.
PutBit(last ? 1 : 0);
PutBit(1);
PutBit(0);
std::size_t end = start + len;
prev_.assign(end, -1);
head_.assign(4096, -1); // chains are per-block over this view
// Seed chains with the history bytes.
for (std::size_t i = 0; i < start; ++i) {
if (i + 3 > end) break;
int h = Hash3(data + i);
prev_[i] = head_[h];
head_[h] = (int)i;
}
std::size_t pos = start;
while (pos < end) {
std::size_t best_len = 0;
std::size_t best_dist = 0;
if (pos + 3 <= end) {
int h = Hash3(data + pos);
int cand = head_[h];
int chain = 0;
std::size_t max_len = end - pos;
if (max_len > 258) max_len = 258;
while (cand >= 0 && chain < 64) {
std::size_t cpos = (std::size_t)cand;
if (pos > cpos && pos - cpos <= 32768) {
std::size_t l = 0;
while (l < max_len && data[cpos + l] == data[pos + l]) ++l;
if (l > best_len) {
best_len = l;
best_dist = pos - cpos;
if (l >= max_len) break;
}
} else if (pos <= cpos) {
break;
}
cand = prev_[cpos];
++chain;
}
prev_[pos] = head_[h];
head_[h] = (int)pos;
}
if (best_len >= 3) {
int li = 28;
for (int i = 0; i < 29; ++i) {
uint32_t hi = (i == 28) ? 258 : kLenBase[i + 1] - 1;
if (best_len >= kLenBase[i] && best_len <= hi) {
li = i;
break;
}
}
PutLitLen(257 + li);
if (kLenExtra[li] > 0) {
PutBitsLsb((uint32_t)(best_len - kLenBase[li]), kLenExtra[li]);
}
int di = 29;
for (int i = 0; i < 30; ++i) {
uint32_t hi = (i == 29) ? 32768 : kDistBase[i + 1] - 1;
if (best_dist >= kDistBase[i] && best_dist <= hi) {
di = i;
break;
}
}
PutDist(di);
if (kDistExtra[di] > 0) {
PutBitsLsb((uint32_t)(best_dist - kDistBase[di]), kDistExtra[di]);
}
// Insert skipped positions into the chains.
for (std::size_t k = 1; k < best_len; ++k) {
if (pos + k + 3 > end) break;
int h = Hash3(data + pos + k);
prev_[pos + k] = head_[h];
head_[h] = (int)(pos + k);
}
pos += best_len;
} else {
PutLitLen((unsigned char)data[pos]);
++pos;
}
}
PutLitLen(256); // end of block
}
CodecResult GzipEncoder::feed(const char* in, std::size_t n, char* out,
std::size_t cap, bool finish) {
CodecResult r;
if (finished_) {
// Drain-only feeds after finish are allowed; new input is not.
if (n != 0) {
r.status = CodecStatus::Error;
return r;
}
std::size_t k = stage_.size() < cap ? stage_.size() : cap;
if (k > 0) {
memcpy(out, stage_.data(), k);
stage_.erase(0, k);
}
r.out_used = k;
r.status = stage_.empty() ? CodecStatus::Done : CodecStatus::NeedOutput;
return r;
}
if (n > 0 && in != nullptr) pending_in_.append(in, n);
r.in_used = n;
if (!header_done_) {
char hdr[10] = {(char)0x1f, (char)0x8b, (char)0x08, (char)0x00,
(char)0x00, (char)0x00, (char)0x00, (char)0x00,
(char)0x00, (char)0x03};
stage_.append(hdr, 10);
header_done_ = true;
}
// Encode 32 KiB fixed-Huffman blocks eagerly; the tail goes out at finish.
while (pending_in_.size() >= 32768 ||
(finish && !pending_in_.empty())) {
std::size_t chunk = pending_in_.size();
if (chunk > 32768) chunk = 32768;
bool last = finish && chunk == pending_in_.size();
std::string view = hist_;
view.append(pending_in_.data(), chunk);
crc_ = Crc32Update(crc_, pending_in_.data(), chunk);
isize_ += (uint32_t)chunk;
EncodeBlock(view.data(), hist_.size(), chunk, last);
if (last) final_emitted_ = true;
hist_.append(pending_in_.data(), chunk);
if (hist_.size() > 32768) hist_.erase(0, hist_.size() - 32768);
pending_in_.erase(0, chunk);
}
if (finish && !trailer_done_ && pending_in_.empty()) {
if (!final_emitted_) {
// Empty input (or an exact block multiple): one final fixed block
// holding only end-of-block.
EncodeBlock(hist_.data(), hist_.size(), 0, true);
final_emitted_ = true;
}
FlushBits();
char tr[8];
uint32_t crc = crc_ ^ 0xffffffffu;
tr[0] = (char)(crc & 0xff);
tr[1] = (char)((crc >> 8) & 0xff);
tr[2] = (char)((crc >> 16) & 0xff);
tr[3] = (char)((crc >> 24) & 0xff);
tr[4] = (char)(isize_ & 0xff);
tr[5] = (char)((isize_ >> 8) & 0xff);
tr[6] = (char)((isize_ >> 16) & 0xff);
tr[7] = (char)((isize_ >> 24) & 0xff);
stage_.append(tr, 8);
trailer_done_ = true;
finished_ = true;
} else if (finish && !pending_in_.empty()) {
// Unreachable: the loop above drains pending when finish is set.
r.status = CodecStatus::Error;
return r;
}
std::size_t k = stage_.size() < cap ? stage_.size() : cap;
if (k > 0) {
memcpy(out, stage_.data(), k);
stage_.erase(0, k);
}
r.out_used = k;
if (finished_ && stage_.empty()) {
r.status = CodecStatus::Done;
} else if (!stage_.empty()) {
r.status = CodecStatus::NeedOutput;
} else {
r.status = CodecStatus::Ok;
}
return r;
}
// ----- Decoder -----
void GzipDecoder::reset() {
staging_.clear();
bytepos_ = 0;
bitpos_ = 0;
header_done_ = false;
stream_end_ = false;
done_ = false;
crc_ = 0xffffffffu;
isize_ = 0;
total_out_ = 0;
total_in_ = 0;
window_.clear();
fixed_ready_ = false;
dynamic_ready_ = false;
in_block_ = false;
block_final_ = false;
block_type_ = 0;
stored_left_ = 0;
match_stage_ = MatchStage::None;
match_len_ = 0;
match_dist_ = 0;
len_extra_ = 0;
dist_extra_ = 0;
}
bool GzipDecoder::BitReader::need(std::size_t k) {
while (have_ < k && bytepos_ < len_) {
acc_ |= (uint64_t)(unsigned char)data_[bytepos_++] << have_;
have_ += 8;
if (have_ >= 56) break;
}
return have_ >= k;
}
void GzipDecoder::BitReader::rewind(std::size_t bp) {
bitpos_ = bp;
bytepos_ = bp / 8;
acc_ = 0;
have_ = 0;
std::size_t skip = bp % 8;
if (skip > 0 && bytepos_ < len_) {
acc_ = (uint64_t)((unsigned char)data_[bytepos_++] >> skip);
have_ = 8 - skip;
}
}
uint32_t GzipDecoder::BitReader::take(std::size_t k) {
uint32_t v = 0;
if (k > 0) {
v = (uint32_t)(acc_ & ((k >= 64) ? ~0ull : ((1ull << k) - 1ull)));
acc_ >>= k;
have_ -= k;
bitpos_ += k;
}
return v;
}
bool GzipDecoder::HuffmanTable::build(const std::vector<int>& lengths) {
count.fill(0);
offset.fill(0);
for (int l : lengths) {
if (l < 0 || l > 15) return false;
if (l > 0) count[(std::size_t)l]++;
}
int offs = 0;
for (int len = 1; len <= 15; ++len) {
offset[(std::size_t)len] = offs;
offs += count[(std::size_t)len];
}
// Over-subscribed check.
int left = 1;
for (int len = 1; len <= 15; ++len) {
left <<= 1;
left -= count[(std::size_t)len];
if (left < 0) return false;
}
symbols.assign((std::size_t)offs, 0);
std::array<int, 16> next = offset;
for (std::size_t s = 0; s < lengths.size(); ++s) {
int l = lengths[s];
if (l > 0) symbols[(std::size_t)next[(std::size_t)l]++] = (int)s;
}
return true;
}
int GzipDecoder::HuffmanTable::decode(BitReader& br) const {
std::size_t snap = br.bitpos();
int code = 0;
int first = 0;
int index = 0;
for (int len = 1; len <= 15; ++len) {
if (!br.need(1)) {
br.rewind(snap);
return -2;
}
code |= (int)br.take(1);
int cnt = count[(std::size_t)len];
if (code - first < cnt) {
std::size_t pos = (std::size_t)(offset[(std::size_t)len] + code - first);
if (pos >= symbols.size()) return -1;
return symbols[pos];
}
index += cnt;
first = (first + cnt) << 1;
code <<= 1;
(void)index;
}
return -1;
}
bool GzipDecoder::BuildFixed() {
if (fixed_ready_) return true;
std::vector<int> lit(288, 0);
for (int i = 0; i <= 143; ++i) lit[(std::size_t)i] = 8;
for (int i = 144; i <= 255; ++i) lit[(std::size_t)i] = 9;
for (int i = 256; i <= 279; ++i) lit[(std::size_t)i] = 7;
for (int i = 280; i <= 287; ++i) lit[(std::size_t)i] = 8;
std::vector<int> dist(32, 5);
if (!litlen_.build(lit)) return false;
if (!dist_.build(dist)) return false;
fixed_ready_ = true;
dynamic_ready_ = false;
return true;
}
bool GzipDecoder::BuildDynamic(BitReader& br) {
if (!br.need(14)) return false;
int hlit = (int)br.take(5) + 257;
int hdist = (int)br.take(5) + 1;
int hclen = (int)br.take(4) + 4;
if (hlit > 288 || hdist > 32) return false;
std::vector<int> cl_lens(19, 0);
for (int i = 0; i < hclen; ++i) {
if (!br.need(3)) return false;
cl_lens[(std::size_t)kClOrder[i]] = (int)br.take(3);
}
HuffmanTable cl;
if (!cl.build(cl_lens)) return false;
std::vector<int> lens((std::size_t)(hlit + hdist), 0);
std::size_t i = 0;
while (i < lens.size()) {
int sym = cl.decode(br);
if (sym < 0) return false;
if (sym <= 15) {
lens[i++] = sym;
} else if (sym == 16) {
if (i == 0) return false;
if (!br.need(2)) return false;
int rep = (int)br.take(2) + 3;
if (i + (std::size_t)rep > lens.size()) return false;
int prev = lens[i - 1];
for (int k = 0; k < rep; ++k) lens[i++] = prev;
} else if (sym == 17) {
if (!br.need(3)) return false;
int rep = (int)br.take(3) + 3;
if (i + (std::size_t)rep > lens.size()) return false;
for (int k = 0; k < rep; ++k) lens[i++] = 0;
} else if (sym == 18) {
if (!br.need(7)) return false;
int rep = (int)br.take(7) + 11;
if (i + (std::size_t)rep > lens.size()) return false;
for (int k = 0; k < rep; ++k) lens[i++] = 0;
} else {
return false;
}
}
// End-of-block code must exist.
if (lens[256] == 0) return false;
std::vector<int> lit(lens.begin(), lens.begin() + hlit);
std::vector<int> dist(lens.begin() + hlit, lens.end());
if (!litlen_.build(lit)) return false;
if (!dist_.build(dist)) return false;
dynamic_ready_ = true;
fixed_ready_ = false;
return true;
}
// 1 = parsed, 0 = need more input, -1 = corrupt.
int GzipDecoder::ParseHeader() {
if (staging_.size() - bytepos_ < 10) return 0;
const char* d = staging_.data() + bytepos_;
if ((unsigned char)d[0] != 0x1f || (unsigned char)d[1] != 0x8b) {
return -1;
}
if ((unsigned char)d[2] != 0x08) return -1; // CM must be deflate
unsigned flg = (unsigned char)d[3];
if (flg & 0xe0) return -1; // reserved bits
std::size_t pos = bytepos_ + 10;
if (flg & 0x04) {
if (staging_.size() - pos < 2) return 0;
uint16_t xlen = (uint16_t)((unsigned char)staging_[pos] |
((unsigned char)staging_[pos + 1] << 8));
pos += 2;
if (staging_.size() - pos < xlen) return 0;
pos += xlen;
}
if (flg & 0x08) {
std::size_t end = staging_.find('\0', pos);
if (end == std::string::npos) return 0;
if (end - pos > caps::kHeaderLineMax) return -1;
pos = end + 1;
}
if (flg & 0x10) {
std::size_t end = staging_.find('\0', pos);
if (end == std::string::npos) return 0;
if (end - pos > caps::kHeaderLineMax) return -1;
pos = end + 1;
}
if (flg & 0x02) {
if (staging_.size() - pos < 2) return 0;
pos += 2; // FHCRC noted but not verified (header integrity only)
}
bytepos_ = pos;
bitpos_ = pos * 8;
header_done_ = true;
return 1;
}
CodecStatus GzipDecoder::Inflate(char* out, std::size_t cap,
std::size_t& produced, bool finish) {
produced = 0;
BitReader br(staging_.data(), staging_.size(), bitpos_);
auto emit = [&](char b) -> bool {
if (produced >= cap) return false;
if (total_out_ >= caps::kDecompressMaxAbsolute) return false;
out[produced++] = b;
crc_ = Crc32Update(crc_, &b, 1);
++isize_;
++total_out_;
window_.push_back(b);
if (window_.size() > 36864) window_.erase(0, window_.size() - 32768);
return true;
};
for (;;) {
if (!in_block_) {
if (!br.need(3)) {
bitpos_ = br.bitpos();
return finish ? CodecStatus::Error : CodecStatus::Ok;
}
std::size_t snap = br.bitpos();
block_final_ = br.take(1) != 0;
block_type_ = (int)br.take(2);
if (block_type_ == 3) return CodecStatus::Error;
if (block_type_ == 2) {
if (!BuildDynamic(br)) {
bitpos_ = snap; // truncated or corrupt header: retry/end
return finish ? CodecStatus::Error : CodecStatus::Ok;
}
} else if (block_type_ == 1) {
if (!BuildFixed()) return CodecStatus::Error;
} else {
stored_left_ = 0;
}
match_stage_ = MatchStage::None;
match_len_ = 0;
match_dist_ = 0;
in_block_ = true;
}
if (block_type_ == 0) {
// Stored: align, LEN/NLEN, then raw bytes.
std::size_t skip = (8 - (br.bitpos() % 8)) % 8;
if (stored_left_ == 0 && match_stage_ == MatchStage::None) {
if (!br.need(skip + 32)) {
bitpos_ = br.bitpos();
return finish ? CodecStatus::Error : CodecStatus::Ok;
}
if (skip > 0) br.take(skip);
uint32_t len = br.take(16);
uint32_t nlen = br.take(16);
if (((len ^ nlen) & 0xffff) != 0xffff) return CodecStatus::Error;
stored_left_ = len;
match_stage_ = MatchStage::Copying;
if (len == 0) match_stage_ = MatchStage::None;
}
while (stored_left_ > 0) {
if (produced >= cap || total_out_ >= caps::kDecompressMaxAbsolute) {
bitpos_ = br.bitpos();
if (total_out_ >= caps::kDecompressMaxAbsolute) {
return CodecStatus::Error; // bomb guard
}
return CodecStatus::NeedOutput;
}
if (!br.need(8)) {
bitpos_ = br.bitpos();
return finish ? CodecStatus::Error : CodecStatus::Ok;
}
char b = (char)br.take(8);
--stored_left_;
emit(b);
}
match_stage_ = MatchStage::None;
in_block_ = false;
if (block_final_) {
bitpos_ = br.bitpos();
stream_end_ = true;
return CodecStatus::Done;
}
continue;
}
// Huffman-coded block.
for (;;) {
if (match_stage_ == MatchStage::None) {
int sym = litlen_.decode(br);
if (sym == -2) {
bitpos_ = br.bitpos();
return finish ? CodecStatus::Error : CodecStatus::Ok;
}
if (sym < 0 || sym > 285) return CodecStatus::Error;
if (sym < 256) {
if (produced >= cap ||
total_out_ >= caps::kDecompressMaxAbsolute) {
bitpos_ = br.bitpos();
// Rewind one symbol: the bitpos already passed it. The symbol
// is a literal, recompute is impossible, so emit-or-fail here:
// cap-full with a decoded literal means output stall. Since the
// literal byte is known, stash it via match state.
match_stage_ = MatchStage::Copying;
match_len_ = 1;
// Stash the literal in match_dist_ (flagged, low byte).
match_dist_ = (uint32_t)(unsigned char)sym | 0x80000000u;
if (total_out_ >= caps::kDecompressMaxAbsolute) {
return CodecStatus::Error;
}
return CodecStatus::NeedOutput;
}
emit((char)sym);
continue;
}
if (sym == 256) {
in_block_ = false;
if (block_final_) {
bitpos_ = br.bitpos();
stream_end_ = true;
return CodecStatus::Done;
}
break; // next block header
}
int li = sym - 257;
match_len_ = kLenBase[li];
len_extra_ = kLenExtra[li];
match_stage_ = MatchStage::LenExtra;
}
if (match_stage_ == MatchStage::LenExtra) {
if (len_extra_ > 0) {
if (!br.need((std::size_t)len_extra_)) {
bitpos_ = br.bitpos();
return finish ? CodecStatus::Error : CodecStatus::Ok;
}
match_len_ += br.take((std::size_t)len_extra_);
}
match_stage_ = MatchStage::DistSym;
}
if (match_stage_ == MatchStage::DistSym) {
int dsym = dist_.decode(br);
if (dsym == -2) {
bitpos_ = br.bitpos();
return finish ? CodecStatus::Error : CodecStatus::Ok;
}
if (dsym < 0 || dsym > 29) return CodecStatus::Error;
match_dist_ = kDistBase[dsym];
dist_extra_ = kDistExtra[dsym];
match_stage_ = MatchStage::DistExtra;
}
if (match_stage_ == MatchStage::DistExtra) {
if (dist_extra_ > 0) {
if (!br.need((std::size_t)dist_extra_)) {
bitpos_ = br.bitpos();
return finish ? CodecStatus::Error : CodecStatus::Ok;
}
match_dist_ += br.take((std::size_t)dist_extra_);
}
if (match_dist_ == 0 || match_dist_ > window_.size()) {
return CodecStatus::Error;
}
match_stage_ = MatchStage::Copying;
}
if (match_stage_ == MatchStage::Copying) {
if (match_dist_ & 0x80000000u) {
// Stashed literal (cap stall path above).
char b = (char)(match_dist_ & 0xff);
match_dist_ = 0;
match_len_ = 0;
match_stage_ = MatchStage::None;
if (!emit(b)) {
bitpos_ = br.bitpos();
return CodecStatus::NeedOutput;
}
continue;
}
while (match_len_ > 0) {
if (produced >= cap ||
total_out_ >= caps::kDecompressMaxAbsolute) {
bitpos_ = br.bitpos();
if (total_out_ >= caps::kDecompressMaxAbsolute) {
return CodecStatus::Error;
}
return CodecStatus::NeedOutput;
}
char b = window_[window_.size() - match_dist_];
--match_len_;
emit(b);
}
match_stage_ = MatchStage::None;
}
}
}
}
CodecResult GzipDecoder::feed(const char* in, std::size_t n, char* out,
std::size_t cap, bool finish) {
CodecResult r;
if (done_) {
r.status = CodecStatus::Error;
return r;
}
if (n > 0 && in != nullptr) {
if (staging_.size() + n > caps::kBodyMaxCeiling) {
r.status = CodecStatus::Error;
return r;
}
staging_.append(in, n);
total_in_ += n;
}
r.in_used = n;
if (!header_done_) {
int ph = ParseHeader();
if (ph < 0 || (ph == 0 && finish)) {
r.status = CodecStatus::Error;
return r;
}
if (ph == 0) {
r.status = CodecStatus::Ok;
return r;
}
}
if (!stream_end_) {
std::size_t produced = 0;
CodecStatus st = Inflate(out, cap, produced, finish);
r.out_used = produced;
if (st == CodecStatus::Error) {
r.status = CodecStatus::Error;
return r;
}
if (st == CodecStatus::NeedOutput) {
r.status = CodecStatus::NeedOutput;
return r;
}
// Ok means need more input; Done falls through to the trailer.
if (st == CodecStatus::Ok) {
r.status = CodecStatus::Ok;
return r;
}
}
// Trailer: 8 bytes after the final block (byte aligned).
std::size_t tpos = (bitpos_ + 7) / 8;
if (staging_.size() - tpos < 8) {
r.status = finish ? CodecStatus::Error : CodecStatus::Ok;
return r;
}
const char* d = staging_.data() + tpos;
uint32_t want_crc = (uint32_t)(unsigned char)d[0] |
((uint32_t)(unsigned char)d[1] << 8) |
((uint32_t)(unsigned char)d[2] << 16) |
((uint32_t)(unsigned char)d[3] << 24);
uint32_t want_size = (uint32_t)(unsigned char)d[4] |
((uint32_t)(unsigned char)d[5] << 8) |
((uint32_t)(unsigned char)d[6] << 16) |
((uint32_t)(unsigned char)d[7] << 24);
if ((crc_ ^ 0xffffffffu) != want_crc || isize_ != want_size) {
r.status = CodecStatus::Error;
return r;
}
if (total_out_ > caps::kDecompressMaxAbsolute ||
(total_in_ > 0 &&
total_out_ > total_in_ * caps::kDecompressMaxRatio)) {
r.status = CodecStatus::Error;
return r;
}
if (staging_.size() - tpos != 8) {
// Trailing bytes: multi-member gzip is out of v1 scope.
r.status = CodecStatus::Error;
return r;
}
done_ = true;
r.status = CodecStatus::Done;
return r;
}
} // namespace molo::runtime::compress
+141
View File
@@ -0,0 +1,141 @@
// Spec: plans/01-runtime.md 6.5, RFC 1952. In-house gzip. The encoder runs
// greedy LZ77 plus fixed-Huffman DEFLATE blocks: real compression,
// streaming, bounded. The decoder implements full inflate (stored, fixed
// and dynamic Huffman) plus gzip framing, CRC32 and ISIZE checks, and the
// bomb guard.
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
#include "compressor.h"
namespace molo::runtime::compress {
uint32_t Crc32Update(uint32_t crc, const char* data, std::size_t len);
inline uint32_t Crc32(const char* data, std::size_t len) {
return Crc32Update(0xffffffffu, data, len) ^ 0xffffffffu;
}
class GzipEncoder : public IEncoder {
public:
GzipEncoder();
CodecResult feed(const char* in, std::size_t n, char* out, std::size_t cap,
bool finish) override;
void reset() override;
const char* name() const override { return "gzip"; }
private:
void PutBit(int bit);
void PutBitsLsb(uint32_t bits, int count);
void PutCode(uint32_t code, int len); // Huffman codes go MSB-first
void PutLitLen(int sym);
void PutDist(int sym);
void FlushBits();
void EncodeBlock(const char* data, std::size_t start, std::size_t len,
bool last);
static int Hash3(const char* p);
bool header_done_ = false;
bool trailer_done_ = false;
bool finished_ = false;
bool final_emitted_ = false;
std::string pending_in_;
std::string hist_; // last 32 KiB of encoded input for matches
std::string stage_;
uint32_t crc_ = 0xffffffffu;
uint32_t isize_ = 0;
uint32_t acc_ = 0;
int have_ = 0;
// LZ77 hash chains over the current view (hist_ + block).
std::vector<int> head_;
std::vector<int> prev_;
};
class GzipDecoder : public IDecoder {
public:
GzipDecoder() = default;
CodecResult feed(const char* in, std::size_t n, char* out, std::size_t cap,
bool finish) override;
void reset() override;
const char* name() const override { return "gzip"; }
private:
class BitReader;
struct HuffmanTable {
// Canonical decode: count[len], offset[len], symbols in code order.
std::array<int, 16> count{};
std::array<int, 16> offset{};
std::vector<int> symbols;
bool build(const std::vector<int>& lengths);
// Symbol, or -1 invalid, or -2 truncated (resume from same bitpos).
int decode(BitReader& br) const;
};
class BitReader {
public:
BitReader(const char* data, std::size_t len, std::size_t bitpos)
: data_(data), len_(len), bitpos_(bitpos) {
bytepos_ = bitpos_ / 8;
std::size_t skip = bitpos_ % 8;
if (skip > 0 && bytepos_ < len_) {
acc_ = (uint64_t)((unsigned char)data_[bytepos_++] >> skip);
have_ = 8 - skip;
}
}
bool need(std::size_t k);
uint32_t take(std::size_t k); // caller ensures need(k), k <= 24
void rewind(std::size_t bp); // restore a snapshot bit position
std::size_t bitpos() const { return bitpos_; }
std::size_t bits_left() const {
return len_ * 8 > bitpos_ ? len_ * 8 - bitpos_ : 0;
}
private:
const char* data_;
std::size_t len_;
std::size_t bitpos_;
uint64_t acc_ = 0;
std::size_t have_ = 0;
std::size_t bytepos_ = 0;
};
enum class MatchStage { None, LenExtra, DistSym, DistExtra, Copying };
int ParseHeader(); // 1 = parsed, 0 = need more, -1 = corrupt
CodecStatus Inflate(char* out, std::size_t cap, std::size_t& produced,
bool finish);
bool BuildFixed();
bool BuildDynamic(BitReader& br);
std::string staging_;
std::size_t bytepos_ = 0; // header/trailer cursor
std::size_t bitpos_ = 0; // inflate cursor (absolute bits in staging_)
bool header_done_ = false;
bool stream_end_ = false;
bool done_ = false;
uint32_t crc_ = 0xffffffffu;
uint32_t isize_ = 0;
std::size_t total_out_ = 0;
std::size_t total_in_ = 0;
std::string window_; // last 32 KiB of output for matches
HuffmanTable litlen_;
HuffmanTable dist_;
bool fixed_ready_ = false;
bool dynamic_ready_ = false;
// Persistent block/match state across feed calls.
bool in_block_ = false;
bool block_final_ = false;
int block_type_ = 0;
std::size_t stored_left_ = 0;
MatchStage match_stage_ = MatchStage::None;
uint32_t match_len_ = 0;
uint32_t match_dist_ = 0;
int len_extra_ = 0;
int dist_extra_ = 0;
};
} // namespace molo::runtime::compress
+167
View File
@@ -0,0 +1,167 @@
// Spec: plans/01-runtime.md 6.5. Accept-Encoding negotiation.
#include "negotiate.h"
#include <cctype>
#include "runtime/caps.h"
namespace molo::runtime::compress {
namespace {
std::string Lower(std::string_view s) {
std::string out(s);
for (char& c : out) c = (char)tolower((unsigned char)c);
return out;
}
std::string Trim(const std::string& s) {
std::size_t b = 0;
while (b < s.size() && (s[b] == ' ' || s[b] == '\t')) ++b;
std::size_t e = s.size();
while (e > b && (s[e - 1] == ' ' || s[e - 1] == '\t')) --e;
return s.substr(b, e - b);
}
// Parses qvalue 0..1 into 0..1000. Returns -1 when malformed.
int ParseQ(const std::string& s) {
if (s.empty() || s[0] != 'q' || s.size() < 3 || s[1] != '=') return -1;
std::string v = s.substr(2);
if (v == "1" || v == "1." || v == "1.0" || v == "1.00" || v == "1.000") {
return 1000;
}
if (v == "0" || v == "0." || v == "0.0" || v == "0.00" || v == "0.000") {
return 0;
}
if (v.size() < 2 || v[0] != '0' || v[1] != '.') return -1;
std::string frac = v.substr(2);
if (frac.empty() || frac.size() > 3) return -1;
for (char c : frac) {
if (c < '0' || c > '9') return -1;
}
while (frac.size() < 3) frac.push_back('0');
return (frac[0] - '0') * 100 + (frac[1] - '0') * 10 + (frac[2] - '0');
}
struct Offers {
int br = -1; // -1 = absent
int gzip = -1;
int wildcard = -1;
};
Offers ParseAcceptEncoding(std::string_view header) {
Offers o;
std::string h(header);
std::size_t pos = 0;
while (pos <= h.size()) {
std::size_t comma = h.find(',', pos);
if (comma == std::string::npos) comma = h.size();
std::string item = Trim(h.substr(pos, comma - pos));
pos = comma + 1;
if (item.empty()) continue;
std::size_t semi = item.find(';');
std::string token = Lower(Trim(item.substr(0, semi)));
int q = 1000;
if (semi != std::string::npos) {
std::string param = Trim(item.substr(semi + 1));
// Only the first parameter matters; it should be q.
std::size_t semi2 = param.find(';');
param = Trim(param.substr(0, semi2));
int parsed = ParseQ(Lower(param));
if (parsed >= 0) q = parsed;
}
if (token == "br") {
if (o.br < 0) o.br = q;
} else if (token == "gzip" || token == "x-gzip") {
if (o.gzip < 0) o.gzip = q;
} else if (token == "*") {
if (o.wildcard < 0) o.wildcard = q;
}
}
return o;
}
} // namespace
bool Negotiator::IsCompressible(std::string_view content_type) {
std::string ct = Lower(content_type);
std::size_t semi = ct.find(';');
if (semi != std::string::npos) ct = Trim(ct.substr(0, semi));
if (ct.compare(0, 5, "text/") == 0) return true;
if (ct == "application/json" || ct == "application/xml" ||
ct == "application/javascript" || ct == "application/x-javascript" ||
ct == "application/ecmascript" || ct == "application/wasm" ||
ct == "image/svg+xml") {
return true;
}
// Structured syntax suffixes.
if (ct.size() > 5 &&
(ct.compare(ct.size() - 5, 5, "+json") == 0 ||
ct.compare(ct.size() - 4, 4, "+xml") == 0)) {
return true;
}
return false;
}
Decision Negotiator::Negotiate(std::string_view accept_encoding,
std::string_view content_type,
std::size_t body_len, bool has_content_encoding,
bool is_head, int status) {
Decision d{"identity"};
if (is_head || has_content_encoding) return d;
if (status == 204 || status == 304 || (status >= 100 && status < 200)) {
return d;
}
if (body_len < caps::kCompressThreshold) return d;
if (!IsCompressible(content_type)) return d;
Offers o = ParseAcceptEncoding(accept_encoding);
int br_q = o.br >= 0 ? o.br : o.wildcard;
int gzip_q = o.gzip >= 0 ? o.gzip : o.wildcard;
if (br_q <= 0 && gzip_q <= 0) return d;
// br wins ties.
if (br_q >= gzip_q && br_q > 0) {
d.encoding = "br";
return d;
}
if (gzip_q > 0) {
d.encoding = "gzip";
return d;
}
return d;
}
Precompressed Negotiator::SelectPrecompressed(std::string_view accept_encoding,
bool has_gz, bool has_br) {
Precompressed p;
if (!has_gz && !has_br) return p;
Offers o = ParseAcceptEncoding(accept_encoding);
int br_q = o.br >= 0 ? o.br : o.wildcard;
int gzip_q = o.gzip >= 0 ? o.gzip : o.wildcard;
if (has_br && br_q > 0 && br_q >= gzip_q) {
p.found = true;
p.suffix = ".br";
p.encoding = "br";
return p;
}
if (has_gz && gzip_q > 0) {
p.found = true;
p.suffix = ".gz";
p.encoding = "gzip";
return p;
}
// Fallthrough: br available but gzip ranked higher is handled above; if
// only br exists and gzip outranks it, still serve br when acceptable.
if (has_br && br_q > 0 && !has_gz) {
p.found = true;
p.suffix = ".br";
p.encoding = "br";
return p;
}
if (has_gz && gzip_q > 0 && !has_br) {
p.found = true;
p.suffix = ".gz";
p.encoding = "gzip";
return p;
}
return p;
}
} // namespace molo::runtime::compress
+33
View File
@@ -0,0 +1,33 @@
// Spec: plans/01-runtime.md 6.5. Accept-Encoding negotiation: br over gzip
// on tied q-values, 1024-byte threshold, content-type allowlist, Vary.
// Never compresses 204/304, HEAD, or already-encoded bodies.
#pragma once
#include <string>
#include <string_view>
namespace molo::runtime::compress {
struct Decision {
std::string encoding; // "br", "gzip", or "identity"
};
struct Precompressed {
bool found = false;
std::string suffix; // ".br" or ".gz"
std::string encoding; // "br" or "gzip"
};
class Negotiator {
public:
static Decision Negotiate(std::string_view accept_encoding,
std::string_view content_type, std::size_t body_len,
bool has_content_encoding, bool is_head,
int status);
static bool IsCompressible(std::string_view content_type);
static Precompressed SelectPrecompressed(std::string_view accept_encoding,
bool has_gz, bool has_br);
static const char* VaryValue() { return "Accept-Encoding"; }
};
} // namespace molo::runtime::compress
+153
View File
@@ -0,0 +1,153 @@
// Spec: plans/01-runtime.md 6.6, RFC 6265 sections 4.1, 4.2, 5.2, 5.3.
#include "cookie.h"
#include <cctype>
#include "runtime/caps.h"
namespace molo::runtime::cookie {
bool IsValidCookieName(std::string_view name) {
if (name.empty() || name.size() > 1024) return false;
for (char c : name) {
unsigned char u = (unsigned char)c;
if (u < 0x21 || u > 0x7e) return false;
switch (c) {
case '(':
case ')':
case '<':
case '>':
case '@':
case ',':
case ';':
case ':':
case '\\':
case '"':
case '/':
case '[':
case ']':
case '?':
case '=':
case '{':
case '}':
case ' ':
case '\t':
return false;
default:
break;
}
}
return true;
}
bool IsValidCookieValue(std::string_view value, bool& needs_quotes) {
needs_quotes = false;
if (value.size() > (std::size_t)caps::kSetCookieMax) return false;
if (value.empty()) return true;
for (char c : value) {
unsigned char u = (unsigned char)c;
if (u < 0x21 || u == 0x22 || u == 0x2c || u == 0x3b || u == 0x5c ||
u == 0x7f) {
if (u == 0x20) {
needs_quotes = true;
continue;
}
return false;
}
}
return true;
}
CookieError ParseCookieHeader(
std::string_view header,
std::vector<std::pair<std::string, std::string>>& out) {
std::size_t pos = 0;
auto trim = [](std::string_view s) {
while (!s.empty() && (s.front() == ' ' || s.front() == '\t')) {
s.remove_prefix(1);
}
while (!s.empty() && (s.back() == ' ' || s.back() == '\t')) {
s.remove_suffix(1);
}
return s;
};
while (pos <= header.size()) {
std::size_t semi = header.find(';', pos);
if (semi == std::string_view::npos) semi = header.size();
std::string_view pair = trim(header.substr(pos, semi - pos));
pos = semi + 1;
if (pair.empty()) continue;
std::size_t eq = pair.find('=');
std::string name, value;
if (eq == std::string_view::npos) {
name = std::string(trim(pair));
} else {
name = std::string(trim(pair.substr(0, eq)));
value = std::string(trim(pair.substr(eq + 1)));
}
if (!IsValidCookieName(name)) return CookieError::BadName;
if (value.size() >= 2 && value.front() == '"' && value.back() == '"') {
value = value.substr(1, value.size() - 2);
}
bool quotes = false;
if (!IsValidCookieValue(value, quotes)) return CookieError::BadValue;
out.emplace_back(std::move(name), std::move(value));
if (out.size() > caps::kCookieCountMax) return CookieError::TooLarge;
}
return CookieError::Ok;
}
CookieError SerializeSetCookie(const Cookie& c, std::string& out) {
if (!IsValidCookieName(c.name)) return CookieError::BadName;
bool quotes = false;
if (!IsValidCookieValue(c.value, quotes)) return CookieError::BadValue;
if (c.same_site == SameSite::None && !c.secure) {
return CookieError::BadValue; // None requires Secure
}
out.clear();
out.append(c.name);
out.push_back('=');
if (quotes) out.push_back('"');
out.append(c.value);
if (quotes) out.push_back('"');
if (!c.domain.empty()) {
out.append("; Domain=");
out.append(c.domain);
}
if (!c.path.empty()) {
out.append("; Path=");
out.append(c.path);
}
if (c.max_age >= 0) {
out.append("; Max-Age=");
out.append(std::to_string(c.max_age));
}
if (!c.expires.empty()) {
out.append("; Expires=");
out.append(c.expires);
}
if (c.secure) out.append("; Secure");
if (c.http_only) out.append("; HttpOnly");
if (c.has_same_site) {
out.append("; SameSite=");
switch (c.same_site) {
case SameSite::None:
out.append("None");
break;
case SameSite::Lax:
out.append("Lax");
break;
case SameSite::Strict:
out.append("Strict");
break;
}
}
if (c.partitioned) out.append("; Partitioned");
if (out.size() > caps::kSetCookieMax) {
out.clear();
return CookieError::TooLarge; // never silently truncated
}
return CookieError::Ok;
}
} // namespace molo::runtime::cookie
+49
View File
@@ -0,0 +1,49 @@
// Spec: plans/01-runtime.md 6.6, RFC 6265. Cookie transport only: parse and
// serialize. Signing/encryption crypto lives in 03-security.md; the ISigner
// interface is in signer.h.
#pragma once
#include <cstddef>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace molo::runtime::cookie {
enum class SameSite { None, Lax, Strict };
struct Cookie {
std::string name;
std::string value;
std::string domain;
std::string path;
long long max_age = -1; // -1 = absent
std::string expires; // already formatted, empty = absent
bool secure = false;
bool http_only = true; // default on per R9
SameSite same_site = SameSite::Lax; // default per R9
bool has_same_site = true;
bool partitioned = false;
};
enum class CookieError {
Ok,
BadName,
BadValue,
TooLarge,
};
// Parses one Cookie request header value into pairs. Lenient about spacing,
// strict about control characters. Stops at the first bad pair.
CookieError ParseCookieHeader(std::string_view header,
std::vector<std::pair<std::string, std::string>>& out);
// Serializes one Set-Cookie line (no folding, never joined).
CookieError SerializeSetCookie(const Cookie& c, std::string& out);
bool IsValidCookieName(std::string_view name);
bool IsValidCookieValue(std::string_view value, bool& needs_quotes);
} // namespace molo::runtime::cookie
+62
View File
@@ -0,0 +1,62 @@
// Spec: plans/01-runtime.md 6.6. Cookie jar.
#include "jar.h"
#include "runtime/caps.h"
namespace molo::runtime::cookie {
JarError CookieJar::ParseRequest(
const std::vector<std::string>& cookie_fields) {
cookies_.clear();
std::size_t total = 0;
std::string joined;
for (std::size_t i = 0; i < cookie_fields.size(); ++i) {
total += cookie_fields[i].size();
if (total > caps::kCookieTotalMax) return JarError::TooLarge;
if (i > 0) joined.append("; ");
joined.append(cookie_fields[i]);
}
if (joined.size() > caps::kCookieTotalMax) return JarError::TooLarge;
std::vector<std::pair<std::string, std::string>> pairs;
CookieError e = ParseCookieHeader(joined, pairs);
if (e == CookieError::TooLarge) return JarError::TooMany;
if (e != CookieError::Ok) return JarError::BadCookie;
if (pairs.size() > caps::kCookieCountMax) return JarError::TooMany;
cookies_ = std::move(pairs);
return JarError::Ok;
}
std::string CookieJar::Get(std::string_view name) const {
for (const auto& kv : cookies_) {
if (kv.first == name) return kv.second;
}
return "";
}
bool CookieJar::VerifySigned(std::string_view name, ISigner& signer,
std::string& payload_out) const {
std::string v = Get(name);
if (v.empty()) return false;
return signer.Verify(v, payload_out);
}
JarError CookieJar::Set(Cookie c) {
if (secure_context_) c.secure = true; // forced on https
std::string line;
CookieError e = SerializeSetCookie(c, line);
if (e == CookieError::TooLarge) return JarError::TooLarge;
if (e != CookieError::Ok) return JarError::BadCookie;
set_headers_.push_back(std::move(line));
return JarError::Ok;
}
JarError CookieJar::SetSigned(std::string name, std::string payload,
ISigner& signer, std::string path) {
Cookie c;
c.name = std::move(name);
c.value = signer.Sign(payload);
c.path = std::move(path);
return Set(std::move(c));
}
} // namespace molo::runtime::cookie
+53
View File
@@ -0,0 +1,53 @@
// Spec: plans/01-runtime.md 6.6. Request jar (joins H2-split Cookie fields
// with "; ", caps 50 cookies and 8 KB, overflow is 431) and response list
// (one Set-Cookie per cookie, never joined). Secure forced on https.
#pragma once
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "cookie.h"
#include "signer.h"
namespace molo::runtime::cookie {
enum class JarError {
Ok,
TooMany, // maps to 431
TooLarge, // maps to 431
BadCookie, // maps to 400
};
class CookieJar {
public:
explicit CookieJar(bool secure_context = false)
: secure_context_(secure_context) {}
// Parses request Cookie fields. H2 may split one logical Cookie across
// several fields: callers pass them all and the jar joins with "; ".
JarError ParseRequest(const std::vector<std::string>& cookie_fields);
std::string Get(std::string_view name) const;
bool VerifySigned(std::string_view name, ISigner& signer,
std::string& payload_out) const;
const std::vector<std::pair<std::string, std::string>>& cookies() const {
return cookies_;
}
void Clear() { cookies_.clear(); }
// Response side.
JarError Set(Cookie c); // applies Secure default in secure contexts
JarError SetSigned(std::string name, std::string payload, ISigner& signer,
std::string path = "/");
const std::vector<std::string>& set_cookie_headers() const {
return set_headers_;
}
private:
bool secure_context_;
std::vector<std::pair<std::string, std::string>> cookies_;
std::vector<std::string> set_headers_;
};
} // namespace molo::runtime::cookie
+45
View File
@@ -0,0 +1,45 @@
// Spec: plans/01-runtime.md 6.6. Cookie signing interface. Format:
// `v1.<base64url(payload)>.<base64url(tag)>`. Real HMAC and key hierarchy
// live in 03-security.md; NullSigner is a deterministic test-only signer,
// clearly marked insecure.
#pragma once
#include <optional>
#include <string>
#include <string_view>
#include "runtime/base64.h"
namespace molo::runtime::cookie {
class ISigner {
public:
virtual ~ISigner() = default;
virtual std::string Sign(std::string_view payload) = 0;
virtual bool Verify(std::string_view signed_value,
std::string& payload_out) = 0;
virtual const char* name() const = 0;
};
// Test-only signer: format-valid, cryptographically void. Never use in prod.
class NullSigner : public ISigner {
public:
std::string Sign(std::string_view payload) override {
return "v1." + Base64Url::Encode(payload) + ".bnVsbA";
}
bool Verify(std::string_view signed_value,
std::string& payload_out) override {
if (signed_value.compare(0, 3, "v1.") != 0) return false;
std::size_t dot = signed_value.find('.', 3);
if (dot == std::string_view::npos) return false;
if (signed_value.substr(dot + 1) != "bnVsbA") return false;
std::optional<std::string> p =
Base64Url::Decode(signed_value.substr(3, dot - 3));
if (!p.has_value()) return false;
payload_out = std::move(*p);
return true;
}
const char* name() const override { return "null-insecure-test-only"; }
};
} // namespace molo::runtime::cookie
+137
View File
@@ -0,0 +1,137 @@
// Spec: plans/01-runtime.md 6.1. One EventLoop per thread.
#include "event_loop.h"
#include "caps.h"
#include "poller_epoll.h"
#include "poller_iocp.h"
#include "poller_kqueue.h"
namespace molo::runtime {
std::unique_ptr<IPoller> MakePoller() {
#if defined(__linux__)
auto p = std::make_unique<EpollPoller>();
if (p->valid()) return p;
#elif defined(_WIN32)
auto p = std::make_unique<IocpPoller>();
if (p->valid()) return p;
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__OpenBSD__) || defined(__DragonFly__)
auto p = std::make_unique<KqueuePoller>();
if (p->valid()) return p;
#endif
// Last resort: try each backend in turn.
{
auto p = std::make_unique<EpollPoller>();
if (p->valid()) return p;
}
{
auto p = std::make_unique<KqueuePoller>();
if (p->valid()) return p;
}
{
auto p = std::make_unique<IocpPoller>();
if (p->valid()) return p;
}
return std::make_unique<EpollPoller>();
}
EventLoop::EventLoop()
: poller_(MakePoller()),
pool_(std::make_unique<ThreadPool>(caps::kThreadPoolThreadsDefault,
caps::kThreadPoolQueueCap)),
timers_(caps::kTimerGranularityMs, caps::kTimerWheelSlots) {}
EventLoop::~EventLoop() { stop(); }
bool EventLoop::defer(std::function<void()> fn) {
if (!fn) return false;
{
std::lock_guard<std::mutex> lock(mutex_);
if (deferred_.size() >= caps::kDeferredQueueCap) {
++deferred_dropped_;
return false;
}
deferred_.push_back(std::move(fn));
}
poller_->wake();
return true;
}
TimerWheel::TimerId EventLoop::schedule_after(std::chrono::milliseconds delay,
std::function<void()> fn) {
// Called from the loop thread or via defer; the wheel itself is not locked.
// Cross-thread callers must route through defer. This direct path serves
// the loop thread and tests that own the loop.
return timers_.add(delay, std::move(fn));
}
bool EventLoop::cancel_timer(TimerWheel::TimerId id) {
return timers_.cancel(id);
}
void EventLoop::run() {
loop_thread_ = std::this_thread::get_id();
while (run_once(-1)) {
}
}
bool EventLoop::run_once(int timeout_ms) {
{
std::lock_guard<std::mutex> lock(mutex_);
if (stop_) return false;
}
std::deque<std::function<void()>> work;
{
std::lock_guard<std::mutex> lock(mutex_);
work.swap(deferred_);
}
while (!work.empty()) {
auto fn = std::move(work.front());
work.pop_front();
fn();
}
timers_.run_due();
int wait_ms = timers_.next_expiry_ms();
if (timeout_ms >= 0 && (wait_ms < 0 || timeout_ms < wait_ms)) {
wait_ms = timeout_ms;
}
poller_->wait(wait_ms);
{
std::lock_guard<std::mutex> lock(mutex_);
if (stop_) return false;
}
return true;
}
void EventLoop::stop() {
{
std::lock_guard<std::mutex> lock(mutex_);
stop_ = true;
}
if (poller_) poller_->wake();
}
bool EventLoop::stopped() const {
std::lock_guard<std::mutex> lock(mutex_);
return stop_;
}
bool EventLoop::in_loop_thread() const {
return std::this_thread::get_id() == loop_thread_;
}
void EventLoop::post(Task<void> task) {
if (!task.valid()) return;
if (in_loop_thread()) {
Detach(std::move(task));
return;
}
auto holder = std::make_shared<Task<void>>(std::move(task));
defer([holder] {
Task<void> t = std::move(*holder);
Detach(std::move(t));
});
}
} // namespace molo::runtime
+98
View File
@@ -0,0 +1,98 @@
// Spec: plans/01-runtime.md 6.1. One EventLoop per thread. Connections are
// pinned to their loop; cross-loop contact goes through Channel<T>.
#pragma once
#include <chrono>
#include <coroutine>
#include <deque>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include "executor.h"
#include "poller.h"
#include "task.h"
#include "timer_wheel.h"
namespace molo::runtime {
class EventLoop {
public:
EventLoop();
~EventLoop();
EventLoop(const EventLoop&) = delete;
EventLoop& operator=(const EventLoop&) = delete;
IPoller& poller() { return *poller_; }
ThreadPool& pool() { return *pool_; }
TimerWheel& timers() { return timers_; }
// Thread-safe. Drops (returns false) past the deferred cap.
bool defer(std::function<void()> fn);
TimerWheel::TimerId schedule_after(std::chrono::milliseconds delay,
std::function<void()> fn);
bool cancel_timer(TimerWheel::TimerId id);
void run();
// One iteration. Returns false once stop() was requested.
bool run_once(int timeout_ms);
void stop();
bool stopped() const;
bool in_loop_thread() const;
// Awaitable: resume after at least d on this loop.
auto sleep(std::chrono::milliseconds d);
// Awaitable: resume on the next loop iteration.
auto yield();
// Start a coroutine on this loop (thread-safe).
void post(Task<void> task);
std::size_t deferred_dropped() const { return deferred_dropped_; }
private:
std::unique_ptr<IPoller> poller_;
std::unique_ptr<ThreadPool> pool_;
TimerWheel timers_;
mutable std::mutex mutex_;
std::deque<std::function<void()>> deferred_;
std::size_t deferred_dropped_ = 0;
bool stop_ = false;
std::thread::id loop_thread_;
};
class SleepAwaitable {
public:
SleepAwaitable(EventLoop& loop, std::chrono::milliseconds d)
: loop_(loop), delay_(d) {}
bool await_ready() const noexcept { return delay_.count() <= 0; }
void await_suspend(std::coroutine_handle<> h) {
loop_.schedule_after(delay_, [h] { h.resume(); });
}
void await_resume() noexcept {}
private:
EventLoop& loop_;
std::chrono::milliseconds delay_;
};
class YieldAwaitable {
public:
explicit YieldAwaitable(EventLoop& loop) : loop_(loop) {}
bool await_ready() const noexcept { return false; }
void await_suspend(std::coroutine_handle<> h) {
if (!loop_.defer([h] { h.resume(); })) h.resume();
}
void await_resume() noexcept {}
private:
EventLoop& loop_;
};
inline auto EventLoop::sleep(std::chrono::milliseconds d) {
return SleepAwaitable(*this, d);
}
inline auto EventLoop::yield() { return YieldAwaitable(*this); }
} // namespace molo::runtime
+76
View File
@@ -0,0 +1,76 @@
// Spec: plans/01-runtime.md 6.1. Bounded pool for blocking work.
#include "executor.h"
#include "caps.h"
namespace molo::runtime {
ThreadPool::ThreadPool(std::size_t threads, std::size_t queue_cap)
: queue_cap_(queue_cap) {
if (threads == 0) threads = 1;
if (threads > caps::kThreadPoolThreadsCeiling) {
threads = caps::kThreadPoolThreadsCeiling;
}
if (queue_cap_ == 0) queue_cap_ = 1;
for (std::size_t i = 0; i < threads; ++i) {
workers_.emplace_back([this] { Worker(); });
}
}
ThreadPool::~ThreadPool() { shutdown(); }
bool ThreadPool::try_submit(Job job) {
if (!job) return false;
{
std::lock_guard<std::mutex> lock(mutex_);
if (stop_) return false;
if (queue_.size() >= queue_cap_) return false;
queue_.push_back(std::move(job));
}
cv_.notify_one();
return true;
}
void ThreadPool::shutdown() {
{
std::lock_guard<std::mutex> lock(mutex_);
if (stop_) {
// Fall through to join below; workers may still need joining.
}
stop_ = true;
}
cv_.notify_all();
for (auto& w : workers_) {
if (w.joinable()) w.join();
}
workers_.clear();
}
std::size_t ThreadPool::pending() const {
std::lock_guard<std::mutex> lock(mutex_);
return queue_.size();
}
bool ThreadPool::stopped() const {
std::lock_guard<std::mutex> lock(mutex_);
return stop_;
}
void ThreadPool::Worker() {
for (;;) {
Job job;
{
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait(lock, [this] { return stop_ || !queue_.empty(); });
if (queue_.empty()) {
if (stop_) return;
continue;
}
job = std::move(queue_.front());
queue_.pop_front();
}
job();
}
}
} // namespace molo::runtime
+44
View File
@@ -0,0 +1,44 @@
// Spec: plans/01-runtime.md 6.1. Bounded pool for blocking work. Default 4
// threads, queue cap 1024 jobs. Overflow returns false (caller maps to 503
// with Retry-After); the pool never grows silently.
#pragma once
#include <condition_variable>
#include <cstddef>
#include <deque>
#include <functional>
#include <mutex>
#include <thread>
#include <vector>
namespace molo::runtime {
class ThreadPool {
public:
using Job = std::function<void()>;
explicit ThreadPool(std::size_t threads = 4,
std::size_t queue_cap = 1024);
~ThreadPool();
ThreadPool(const ThreadPool&) = delete;
ThreadPool& operator=(const ThreadPool&) = delete;
// False when the queue is full or the pool is stopped.
bool try_submit(Job job);
void shutdown();
std::size_t pending() const;
std::size_t threads() const { return workers_.size(); }
bool stopped() const;
private:
void Worker();
mutable std::mutex mutex_;
std::condition_variable cv_;
std::deque<Job> queue_;
std::size_t queue_cap_;
std::vector<std::thread> workers_;
bool stop_ = false;
};
} // namespace molo::runtime
+672
View File
@@ -0,0 +1,672 @@
// 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
+118
View File
@@ -0,0 +1,118 @@
// Spec: plans/01-runtime.md 6.2. Connection and request lifecycle: accept,
// TLS handshake, protocol detect, head parse, body read, dispatch, handler,
// serialize, keep-alive or close. H1 persistent by default, H2 via GOAWAY on
// drain. Timeouts: header 10 s, body 30 s, idle 75 s, handler budget 30 s.
#pragma once
#include <chrono>
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include "runtime/caps.h"
#include "runtime/event_loop.h"
#include "runtime/task.h"
#include "runtime/tls_handle.h"
#include "runtime/transport.h"
#include "fd_waiter.h"
#include "message.h"
#include "protocol_detect.h"
#include "v1/parser.h"
namespace molo::runtime::http {
namespace v2 {
class H2Connection;
} // namespace v2
using Handler = std::function<Task<FullResponse>(const FullRequest&)>;
struct ConnectionConfig {
std::chrono::milliseconds header_timeout = caps::kHeaderTimeout;
std::chrono::milliseconds body_timeout = caps::kBodyTimeout;
std::chrono::milliseconds idle_timeout = caps::kIdleKeepAliveTimeout;
std::chrono::milliseconds handler_budget = caps::kHandlerBudget;
std::chrono::milliseconds tls_handshake_timeout = caps::kTlsHandshakeTimeout;
std::size_t body_cap = caps::kBodyMaxDefault;
std::size_t max_requests = caps::kMaxRequestsPerH1Connection;
std::size_t high_watermark = caps::kWriteHighWatermark;
std::size_t write_ceiling = caps::kWriteBufferCeiling;
bool allow_h2c_upgrade = true;
};
class Connection : public std::enable_shared_from_this<Connection> {
public:
static std::shared_ptr<Connection> Create(
EventLoop& loop, std::unique_ptr<Transport> transport,
std::unique_ptr<tls::ITlsHandle> tls, Handler handler,
ConnectionConfig config = ConnectionConfig{});
~Connection();
void start();
// Graceful: finish the in-flight request, then GOAWAY (H2) or
// Connection: close (H1), then hard close.
void drain();
void close();
bool closed() const { return closed_; }
bool draining() const { return draining_; }
std::size_t requests_served() const { return requests_; }
std::string protocol() const;
// Shared with H2Connection, which runs on this connection's transport.
// Interrupted means woken without data (drain poke or readiness race):
// the caller re-checks its exit conditions and retries.
enum class ReadResult { Ok, Eof, Error, Timeout, Closed, Interrupted };
Task<ReadResult> PumpRead(std::chrono::milliseconds timeout);
Task<bool> Flush(std::chrono::milliseconds timeout);
EventLoop& loop() { return loop_; }
Transport& transport() { return *transport_; }
Handler& handler() { return handler_; }
std::string& inbox() { return in_; }
std::string& outbox() { return out_; }
void set_in_request(bool v) { in_request_ = v; }
bool is_tls() const;
std::string peer() const;
// Pokes a parked PumpRead/Flush so the owner re-checks exit conditions.
void InterruptRead();
void set_active_h2(v2::H2Connection* h2) { active_h2_ = h2; }
private:
Connection(EventLoop& loop, std::unique_ptr<Transport> transport,
std::unique_ptr<tls::ITlsHandle> tls, Handler handler,
ConnectionConfig config);
Task<void> Run();
static Task<void> Runner(std::shared_ptr<Connection> self);
Task<bool> DoHandshake();
Task<void> RunH1();
Task<void> RunH2(bool expect_magic, bool from_upgrade,
const RequestHead& upgrade_head, const std::string& upgrade_body);
Task<void> SendError(int status, bool close_after);
// Sends a file-backed body (takes fd ownership, always closes it).
Task<bool> SendFileBody(FileBody fb, std::chrono::milliseconds timeout);
Task<bool> AwaitWritable(std::chrono::milliseconds timeout);
bool IsH2cUpgrade(const RequestHead& head) const;
EventLoop& loop_;
std::unique_ptr<Transport> transport_;
std::unique_ptr<tls::ITlsHandle> tls_;
std::unique_ptr<FdWaiter> waiter_;
Handler handler_;
ConnectionConfig config_;
std::string raw_; // bytes from transport, pre-TLS
std::string in_; // plaintext bytes awaiting parse
std::string out_; // plaintext bytes awaiting send
bool closed_ = false;
bool draining_ = false;
bool tls_done_ = false;
bool budget_exceeded_ = false;
bool in_request_ = false;
v2::H2Connection* active_h2_ = nullptr;
std::size_t requests_ = 0;
Protocol protocol_ = Protocol::NeedMore;
};
} // namespace molo::runtime::http
+127
View File
@@ -0,0 +1,127 @@
// One fd readiness waiter for connection coroutines. A connection parks at
// most one reader and one writer; close() resumes both with an error so no
// coroutine leaks parked on a dead fd.
#pragma once
#include <coroutine>
#include "../event_loop.h"
#include "../poller.h"
namespace molo::runtime::http {
class FdWaiter {
public:
FdWaiter(EventLoop& loop, int fd) : loop_(loop), fd_(fd) {}
~FdWaiter() { cancel(); }
FdWaiter(const FdWaiter&) = delete;
FdWaiter& operator=(const FdWaiter&) = delete;
struct Awaitable {
FdWaiter* self;
uint32_t events;
std::coroutine_handle<>* slot;
bool await_ready() const noexcept { return self->cancelled_; }
void await_suspend(std::coroutine_handle<> h) {
*slot = h;
self->Arm(events);
}
bool await_resume() const noexcept { return !self->cancelled_; }
};
Awaitable readable() { return Awaitable{this, IPoller::kRead, &read_h_}; }
Awaitable writable() { return Awaitable{this, IPoller::kWrite, &write_h_}; }
void cancel() {
if (cancelled_) return;
cancelled_ = true;
if (fd_ >= 0) loop_.poller().remove_fd(fd_);
// Null slots before resuming: a resume may destroy this waiter.
auto rh = read_h_;
read_h_ = nullptr;
auto wh = write_h_;
write_h_ = nullptr;
if (rh) rh.resume();
if (wh) wh.resume();
}
// Resume parked waiters without cancelling: used by timeouts. The waiter
// stays usable; callers check their own fired flag.
void Wake() {
if (cancelled_) return;
if (registered_) {
loop_.poller().remove_fd(fd_);
registered_ = false;
}
armed_ = 0;
auto rh = read_h_;
read_h_ = nullptr;
auto wh = write_h_;
write_h_ = nullptr;
if (rh) rh.resume();
if (wh) wh.resume();
}
bool cancelled() const { return cancelled_; }
private:
void Arm(uint32_t events) {
if (cancelled_) return;
uint32_t want = armed_ | events;
if (!registered_) {
bool ok = loop_.poller().add_fd(
fd_, want, [this](int, uint32_t ev) { OnEvent(ev); });
if (!ok) {
cancel();
return;
}
registered_ = true;
} else if (want != armed_) {
loop_.poller().modify_fd(fd_, want);
}
armed_ = want;
}
void OnEvent(uint32_t ev) {
// Error acts as readiness; the syscall reports the real cause.
bool want_r = (ev & (IPoller::kRead | IPoller::kError)) && read_h_;
bool want_w = (ev & (IPoller::kWrite | IPoller::kError)) && write_h_;
std::coroutine_handle<> rh = nullptr;
std::coroutine_handle<> wh = nullptr;
if (want_r) {
rh = read_h_;
read_h_ = nullptr;
armed_ &= ~IPoller::kRead;
}
if (want_w) {
wh = write_h_;
write_h_ = nullptr;
armed_ &= ~IPoller::kWrite;
}
Refresh();
// No member access after the first resume: it may destroy this waiter.
if (rh) rh.resume();
if (wh) wh.resume();
}
void Refresh() {
if (!registered_) return;
if (armed_ == 0) {
loop_.poller().remove_fd(fd_);
registered_ = false;
return;
}
loop_.poller().modify_fd(fd_, armed_);
}
EventLoop& loop_;
int fd_;
bool cancelled_ = false;
bool registered_ = false;
uint32_t armed_ = 0;
std::coroutine_handle<> read_h_ = nullptr;
std::coroutine_handle<> write_h_ = nullptr;
};
} // namespace molo::runtime::http
+175
View File
@@ -0,0 +1,175 @@
// Spec: plans/01-runtime.md 6.2. TCP listener.
#include "listener.h"
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "runtime/caps.h"
#include "runtime/event_loop.h"
#include "runtime/http/connection.h"
#include "runtime/observability_hooks.h"
namespace molo::runtime::http {
Listener::Listener(EventLoop& loop) : loop_(loop) {}
Listener::~Listener() { stop(); }
bool Listener::bind(const std::string& host, uint16_t port, bool reuse_port) {
if (fd_ >= 0) return false;
int fd = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
bool v6 = true;
if (fd < 0) {
fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
v6 = false;
}
if (fd < 0) return false;
int one = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
#if defined(SO_REUSEPORT)
if (reuse_port) setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
#else
(void)reuse_port;
#endif
if (v6) {
int off = 0;
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
}
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
if (v6) {
struct sockaddr_in6 addr;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port);
if (host.empty() || host == "*" || host == "::") {
addr.sin6_addr = in6addr_any;
} else if (inet_pton(AF_INET6, host.c_str(), &addr.sin6_addr) != 1) {
struct in_addr v4;
if (inet_pton(AF_INET, host.c_str(), &v4) == 1) {
addr.sin6_addr.s6_addr[10] = 0xff;
addr.sin6_addr.s6_addr[11] = 0xff;
memcpy(&addr.sin6_addr.s6_addr[12], &v4, 4);
} else {
close(fd);
return false;
}
}
if (::bind(fd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
close(fd);
return false;
}
} else {
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (host.empty() || host == "*") {
addr.sin_addr.s_addr = htonl(INADDR_ANY);
} else if (inet_pton(AF_INET, host.c_str(), &addr.sin_addr) != 1) {
close(fd);
return false;
}
if (::bind(fd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
close(fd);
return false;
}
}
if (listen(fd, 1024) != 0) {
close(fd);
return false;
}
if (port == 0) {
struct sockaddr_in6 addr;
socklen_t len = sizeof(addr);
if (getsockname(fd, (struct sockaddr*)&addr, &len) == 0) {
port_ = ntohs(addr.sin6_port);
}
} else {
port_ = port;
}
bool ok = loop_.poller().add_fd(
fd, IPoller::kRead, [this](int, uint32_t) { OnReadable(); });
if (!ok) {
close(fd);
return false;
}
fd_ = fd;
return true;
}
void Listener::stop() {
if (fd_ >= 0) {
loop_.poller().remove_fd(fd_);
close(fd_);
fd_ = -1;
}
}
void Listener::track(std::shared_ptr<Connection> conn) {
tracked_.push_back(std::move(conn));
}
std::size_t Listener::LiveCount() {
std::size_t n = 0;
std::vector<std::weak_ptr<Connection>> kept;
kept.reserve(tracked_.size());
for (auto& w : tracked_) {
if (auto s = w.lock()) {
if (!s->closed()) {
++n;
kept.push_back(std::move(w));
}
}
}
tracked_.swap(kept);
return n;
}
void Listener::OnReadable() {
for (int i = 0; i < 64; ++i) {
struct sockaddr_in6 addr;
socklen_t len = sizeof(addr);
int cfd =
accept4(fd_, (struct sockaddr*)&addr, &len, SOCK_CLOEXEC | SOCK_NONBLOCK);
if (cfd < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return;
if (errno == EINTR) continue;
return;
}
int one = 1;
setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
char host[INET6_ADDRSTRLEN] = {0};
uint16_t cport = 0;
if (addr.sin6_family == AF_INET6) {
inet_ntop(AF_INET6, &addr.sin6_addr, host, sizeof(host));
cport = ntohs(addr.sin6_port);
} else if (addr.sin6_family == AF_INET) {
struct sockaddr_in* v4 = (struct sockaddr_in*)&addr;
inet_ntop(AF_INET, &v4->sin_addr, host, sizeof(host));
cport = ntohs(v4->sin_port);
}
std::string peer = std::string(host) + ":" + std::to_string(cport);
std::size_t cap = max_connections_;
if (cap > caps::kMaxConnectionsCeiling) cap = caps::kMaxConnectionsCeiling;
if (LiveCount() >= cap) {
++refused_;
MOLO_RUNTIME_COUNT("runtime.accept_refused");
close(cfd);
continue;
}
if (accept_cb_) {
accept_cb_(cfd, peer);
} else {
close(cfd);
}
}
}
} // namespace molo::runtime::http
+53
View File
@@ -0,0 +1,53 @@
// Spec: plans/01-runtime.md 6.2. TCP listener: bind, SO_REUSEPORT where
// available, non-blocking accept loop, per-process connection cap.
#pragma once
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <vector>
namespace molo::runtime {
class EventLoop;
namespace http {
class Connection;
class Listener {
public:
using AcceptCallback =
std::function<void(int fd, const std::string& peer)>;
explicit Listener(EventLoop& loop);
~Listener();
Listener(const Listener&) = delete;
Listener& operator=(const Listener&) = delete;
bool bind(const std::string& host, uint16_t port, bool reuse_port = true);
void on_accept(AcceptCallback cb) { accept_cb_ = std::move(cb); }
void set_max_connections(std::size_t n) { max_connections_ = n; }
// Graceful drain step 1: stop accepting, keep existing connections.
void stop();
bool listening() const { return fd_ >= 0; }
uint16_t port() const { return port_; }
std::size_t refused_connections() const { return refused_; }
void track(std::shared_ptr<Connection> conn);
private:
void OnReadable();
std::size_t LiveCount();
EventLoop& loop_;
int fd_ = -1;
uint16_t port_ = 0;
AcceptCallback accept_cb_;
std::size_t max_connections_ = 16384;
std::size_t refused_ = 0;
std::vector<std::weak_ptr<Connection>> tracked_;
};
} // namespace http
} // namespace molo::runtime
+147
View File
@@ -0,0 +1,147 @@
// Spec: plans/01-runtime.md, RFC 9110. Semantic message layer.
#include "message.h"
#include <sys/stat.h>
#include <cctype>
#include <cstdint>
namespace molo::runtime::http {
bool ClampFileBody(FileBody& fb) {
if (fb.fd < 0) return false;
struct stat st;
if (::fstat(fb.fd, &st) != 0 || st.st_size < 0) return false;
uint64_t size = (uint64_t)st.st_size;
uint64_t start = fb.offset > size ? size : fb.offset;
uint64_t avail = size - start;
fb.offset = start;
fb.length = fb.length > avail ? avail : fb.length;
return true;
}
std::string ToLower(std::string_view s) {
std::string out(s);
for (char& c : out) c = (char)tolower((unsigned char)c);
return out;
}
bool EqCaseInsensitive(std::string_view a, std::string_view b) {
if (a.size() != b.size()) return false;
for (std::size_t i = 0; i < a.size(); ++i) {
if (tolower((unsigned char)a[i]) != tolower((unsigned char)b[i])) {
return false;
}
}
return true;
}
std::string Trim(std::string_view s) {
std::size_t b = 0;
while (b < s.size() && (s[b] == ' ' || s[b] == '\t')) ++b;
std::size_t e = s.size();
while (e > b && (s[e - 1] == ' ' || s[e - 1] == '\t')) --e;
return std::string(s.substr(b, e - b));
}
void Headers::add(std::string name, std::string value) {
bytes_ += name.size() + value.size() + 4;
entries_.emplace_back(std::move(name), std::move(value));
}
std::string Headers::get(std::string_view name) const {
for (const auto& kv : entries_) {
if (EqCaseInsensitive(kv.first, name)) return kv.second;
}
return "";
}
std::vector<std::string> Headers::get_all(std::string_view name) const {
std::vector<std::string> out;
for (const auto& kv : entries_) {
if (EqCaseInsensitive(kv.first, name)) out.push_back(kv.second);
}
return out;
}
void Headers::set(std::string name, std::string value) {
remove(name);
add(std::move(name), std::move(value));
}
bool Headers::has(std::string_view name) const {
for (const auto& kv : entries_) {
if (EqCaseInsensitive(kv.first, name)) return true;
}
return false;
}
void Headers::remove(std::string_view name) {
std::vector<std::pair<std::string, std::string>> kept;
kept.reserve(entries_.size());
bytes_ = 0;
for (auto& kv : entries_) {
if (EqCaseInsensitive(kv.first, name)) continue;
bytes_ += kv.first.size() + kv.second.size() + 4;
kept.push_back(std::move(kv));
}
entries_ = std::move(kept);
}
std::string StatusReason(int status) {
switch (status) {
case 100: return "Continue";
case 101: return "Switching Protocols";
case 103: return "Early Hints";
case 200: return "OK";
case 201: return "Created";
case 204: return "No Content";
case 206: return "Partial Content";
case 301: return "Moved Permanently";
case 302: return "Found";
case 303: return "See Other";
case 304: return "Not Modified";
case 307: return "Temporary Redirect";
case 308: return "Permanent Redirect";
case 400: return "Bad Request";
case 401: return "Unauthorized";
case 403: return "Forbidden";
case 404: return "Not Found";
case 405: return "Method Not Allowed";
case 408: return "Request Timeout";
case 411: return "Length Required";
case 413: return "Content Too Large";
case 414: return "URI Too Long";
case 416: return "Range Not Satisfiable";
case 426: return "Upgrade Required";
case 429: return "Too Many Requests";
case 431: return "Request Header Fields Too Large";
case 500: return "Internal Server Error";
case 501: return "Not Implemented";
case 502: return "Bad Gateway";
case 503: return "Service Unavailable";
default: return "Unknown";
}
}
std::optional<std::size_t> ParseContentLength(const Headers& h,
bool& conflict) {
conflict = false;
std::optional<std::size_t> value;
for (const std::string& raw : h.get_all("content-length")) {
std::string v = Trim(raw);
if (v.empty()) return std::nullopt;
std::size_t n = 0;
for (char c : v) {
if (c < '0' || c > '9') return std::nullopt;
std::size_t d = (std::size_t)(c - '0');
if (n > (SIZE_MAX - d) / 10) return std::nullopt;
n = n * 10 + d;
}
if (value.has_value() && *value != n) conflict = true;
value = n;
}
return value;
}
} // namespace molo::runtime::http
+91
View File
@@ -0,0 +1,91 @@
// Spec: plans/01-runtime.md 6.x, RFC 9110. One version-independent semantic
// layer shared by H1 and H2. Handlers never see framing differences.
#pragma once
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace molo::runtime::http {
std::string ToLower(std::string_view s);
bool EqCaseInsensitive(std::string_view a, std::string_view b);
std::string Trim(std::string_view s);
class Headers {
public:
void add(std::string name, std::string value);
std::string get(std::string_view name) const;
std::vector<std::string> get_all(std::string_view name) const;
void set(std::string name, std::string value);
bool has(std::string_view name) const;
void remove(std::string_view name);
std::size_t count() const { return entries_.size(); }
std::size_t bytes() const { return bytes_; }
const std::vector<std::pair<std::string, std::string>>& entries() const {
return entries_;
}
void clear() {
entries_.clear();
bytes_ = 0;
}
private:
std::vector<std::pair<std::string, std::string>> entries_;
std::size_t bytes_ = 0;
};
struct RequestHead {
std::string method;
std::string target;
std::string version = "HTTP/1.1";
Headers headers;
};
struct ResponseHead {
int status = 200;
Headers headers;
};
// Integration seam for 05-request-routing.md: the full semantic exchange.
// Today Connection fills these directly; routing builds its Request/Response
// views on top without touching framing.
struct FullRequest {
RequestHead head;
std::string body;
Headers trailers;
bool is_tls = false;
std::string peer;
std::string protocol; // "HTTP/1.1" or "HTTP/2"
};
// File-backed body (plans/01-runtime.md 6.7): the handler transfers ownership
// of a seekable fd to the Connection, which sends [offset, offset+length)
// via sendfile on plain H1 or bounded 64 KB streaming otherwise, then closes
// the fd. The Connection clamps length to the file size and closes the fd on
// every path, including HEAD, 204/304, errors, and resets.
struct FileBody {
int fd = -1;
uint64_t offset = 0;
uint64_t length = 0;
};
struct FullResponse {
ResponseHead head;
std::string body; // used only when file is not set
std::optional<FileBody> file; // when set, body must be empty
};
// Clamps fb.offset/length into the file size in place. False when the fd is
// not a stat-able file; then fb is untouched.
bool ClampFileBody(FileBody& fb);
std::string StatusReason(int status);
// Parses a single Content-Length value. Nullopt when missing or invalid.
std::optional<std::size_t> ParseContentLength(const Headers& h, bool& conflict);
} // namespace molo::runtime::http
+41
View File
@@ -0,0 +1,41 @@
// Spec: plans/01-runtime.md 6.4. Protocol detection.
#include "protocol_detect.h"
namespace molo::runtime::http {
void ProtocolDetector::reset() {
matched_ = 0;
decided_ = false;
decision_ = Protocol::NeedMore;
}
Protocol ProtocolDetector::feed(const char* data, std::size_t len) {
if (decided_) return decision_;
for (std::size_t i = 0; i < len; ++i) {
if (matched_ >= kH2Preface.size()) {
decided_ = true;
decision_ = Protocol::H2PriorKnowledge;
return decision_;
}
if (data[i] == kH2Preface[matched_]) {
++matched_;
if (matched_ >= kH2Preface.size()) {
decided_ = true;
decision_ = Protocol::H2PriorKnowledge;
return decision_;
}
continue;
}
decided_ = true;
decision_ = Protocol::Http1;
return decision_;
}
return Protocol::NeedMore;
}
Protocol ProtocolDetector::from_alpn(std::string_view alpn) {
if (alpn == "h2") return Protocol::H2PriorKnowledge;
return Protocol::Http1;
}
} // namespace molo::runtime::http
+30
View File
@@ -0,0 +1,30 @@
// Spec: plans/01-runtime.md 6.4. Protocol detection: ALPN wiring point from
// the TLS handle, h2c prior knowledge (PRI preface), H1 Upgrade to h2c.
#pragma once
#include <cstddef>
#include <string_view>
namespace molo::runtime::http {
enum class Protocol { NeedMore, Http1, H2PriorKnowledge };
// Exact 24-byte client connection preface, RFC 9113 section 3.4.
inline constexpr std::string_view kH2Preface =
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
class ProtocolDetector {
public:
ProtocolDetector() = default;
void reset();
Protocol feed(const char* data, std::size_t len);
// ALPN wiring point: the TLS handle reports "h2" or "http/1.1".
static Protocol from_alpn(std::string_view alpn);
private:
std::size_t matched_ = 0;
bool decided_ = false;
Protocol decision_ = Protocol::NeedMore;
};
} // namespace molo::runtime::http
+425
View File
@@ -0,0 +1,425 @@
// Spec: plans/01-runtime.md 6.3, RFC 9112. Strict incremental parser.
#include "parser.h"
#include <cctype>
#include "runtime/caps.h"
namespace molo::runtime::http::v1 {
Parser::Parser(std::size_t body_cap) : body_cap_(body_cap) {}
void Parser::reset() {
state_ = State::RequestLine;
head_ = RequestHead{};
trailers_ = Headers{};
body_.clear();
pending_.clear();
consumed_ = 0;
content_length_ = 0;
chunk_left_ = 0;
header_bytes_ = 0;
chunked_ = false;
keep_alive_ = true;
expect_continue_ = false;
trailers_section_ = false;
}
bool Parser::IsTokenChar(char c) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9')) {
return true;
}
switch (c) {
case '!':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '*':
case '+':
case '-':
case '.':
case '^':
case '_':
case '`':
case '|':
case '~':
return true;
default:
return false;
}
}
bool Parser::IsMethodChar(char c) {
return (c >= 'A' && c <= 'Z') || c == '-' || c == '_';
}
bool Parser::TakeLine(std::string& out) {
std::size_t pos = pending_.find("\r\n");
if (pos == std::string::npos) {
if (pending_.size() > caps::kHeaderLineMax) {
// Overlong line without terminator: caller maps to 431.
return false;
}
return false;
}
if (pos > caps::kHeaderLineMax) return false;
out.assign(pending_.data(), pos);
pending_.erase(0, pos + 2);
consumed_ += pos + 2;
return true;
}
ParseResult Parser::feed(const char* data, std::size_t len) {
pending_.append(data, len);
for (;;) {
State before = state_;
ParseResult r = ParseResult::NeedMore;
switch (state_) {
case State::RequestLine:
r = FeedRequestLine();
break;
case State::Headers:
r = FeedHeaders();
break;
case State::BodyIdentity:
r = FeedBodyIdentity();
break;
case State::ChunkSize:
r = FeedChunkSize();
break;
case State::ChunkData:
r = FeedChunkData();
break;
case State::ChunkDataEnd:
r = FeedChunkDataEnd();
break;
case State::Trailers:
r = FeedTrailers();
break;
case State::Done:
return ParseResult::Complete;
case State::Failed:
return ParseResult::Error400;
}
// Terminal results return at once; NeedMore loops only on progress.
if (r != ParseResult::NeedMore || state_ == before) return r;
}
}
ParseResult Parser::FeedRequestLine() {
std::string line;
if (!TakeLine(line)) {
if (pending_.size() > caps::kHeaderLineMax) {
state_ = State::Failed;
return ParseResult::Error431;
}
return ParseResult::NeedMore;
}
// METHOD SP TARGET SP VERSION
std::size_t sp1 = line.find(' ');
if (sp1 == std::string::npos || sp1 == 0) {
state_ = State::Failed;
return ParseResult::Error400;
}
std::size_t sp2 = line.find(' ', sp1 + 1);
if (sp2 == std::string::npos || sp2 + 1 >= line.size()) {
state_ = State::Failed;
return ParseResult::Error400;
}
std::string method = line.substr(0, sp1);
std::string target = line.substr(sp1 + 1, sp2 - sp1 - 1);
std::string version = line.substr(sp2 + 1);
for (char c : method) {
if (!IsMethodChar(c)) {
state_ = State::Failed;
return ParseResult::Error400;
}
}
if (target.empty() || target.size() > caps::kUriMax) {
state_ = State::Failed;
return target.size() > caps::kUriMax ? ParseResult::Error431
: ParseResult::Error400;
}
for (char c : target) {
unsigned char u = (unsigned char)c;
if (u <= 0x20 || u == 0x7f) {
state_ = State::Failed;
return ParseResult::Error400;
}
}
if (version != "HTTP/1.0" && version != "HTTP/1.1") {
state_ = State::Failed;
return ParseResult::Error400;
}
head_.method = method;
head_.target = target;
head_.version = version;
keep_alive_ = (version == "HTTP/1.1");
header_bytes_ = 0;
state_ = State::Headers;
return ParseResult::NeedMore;
}
ParseResult Parser::FeedHeaders() {
for (;;) {
std::string line;
std::size_t before = pending_.size();
if (!TakeLine(line)) {
if (pending_.size() > caps::kHeaderLineMax ||
before > caps::kHeaderLineMax) {
state_ = State::Failed;
return ParseResult::Error431;
}
return ParseResult::NeedMore;
}
if (line.empty()) {
return BeginBody();
}
if (line[0] == ' ' || line[0] == '\t') {
// obs-fold rejected per R5.
state_ = State::Failed;
return ParseResult::Error400;
}
std::size_t colon = line.find(':');
if (colon == std::string::npos || colon == 0) {
state_ = State::Failed;
return ParseResult::Error400;
}
std::string name = line.substr(0, colon);
for (char c : name) {
if (!IsTokenChar(c)) {
state_ = State::Failed;
return ParseResult::Error400;
}
}
std::string value = Trim(line.substr(colon + 1));
for (char c : value) {
unsigned char u = (unsigned char)c;
if (u < 0x20 && u != '\t') {
state_ = State::Failed;
return ParseResult::Error400;
}
if (u == 0x7f) {
state_ = State::Failed;
return ParseResult::Error400;
}
}
header_bytes_ += name.size() + value.size() + 4;
if (header_bytes_ > caps::kHeaderBlockMax) {
state_ = State::Failed;
return ParseResult::Error431;
}
head_.headers.add(ToLower(name), value);
if (head_.headers.count() > caps::kHeaderFieldMax) {
state_ = State::Failed;
return ParseResult::Error431;
}
}
}
ParseResult Parser::BeginBody() {
if (!head_.headers.has("host")) {
state_ = State::Failed;
return ParseResult::Error400;
}
std::string conn = ToLower(head_.headers.get("connection"));
if (conn.find("close") != std::string::npos) keep_alive_ = false;
if (head_.version == "HTTP/1.0" &&
conn.find("keep-alive") != std::string::npos) {
keep_alive_ = true;
}
std::string expect = ToLower(head_.headers.get("expect"));
expect_continue_ = (expect == "100-continue");
if (!expect.empty() && !expect_continue_) {
state_ = State::Failed;
return ParseResult::Error400;
}
bool has_cl = head_.headers.has("content-length");
bool has_te = head_.headers.has("transfer-encoding");
if (has_cl && has_te) {
// Smuggling defense: never accept both.
state_ = State::Failed;
return ParseResult::Error400;
}
if (has_te) {
std::string te = ToLower(head_.headers.get("transfer-encoding"));
// Only chunked (possibly last of a list we otherwise reject).
bool chunked = false;
std::size_t pos = 0;
while (pos <= te.size()) {
std::size_t comma = te.find(',', pos);
if (comma == std::string::npos) comma = te.size();
std::string coding = Trim(te.substr(pos, comma - pos));
if (coding == "chunked") {
chunked = true;
} else if (!coding.empty() && coding != "identity") {
state_ = State::Failed;
return ParseResult::Error501;
}
pos = comma + 1;
}
if (!chunked) {
// Identity only: no body framing.
state_ = State::Done;
return ParseResult::Complete;
}
chunked_ = true;
state_ = State::ChunkSize;
return ParseResult::NeedMore;
}
if (has_cl) {
bool conflict = false;
std::optional<std::size_t> len =
ParseContentLength(head_.headers, conflict);
if (!len.has_value() || conflict) {
state_ = State::Failed;
return ParseResult::Error400;
}
content_length_ = *len;
if (content_length_ > body_cap_) {
state_ = State::Failed;
return ParseResult::Error413;
}
if (content_length_ == 0) {
state_ = State::Done;
return ParseResult::Complete;
}
state_ = State::BodyIdentity;
return ParseResult::NeedMore;
}
state_ = State::Done;
return ParseResult::Complete;
}
ParseResult Parser::FeedBodyIdentity() {
std::size_t want = content_length_ - body_.size();
std::size_t have = pending_.size() < want ? pending_.size() : want;
body_.append(pending_.data(), have);
pending_.erase(0, have);
consumed_ += have;
if (body_.size() >= content_length_) {
state_ = State::Done;
return ParseResult::Complete;
}
return ParseResult::NeedMore;
}
ParseResult Parser::FeedChunkSize() {
std::string line;
if (!TakeLine(line)) {
if (pending_.size() > caps::kHeaderLineMax) {
state_ = State::Failed;
return ParseResult::Error400;
}
return ParseResult::NeedMore;
}
// Strip extensions.
std::size_t semi = line.find(';');
std::string num = semi == std::string::npos ? line : line.substr(0, semi);
num = Trim(num);
if (num.empty() || num.size() > 16) {
state_ = State::Failed;
return ParseResult::Error400;
}
std::size_t size = 0;
for (char c : num) {
unsigned v;
if (c >= '0' && c <= '9') v = (unsigned)(c - '0');
else if (c >= 'a' && c <= 'f') v = (unsigned)(c - 'a' + 10);
else if (c >= 'A' && c <= 'F') v = (unsigned)(c - 'A' + 10);
else {
state_ = State::Failed;
return ParseResult::Error400;
}
if (size > (SIZE_MAX - v) / 16) {
state_ = State::Failed;
return ParseResult::Error400;
}
size = size * 16 + v;
}
if (size == 0) {
trailers_section_ = true;
state_ = State::Trailers;
return ParseResult::NeedMore;
}
if (body_.size() + size > body_cap_) {
state_ = State::Failed;
return ParseResult::Error413;
}
chunk_left_ = size;
state_ = State::ChunkData;
return ParseResult::NeedMore;
}
ParseResult Parser::FeedChunkData() {
std::size_t have = pending_.size() < chunk_left_ ? pending_.size()
: chunk_left_;
body_.append(pending_.data(), have);
pending_.erase(0, have);
consumed_ += have;
chunk_left_ -= have;
if (chunk_left_ > 0) return ParseResult::NeedMore;
state_ = State::ChunkDataEnd;
return ParseResult::NeedMore;
}
ParseResult Parser::FeedChunkDataEnd() {
if (pending_.size() < 2) return ParseResult::NeedMore;
if (pending_[0] != '\r' || pending_[1] != '\n') {
state_ = State::Failed;
return ParseResult::Error400;
}
pending_.erase(0, 2);
consumed_ += 2;
state_ = State::ChunkSize;
return ParseResult::NeedMore;
}
ParseResult Parser::FeedTrailers() {
for (;;) {
std::string line;
if (!TakeLine(line)) {
if (pending_.size() > caps::kHeaderLineMax) {
state_ = State::Failed;
return ParseResult::Error400;
}
return ParseResult::NeedMore;
}
if (line.empty()) {
state_ = State::Done;
trailers_section_ = false;
return ParseResult::Complete;
}
if (line[0] == ' ' || line[0] == '\t') {
state_ = State::Failed;
return ParseResult::Error400;
}
std::size_t colon = line.find(':');
if (colon == std::string::npos || colon == 0) {
state_ = State::Failed;
return ParseResult::Error400;
}
std::string name = line.substr(0, colon);
for (char c : name) {
if (!IsTokenChar(c)) {
state_ = State::Failed;
return ParseResult::Error400;
}
}
header_bytes_ += name.size() + line.size();
if (header_bytes_ > caps::kHeaderBlockMax) {
state_ = State::Failed;
return ParseResult::Error431;
}
trailers_.add(ToLower(name), Trim(line.substr(colon + 1)));
if (head_.headers.count() + trailers_.count() > caps::kHeaderFieldMax) {
state_ = State::Failed;
return ParseResult::Error431;
}
}
}
} // namespace molo::runtime::http::v1
+92
View File
@@ -0,0 +1,92 @@
// Spec: plans/01-runtime.md 6.3, RFC 9112 sections 2, 5, 6, 7. Strict
// incremental request parser. Rejects ambiguous framing with 400 and close,
// requires Host, accepts chunked, answers unknown transfer coding with 501.
#pragma once
#include <cstddef>
#include <string>
#include "../message.h"
namespace molo::runtime::http::v1 {
enum class ParseResult {
NeedMore,
Complete,
Error400,
Error413,
Error431,
Error501,
};
class Parser {
public:
explicit Parser(std::size_t body_cap = 10 * 1024 * 1024);
void reset();
// Feeds bytes. Consumed prefix length is tracked in consumed(); the caller
// keeps the buffer and erases the consumed prefix after Complete.
ParseResult feed(const char* data, std::size_t len);
std::size_t consumed() const { return consumed_; }
const RequestHead& head() const { return head_; }
const std::string& body() const { return body_; }
const Headers& trailers() const { return trailers_; }
bool keep_alive() const { return keep_alive_; }
bool expect_continue() const { return expect_continue_; }
bool head_done() const {
return state_ == State::BodyIdentity || state_ == State::ChunkSize ||
state_ == State::ChunkData || state_ == State::ChunkDataEnd ||
state_ == State::Trailers || state_ == State::Done;
}
// Bytes fed but not part of this message (pipelined next request).
std::string take_pending() {
std::string out = std::move(pending_);
pending_.clear();
return out;
}
private:
enum class State {
RequestLine,
Headers,
BodyIdentity,
ChunkSize,
ChunkData,
ChunkDataEnd,
Trailers,
Done,
Failed,
};
ParseResult FeedRequestLine();
ParseResult FeedHeaders();
ParseResult FeedBodyIdentity();
ParseResult FeedChunkSize();
ParseResult FeedChunkData();
ParseResult FeedChunkDataEnd();
ParseResult FeedTrailers();
ParseResult BeginBody();
// Returns line without CRLF and consumes it, or nullopt when incomplete.
bool TakeLine(std::string& out);
static bool IsTokenChar(char c);
static bool IsMethodChar(char c);
State state_ = State::RequestLine;
RequestHead head_;
Headers trailers_;
std::string body_;
std::string pending_;
std::size_t consumed_ = 0;
std::size_t body_cap_;
std::size_t content_length_ = 0;
std::size_t chunk_left_ = 0;
std::size_t header_bytes_ = 0;
bool chunked_ = false;
bool keep_alive_ = true;
bool expect_continue_ = false;
bool trailers_section_ = false;
};
} // namespace molo::runtime::http::v1
+83
View File
@@ -0,0 +1,83 @@
// Spec: plans/01-runtime.md 6.3, RFC 9112. Response serializer.
#include "serializer.h"
#include <cstdio>
#include <ctime>
namespace molo::runtime::http::v1 {
std::string Serializer::HttpDateNow() {
std::time_t t = std::time(nullptr);
struct tm tmv;
gmtime_r(&t, &tmv);
char buf[40];
strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", &tmv);
return std::string(buf);
}
std::string Serializer::SerializeHead(const ResponseHead& head,
std::string_view version, bool chunked,
std::size_t content_length,
bool head_only, bool close) {
std::string out;
out.reserve(512);
out.append(version);
out.push_back(' ');
out.append(std::to_string(head.status));
out.push_back(' ');
out.append(StatusReason(head.status));
out.append("\r\n");
bool has_date = false;
bool has_length = false;
bool has_te = false;
bool has_connection = false;
for (const auto& kv : head.headers.entries()) {
if (EqCaseInsensitive(kv.first, "date")) has_date = true;
if (EqCaseInsensitive(kv.first, "content-length")) has_length = true;
if (EqCaseInsensitive(kv.first, "transfer-encoding")) has_te = true;
if (EqCaseInsensitive(kv.first, "connection")) has_connection = true;
out.append(kv.first);
out.append(": ");
out.append(kv.second);
out.append("\r\n");
}
// RFC 9110 section 8.6: HEAD responses carry the Content-Length the
// matching GET would send. Only 204/304/1xx never carry framing.
// head_only is informational here: the caller suppresses the body bytes.
(void)head_only;
bool never_body = head.status == 204 || head.status == 304 ||
(head.status >= 100 && head.status < 200);
if (!has_date) {
out.append("date: ");
out.append(HttpDateNow());
out.append("\r\n");
}
if (!never_body && !has_te && !has_length) {
if (chunked) {
out.append("transfer-encoding: chunked\r\n");
} else {
out.append("content-length: ");
out.append(std::to_string(content_length));
out.append("\r\n");
}
}
if (!has_connection && (close || version == "HTTP/1.0")) {
out.append("connection: close\r\n");
}
out.append("\r\n");
return out;
}
std::string Serializer::Chunk(const char* data, std::size_t len) {
if (len == 0) return "";
char hex[24];
snprintf(hex, sizeof(hex), "%zx\r\n", len);
std::string out(hex);
out.append(data, len);
out.append("\r\n");
return out;
}
std::string Serializer::LastChunk() { return "0\r\n\r\n"; }
} // namespace molo::runtime::http::v1
+28
View File
@@ -0,0 +1,28 @@
// Spec: plans/01-runtime.md 6.3, RFC 9112 sections 6, 7. Response serializer:
// automatic Content-Length or chunked, HEAD sends head only, 204/304 never
// carry a body.
#pragma once
#include <cstddef>
#include <string>
#include <string_view>
#include "../message.h"
namespace molo::runtime::http::v1 {
class Serializer {
public:
// Builds the status line plus head fields plus terminating CRLF. Adds
// Content-Length (identity) or Transfer-Encoding: chunked, plus Date when
// the head lacks it. head_only covers HEAD requests.
static std::string SerializeHead(const ResponseHead& head,
std::string_view version, bool chunked,
std::size_t content_length, bool head_only,
bool close);
static std::string Chunk(const char* data, std::size_t len);
static std::string LastChunk();
static std::string HttpDateNow();
};
} // namespace molo::runtime::http::v1
+962
View File
@@ -0,0 +1,962 @@
// Spec: plans/01-runtime.md 6.4, RFC 9113. H2 connection engine.
#include "connection.h"
#include <unistd.h>
#include "runtime/base64.h"
#include "runtime/event_loop.h"
#include "runtime/http/connection.h"
#include "runtime/http/protocol_detect.h"
#include "runtime/observability_hooks.h"
#include "runtime/task.h"
namespace molo::runtime::http::v2 {
namespace {
constexpr uint8_t kData = 0x0;
constexpr uint8_t kHeaders = 0x1;
constexpr uint8_t kPriority = 0x2;
constexpr uint8_t kRst = 0x3;
constexpr uint8_t kSettings = 0x4;
constexpr uint8_t kPushPromise = 0x5;
constexpr uint8_t kPing = 0x6;
constexpr uint8_t kGoaway = 0x7;
constexpr uint8_t kWindowUpdate = 0x8;
constexpr uint8_t kContinuation = 0x9;
constexpr uint8_t kFlagEndStream = 0x1;
constexpr uint8_t kFlagAck = 0x1;
constexpr uint8_t kFlagEndHeaders = 0x4;
constexpr uint8_t kFlagPadded = 0x8;
constexpr uint8_t kFlagPriority = 0x20;
constexpr uint32_t kNoError = 0x0;
constexpr uint32_t kProtocolError = 0x1;
constexpr uint32_t kInternalError = 0x2;
constexpr uint32_t kFlowControlError = 0x3;
constexpr uint32_t kStreamClosed = 0x5;
constexpr uint32_t kFrameSizeError = 0x6;
constexpr uint32_t kRefusedStream = 0x7;
constexpr uint32_t kCancel = 0x8;
constexpr uint32_t kCompressionError = 0x9;
bool IsH2HopHeader(std::string_view name) {
return EqCaseInsensitive(name, "connection") ||
EqCaseInsensitive(name, "keep-alive") ||
EqCaseInsensitive(name, "proxy-connection") ||
EqCaseInsensitive(name, "transfer-encoding") ||
EqCaseInsensitive(name, "upgrade") ||
EqCaseInsensitive(name, "http2-settings");
}
} // namespace
H2Connection::H2Connection(Connection& conn, H2Config config)
: conn_(conn), config_(config) {
if (config_.max_concurrent_streams > caps::kH2MaxConcurrentStreamsCeiling) {
config_.max_concurrent_streams = caps::kH2MaxConcurrentStreamsCeiling;
}
local_.max_concurrent_streams =
(uint32_t)config_.max_concurrent_streams;
decoder_.SetMaxTableSize(local_.header_table_size);
}
void H2Connection::drain() {
draining_ = true;
SendGoaway(kNoError);
conn_.InterruptRead(); // unpark the read loop for a prompt exit
}
std::string H2Connection::FrameBytes(uint8_t type, uint8_t flags,
uint32_t stream_id, const char* payload,
std::size_t len) {
std::string out;
out.resize(9 + len);
out[0] = (char)((len >> 16) & 0xff);
out[1] = (char)((len >> 8) & 0xff);
out[2] = (char)(len & 0xff);
out[3] = (char)type;
out[4] = (char)flags;
out[5] = (char)((stream_id >> 24) & 0x7f);
out[6] = (char)((stream_id >> 16) & 0xff);
out[7] = (char)((stream_id >> 8) & 0xff);
out[8] = (char)(stream_id & 0xff);
if (len > 0) out.replace(9, len, payload, len);
return out;
}
void H2Connection::EnqueueControl(uint32_t stream_id,
const std::string& frame_bytes) {
OutItem item;
item.stream_id = stream_id;
item.bytes = frame_bytes;
item.is_data = false;
item.end_stream = false;
out_queued_bytes_ += item.bytes.size();
out_queue_.push_back(std::move(item));
KickWriter();
}
void H2Connection::EnqueueData(uint32_t stream_id, std::string payload,
bool end_stream) {
OutItem item;
item.stream_id = stream_id;
item.bytes = std::move(payload);
item.is_data = true;
item.end_stream = end_stream;
out_queued_bytes_ += item.bytes.size();
out_queue_.push_back(std::move(item));
KickWriter();
}
void H2Connection::KickWriter() {
if (writer_active_ || conn_.closed()) return;
writer_active_ = true;
conn_.loop().post(WriterTask(weak_from_this()));
}
void H2Connection::WakeWriter() {
if (window_waiter_) {
auto h = window_waiter_;
window_waiter_ = nullptr;
h.resume();
}
}
void H2Connection::SendSettingsAck() {
EnqueueControl(0, FrameBytes(kSettings, kFlagAck, 0, nullptr, 0));
}
void H2Connection::SendPingAck(const char* payload) {
EnqueueControl(0, FrameBytes(kPing, kFlagAck, 0, payload, 8));
}
void H2Connection::SendRst(uint32_t stream_id, uint32_t code) {
char p[4] = {(char)(code >> 24), (char)((code >> 16) & 0xff),
(char)((code >> 8) & 0xff), (char)(code & 0xff)};
EnqueueControl(stream_id, FrameBytes(kRst, 0, stream_id, p, 4));
}
void H2Connection::SendGoaway(uint32_t code) {
if (goaway_sent_) return;
goaway_sent_ = true;
char p[8];
p[0] = (char)((max_stream_id_ >> 24) & 0x7f);
p[1] = (char)((max_stream_id_ >> 16) & 0xff);
p[2] = (char)((max_stream_id_ >> 8) & 0xff);
p[3] = (char)(max_stream_id_ & 0xff);
p[4] = (char)(code >> 24);
p[5] = (char)((code >> 16) & 0xff);
p[6] = (char)((code >> 8) & 0xff);
p[7] = (char)(code & 0xff);
EnqueueControl(0, FrameBytes(kGoaway, 0, 0, p, 8));
}
void H2Connection::SendWindowUpdate(uint32_t stream_id, uint32_t increment) {
if (increment == 0) return;
char p[4] = {(char)((increment >> 24) & 0x7f),
(char)((increment >> 16) & 0xff),
(char)((increment >> 8) & 0xff), (char)(increment & 0xff)};
EnqueueControl(stream_id, FrameBytes(kWindowUpdate, 0, stream_id, p, 4));
}
void H2Connection::Fatal(uint32_t code) {
if (fatal_) return;
fatal_ = true;
close_after_flush_ = true;
SendGoaway(code);
WakeWriter();
}
Task<bool> H2Connection::Ensure(std::size_t n) {
std::string& in = conn_.inbox();
while (in.size() < n) {
if (conn_.closed() || fatal_) co_return false;
Connection::ReadResult r = co_await conn_.PumpRead(config_.idle_timeout);
if (r == Connection::ReadResult::Ok) continue;
if (r == Connection::ReadResult::Interrupted) {
// Drain poke or race: return so the read loop re-checks its exit
// conditions. The loop must verify byte counts before parsing.
co_return true;
}
if (r == Connection::ReadResult::Timeout) {
if (!streams_.empty()) continue; // in-flight work: keep waiting
SendGoaway(kNoError); // idle: graceful close
co_return false;
}
co_return false;
}
co_return true;
}
Task<void> H2Connection::Run(bool expect_magic, bool from_upgrade,
const RequestHead& upgrade_head,
const std::string& upgrade_body) {
auto self = shared_from_this();
if (expect_magic) {
for (;;) {
if (!co_await Ensure(kH2Preface.size())) co_return;
if (conn_.closed() || fatal_) co_return;
if (conn_.inbox().size() >= kH2Preface.size()) break;
// Interrupted with a short buffer: re-park.
}
std::string& in = conn_.inbox();
if (in.compare(0, kH2Preface.size(), kH2Preface) != 0) {
Fatal(kProtocolError);
} else {
in.erase(0, kH2Preface.size());
}
if (fatal_) {
read_done_ = true;
WakeWriter();
co_return;
}
}
// Server preface.
{
std::string payload = EncodeSettingsPayload(local_);
conn_.outbox().append(
FrameBytes(kSettings, 0, 0, payload.data(), payload.size()));
if (!co_await conn_.Flush(config_.idle_timeout)) co_return;
}
if (from_upgrade) {
if (!ApplyUpgrade(upgrade_head, upgrade_body)) {
read_done_ = true;
WakeWriter();
co_return;
}
co_await ReadLoop(false);
} else {
co_await ReadLoop(true);
}
read_done_ = true;
WakeWriter();
// Join the writer so GOAWAY is delivered before the socket closes.
if (writer_active_ || !out_queue_.empty()) {
struct JoinAwait {
H2Connection* self;
bool await_ready() const noexcept { return false; }
void await_suspend(std::coroutine_handle<> h) {
self->writer_done_waiter_ = h;
H2Connection* h2 = self;
h2->conn_.loop().schedule_after(std::chrono::milliseconds(5000),
[h2] {
if (h2->writer_done_waiter_) {
auto w =
h2->writer_done_waiter_;
h2->writer_done_waiter_ =
nullptr;
w.resume();
}
});
}
void await_resume() noexcept {}
};
KickWriter();
co_await JoinAwait{this};
writer_done_waiter_ = nullptr;
}
}
Task<void> H2Connection::ReadLoop(bool first_must_be_settings) {
auto self = shared_from_this();
bool first = first_must_be_settings;
std::string& in = conn_.inbox();
for (;;) {
if (conn_.closed() || fatal_) co_return;
if ((peer_goaway_ || draining_) && streams_.empty()) co_return;
if (!co_await Ensure(9)) co_return;
if (conn_.closed() || fatal_) co_return;
if ((peer_goaway_ || draining_) && streams_.empty()) co_return;
if (in.size() < 9) continue; // interrupted: re-check, then re-park
Frame fr;
fr.length = ((uint32_t)(unsigned char)in[0] << 16) |
((uint32_t)(unsigned char)in[1] << 8) |
(uint32_t)(unsigned char)in[2];
fr.type = (unsigned char)in[3];
fr.flags = (unsigned char)in[4];
fr.stream_id = ((uint32_t)((unsigned char)in[5] & 0x7f) << 24) |
((uint32_t)(unsigned char)in[6] << 16) |
((uint32_t)(unsigned char)in[7] << 8) |
(uint32_t)(unsigned char)in[8];
if (fr.length > local_.max_frame_size) {
Fatal(kFrameSizeError);
co_return;
}
if (!co_await Ensure(9 + fr.length)) co_return;
if (conn_.closed() || fatal_) co_return;
if ((peer_goaway_ || draining_) && streams_.empty()) co_return;
if (in.size() < 9 + fr.length) continue; // interrupted: re-park
std::string payload(in.data() + 9, fr.length);
in.erase(0, 9 + fr.length);
if (first) {
first = false;
if (fr.type != kSettings) {
Fatal(kProtocolError);
co_return;
}
}
if (!ProcessFrame(fr, payload.data())) co_return;
}
}
bool H2Connection::ProcessFrame(const Frame& fr, const char* payload) {
if (cont_expected_ && fr.type != kContinuation) {
Fatal(kProtocolError);
return false;
}
switch (fr.type) {
case kData:
return OnData(fr, payload);
case kHeaders:
return OnHeaders(fr, payload);
case kPriority:
return OnPriority(fr, payload);
case kRst:
return OnRst(fr, payload);
case kSettings:
return OnSettings(fr, payload);
case kPushPromise:
Fatal(kProtocolError); // server push is gone; never accept it
return false;
case kPing:
return OnPing(fr, payload);
case kGoaway:
return OnGoaway(fr, payload);
case kWindowUpdate:
return OnWindowUpdate(fr, payload);
case kContinuation:
return OnContinuation(fr, payload);
default:
return true; // unknown types are ignored
}
}
bool H2Connection::OnSettings(const Frame& fr, const char* payload) {
if (fr.stream_id != 0) {
Fatal(kProtocolError);
return false;
}
if (fr.flags & kFlagAck) {
if (fr.length != 0) {
Fatal(kFrameSizeError);
return false;
}
return true;
}
uint32_t old_window = peer_.initial_window_size;
Settings next = peer_;
SettingsError e = DecodeSettingsPayload(payload, fr.length, next);
if (e == SettingsError::ProtocolError) {
Fatal(kProtocolError);
return false;
}
if (e == SettingsError::FlowControlError) {
Fatal(kFlowControlError);
return false;
}
if (e == SettingsError::CompressionError) {
Fatal(kCompressionError);
return false;
}
peer_ = next;
encoder_.SetMaxTableSize(peer_.header_table_size);
if (peer_.initial_window_size != old_window) {
int64_t delta =
(int64_t)peer_.initial_window_size - (int64_t)old_window;
if (!flow_.AdjustAllStreams(delta, peer_.initial_window_size)) {
Fatal(kFlowControlError);
return false;
}
}
SendSettingsAck();
return true;
}
bool H2Connection::OnHeaders(const Frame& fr, const char* payload) {
if (fr.stream_id == 0) {
Fatal(kProtocolError);
return false;
}
std::size_t off = 0;
std::size_t len = fr.length;
if (fr.flags & kFlagPadded) {
if (len < 1) {
Fatal(kProtocolError);
return false;
}
std::size_t pad = (unsigned char)payload[0];
if (pad + 1 > len) {
Fatal(kProtocolError);
return false;
}
off = 1;
len -= 1 + pad;
}
if (fr.flags & kFlagPriority) {
if (len < 5) {
Fatal(kProtocolError);
return false;
}
off += 5;
len -= 5;
}
bool end_stream = (fr.flags & kFlagEndStream) != 0;
if (!(fr.flags & kFlagEndHeaders)) {
cont_expected_ = true;
cont_stream_ = fr.stream_id;
cont_buf_.assign(payload + off, len);
cont_end_stream_ = end_stream;
return true;
}
return FinishHeaders(fr.stream_id, std::string(payload + off, len),
end_stream);
}
bool H2Connection::FinishHeaders(uint32_t stream_id,
const std::string& block, bool end_stream) {
std::vector<HpackField> fields;
HpackError e = decoder_.Decode(block.data(), block.size(), fields);
if (e != HpackError::Ok) {
Fatal(kCompressionError);
return false;
}
auto it = streams_.find(stream_id);
if (it == streams_.end()) {
if ((stream_id % 2) == 0) {
Fatal(kProtocolError);
return false;
}
if (stream_id <= max_stream_id_) {
SendRst(stream_id, kStreamClosed);
return true;
}
if (goaway_sent_ || peer_goaway_ || draining_) return true;
if (streams_.size() >= config_.max_concurrent_streams) {
max_stream_id_ = stream_id;
SendRst(stream_id, kRefusedStream);
return true;
}
max_stream_id_ = stream_id;
streams_.emplace(stream_id, H2Stream(stream_id, config_.body_cap));
flow_.AddStream(stream_id, peer_.initial_window_size);
stream_in_window_[stream_id] = (int64_t)local_.initial_window_size;
it = streams_.find(stream_id);
}
StreamOutcome oc = it->second.OnHeaders(fields, end_stream);
if (oc.event == StreamEvent::LocalReset) {
SendRst(stream_id, oc.rst_code);
EraseStream(stream_id);
return true;
}
if (oc.event == StreamEvent::MessageComplete) Dispatch(stream_id);
return true;
}
bool H2Connection::OnContinuation(const Frame& fr, const char* payload) {
if (!cont_expected_ || fr.stream_id != cont_stream_) {
Fatal(kProtocolError);
return false;
}
if (cont_buf_.size() + fr.length > caps::kH2HeaderListMax) {
Fatal(kCompressionError);
return false;
}
cont_buf_.append(payload, fr.length);
if (!(fr.flags & kFlagEndHeaders)) return true;
cont_expected_ = false;
std::string block = std::move(cont_buf_);
cont_buf_.clear();
return FinishHeaders(fr.stream_id, block, cont_end_stream_);
}
bool H2Connection::OnData(const Frame& fr, const char* payload) {
if (fr.stream_id == 0) {
Fatal(kProtocolError);
return false;
}
std::size_t off = 0;
std::size_t len = fr.length;
if (fr.flags & kFlagPadded) {
if (len < 1) {
Fatal(kProtocolError);
return false;
}
std::size_t pad = (unsigned char)payload[0];
if (pad + 1 > len) {
Fatal(kProtocolError);
return false;
}
off = 1;
len -= 1 + pad;
}
bool end_stream = (fr.flags & kFlagEndStream) != 0;
auto it = streams_.find(fr.stream_id);
if (it == streams_.end()) {
if (fr.stream_id <= max_stream_id_) {
SendRst(fr.stream_id, kStreamClosed);
return true;
}
Fatal(kProtocolError);
return false;
}
if ((int64_t)fr.length > conn_in_window_) {
Fatal(kFlowControlError);
return false;
}
auto win = stream_in_window_.find(fr.stream_id);
if (win == stream_in_window_.end() ||
(int64_t)fr.length > win->second) {
Fatal(kFlowControlError);
return false;
}
conn_in_window_ -= (int64_t)fr.length;
win->second -= (int64_t)fr.length;
flow_.ConsumedConnection(fr.length);
flow_.ConsumedStream(fr.stream_id, fr.length);
// Immediate refund keeps the peer flowing; bounded by frame caps.
SendWindowUpdate(0, (uint32_t)flow_.TakeConnectionRefund());
SendWindowUpdate(fr.stream_id,
(uint32_t)flow_.TakeStreamRefund(fr.stream_id));
conn_in_window_ += (int64_t)fr.length;
win->second += (int64_t)fr.length;
StreamOutcome oc =
it->second.OnData(payload + off, len, end_stream);
if (oc.event == StreamEvent::LocalReset) {
SendRst(fr.stream_id, oc.rst_code);
EraseStream(fr.stream_id);
return true;
}
if (oc.event == StreamEvent::MessageComplete) {
Dispatch(fr.stream_id);
}
return true;
}
bool H2Connection::OnRst(const Frame& fr, const char* payload) {
if (fr.stream_id == 0) {
Fatal(kProtocolError);
return false;
}
if (fr.length != 4) {
Fatal(kFrameSizeError);
return false;
}
auto it = streams_.find(fr.stream_id);
if (it == streams_.end()) return true; // idle or closed: ignore
uint32_t code = ((uint32_t)(unsigned char)payload[0] << 24) |
((uint32_t)(unsigned char)payload[1] << 16) |
((uint32_t)(unsigned char)payload[2] << 8) |
(uint32_t)(unsigned char)payload[3];
it->second.OnRst(code);
EraseStream(fr.stream_id);
return true;
}
bool H2Connection::OnPing(const Frame& fr, const char* payload) {
if (fr.stream_id != 0) {
Fatal(kProtocolError);
return false;
}
if (fr.length != 8) {
Fatal(kFrameSizeError);
return false;
}
if (fr.flags & kFlagAck) return true;
SendPingAck(payload);
return true;
}
bool H2Connection::OnGoaway(const Frame& fr, const char* payload) {
if (fr.stream_id != 0) {
Fatal(kProtocolError);
return false;
}
if (fr.length < 8) {
Fatal(kFrameSizeError);
return false;
}
(void)payload;
peer_goaway_ = true;
return true;
}
bool H2Connection::OnWindowUpdate(const Frame& fr, const char* payload) {
if (fr.length != 4) {
Fatal(kFrameSizeError);
return false;
}
uint32_t inc = ((uint32_t)((unsigned char)payload[0] & 0x7f) << 24) |
((uint32_t)(unsigned char)payload[1] << 16) |
((uint32_t)(unsigned char)payload[2] << 8) |
(uint32_t)(unsigned char)payload[3];
if (inc == 0) {
Fatal(kFlowControlError);
return false;
}
if (fr.stream_id == 0) {
flow_.OnConnectionWindowUpdate(inc);
WakeWriter();
return true;
}
auto it = streams_.find(fr.stream_id);
if (it == streams_.end()) {
if (fr.stream_id > max_stream_id_) {
Fatal(kProtocolError);
return false;
}
return true; // closed: ignore
}
StreamOutcome oc = it->second.OnWindowUpdate(inc);
if (oc.event == StreamEvent::LocalReset) {
SendRst(fr.stream_id, oc.rst_code);
EraseStream(fr.stream_id);
return true;
}
flow_.OnStreamWindowUpdate(fr.stream_id, inc);
WakeWriter();
return true;
}
bool H2Connection::OnPriority(const Frame& fr, const char* payload) {
(void)payload;
if (fr.stream_id == 0) {
Fatal(kProtocolError);
return false;
}
if (fr.length != 5) {
Fatal(kFrameSizeError);
return false;
}
return true; // accepted and ignored per plan open question 2
}
void H2Connection::EraseStream(uint32_t stream_id) {
streams_.erase(stream_id);
flow_.RemoveStream(stream_id);
stream_in_window_.erase(stream_id);
if ((draining_ || peer_goaway_) && streams_.empty()) {
conn_.InterruptRead(); // let the read loop notice the drain is done
}
}
void H2Connection::Dispatch(uint32_t stream_id) {
auto it = streams_.find(stream_id);
if (it == streams_.end()) return;
FullRequest req;
req.head = it->second.head();
req.body = it->second.body();
req.trailers = it->second.trailers();
req.is_tls = conn_.is_tls();
req.peer = conn_.peer();
req.protocol = "HTTP/2";
++requests_;
MOLO_RUNTIME_COUNT("runtime.h2.requests");
conn_.loop().post(StreamTask(weak_from_this(), stream_id, std::move(req)));
}
Task<void> H2Connection::StreamTask(std::weak_ptr<H2Connection> weak,
uint32_t stream_id, FullRequest req) {
auto self = weak.lock();
if (!self) co_return;
TimerWheel::TimerId budget = self->conn_.loop().schedule_after(
self->config_.handler_budget, [weak, stream_id] {
if (auto s = weak.lock()) {
auto it = s->streams_.find(stream_id);
if (it != s->streams_.end()) {
s->SendRst(stream_id, kCancel);
s->EraseStream(stream_id);
}
}
});
FullResponse resp;
bool ok = true;
try {
resp = co_await self->conn_.handler()(req);
} catch (...) {
ok = false;
}
self = weak.lock();
if (!self) co_return;
self->conn_.loop().cancel_timer(budget);
if (!ok) {
resp.head.status = 500;
resp.head.headers.clear();
resp.head.headers.set("content-type", "text/plain; charset=utf-8");
resp.body = "500 Internal Server Error\n";
}
std::optional<FileBody> fb;
if (resp.file.has_value()) {
fb = resp.file;
resp.file.reset();
}
if (fb.has_value() && fb->fd >= 0 && !resp.body.empty()) {
// Contract violation: file and string body at once. Fail closed.
::close(fb->fd);
fb.reset();
resp.head.status = 500;
resp.head.headers.clear();
resp.head.headers.set("content-type", "text/plain; charset=utf-8");
resp.body = "500 Internal Server Error\n";
}
bool file_follows = fb.has_value() && fb->fd >= 0;
bool stream_file = self->CompleteResponse(stream_id, std::move(resp),
file_follows);
if (stream_file && file_follows) {
co_await FileBodyTask(weak, stream_id, *fb);
} else if (file_follows) {
::close(fb->fd); // HEAD, reset, or no-body status: drop the bytes
}
}
Task<void> H2Connection::FileBodyTask(std::weak_ptr<H2Connection> weak,
uint32_t stream_id, FileBody fb) {
struct Closer {
int fd;
~Closer() {
if (fd >= 0) ::close(fd);
}
} closer{fb.fd};
if (!ClampFileBody(fb)) {
if (auto s = weak.lock()) s->SendRst(stream_id, kInternalError);
co_return;
}
auto self = weak.lock();
if (!self) co_return;
if (fb.length == 0) {
// Empty file still needs the terminal flag.
if (self->streams_.find(stream_id) != self->streams_.end()) {
self->EnqueueData(stream_id, std::string(), true);
}
co_return;
}
// Framing forbids sendfile here, so stream bounded 64 KB DATA items with
// queue backpressure: park on yield until the writer drains below the
// high watermark.
uint64_t pos = fb.offset;
uint64_t left = fb.length;
std::string chunk;
chunk.resize(caps::kFileChunkSize);
while (left > 0) {
self = weak.lock();
if (!self || self->conn_.closed()) co_return;
if (self->streams_.find(stream_id) == self->streams_.end()) co_return;
std::size_t n =
left > chunk.size() ? chunk.size() : (std::size_t)left;
ssize_t r = ::pread(fb.fd, chunk.data(), n, (off_t)pos);
if (r <= 0) {
self->SendRst(stream_id, kInternalError);
co_return;
}
pos += (uint64_t)r;
left -= (uint64_t)r;
self->EnqueueData(stream_id, chunk.substr(0, (std::size_t)r),
left == 0);
while (self && !self->conn_.closed() &&
self->out_queued_bytes_ > caps::kWriteHighWatermark &&
self->streams_.find(stream_id) != self->streams_.end()) {
co_await self->conn_.loop().yield();
self = weak.lock();
}
}
}
bool H2Connection::CompleteResponse(uint32_t stream_id, FullResponse resp,
bool file_follows) {
auto it = streams_.find(stream_id);
if (it == streams_.end()) return false; // reset or timed out: drop
if (it->second.state() == StreamState::Closed) {
EraseStream(stream_id);
return false;
}
std::vector<HpackField> fields;
fields.push_back({":status", std::to_string(resp.head.status)});
for (const auto& kv : resp.head.headers.entries()) {
if (IsH2HopHeader(kv.first)) continue;
if (!kv.first.empty() && kv.first[0] == ':') continue;
fields.push_back({kv.first, kv.second});
}
std::string block = encoder_.Encode(fields);
bool head_only = (it->second.head().method == "HEAD");
bool no_body = head_only || resp.head.status == 204 ||
resp.head.status == 304 ||
(resp.head.status >= 100 && resp.head.status < 200) ||
(!file_follows && resp.body.empty());
std::size_t maxf = peer_.max_frame_size;
if (maxf == 0) maxf = caps::kH2MaxFrameDefault;
std::size_t off = 0;
bool first = true;
// HEADERS plus CONTINUATIONs as one control item.
std::string head_frames;
while (off < block.size()) {
std::size_t n = block.size() - off;
if (n > maxf) n = maxf;
bool last = (off + n >= block.size());
uint8_t flags = 0;
if (last) flags |= kFlagEndHeaders;
if (last && no_body) flags |= kFlagEndStream;
head_frames.append(FrameBytes(first ? kHeaders : kContinuation, flags,
stream_id, block.data() + off, n));
off += n;
first = false;
}
if (block.empty()) {
uint8_t flags = kFlagEndHeaders;
if (no_body) flags |= kFlagEndStream;
head_frames.append(FrameBytes(kHeaders, flags, stream_id, nullptr, 0));
}
OutItem ctl;
ctl.stream_id = stream_id;
ctl.bytes = std::move(head_frames);
ctl.is_data = false;
ctl.end_stream = no_body;
out_queued_bytes_ += ctl.bytes.size();
out_queue_.push_back(std::move(ctl));
if (no_body) {
KickWriter();
return false;
}
if (file_follows) {
KickWriter(); // HEADERS flush while the file streams behind
return true;
}
EnqueueData(stream_id, std::move(resp.body), true);
return false;
}
Task<void> H2Connection::WriterTask(std::weak_ptr<H2Connection> weak) {
auto self = weak.lock();
if (!self) co_return;
struct WindowAwait {
H2Connection* h2;
bool await_ready() const noexcept { return false; }
void await_suspend(std::coroutine_handle<> h) { h2->window_waiter_ = h; }
void await_resume() noexcept {}
};
while (self && !self->out_queue_.empty() && !self->conn_.closed()) {
OutItem& item = self->out_queue_.front();
if (!item.is_data) {
self->conn_.outbox().append(item.bytes);
bool ok = co_await self->conn_.Flush(self->config_.idle_timeout);
self = weak.lock();
if (!self) co_return;
if (!ok) break;
bool end = item.end_stream;
uint32_t sid = item.stream_id;
self->UnqueueBytes(item.bytes.size() - item.offset);
self->out_queue_.pop_front();
auto it = self->streams_.find(sid);
if (it != self->streams_.end()) {
it->second.MarkResponseHeadersSent(end);
if (end) self->EraseStream(sid);
}
continue;
}
// DATA with flow control.
auto it = self->streams_.find(item.stream_id);
if (it == self->streams_.end()) {
self->UnqueueBytes(item.bytes.size() - item.offset);
self->out_queue_.pop_front(); // reset meanwhile: drop
continue;
}
std::size_t remaining = item.bytes.size() - item.offset;
int64_t cw = self->flow_.ConnectionAvailable();
int64_t sw = self->flow_.StreamAvailable(item.stream_id);
if (cw <= 0 || sw <= 0) {
if (self->read_done_) {
self->UnqueueBytes(item.bytes.size() - item.offset);
self->out_queue_.pop_front(); // no more updates coming
continue;
}
co_await WindowAwait{self.get()};
self = weak.lock();
if (!self) co_return;
continue;
}
std::size_t n = remaining;
if ((int64_t)n > cw) n = (std::size_t)cw;
if ((int64_t)n > sw) n = (std::size_t)sw;
std::size_t maxf = self->peer_.max_frame_size;
if (maxf == 0) maxf = caps::kH2MaxFrameDefault;
if (n > maxf) n = maxf;
self->flow_.TakeConnection(n);
self->flow_.TakeStream(item.stream_id, n);
bool last = (item.offset + n >= item.bytes.size());
bool end = last && item.end_stream;
uint8_t flags = end ? kFlagEndStream : 0;
self->conn_.outbox().append(FrameBytes(kData, flags, item.stream_id,
item.bytes.data() + item.offset,
n));
bool ok = co_await self->conn_.Flush(self->config_.idle_timeout);
self = weak.lock();
if (!self) co_return;
if (!ok) break;
item.offset += n;
self->UnqueueBytes(n);
auto jt = self->streams_.find(item.stream_id);
if (jt != self->streams_.end()) {
jt->second.MarkResponseDataSent(end);
if (end) self->EraseStream(item.stream_id);
}
if (last) self->out_queue_.pop_front();
}
if (!self) co_return;
self->writer_active_ = false;
self->window_waiter_ = nullptr;
if (self->writer_done_waiter_) {
auto h = self->writer_done_waiter_;
self->writer_done_waiter_ = nullptr;
h.resume();
}
}
bool H2Connection::ApplyUpgrade(const RequestHead& head,
const std::string& body) {
std::string encoded = head.headers.get("http2-settings");
std::optional<std::string> raw = Base64Url::Decode(encoded);
if (!raw.has_value()) {
Fatal(kProtocolError);
return false;
}
Settings next = peer_;
SettingsError e = DecodeSettingsPayload(raw->data(), raw->size(), next);
if (e != SettingsError::Ok) {
Fatal(kProtocolError);
return false;
}
peer_ = next;
encoder_.SetMaxTableSize(peer_.header_table_size);
// Stream 1 carries the upgraded request, half-closed (remote).
std::vector<HpackField> fields;
fields.push_back({":method", head.method});
fields.push_back({":scheme", conn_.is_tls() ? "https" : "http"});
std::string authority = head.headers.get("host");
if (authority.empty()) authority = head.headers.get(":authority");
fields.push_back({":authority", authority});
fields.push_back({":path", head.target});
for (const auto& kv : head.headers.entries()) {
if (IsH2HopHeader(kv.first)) continue;
if (EqCaseInsensitive(kv.first, "host")) continue;
fields.push_back({kv.first, kv.second});
}
streams_.emplace(1, H2Stream(1, config_.body_cap));
max_stream_id_ = 1;
flow_.AddStream(1, peer_.initial_window_size);
stream_in_window_[1] = (int64_t)local_.initial_window_size;
auto it = streams_.find(1);
StreamOutcome oc = it->second.OnHeaders(fields, body.empty());
if (oc.event == StreamEvent::LocalReset) {
Fatal(kProtocolError);
return false;
}
if (!body.empty()) {
oc = it->second.OnData(body.data(), body.size(), true);
if (oc.event != StreamEvent::MessageComplete) {
Fatal(kProtocolError);
return false;
}
}
if (oc.event == StreamEvent::MessageComplete) Dispatch(1);
return true;
}
} // namespace molo::runtime::http::v2
+152
View File
@@ -0,0 +1,152 @@
// Spec: plans/01-runtime.md 6.4, RFC 9113. H2 framing, settings handshake,
// HPACK, stream multiplexing, dual flow control, GOAWAY/RST/PING. No server
// push. PRIORITY accepted and ignored. Runs on the owning Connection's
// transport, waiter and buffers; the Connection keeps lifecycle ownership.
#pragma once
#include <chrono>
#include <coroutine>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "runtime/caps.h"
#include "runtime/http/message.h"
#include "runtime/task.h"
#include "flow_control.h"
#include "hpack.h"
#include "settings.h"
#include "stream.h"
namespace molo::runtime {
class EventLoop;
namespace tls {
class ITlsHandle;
}
namespace http {
class Connection;
struct FullRequest;
struct FullResponse;
} // namespace http
} // namespace molo::runtime
namespace molo::runtime::http::v2 {
struct H2Config {
std::size_t body_cap = caps::kBodyMaxDefault;
std::size_t max_concurrent_streams = caps::kH2MaxConcurrentStreamsDefault;
std::chrono::milliseconds idle_timeout = caps::kIdleKeepAliveTimeout;
std::chrono::milliseconds handler_budget = caps::kHandlerBudget;
};
class H2Connection : public std::enable_shared_from_this<H2Connection> {
public:
H2Connection(Connection& conn, H2Config config = H2Config{});
~H2Connection() = default;
Task<void> Run(bool expect_magic, bool from_upgrade,
const RequestHead& upgrade_head,
const std::string& upgrade_body);
void drain();
std::size_t requests_served() const { return requests_; }
private:
struct Frame {
uint32_t length = 0;
uint8_t type = 0;
uint8_t flags = 0;
uint32_t stream_id = 0;
};
struct OutItem {
uint32_t stream_id = 0;
std::string bytes; // payload for DATA, full frame(s) otherwise
bool is_data = false;
bool end_stream = false;
std::size_t offset = 0;
};
static Task<void> StreamTask(std::weak_ptr<H2Connection> weak,
uint32_t stream_id, FullRequest req);
static Task<void> WriterTask(std::weak_ptr<H2Connection> weak);
static Task<void> FileBodyTask(std::weak_ptr<H2Connection> weak,
uint32_t stream_id, FileBody fb);
Task<bool> Ensure(std::size_t n);
Task<void> ReadLoop(bool first_must_be_settings);
bool ProcessFrame(const Frame& fr, const char* payload);
// Sends HEADERS. file_follows means a file body streams next: the stream
// stays open unless HEAD or a no-body status applies. Returns true when
// the caller must stream the file body (via FileBodyTask).
bool CompleteResponse(uint32_t stream_id, FullResponse resp,
bool file_follows);
void EnqueueControl(uint32_t stream_id, const std::string& frame_bytes);
void EnqueueData(uint32_t stream_id, std::string payload, bool end_stream);
void KickWriter();
void WakeWriter();
void SendSettingsAck();
void SendPingAck(const char* payload);
void SendRst(uint32_t stream_id, uint32_t code);
void SendGoaway(uint32_t code);
void SendWindowUpdate(uint32_t stream_id, uint32_t increment);
void Fatal(uint32_t code);
static std::string FrameBytes(uint8_t type, uint8_t flags,
uint32_t stream_id, const char* payload,
std::size_t len);
bool OnSettings(const Frame& fr, const char* payload);
bool OnHeaders(const Frame& fr, const char* payload);
bool OnData(const Frame& fr, const char* payload);
bool OnRst(const Frame& fr, const char* payload);
bool OnPing(const Frame& fr, const char* payload);
bool OnGoaway(const Frame& fr, const char* payload);
bool OnWindowUpdate(const Frame& fr, const char* payload);
bool OnPriority(const Frame& fr, const char* payload);
bool OnContinuation(const Frame& fr, const char* payload);
bool FinishHeaders(uint32_t stream_id, const std::string& block,
bool end_stream);
void EraseStream(uint32_t stream_id);
void Dispatch(uint32_t stream_id);
bool ApplyUpgrade(const RequestHead& head, const std::string& body);
Connection& conn_;
H2Config config_;
Settings local_;
Settings peer_;
HpackEncoder encoder_;
HpackDecoder decoder_;
FlowController flow_;
std::unordered_map<uint32_t, H2Stream> streams_;
int64_t conn_in_window_ = 65535;
std::unordered_map<uint32_t, int64_t> stream_in_window_;
std::deque<OutItem> out_queue_;
// Payload bytes currently queued; bounds file-body streaming.
std::size_t out_queued_bytes_ = 0;
void UnqueueBytes(std::size_t n) {
out_queued_bytes_ -=
n > out_queued_bytes_ ? out_queued_bytes_ : n;
}
bool writer_active_ = false;
bool read_done_ = false;
bool close_after_flush_ = false;
bool draining_ = false;
bool goaway_sent_ = false;
bool peer_goaway_ = false;
bool fatal_ = false;
uint32_t max_stream_id_ = 0;
std::size_t requests_ = 0;
// CONTINUATION reassembly state.
bool cont_expected_ = false;
uint32_t cont_stream_ = 0;
std::string cont_buf_;
bool cont_end_stream_ = false;
std::coroutine_handle<> window_waiter_ = nullptr;
std::coroutine_handle<> writer_done_waiter_ = nullptr;
};
} // namespace molo::runtime::http::v2
+82
View File
@@ -0,0 +1,82 @@
// Spec: plans/01-runtime.md 6.4, RFC 9113 section 6.9. Flow control.
#include "flow_control.h"
namespace molo::runtime::http::v2 {
namespace {
constexpr int64_t kWindowMax = 0x7fffffffLL;
}
FlowController::FlowController(uint32_t initial_window)
: conn_out_((int64_t)initial_window) {}
bool FlowController::TakeConnection(std::size_t n) {
if ((int64_t)n > conn_out_) return false;
conn_out_ -= (int64_t)n;
return true;
}
bool FlowController::TakeStream(uint32_t stream_id, std::size_t n) {
auto it = stream_out_.find(stream_id);
if (it == stream_out_.end()) return false;
if ((int64_t)n > it->second) return false;
it->second -= (int64_t)n;
return true;
}
void FlowController::OnConnectionWindowUpdate(uint32_t increment) {
conn_out_ += (int64_t)increment;
if (conn_out_ > kWindowMax) conn_out_ = kWindowMax;
}
void FlowController::OnStreamWindowUpdate(uint32_t stream_id,
uint32_t increment) {
auto it = stream_out_.find(stream_id);
if (it == stream_out_.end()) return;
it->second += (int64_t)increment;
if (it->second > kWindowMax) it->second = kWindowMax;
}
bool FlowController::AdjustAllStreams(int64_t delta, uint32_t new_initial) {
(void)new_initial;
for (const auto& kv : stream_out_) {
if (kv.second + delta > kWindowMax) return false;
}
for (auto& kv : stream_out_) kv.second += delta;
return true;
}
void FlowController::AddStream(uint32_t stream_id, uint32_t initial_window) {
stream_out_[stream_id] = (int64_t)initial_window;
}
void FlowController::RemoveStream(uint32_t stream_id) {
stream_out_.erase(stream_id);
stream_in_used_.erase(stream_id);
}
int64_t FlowController::StreamAvailable(uint32_t stream_id) const {
auto it = stream_out_.find(stream_id);
if (it == stream_out_.end()) return 0;
return it->second;
}
void FlowController::ConsumedStream(uint32_t stream_id, std::size_t n) {
stream_in_used_[stream_id] += n;
}
std::size_t FlowController::TakeConnectionRefund() {
std::size_t n = conn_in_used_;
conn_in_used_ = 0;
return n;
}
std::size_t FlowController::TakeStreamRefund(uint32_t stream_id) {
auto it = stream_in_used_.find(stream_id);
if (it == stream_in_used_.end()) return 0;
std::size_t n = it->second;
it->second = 0;
return n;
}
} // namespace molo::runtime::http::v2
+43
View File
@@ -0,0 +1,43 @@
// Spec: plans/01-runtime.md 6.4, RFC 9113 section 6.9. Connection-level plus
// stream-level flow control accounting. Windows are signed: a SETTINGS
// reduction may push a stream window negative, which only blocks sends.
#pragma once
#include <cstddef>
#include <cstdint>
#include <unordered_map>
namespace molo::runtime::http::v2 {
class FlowController {
public:
explicit FlowController(uint32_t initial_window = 65535);
// Outbound (peer windows): take returns false when the window is too
// small; the caller waits for WINDOW_UPDATE.
bool TakeConnection(std::size_t n);
bool TakeStream(uint32_t stream_id, std::size_t n);
void OnConnectionWindowUpdate(uint32_t increment);
void OnStreamWindowUpdate(uint32_t stream_id, uint32_t increment);
// Applies an INITIAL_WINDOW_SIZE change to every live stream. False when
// a window would exceed 2^31-1 (FLOW_CONTROL_ERROR).
bool AdjustAllStreams(int64_t delta, uint32_t new_initial);
void AddStream(uint32_t stream_id, uint32_t initial_window);
void RemoveStream(uint32_t stream_id);
int64_t ConnectionAvailable() const { return conn_out_; }
int64_t StreamAvailable(uint32_t stream_id) const;
// Inbound: track consumed bytes; the connection emits WINDOW_UPDATE.
void ConsumedConnection(std::size_t n) { conn_in_used_ += n; }
void ConsumedStream(uint32_t stream_id, std::size_t n);
std::size_t TakeConnectionRefund();
std::size_t TakeStreamRefund(uint32_t stream_id);
private:
int64_t conn_out_;
std::unordered_map<uint32_t, int64_t> stream_out_;
std::size_t conn_in_used_ = 0;
std::unordered_map<uint32_t, std::size_t> stream_in_used_;
};
} // namespace molo::runtime::http::v2
+344
View File
@@ -0,0 +1,344 @@
// Spec: plans/01-runtime.md 6.4, RFC 7541. HPACK codec.
#include "hpack.h"
#include "runtime/caps.h"
namespace molo::runtime::http::v2 {
namespace {
struct StaticEntry {
const char* name;
const char* value;
};
const uint32_t kHuffmanCodes[][2] = {
#include "huffman_table.inc"
};
const StaticEntry kStaticTable[] = {
#include "static_table.inc"
};
constexpr std::size_t kStaticCount =
sizeof(kStaticTable) / sizeof(kStaticTable[0]); // 62 with slot 0 unused
} // namespace
std::string HuffmanCodec::Encode(std::string_view in) {
std::string out;
uint64_t acc = 0;
int bits = 0;
auto push = [&](uint32_t code, int len) {
acc = (acc << len) | code;
bits += len;
while (bits >= 8) {
bits -= 8;
out.push_back((char)((acc >> bits) & 0xff));
}
};
for (unsigned char c : in) {
push(kHuffmanCodes[c][0], (int)kHuffmanCodes[c][1]);
}
if (bits > 0) {
// Pad with ones (EOS prefix) per RFC 7541 section 5.2.
acc = (acc << (8 - bits)) | ((1u << (8 - bits)) - 1u);
out.push_back((char)(acc & 0xff));
}
return out;
}
namespace {
struct TrieNode {
int child[2] = {-1, -1};
int symbol = -1; // 0..256, or -1 for interior
};
const std::vector<TrieNode>& DecodeTrie() {
static const std::vector<TrieNode> trie = [] {
std::vector<TrieNode> t(1);
for (int s = 0; s < 257; ++s) {
uint32_t code = kHuffmanCodes[s][0];
int nbits = (int)kHuffmanCodes[s][1];
int node = 0;
for (int i = nbits - 1; i >= 0; --i) {
int bit = (code >> i) & 1;
if (t[(std::size_t)node].child[bit] < 0) {
t[(std::size_t)node].child[bit] = (int)t.size();
t.emplace_back();
}
node = t[(std::size_t)node].child[bit];
}
t[(std::size_t)node].symbol = s;
}
return t;
}();
return trie;
}
} // namespace
std::optional<std::string> HuffmanCodec::Decode(const char* data,
std::size_t len) {
const std::vector<TrieNode>& trie = DecodeTrie();
std::string out;
int node = 0;
int depth = 0; // bits since the last emitted symbol
bool ones_only = true; // partial path is all ones (EOS prefix)
for (std::size_t i = 0; i < len; ++i) {
unsigned char c = (unsigned char)data[i];
for (int b = 7; b >= 0; --b) {
int bit = (c >> b) & 1;
int next = trie[(std::size_t)node].child[bit];
if (next < 0) return std::nullopt;
node = next;
++depth;
ones_only = ones_only && (bit == 1);
int sym = trie[(std::size_t)node].symbol;
if (sym >= 0) {
if (sym == 256) return std::nullopt; // EOS in content
out.push_back((char)sym);
node = 0;
depth = 0;
ones_only = true;
}
}
}
// End state: exact symbol boundary, or an all-ones EOS-prefix padding of
// at most 7 bits. This matches the reference COMPLETE flag semantics.
if (node == 0) return out;
if (ones_only && depth <= 7) return out;
return std::nullopt;
}
HpackEncoder::HpackEncoder(std::size_t max_table_size)
: max_table_size_(max_table_size) {}
void HpackEncoder::SetMaxTableSize(std::size_t n) { max_table_size_ = n; }
namespace {
void EncodeInt(std::string& out, uint32_t value, uint8_t prefix_bits,
uint8_t prefix) {
uint8_t max = (uint8_t)((1u << prefix_bits) - 1u);
if (value < max) {
out.push_back((char)(prefix | (uint8_t)value));
return;
}
out.push_back((char)(prefix | max));
value -= max;
while (value >= 128) {
out.push_back((char)((value & 0x7f) | 0x80));
value >>= 7;
}
out.push_back((char)value);
}
void EncodeString(std::string& out, const std::string& s) {
// Literal without Huffman: smaller code, always valid. Huffman saving is
// an encoder optimization, not an interop requirement.
EncodeInt(out, (uint32_t)s.size(), 7, 0x00);
out.append(s);
}
} // namespace
std::string HpackEncoder::Encode(const std::vector<HpackField>& fields) {
(void)max_table_size_;
std::string out;
for (const auto& f : fields) {
int full = -1;
int name_only = -1;
for (std::size_t i = 1; i < kStaticCount; ++i) {
if (kStaticTable[i].name == f.name) {
if (name_only < 0) name_only = (int)i;
if (kStaticTable[i].value == f.value) {
full = (int)i;
break;
}
}
}
if (full >= 0) {
EncodeInt(out, (uint32_t)full, 7, 0x80); // indexed field
continue;
}
if (name_only >= 0) {
// Literal without indexing, indexed name.
EncodeInt(out, (uint32_t)name_only, 4, 0x00);
EncodeString(out, f.value);
continue;
}
// Literal without indexing, new name.
out.push_back((char)0x00);
EncodeString(out, f.name);
EncodeString(out, f.value);
}
return out;
}
HpackDecoder::HpackDecoder(std::size_t max_table_size,
std::size_t max_header_list)
: max_table_size_(max_table_size), max_header_list_(max_header_list) {}
void HpackDecoder::SetMaxTableSize(std::size_t n) {
max_table_size_ = n;
EvictTo(n);
}
HpackError HpackDecoder::DecodeInt(const char*& p, const char* end,
uint8_t prefix_bits, uint32_t& out) {
if (p >= end) return HpackError::Truncated;
uint8_t max = (uint8_t)((1u << prefix_bits) - 1u);
uint32_t v = (uint32_t)((unsigned char)*p & max);
++p;
if (v < max) {
out = v;
return HpackError::Ok;
}
uint32_t m = 0;
for (;;) {
if (p >= end) return HpackError::Truncated;
uint8_t b = (unsigned char)*p++;
if (m >= 28 && (b & 0x7f) >= (1u << (32 - m))) {
return HpackError::BadInteger;
}
v += (uint32_t)(b & 0x7f) << m;
if ((b & 0x80) == 0) break;
m += 7;
if (m >= 35) return HpackError::BadInteger;
}
out = v;
return HpackError::Ok;
}
HpackError HpackDecoder::DecodeString(const char*& p, const char* end,
std::string& out) {
if (p >= end) return HpackError::Truncated;
bool huffman = ((unsigned char)*p & 0x80) != 0;
uint32_t len = 0;
HpackError e = DecodeInt(p, end, 7, len);
if (e != HpackError::Ok) return e;
if ((std::size_t)(end - p) < len) return HpackError::Truncated;
if (huffman) {
std::optional<std::string> s = HuffmanCodec::Decode(p, len);
if (!s.has_value()) return HpackError::BadHuffman;
out = std::move(*s);
} else {
out.assign(p, len);
}
p += len;
return HpackError::Ok;
}
HpackError HpackDecoder::Lookup(uint32_t index, HpackField& out) const {
if (index == 0) return HpackError::BadIndex;
if (index < kStaticCount) {
out.name = kStaticTable[index].name;
out.value = kStaticTable[index].value;
return HpackError::Ok;
}
uint32_t dyn = index - (uint32_t)kStaticCount;
if (dyn >= dynamic_.size()) return HpackError::BadIndex;
out = dynamic_[dyn];
return HpackError::Ok;
}
void HpackDecoder::EvictTo(std::size_t limit) {
while (table_bytes_ > limit && !dynamic_.empty()) {
const HpackField& back = dynamic_.back();
table_bytes_ -= back.name.size() + back.value.size() + kEntryOverhead;
dynamic_.pop_back();
}
}
void HpackDecoder::Insert(HpackField f) {
std::size_t sz = f.name.size() + f.value.size() + kEntryOverhead;
if (sz > max_table_size_) {
dynamic_.clear();
table_bytes_ = 0;
return;
}
EvictTo(max_table_size_ - sz);
table_bytes_ += sz;
dynamic_.push_front(std::move(f));
}
HpackError HpackDecoder::Decode(const char* data, std::size_t len,
std::vector<HpackField>& out) {
const char* p = data;
const char* end = data + len;
std::size_t list_bytes = 0;
while (p < end) {
uint8_t b = (unsigned char)*p;
if (b & 0x80) {
// Indexed field.
uint32_t index = 0;
HpackError e = DecodeInt(p, end, 7, index);
if (e != HpackError::Ok) return e;
HpackField f;
e = Lookup(index, f);
if (e != HpackError::Ok) return e;
list_bytes += f.name.size() + f.value.size() + kEntryOverhead;
if (list_bytes > max_header_list_) {
return HpackError::HeaderListTooLarge;
}
out.push_back(std::move(f));
} else if ((b & 0xc0) == 0x40) {
// Literal with incremental indexing.
uint32_t index = 0;
HpackError e = DecodeInt(p, end, 6, index);
if (e != HpackError::Ok) return e;
HpackField f;
if (index == 0) {
e = DecodeString(p, end, f.name);
if (e != HpackError::Ok) return e;
} else {
HpackField t;
e = Lookup(index, t);
if (e != HpackError::Ok) return e;
f.name = std::move(t.name);
}
e = DecodeString(p, end, f.value);
if (e != HpackError::Ok) return e;
list_bytes += f.name.size() + f.value.size() + kEntryOverhead;
if (list_bytes > max_header_list_) {
return HpackError::HeaderListTooLarge;
}
out.push_back(f);
Insert(std::move(f));
} else if ((b & 0xe0) == 0x20) {
// Table size update.
uint32_t size = 0;
HpackError e = DecodeInt(p, end, 5, size);
if (e != HpackError::Ok) return e;
if (size > max_table_size_) return HpackError::TableSizeChanged;
EvictTo(size);
} else {
// Literal without indexing (0x00) or never indexed (0x10).
uint32_t index = 0;
HpackError e = DecodeInt(p, end, 4, index);
if (e != HpackError::Ok) return e;
HpackField f;
if (index == 0) {
e = DecodeString(p, end, f.name);
if (e != HpackError::Ok) return e;
} else {
HpackField t;
e = Lookup(index, t);
if (e != HpackError::Ok) return e;
f.name = std::move(t.name);
}
e = DecodeString(p, end, f.value);
if (e != HpackError::Ok) return e;
list_bytes += f.name.size() + f.value.size() + kEntryOverhead;
if (list_bytes > max_header_list_) {
return HpackError::HeaderListTooLarge;
}
out.push_back(std::move(f));
}
if (out.size() > caps::kHeaderFieldMax) return HpackError::TooManyFields;
}
return HpackError::Ok;
}
} // namespace molo::runtime::http::v2
+76
View File
@@ -0,0 +1,76 @@
// Spec: plans/01-runtime.md 6.4, RFC 7541. HPACK field compression: static
// plus dynamic table, Huffman string codec. The encoder emits indexed fields
// for static hits and literals without indexing otherwise (no dynamic table
// inserts); the decoder implements the full representation set including
// incremental indexing and table size updates for interop.
#pragma once
#include <cstddef>
#include <cstdint>
#include <deque>
#include <optional>
#include <string>
#include <utility>
#include <vector>
namespace molo::runtime::http::v2 {
struct HpackField {
std::string name;
std::string value;
};
enum class HpackError {
Ok,
Truncated,
BadIndex,
BadInteger,
BadHuffman,
TableSizeChanged,
HeaderListTooLarge,
TooManyFields,
};
// Huffman string codec over the RFC 7541 Appendix B table.
class HuffmanCodec {
public:
static std::string Encode(std::string_view in);
// Nullopt on invalid encoding (bad padding, EOS symbol, overlong).
static std::optional<std::string> Decode(const char* data, std::size_t len);
};
class HpackEncoder {
public:
explicit HpackEncoder(std::size_t max_table_size = 4096);
std::string Encode(const std::vector<HpackField>& fields);
void SetMaxTableSize(std::size_t n);
private:
std::size_t max_table_size_;
};
class HpackDecoder {
public:
explicit HpackDecoder(std::size_t max_table_size = 4096,
std::size_t max_header_list = 65536);
HpackError Decode(const char* data, std::size_t len,
std::vector<HpackField>& out);
void SetMaxTableSize(std::size_t n);
std::size_t table_size() const { return table_bytes_; }
private:
static constexpr std::size_t kEntryOverhead = 32;
HpackError DecodeInt(const char*& p, const char* end, uint8_t prefix_bits,
uint32_t& out);
HpackError DecodeString(const char*& p, const char* end, std::string& out);
HpackError Lookup(uint32_t index, HpackField& out) const;
void Insert(HpackField f);
void EvictTo(std::size_t limit);
std::size_t max_table_size_;
std::size_t max_header_list_;
std::size_t table_bytes_ = 0;
std::deque<HpackField> dynamic_;
};
} // namespace molo::runtime::http::v2
+259
View File
@@ -0,0 +1,259 @@
// Generated from RFC 7541 Appendix B, cross-checked against python-hpack.
// Entry: {code (LSB aligned), bit length}. Index 256 is EOS (encode-only guard).
{0x1ff8u, 13u}, // 0
{0x7fffd8u, 23u}, // 1
{0xfffffe2u, 28u}, // 2
{0xfffffe3u, 28u}, // 3
{0xfffffe4u, 28u}, // 4
{0xfffffe5u, 28u}, // 5
{0xfffffe6u, 28u}, // 6
{0xfffffe7u, 28u}, // 7
{0xfffffe8u, 28u}, // 8
{0xffffeau, 24u}, // 9
{0x3ffffffcu, 30u}, // 10
{0xfffffe9u, 28u}, // 11
{0xfffffeau, 28u}, // 12
{0x3ffffffdu, 30u}, // 13
{0xfffffebu, 28u}, // 14
{0xfffffecu, 28u}, // 15
{0xfffffedu, 28u}, // 16
{0xfffffeeu, 28u}, // 17
{0xfffffefu, 28u}, // 18
{0xffffff0u, 28u}, // 19
{0xffffff1u, 28u}, // 20
{0xffffff2u, 28u}, // 21
{0x3ffffffeu, 30u}, // 22
{0xffffff3u, 28u}, // 23
{0xffffff4u, 28u}, // 24
{0xffffff5u, 28u}, // 25
{0xffffff6u, 28u}, // 26
{0xffffff7u, 28u}, // 27
{0xffffff8u, 28u}, // 28
{0xffffff9u, 28u}, // 29
{0xffffffau, 28u}, // 30
{0xffffffbu, 28u}, // 31
{0x14u, 6u}, // 32
{0x3f8u, 10u}, // 33
{0x3f9u, 10u}, // 34
{0xffau, 12u}, // 35
{0x1ff9u, 13u}, // 36
{0x15u, 6u}, // 37
{0xf8u, 8u}, // 38
{0x7fau, 11u}, // 39
{0x3fau, 10u}, // 40
{0x3fbu, 10u}, // 41
{0xf9u, 8u}, // 42
{0x7fbu, 11u}, // 43
{0xfau, 8u}, // 44
{0x16u, 6u}, // 45
{0x17u, 6u}, // 46
{0x18u, 6u}, // 47
{0x0u, 5u}, // 48
{0x1u, 5u}, // 49
{0x2u, 5u}, // 50
{0x19u, 6u}, // 51
{0x1au, 6u}, // 52
{0x1bu, 6u}, // 53
{0x1cu, 6u}, // 54
{0x1du, 6u}, // 55
{0x1eu, 6u}, // 56
{0x1fu, 6u}, // 57
{0x5cu, 7u}, // 58
{0xfbu, 8u}, // 59
{0x7ffcu, 15u}, // 60
{0x20u, 6u}, // 61
{0xffbu, 12u}, // 62
{0x3fcu, 10u}, // 63
{0x1ffau, 13u}, // 64
{0x21u, 6u}, // 65
{0x5du, 7u}, // 66
{0x5eu, 7u}, // 67
{0x5fu, 7u}, // 68
{0x60u, 7u}, // 69
{0x61u, 7u}, // 70
{0x62u, 7u}, // 71
{0x63u, 7u}, // 72
{0x64u, 7u}, // 73
{0x65u, 7u}, // 74
{0x66u, 7u}, // 75
{0x67u, 7u}, // 76
{0x68u, 7u}, // 77
{0x69u, 7u}, // 78
{0x6au, 7u}, // 79
{0x6bu, 7u}, // 80
{0x6cu, 7u}, // 81
{0x6du, 7u}, // 82
{0x6eu, 7u}, // 83
{0x6fu, 7u}, // 84
{0x70u, 7u}, // 85
{0x71u, 7u}, // 86
{0x72u, 7u}, // 87
{0xfcu, 8u}, // 88
{0x73u, 7u}, // 89
{0xfdu, 8u}, // 90
{0x1ffbu, 13u}, // 91
{0x7fff0u, 19u}, // 92
{0x1ffcu, 13u}, // 93
{0x3ffcu, 14u}, // 94
{0x22u, 6u}, // 95
{0x7ffdu, 15u}, // 96
{0x3u, 5u}, // 97
{0x23u, 6u}, // 98
{0x4u, 5u}, // 99
{0x24u, 6u}, // 100
{0x5u, 5u}, // 101
{0x25u, 6u}, // 102
{0x26u, 6u}, // 103
{0x27u, 6u}, // 104
{0x6u, 5u}, // 105
{0x74u, 7u}, // 106
{0x75u, 7u}, // 107
{0x28u, 6u}, // 108
{0x29u, 6u}, // 109
{0x2au, 6u}, // 110
{0x7u, 5u}, // 111
{0x2bu, 6u}, // 112
{0x76u, 7u}, // 113
{0x2cu, 6u}, // 114
{0x8u, 5u}, // 115
{0x9u, 5u}, // 116
{0x2du, 6u}, // 117
{0x77u, 7u}, // 118
{0x78u, 7u}, // 119
{0x79u, 7u}, // 120
{0x7au, 7u}, // 121
{0x7bu, 7u}, // 122
{0x7ffeu, 15u}, // 123
{0x7fcu, 11u}, // 124
{0x3ffdu, 14u}, // 125
{0x1ffdu, 13u}, // 126
{0xffffffcu, 28u}, // 127
{0xfffe6u, 20u}, // 128
{0x3fffd2u, 22u}, // 129
{0xfffe7u, 20u}, // 130
{0xfffe8u, 20u}, // 131
{0x3fffd3u, 22u}, // 132
{0x3fffd4u, 22u}, // 133
{0x3fffd5u, 22u}, // 134
{0x7fffd9u, 23u}, // 135
{0x3fffd6u, 22u}, // 136
{0x7fffdau, 23u}, // 137
{0x7fffdbu, 23u}, // 138
{0x7fffdcu, 23u}, // 139
{0x7fffddu, 23u}, // 140
{0x7fffdeu, 23u}, // 141
{0xffffebu, 24u}, // 142
{0x7fffdfu, 23u}, // 143
{0xffffecu, 24u}, // 144
{0xffffedu, 24u}, // 145
{0x3fffd7u, 22u}, // 146
{0x7fffe0u, 23u}, // 147
{0xffffeeu, 24u}, // 148
{0x7fffe1u, 23u}, // 149
{0x7fffe2u, 23u}, // 150
{0x7fffe3u, 23u}, // 151
{0x7fffe4u, 23u}, // 152
{0x1fffdcu, 21u}, // 153
{0x3fffd8u, 22u}, // 154
{0x7fffe5u, 23u}, // 155
{0x3fffd9u, 22u}, // 156
{0x7fffe6u, 23u}, // 157
{0x7fffe7u, 23u}, // 158
{0xffffefu, 24u}, // 159
{0x3fffdau, 22u}, // 160
{0x1fffddu, 21u}, // 161
{0xfffe9u, 20u}, // 162
{0x3fffdbu, 22u}, // 163
{0x3fffdcu, 22u}, // 164
{0x7fffe8u, 23u}, // 165
{0x7fffe9u, 23u}, // 166
{0x1fffdeu, 21u}, // 167
{0x7fffeau, 23u}, // 168
{0x3fffddu, 22u}, // 169
{0x3fffdeu, 22u}, // 170
{0xfffff0u, 24u}, // 171
{0x1fffdfu, 21u}, // 172
{0x3fffdfu, 22u}, // 173
{0x7fffebu, 23u}, // 174
{0x7fffecu, 23u}, // 175
{0x1fffe0u, 21u}, // 176
{0x1fffe1u, 21u}, // 177
{0x3fffe0u, 22u}, // 178
{0x1fffe2u, 21u}, // 179
{0x7fffedu, 23u}, // 180
{0x3fffe1u, 22u}, // 181
{0x7fffeeu, 23u}, // 182
{0x7fffefu, 23u}, // 183
{0xfffeau, 20u}, // 184
{0x3fffe2u, 22u}, // 185
{0x3fffe3u, 22u}, // 186
{0x3fffe4u, 22u}, // 187
{0x7ffff0u, 23u}, // 188
{0x3fffe5u, 22u}, // 189
{0x3fffe6u, 22u}, // 190
{0x7ffff1u, 23u}, // 191
{0x3ffffe0u, 26u}, // 192
{0x3ffffe1u, 26u}, // 193
{0xfffebu, 20u}, // 194
{0x7fff1u, 19u}, // 195
{0x3fffe7u, 22u}, // 196
{0x7ffff2u, 23u}, // 197
{0x3fffe8u, 22u}, // 198
{0x1ffffecu, 25u}, // 199
{0x3ffffe2u, 26u}, // 200
{0x3ffffe3u, 26u}, // 201
{0x3ffffe4u, 26u}, // 202
{0x7ffffdeu, 27u}, // 203
{0x7ffffdfu, 27u}, // 204
{0x3ffffe5u, 26u}, // 205
{0xfffff1u, 24u}, // 206
{0x1ffffedu, 25u}, // 207
{0x7fff2u, 19u}, // 208
{0x1fffe3u, 21u}, // 209
{0x3ffffe6u, 26u}, // 210
{0x7ffffe0u, 27u}, // 211
{0x7ffffe1u, 27u}, // 212
{0x3ffffe7u, 26u}, // 213
{0x7ffffe2u, 27u}, // 214
{0xfffff2u, 24u}, // 215
{0x1fffe4u, 21u}, // 216
{0x1fffe5u, 21u}, // 217
{0x3ffffe8u, 26u}, // 218
{0x3ffffe9u, 26u}, // 219
{0xffffffdu, 28u}, // 220
{0x7ffffe3u, 27u}, // 221
{0x7ffffe4u, 27u}, // 222
{0x7ffffe5u, 27u}, // 223
{0xfffecu, 20u}, // 224
{0xfffff3u, 24u}, // 225
{0xfffedu, 20u}, // 226
{0x1fffe6u, 21u}, // 227
{0x3fffe9u, 22u}, // 228
{0x1fffe7u, 21u}, // 229
{0x1fffe8u, 21u}, // 230
{0x7ffff3u, 23u}, // 231
{0x3fffeau, 22u}, // 232
{0x3fffebu, 22u}, // 233
{0x1ffffeeu, 25u}, // 234
{0x1ffffefu, 25u}, // 235
{0xfffff4u, 24u}, // 236
{0xfffff5u, 24u}, // 237
{0x3ffffeau, 26u}, // 238
{0x7ffff4u, 23u}, // 239
{0x3ffffebu, 26u}, // 240
{0x7ffffe6u, 27u}, // 241
{0x3ffffecu, 26u}, // 242
{0x3ffffedu, 26u}, // 243
{0x7ffffe7u, 27u}, // 244
{0x7ffffe8u, 27u}, // 245
{0x7ffffe9u, 27u}, // 246
{0x7ffffeau, 27u}, // 247
{0x7ffffebu, 27u}, // 248
{0xffffffeu, 28u}, // 249
{0x7ffffecu, 27u}, // 250
{0x7ffffedu, 27u}, // 251
{0x7ffffeeu, 27u}, // 252
{0x7ffffefu, 27u}, // 253
{0x7fffff0u, 27u}, // 254
{0x3ffffeeu, 26u}, // 255
{0x3fffffffu, 30u}, // 256
+102
View File
@@ -0,0 +1,102 @@
// Spec: plans/01-runtime.md 6.4, RFC 9113 sections 6.5, 10. SETTINGS frame
// payload codec with validation. No server push: ENABLE_PUSH must be 0.
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
#include "runtime/caps.h"
namespace molo::runtime::http::v2 {
struct Settings {
uint32_t header_table_size = 4096;
bool enable_push = false; // MUST stay false; push is gone in RFC 9113.
uint32_t max_concurrent_streams = caps::kH2MaxConcurrentStreamsDefault;
uint32_t initial_window_size = 65535;
uint32_t max_frame_size = 16384;
uint32_t max_header_list_size = 65536;
};
enum class SettingsError {
Ok,
ProtocolError, // bad length, ENABLE_PUSH=1, bad MAX_FRAME_SIZE
FlowControlError, // bad INITIAL_WINDOW_SIZE
CompressionError, // bad HEADER_TABLE_SIZE change discipline
};
// Decodes a SETTINGS payload into out (applies peer values, clamped to our
// ceilings). Returns the connection error to raise, if any.
inline SettingsError DecodeSettingsPayload(const char* data, std::size_t len,
Settings& out) {
if (len % 6 != 0) return SettingsError::ProtocolError;
for (std::size_t i = 0; i < len; i += 6) {
uint16_t id = ((uint16_t)(unsigned char)data[i] << 8) |
(uint16_t)(unsigned char)data[i + 1];
uint32_t v = ((uint32_t)(unsigned char)data[i + 2] << 24) |
((uint32_t)(unsigned char)data[i + 3] << 16) |
((uint32_t)(unsigned char)data[i + 4] << 8) |
(uint32_t)(unsigned char)data[i + 5];
switch (id) {
case 0x1:
if (v > caps::kH2HeaderTableCeiling) return SettingsError::CompressionError;
out.header_table_size = v;
break;
case 0x2:
// Deprecated push knob. Real clients (hyper-h2 included) still send
// ENABLE_PUSH=1; accept and ignore it. We never push, so the value
// is meaningless to us either way.
out.enable_push = false;
break;
case 0x3:
out.max_concurrent_streams = v;
if (out.max_concurrent_streams > caps::kH2MaxConcurrentStreamsCeiling) {
out.max_concurrent_streams = caps::kH2MaxConcurrentStreamsCeiling;
}
break;
case 0x4:
if (v > 0x7fffffffu) return SettingsError::FlowControlError;
out.initial_window_size = v;
break;
case 0x5:
if (v < 16384 || v > caps::kH2MaxFrameCeiling) {
return SettingsError::ProtocolError;
}
out.max_frame_size = v;
break;
case 0x6:
out.max_header_list_size = v;
if (out.max_header_list_size > caps::kH2HeaderListMax) {
out.max_header_list_size = caps::kH2HeaderListMax;
}
break;
default:
// Unknown identifiers are ignored.
break;
}
}
return SettingsError::Ok;
}
// Encodes our SETTINGS for the server preface.
inline std::string EncodeSettingsPayload(const Settings& s) {
std::string out;
auto param = [&out](uint16_t id, uint32_t v) {
out.push_back((char)(id >> 8));
out.push_back((char)(id & 0xff));
out.push_back((char)(v >> 24));
out.push_back((char)((v >> 16) & 0xff));
out.push_back((char)((v >> 8) & 0xff));
out.push_back((char)(v & 0xff));
};
param(0x1, s.header_table_size);
param(0x3, s.max_concurrent_streams);
param(0x4, s.initial_window_size);
param(0x5, s.max_frame_size);
param(0x6, s.max_header_list_size);
return out;
}
} // namespace molo::runtime::http::v2
+64
View File
@@ -0,0 +1,64 @@
// HPACK static table, RFC 7541 Appendix A, cross-checked against python-hpack.
// Index 0 unused; entries 1..61.
{"", ""},
{":authority", ""},
{":method", "GET"},
{":method", "POST"},
{":path", "/"},
{":path", "/index.html"},
{":scheme", "http"},
{":scheme", "https"},
{":status", "200"},
{":status", "204"},
{":status", "206"},
{":status", "304"},
{":status", "400"},
{":status", "404"},
{":status", "500"},
{"accept-charset", ""},
{"accept-encoding", "gzip, deflate"},
{"accept-language", ""},
{"accept-ranges", ""},
{"accept", ""},
{"access-control-allow-origin", ""},
{"age", ""},
{"allow", ""},
{"authorization", ""},
{"cache-control", ""},
{"content-disposition", ""},
{"content-encoding", ""},
{"content-language", ""},
{"content-length", ""},
{"content-location", ""},
{"content-range", ""},
{"content-type", ""},
{"cookie", ""},
{"date", ""},
{"etag", ""},
{"expect", ""},
{"expires", ""},
{"from", ""},
{"host", ""},
{"if-match", ""},
{"if-modified-since", ""},
{"if-none-match", ""},
{"if-range", ""},
{"if-unmodified-since", ""},
{"last-modified", ""},
{"link", ""},
{"location", ""},
{"max-forwards", ""},
{"proxy-authenticate", ""},
{"proxy-authorization", ""},
{"range", ""},
{"referer", ""},
{"refresh", ""},
{"retry-after", ""},
{"server", ""},
{"set-cookie", ""},
{"strict-transport-security", ""},
{"transfer-encoding", ""},
{"user-agent", ""},
{"vary", ""},
{"via", ""},
{"www-authenticate", ""},
+145
View File
@@ -0,0 +1,145 @@
// Spec: plans/01-runtime.md 6.4, RFC 9113 section 5. Stream states.
#include "stream.h"
namespace molo::runtime::http::v2 {
namespace {
constexpr uint32_t kNoError = 0x0;
constexpr uint32_t kProtocolError = 0x1;
constexpr uint32_t kCancel = 0x8;
constexpr uint32_t kRefusedStream = 0x7;
} // namespace
H2Stream::H2Stream(uint32_t id, std::size_t body_cap)
: id_(id), body_cap_(body_cap) {}
StreamOutcome H2Stream::OnHeaders(const std::vector<HpackField>& fields,
bool end_stream) {
if (state_ == StreamState::Idle) {
// First HEADERS: build the request head from pseudo-headers.
std::string method, scheme, authority, path;
for (const auto& f : fields) {
if (f.name.empty() || f.name[0] != ':') {
head_.headers.add(f.name, f.value);
continue;
}
if (f.name == ":method") {
if (!method.empty()) return {StreamEvent::LocalReset, kProtocolError};
method = f.value;
} else if (f.name == ":scheme") {
if (!scheme.empty()) return {StreamEvent::LocalReset, kProtocolError};
scheme = f.value;
} else if (f.name == ":authority") {
if (!authority.empty()) {
return {StreamEvent::LocalReset, kProtocolError};
}
authority = f.value;
} else if (f.name == ":path") {
if (!path.empty()) return {StreamEvent::LocalReset, kProtocolError};
path = f.value;
} else {
return {StreamEvent::LocalReset, kProtocolError};
}
}
if (method.empty() || scheme.empty() || path.empty()) {
return {StreamEvent::LocalReset, kProtocolError};
}
if (method == "CONNECT") {
// v1: plain tunnels are out of scope.
return {StreamEvent::LocalReset, kRefusedStream};
}
head_.method = method;
head_.target = path;
head_.version = "HTTP/2";
if (!authority.empty()) head_.headers.add("host", authority);
head_complete_ = true;
if (end_stream) {
state_ = StreamState::HalfClosedRemote;
return CompleteMessage();
}
state_ = StreamState::Open;
return {StreamEvent::HeadersComplete, kNoError};
}
if (state_ == StreamState::Open) {
// Trailers (second HEADERS must carry only regular fields).
for (const auto& f : fields) {
if (!f.name.empty() && f.name[0] == ':') {
return {StreamEvent::LocalReset, kProtocolError};
}
trailers_.add(f.name, f.value);
}
trailers_phase_ = true;
if (end_stream) {
state_ = StreamState::HalfClosedRemote;
return CompleteMessage();
}
return {StreamEvent::HeadersComplete, kNoError};
}
if (state_ == StreamState::HalfClosedRemote) {
return {StreamEvent::LocalReset, kProtocolError};
}
return {StreamEvent::LocalReset, kProtocolError};
}
StreamOutcome H2Stream::OnData(const char* data, std::size_t len,
bool end_stream) {
if (state_ != StreamState::Open) {
return {StreamEvent::LocalReset, kProtocolError};
}
if (body_.size() + len > body_cap_) {
return {StreamEvent::LocalReset, kCancel};
}
body_.append(data, len);
if (end_stream) {
state_ = StreamState::HalfClosedRemote;
return CompleteMessage();
}
return {StreamEvent::None, kNoError};
}
StreamOutcome H2Stream::OnRst(uint32_t code) {
peer_code_ = code;
state_ = StreamState::Closed;
return {StreamEvent::ResetByPeer, kNoError};
}
StreamOutcome H2Stream::OnWindowUpdate(uint32_t increment) {
if (increment == 0) return {StreamEvent::LocalReset, kProtocolError};
if (state_ == StreamState::Idle) {
return {StreamEvent::LocalReset, kProtocolError};
}
return {StreamEvent::None, kNoError};
}
void H2Stream::MarkResponseHeadersSent(bool end_stream) {
if (state_ == StreamState::HalfClosedRemote) {
state_ = end_stream ? StreamState::Closed : StreamState::HalfClosedLocal;
} else if (state_ == StreamState::Open) {
state_ =
end_stream ? StreamState::HalfClosedLocal : StreamState::Open;
} else if (state_ == StreamState::HalfClosedLocal && end_stream) {
state_ = StreamState::Closed;
}
}
void H2Stream::MarkResponseDataSent(bool end_stream) {
if (end_stream) {
if (state_ == StreamState::HalfClosedRemote ||
state_ == StreamState::HalfClosedLocal) {
state_ = StreamState::Closed;
} else if (state_ == StreamState::Open) {
state_ = StreamState::HalfClosedLocal;
}
}
}
StreamOutcome H2Stream::LocalClose(uint32_t code) {
state_ = StreamState::Closed;
return {StreamEvent::LocalReset, code};
}
StreamOutcome H2Stream::CompleteMessage() {
return {StreamEvent::MessageComplete, kNoError};
}
} // namespace molo::runtime::http::v2
+81
View File
@@ -0,0 +1,81 @@
// Spec: plans/01-runtime.md 6.4, RFC 9113 section 5. Server-side stream state
// machine: idle, reserved, open, half-closed, closed. RST_STREAM on handler
// cancel or caps.
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
#include "runtime/http/message.h"
#include "hpack.h"
namespace molo::runtime::http::v2 {
enum class StreamState {
Idle,
ReservedLocal,
ReservedRemote,
Open,
HalfClosedLocal,
HalfClosedRemote,
Closed,
};
enum class StreamEvent {
None,
// HEADERS completed the request head; body may follow via DATA.
HeadersComplete,
// END_STREAM seen: the full request is ready for dispatch.
MessageComplete,
ResetByPeer,
// Local error with the RST code the connection must send.
LocalReset,
};
struct StreamOutcome {
StreamEvent event = StreamEvent::None;
uint32_t rst_code = 0;
};
class H2Stream {
public:
explicit H2Stream(uint32_t id, std::size_t body_cap);
uint32_t id() const { return id_; }
StreamState state() const { return state_; }
const RequestHead& head() const { return head_; }
const std::string& body() const { return body_; }
const Headers& trailers() const { return trailers_; }
bool head_complete() const { return head_complete_; }
// HEADERS frame payload (one decoded header list, CONTINUATION already
// reassembled by the connection). end_stream reflects END_STREAM.
StreamOutcome OnHeaders(const std::vector<HpackField>& fields,
bool end_stream);
StreamOutcome OnData(const char* data, std::size_t len, bool end_stream);
StreamOutcome OnRst(uint32_t code);
StreamOutcome OnWindowUpdate(uint32_t increment);
// Response path: HEADERS then DATA then close.
void MarkResponseHeadersSent(bool end_stream);
void MarkResponseDataSent(bool end_stream);
StreamOutcome LocalClose(uint32_t code); // send RST_STREAM
private:
StreamOutcome CompleteMessage();
uint32_t id_;
std::size_t body_cap_;
StreamState state_ = StreamState::Idle;
RequestHead head_;
std::string body_;
Headers trailers_;
bool head_complete_ = false;
bool trailers_phase_ = false;
uint32_t peer_code_ = 0;
};
} // namespace molo::runtime::http::v2
+39
View File
@@ -0,0 +1,39 @@
// Spec: plans/01-runtime.md 6.x, integration with 02-observability.md.
//
// STUB: zero-cost hook points. Accept, parse, compress and static hit/miss
// call these. They compile to nothing today; 02-observability.md replaces
// this header with real MOLO_LOG_*/counter/span wiring behind identical
// names, keeping disabled builds at near-zero overhead.
#pragma once
#include <cstdint>
#include <string_view>
#define MOLO_RUNTIME_COUNT(name) \
::molo::runtime::obs::Counter((name), 1)
#define MOLO_RUNTIME_COUNT_N(name, n) \
::molo::runtime::obs::Counter((name), (n))
#define MOLO_RUNTIME_GAUGE(name, value) \
::molo::runtime::obs::Gauge((name), (value))
#define MOLO_RUNTIME_SPAN(name) \
::molo::runtime::obs::NullSpan _molo_span_##__LINE__((name))
namespace molo::runtime::obs {
struct NullSpan {
explicit NullSpan(std::string_view) {}
~NullSpan() = default;
void end() {}
};
inline void Counter(std::string_view, uint64_t) {}
inline void Gauge(std::string_view, int64_t) {}
inline void OnAccept(std::string_view) {}
inline void OnClose(std::string_view, uint64_t, uint64_t) {}
inline void OnParseError(int) {}
inline void OnCompress(std::string_view, std::size_t, std::size_t) {}
inline void OnStaticHit(std::string_view) {}
inline void OnStaticMiss(std::string_view) {}
} // namespace molo::runtime::obs
+36
View File
@@ -0,0 +1,36 @@
// Spec: plans/01-runtime.md 6.1. One Poller interface hiding epoll, kqueue
// and IOCP. Default is level-triggered for safety; edge-triggered stays
// inside a Poller implementation with drain-to-EAGAIN.
#pragma once
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
namespace molo::runtime {
class IPoller {
public:
static constexpr uint32_t kRead = 1u << 0;
static constexpr uint32_t kWrite = 1u << 1;
static constexpr uint32_t kError = 1u << 2;
using Callback = std::function<void(int fd, uint32_t events)>;
virtual ~IPoller() = default;
virtual bool add_fd(int fd, uint32_t events, Callback cb) = 0;
virtual bool modify_fd(int fd, uint32_t events) = 0;
virtual void remove_fd(int fd) = 0;
// Blocks up to timeout_ms, dispatches callbacks, returns dispatched count.
virtual int wait(int timeout_ms) = 0;
// Thread-safe: wakes a thread blocked in wait().
virtual void wake() = 0;
virtual std::size_t watched() const = 0;
};
// Best Poller for the build platform. Never null; falls back to a stub that
// reports failure when no backend exists (should not happen on supported OS).
std::unique_ptr<IPoller> MakePoller();
} // namespace molo::runtime
+148
View File
@@ -0,0 +1,148 @@
// Spec: plans/01-runtime.md 6.1. Linux epoll backend, level-triggered.
#include "poller_epoll.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <unistd.h>
#include "caps.h"
namespace molo::runtime {
EpollPoller::EpollPoller() : batch_(caps::kPollerBatchMax) {
#if defined(__linux__)
epoll_fd_ = epoll_create1(EPOLL_CLOEXEC);
if (epoll_fd_ < 0) return;
wake_fd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (wake_fd_ < 0) {
close(epoll_fd_);
epoll_fd_ = -1;
return;
}
::epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
ev.data.fd = wake_fd_;
if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, wake_fd_, &ev) != 0) {
close(wake_fd_);
close(epoll_fd_);
wake_fd_ = -1;
epoll_fd_ = -1;
}
#endif
}
EpollPoller::~EpollPoller() {
if (wake_fd_ >= 0) close(wake_fd_);
if (epoll_fd_ >= 0) close(epoll_fd_);
}
uint32_t EpollPoller::ToEpoll(uint32_t events) {
uint32_t out = 0;
if (events & kRead) out |= EPOLLIN;
if (events & kWrite) out |= EPOLLOUT;
out |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
return out;
}
bool EpollPoller::add_fd(int fd, uint32_t events, Callback cb) {
#if defined(__linux__)
if (epoll_fd_ < 0 || fd < 0 || !cb) return false;
if (callbacks_.size() >= caps::kPollerFdsCeiling) return false;
if (callbacks_.find(fd) != callbacks_.end()) return false;
::epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = ToEpoll(events);
ev.data.fd = fd;
if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &ev) != 0) return false;
callbacks_.emplace(fd, std::move(cb));
return true;
#else
(void)fd;
(void)events;
(void)cb;
return false;
#endif
}
bool EpollPoller::modify_fd(int fd, uint32_t events) {
#if defined(__linux__)
if (epoll_fd_ < 0) return false;
auto it = callbacks_.find(fd);
if (it == callbacks_.end()) return false;
::epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = ToEpoll(events);
ev.data.fd = fd;
return epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, fd, &ev) == 0;
#else
(void)fd;
(void)events;
return false;
#endif
}
void EpollPoller::remove_fd(int fd) {
callbacks_.erase(fd);
#if defined(__linux__)
if (epoll_fd_ >= 0 && fd >= 0) {
epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, nullptr);
}
#endif
}
void EpollPoller::DrainWake() {
#if defined(__linux__)
uint64_t v = 0;
while (read(wake_fd_, &v, sizeof(v)) == (ssize_t)sizeof(v)) {
if (v == 0) break;
}
#endif
}
int EpollPoller::wait(int timeout_ms) {
#if defined(__linux__)
if (epoll_fd_ < 0) return -1;
if (timeout_ms < -1) timeout_ms = -1;
int n = epoll_wait(epoll_fd_, batch_.data(), (int)batch_.size(), timeout_ms);
if (n < 0) {
if (errno == EINTR) return 0;
return -1;
}
int dispatched = 0;
for (int i = 0; i < n; ++i) {
int fd = batch_[i].data.fd;
if (fd == wake_fd_) {
DrainWake();
continue;
}
auto it = callbacks_.find(fd);
if (it == callbacks_.end()) continue;
uint32_t ev = 0;
if (batch_[i].events & EPOLLIN) ev |= kRead;
if (batch_[i].events & EPOLLOUT) ev |= kWrite;
if (batch_[i].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) ev |= kError;
Callback cb = it->second;
cb(fd, ev);
++dispatched;
}
return dispatched;
#else
(void)timeout_ms;
return -1;
#endif
}
void EpollPoller::wake() {
#if defined(__linux__)
if (wake_fd_ < 0) return;
uint64_t v = 1;
ssize_t r = write(wake_fd_, &v, sizeof(v));
(void)r;
#endif
}
} // namespace molo::runtime
+52
View File
@@ -0,0 +1,52 @@
// Spec: plans/01-runtime.md 6.1. Linux epoll backend, level-triggered.
#pragma once
#include <cstddef>
#include <cstdint>
#include <unordered_map>
#include <vector>
#if defined(__linux__)
#include <sys/epoll.h>
#else
struct epoll_event {
uint32_t events;
union {
void* ptr;
int fd;
uint32_t u32;
uint64_t u64;
} data;
};
#endif
#include "poller.h"
namespace molo::runtime {
class EpollPoller : public IPoller {
public:
EpollPoller();
~EpollPoller() override;
EpollPoller(const EpollPoller&) = delete;
EpollPoller& operator=(const EpollPoller&) = delete;
bool valid() const { return epoll_fd_ >= 0; }
bool add_fd(int fd, uint32_t events, Callback cb) override;
bool modify_fd(int fd, uint32_t events) override;
void remove_fd(int fd) override;
int wait(int timeout_ms) override;
void wake() override;
std::size_t watched() const override { return callbacks_.size(); }
private:
static uint32_t ToEpoll(uint32_t events);
void DrainWake();
int epoll_fd_ = -1;
int wake_fd_ = -1;
std::unordered_map<int, Callback> callbacks_;
std::vector<::epoll_event> batch_;
};
} // namespace molo::runtime
+130
View File
@@ -0,0 +1,130 @@
// Spec: plans/01-runtime.md 6.1. Windows backend. v1 uses select() based
// readiness (level-triggered, same contract as epoll/kqueue here) inside the
// IOCP file slot; a native overlapped IOCP completion path is the documented
// upgrade once Windows CI exists. Non-Windows builds get an inert stub.
#include "poller_iocp.h"
#include "caps.h"
#if defined(_WIN32)
#include <winsock2.h>
#endif
namespace molo::runtime {
IocpPoller::IocpPoller() {
#if defined(_WIN32)
port_ = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 1);
#endif
}
IocpPoller::~IocpPoller() {
#if defined(_WIN32)
if (port_) CloseHandle((HANDLE)port_);
#endif
}
bool IocpPoller::valid() const {
#if defined(_WIN32)
return port_ != nullptr;
#else
return false;
#endif
}
bool IocpPoller::add_fd(int fd, uint32_t events, Callback cb) {
#if defined(_WIN32)
if (!port_ || fd < 0 || !cb) return false;
if (callbacks_.size() >= caps::kPollerFdsCeiling) return false;
if (callbacks_.find(fd) != callbacks_.end()) return false;
callbacks_.emplace(fd, std::move(cb));
masks_.emplace(fd, events);
return true;
#else
(void)fd;
(void)events;
(void)cb;
return false;
#endif
}
bool IocpPoller::modify_fd(int fd, uint32_t events) {
#if defined(_WIN32)
auto it = callbacks_.find(fd);
if (it == callbacks_.end()) return false;
masks_[fd] = events;
return true;
#else
(void)fd;
(void)events;
return false;
#endif
}
void IocpPoller::remove_fd(int fd) {
callbacks_.erase(fd);
masks_.erase(fd);
}
int IocpPoller::wait(int timeout_ms) {
#if defined(_WIN32)
if (!port_) return -1;
fd_set rfds, wfds, efds;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&efds);
for (const auto& kv : masks_) {
SOCKET s = (SOCKET)kv.first;
if (kv.second & kRead) FD_SET(s, &rfds);
if (kv.second & kWrite) FD_SET(s, &wfds);
FD_SET(s, &efds);
}
struct timeval tv;
tv.tv_sec = timeout_ms < 0 ? 0 : timeout_ms / 1000;
tv.tv_usec = timeout_ms < 0 ? 0 : (timeout_ms % 1000) * 1000;
int n = select(0, &rfds, &wfds, &efds, timeout_ms < 0 ? nullptr : &tv);
if (n == SOCKET_ERROR) {
if (WSAGetLastError() == WSAEINTR) return 0;
PostQueuedCompletionStatus((HANDLE)port_, 0, 0, nullptr);
DWORD bytes = 0;
ULONG_PTR key = 0;
OVERLAPPED* ov = nullptr;
if (GetQueuedCompletionStatus((HANDLE)port_, &bytes, &key, &ov, 0)) {
return 0;
}
return -1;
}
DWORD bytes = 0;
ULONG_PTR key = 0;
OVERLAPPED* ov = nullptr;
while (GetQueuedCompletionStatus((HANDLE)port_, &bytes, &key, &ov, 0)) {
}
int dispatched = 0;
std::size_t budget = caps::kPollerBatchMax;
for (const auto& kv : callbacks_) {
if (budget == 0) break;
SOCKET s = (SOCKET)kv.first;
uint32_t ev = 0;
if (FD_ISSET(s, &rfds)) ev |= kRead;
if (FD_ISSET(s, &wfds)) ev |= kWrite;
if (FD_ISSET(s, &efds)) ev |= kError;
if (ev == 0) continue;
Callback cb = kv.second;
cb(kv.first, ev);
++dispatched;
--budget;
}
return dispatched;
#else
(void)timeout_ms;
return -1;
#endif
}
void IocpPoller::wake() {
#if defined(_WIN32)
if (port_) PostQueuedCompletionStatus((HANDLE)port_, 0, 0, nullptr);
#endif
}
} // namespace molo::runtime
+39
View File
@@ -0,0 +1,39 @@
// Spec: plans/01-runtime.md 6.1. IOCP backend for Windows. On other
// platforms this compiles to a stub that reports unsupported; MakePoller
// never selects it there.
#pragma once
#include <cstddef>
#include <cstdint>
#include <unordered_map>
#include "poller.h"
namespace molo::runtime {
class IocpPoller : public IPoller {
public:
IocpPoller();
~IocpPoller() override;
IocpPoller(const IocpPoller&) = delete;
IocpPoller& operator=(const IocpPoller&) = delete;
bool valid() const;
bool add_fd(int fd, uint32_t events, Callback cb) override;
bool modify_fd(int fd, uint32_t events) override;
void remove_fd(int fd) override;
int wait(int timeout_ms) override;
void wake() override;
std::size_t watched() const override { return callbacks_.size(); }
private:
#if defined(_WIN32)
void* port_ = nullptr;
#else
int port_ = -1;
#endif
std::unordered_map<int, Callback> callbacks_;
std::unordered_map<int, uint32_t> masks_;
};
} // namespace molo::runtime
+169
View File
@@ -0,0 +1,169 @@
// Spec: plans/01-runtime.md 6.1. kqueue backend for BSD/macOS.
#include "poller_kqueue.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__OpenBSD__) || defined(__DragonFly__)
#include <sys/event.h>
#define MOLO_HAS_KQUEUE 1
#endif
#include "caps.h"
namespace molo::runtime {
KqueuePoller::KqueuePoller() {
#if defined(MOLO_HAS_KQUEUE)
kq_ = kqueue();
if (kq_ < 0) return;
if (pipe(wake_pipe_) != 0) {
close(kq_);
kq_ = -1;
return;
}
fcntl(wake_pipe_[0], F_SETFD, FD_CLOEXEC);
fcntl(wake_pipe_[1], F_SETFD, FD_CLOEXEC);
int flags = fcntl(wake_pipe_[0], F_GETFL, 0);
fcntl(wake_pipe_[0], F_SETFL, flags | O_NONBLOCK);
flags = fcntl(wake_pipe_[1], F_GETFL, 0);
fcntl(wake_pipe_[1], F_SETFL, flags | O_NONBLOCK);
struct kevent ev;
EV_SET(&ev, (uintptr_t)wake_pipe_[0], EVFILT_READ, EV_ADD, 0, 0, nullptr);
if (kevent(kq_, &ev, 1, nullptr, 0, nullptr) != 0) {
close(wake_pipe_[0]);
close(wake_pipe_[1]);
close(kq_);
kq_ = -1;
wake_pipe_[0] = wake_pipe_[1] = -1;
}
#endif
}
KqueuePoller::~KqueuePoller() {
if (wake_pipe_[0] >= 0) close(wake_pipe_[0]);
if (wake_pipe_[1] >= 0) close(wake_pipe_[1]);
if (kq_ >= 0) close(kq_);
}
#if defined(MOLO_HAS_KQUEUE)
static void ApplyMask(int kq, int fd, uint32_t old_mask, uint32_t new_mask) {
struct kevent evs[2];
int n = 0;
bool old_r = (old_mask & IPoller::kRead) != 0;
bool new_r = (new_mask & IPoller::kRead) != 0;
bool old_w = (old_mask & IPoller::kWrite) != 0;
bool new_w = (new_mask & IPoller::kWrite) != 0;
if (new_r && !old_r) {
EV_SET(&evs[n++], (uintptr_t)fd, EVFILT_READ, EV_ADD, 0, 0, nullptr);
} else if (!new_r && old_r) {
EV_SET(&evs[n++], (uintptr_t)fd, EVFILT_READ, EV_DELETE, 0, 0, nullptr);
}
if (new_w && !old_w) {
EV_SET(&evs[n++], (uintptr_t)fd, EVFILT_WRITE, EV_ADD, 0, 0, nullptr);
} else if (!new_w && old_w) {
EV_SET(&evs[n++], (uintptr_t)fd, EVFILT_WRITE, EV_DELETE, 0, 0,
nullptr);
}
if (n > 0) kevent(kq, evs, n, nullptr, 0, nullptr);
}
#endif
bool KqueuePoller::add_fd(int fd, uint32_t events, Callback cb) {
#if defined(MOLO_HAS_KQUEUE)
if (kq_ < 0 || fd < 0 || !cb) return false;
if (callbacks_.size() >= caps::kPollerFdsCeiling) return false;
if (callbacks_.find(fd) != callbacks_.end()) return false;
ApplyMask(kq_, fd, 0, events);
callbacks_.emplace(fd, std::move(cb));
masks_.emplace(fd, events);
return true;
#else
(void)fd;
(void)events;
(void)cb;
return false;
#endif
}
bool KqueuePoller::modify_fd(int fd, uint32_t events) {
#if defined(MOLO_HAS_KQUEUE)
if (kq_ < 0) return false;
auto it = callbacks_.find(fd);
if (it == callbacks_.end()) return false;
ApplyMask(kq_, fd, masks_[fd], events);
masks_[fd] = events;
return true;
#else
(void)fd;
(void)events;
return false;
#endif
}
void KqueuePoller::remove_fd(int fd) {
#if defined(MOLO_HAS_KQUEUE)
auto it = masks_.find(fd);
if (it != masks_.end()) {
if (kq_ >= 0) ApplyMask(kq_, fd, it->second, 0);
masks_.erase(it);
}
#endif
callbacks_.erase(fd);
}
int KqueuePoller::wait(int timeout_ms) {
#if defined(MOLO_HAS_KQUEUE)
if (kq_ < 0) return -1;
struct kevent evs[caps::kPollerBatchMax];
struct timespec ts;
struct timespec* tsp = nullptr;
if (timeout_ms >= 0) {
ts.tv_sec = timeout_ms / 1000;
ts.tv_nsec = (long)(timeout_ms % 1000) * 1000000L;
tsp = &ts;
}
int n = kevent(kq_, nullptr, 0, evs, caps::kPollerBatchMax, tsp);
if (n < 0) {
if (errno == EINTR) return 0;
return -1;
}
int dispatched = 0;
for (int i = 0; i < n; ++i) {
int fd = (int)evs[i].ident;
if (fd == wake_pipe_[0]) {
char buf[64];
while (read(wake_pipe_[0], buf, sizeof(buf)) > 0) {
}
continue;
}
auto it = callbacks_.find(fd);
if (it == callbacks_.end()) continue;
uint32_t ev = 0;
if (evs[i].filter == EVFILT_READ) ev |= kRead;
if (evs[i].filter == EVFILT_WRITE) ev |= kWrite;
if (evs[i].flags & (EV_ERROR | EV_EOF)) ev |= kError;
Callback cb = it->second;
cb(fd, ev);
++dispatched;
}
return dispatched;
#else
(void)timeout_ms;
return -1;
#endif
}
void KqueuePoller::wake() {
#if defined(MOLO_HAS_KQUEUE)
if (wake_pipe_[1] < 0) return;
char c = 1;
ssize_t r = write(wake_pipe_[1], &c, 1);
(void)r;
#endif
}
} // namespace molo::runtime
+36
View File
@@ -0,0 +1,36 @@
// Spec: plans/01-runtime.md 6.1. kqueue backend for BSD/macOS. On other
// platforms this compiles to a stub that reports unsupported; MakePoller
// never selects it there.
#pragma once
#include <cstddef>
#include <cstdint>
#include <unordered_map>
#include "poller.h"
namespace molo::runtime {
class KqueuePoller : public IPoller {
public:
KqueuePoller();
~KqueuePoller() override;
KqueuePoller(const KqueuePoller&) = delete;
KqueuePoller& operator=(const KqueuePoller&) = delete;
bool valid() const { return kq_ >= 0; }
bool add_fd(int fd, uint32_t events, Callback cb) override;
bool modify_fd(int fd, uint32_t events) override;
void remove_fd(int fd) override;
int wait(int timeout_ms) override;
void wake() override;
std::size_t watched() const override { return callbacks_.size(); }
private:
int kq_ = -1;
int wake_pipe_[2] = {-1, -1};
std::unordered_map<int, Callback> callbacks_;
std::unordered_map<int, uint32_t> masks_;
};
} // namespace molo::runtime
+94
View File
@@ -0,0 +1,94 @@
# Runtime part (plans/01-runtime.md): reactor, HTTP/1.1 + HTTP/2, compression,
# cookies, static files. Outputs only under build/.
RUNTIME_SRCS := \
src/runtime/poller_epoll.cpp \
src/runtime/poller_kqueue.cpp \
src/runtime/poller_iocp.cpp \
src/runtime/timer_wheel.cpp \
src/runtime/executor.cpp \
src/runtime/event_loop.cpp \
src/runtime/signal_handler.cpp \
src/runtime/transport_tcp.cpp \
src/runtime/http/message.cpp \
src/runtime/http/protocol_detect.cpp \
src/runtime/http/connection.cpp \
src/runtime/http/listener.cpp \
src/runtime/http/v1/parser.cpp \
src/runtime/http/v1/serializer.cpp \
src/runtime/http/v2/hpack.cpp \
src/runtime/http/v2/flow_control.cpp \
src/runtime/http/v2/stream.cpp \
src/runtime/http/v2/connection.cpp \
src/runtime/compress/gzip.cpp \
src/runtime/compress/brotli.cpp \
src/runtime/compress/negotiate.cpp \
src/runtime/cookie/cookie.cpp \
src/runtime/cookie/jar.cpp \
src/runtime/static/path_guard.cpp \
src/runtime/static/asset_provider.cpp \
src/runtime/static/disk_provider.cpp \
src/runtime/static/embedded_provider.cpp \
src/runtime/static/etag.cpp \
src/runtime/static/file_server.cpp
RUNTIME_TEST_SRCS := \
tests/src/runtime/main.cpp \
tests/src/runtime/test_task.cpp \
tests/src/runtime/test_timer.cpp \
tests/src/runtime/test_executor.cpp \
tests/src/runtime/test_event_loop.cpp \
tests/src/runtime/test_channel_strand.cpp \
tests/src/runtime/test_base64.cpp \
tests/src/runtime/http/test_message.cpp \
tests/src/runtime/http/test_protocol_detect.cpp \
tests/src/runtime/http/v1/test_parser.cpp \
tests/src/runtime/http/v1/test_serializer.cpp \
tests/src/runtime/http/v2/test_hpack.cpp \
tests/src/runtime/http/v2/test_settings.cpp \
tests/src/runtime/http/v2/test_flow.cpp \
tests/src/runtime/http/v2/test_stream.cpp \
tests/src/runtime/http/v2/test_h2connection.cpp \
tests/src/runtime/http/test_integration.cpp \
tests/src/runtime/compress/test_gzip.cpp \
tests/src/runtime/compress/test_brotli.cpp \
tests/src/runtime/compress/test_negotiate.cpp \
tests/src/runtime/cookie/test_cookie.cpp \
tests/src/runtime/static/test_path_guard.cpp \
tests/src/runtime/static/test_file_server.cpp
RUNTIME_OBJS := $(RUNTIME_SRCS:src/runtime/%.cpp=build/obj/runtime/%.o)
RUNTIME_TEST_OBJS := $(RUNTIME_TEST_SRCS:tests/%.cpp=build/obj/runtime-tests/%.o)
RUNTIME_LIB := build/lib/libruntime.a
RUNTIME_TESTS := build/bin/runtime_tests
RUNTIME_INTEROP_SERVER := build/bin/runtime_interop_server
MOLO_ALL += $(RUNTIME_LIB)
MOLO_TEST_BINS += $(RUNTIME_TESTS)
MOLO_INTEROP += runtime-interop
$(RUNTIME_LIB): $(RUNTIME_OBJS)
mkdir -p $(dir $@)
$(AR) rcs $@ $^
$(RUNTIME_TESTS): $(RUNTIME_TEST_OBJS) $(RUNTIME_LIB)
mkdir -p $(dir $@)
$(CXX) $(MOLO_CXXFLAGS) -o $@ $(RUNTIME_TEST_OBJS) $(RUNTIME_LIB)
$(RUNTIME_INTEROP_SERVER): tests/src/runtime/interop/test_server.cpp $(RUNTIME_LIB)
mkdir -p $(dir $@)
$(CXX) $(MOLO_CXXFLAGS) -Itests/src/runtime -o $@ tests/src/runtime/interop/test_server.cpp $(RUNTIME_LIB)
build/obj/runtime/%.o: src/runtime/%.cpp
mkdir -p $(dir $@)
$(CXX) $(MOLO_CXXFLAGS) -MMD -MP -c $< -o $@
build/obj/runtime-tests/%.o: tests/%.cpp
mkdir -p $(dir $@)
$(CXX) $(MOLO_CXXFLAGS) -MMD -MP -Itests/src/runtime -c $< -o $@
-include $(RUNTIME_OBJS:.o=.d) $(RUNTIME_TEST_OBJS:.o=.d)
runtime-interop: $(RUNTIME_INTEROP_SERVER)
python3 tests/src/runtime/interop/interop.py $(RUNTIME_INTEROP_SERVER)
.PHONY: runtime-interop
+111
View File
@@ -0,0 +1,111 @@
// Spec: plans/01-runtime.md 6.2. Signal delivery on the loop thread.
#include "signal_handler.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "event_loop.h"
#if defined(__linux__)
#include <sys/signalfd.h>
#endif
namespace molo::runtime {
namespace {
#if !defined(__linux__)
int g_pipe_write = -1;
void PipeHandler(int signo) {
if (g_pipe_write >= 0) {
char c = (char)signo;
ssize_t r = write(g_pipe_write, &c, 1);
(void)r;
}
}
#endif
} // namespace
SignalHandler::SignalHandler(EventLoop& loop) : loop_(loop) {}
SignalHandler::~SignalHandler() { uninstall(); }
bool SignalHandler::install(Callback cb) {
if (installed_ || !cb) return false;
cb_ = std::move(cb);
#if defined(__linux__)
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGHUP);
if (pthread_sigmask(SIG_BLOCK, &mask, &old_mask_) != 0) return false;
mask_saved_ = true;
fd_ = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
if (fd_ < 0) return false;
#else
if (pipe(pipe_fds_) != 0) return false;
fcntl(pipe_fds_[0], F_SETFD, FD_CLOEXEC);
fcntl(pipe_fds_[1], F_SETFD, FD_CLOEXEC);
fd_ = pipe_fds_[0];
g_pipe_write = pipe_fds_[1];
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = PipeHandler;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, nullptr);
sigaction(SIGTERM, &sa, nullptr);
sigaction(SIGHUP, &sa, nullptr);
#endif
bool ok = loop_.poller().add_fd(fd_, IPoller::kRead,
[this](int, uint32_t) { OnReadable(); });
if (!ok) {
uninstall();
return false;
}
installed_ = true;
return true;
}
void SignalHandler::uninstall() {
if (installed_) {
loop_.poller().remove_fd(fd_);
installed_ = false;
}
#if defined(__linux__)
if (fd_ >= 0) {
close(fd_);
fd_ = -1;
}
if (mask_saved_) {
pthread_sigmask(SIG_SETMASK, &old_mask_, nullptr);
mask_saved_ = false;
}
#else
if (pipe_fds_[0] >= 0) close(pipe_fds_[0]);
if (pipe_fds_[1] >= 0) close(pipe_fds_[1]);
pipe_fds_[0] = pipe_fds_[1] = -1;
g_pipe_write = -1;
fd_ = -1;
#endif
cb_ = nullptr;
}
void SignalHandler::OnReadable() {
#if defined(__linux__)
struct signalfd_siginfo info;
while (read(fd_, &info, sizeof(info)) == (ssize_t)sizeof(info)) {
if (cb_) cb_((int)info.ssi_signo);
}
#else
char buf[32];
ssize_t n = read(fd_, buf, sizeof(buf));
for (ssize_t i = 0; i < n; ++i) {
if (cb_) cb_((int)buf[i]);
}
#endif
}
} // namespace molo::runtime
+45
View File
@@ -0,0 +1,45 @@
// Spec: plans/01-runtime.md 6.2. SIGINT/SIGTERM start drain, SIGHUP reloads
// config where safe, SIGTERM during drain shortens the deadline to 5 s. The
// policy lives in the caller; this class only delivers the signal number on
// the loop thread.
#pragma once
#include <functional>
#if defined(__linux__)
#include <signal.h>
#endif
namespace molo::runtime {
class EventLoop;
class SignalHandler {
public:
using Callback = std::function<void(int signo)>;
explicit SignalHandler(EventLoop& loop);
~SignalHandler();
SignalHandler(const SignalHandler&) = delete;
SignalHandler& operator=(const SignalHandler&) = delete;
bool install(Callback cb);
void uninstall();
private:
void OnReadable();
EventLoop& loop_;
Callback cb_;
int fd_ = -1;
bool installed_ = false;
#if defined(__linux__)
sigset_t old_mask_;
bool mask_saved_ = false;
#endif
#if !defined(__linux__)
int pipe_fds_[2] = {-1, -1};
#endif
};
} // namespace molo::runtime
+75
View File
@@ -0,0 +1,75 @@
// Spec: plans/01-runtime.md 6.7. Asset RAII.
#include "asset_provider.h"
#include <unistd.h>
#include <utility>
namespace molo::runtime::static_ {
Asset::~Asset() {
if (fd_ >= 0) close(fd_);
}
Asset::Asset(Asset&& other) noexcept
: valid_(other.valid_),
meta_(other.meta_),
fd_(other.fd_),
data_(other.data_),
size_(other.size_) {
other.valid_ = false;
other.fd_ = -1;
other.data_ = nullptr;
other.size_ = 0;
}
Asset& Asset::operator=(Asset&& other) noexcept {
if (this != &other) {
if (fd_ >= 0) close(fd_);
valid_ = other.valid_;
meta_ = other.meta_;
fd_ = other.fd_;
data_ = other.data_;
size_ = other.size_;
other.valid_ = false;
other.fd_ = -1;
other.data_ = nullptr;
other.size_ = 0;
}
return *this;
}
int Asset::release() {
int fd = fd_;
fd_ = -1;
valid_ = false;
data_ = nullptr;
size_ = 0;
return fd;
}
Asset Asset::ForFile(int fd, AssetMeta meta) {
Asset a;
a.valid_ = true;
a.meta_ = meta;
a.fd_ = fd;
return a;
}
Asset Asset::ForMemory(const char* data, std::size_t size, AssetMeta meta) {
Asset a;
a.valid_ = true;
a.meta_ = meta;
a.data_ = data;
a.size_ = size;
return a;
}
Asset Asset::ForDir(AssetMeta meta) {
Asset a;
a.valid_ = true;
a.meta_ = meta;
return a;
}
} // namespace molo::runtime::static_
+59
View File
@@ -0,0 +1,59 @@
// Spec: plans/01-runtime.md 6.7. Asset provider interface: DiskProvider
// (root jailed) and EmbeddedProvider (from the 09-cli-ops.md embedder) share
// one FileServer logic.
#pragma once
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
namespace molo::runtime::static_ {
struct AssetMeta {
uint64_t length = 0;
int64_t mtime = 0; // unix seconds
bool is_file = false;
bool is_dir = false;
};
// Move-only open asset. Disk assets carry an fd for sendfile; embedded
// assets carry a memory view that stays valid while the provider lives.
class Asset {
public:
Asset() = default;
~Asset();
Asset(Asset&& other) noexcept;
Asset& operator=(Asset&& other) noexcept;
Asset(const Asset&) = delete;
Asset& operator=(const Asset&) = delete;
bool valid() const { return valid_; }
AssetMeta meta() const { return meta_; }
int fd() const { return fd_; } // -1 for memory assets
const char* data() const { return data_; }
std::size_t size() const { return size_; }
// Hands the fd to the caller (for FileBody); this asset becomes invalid.
int release();
static Asset ForFile(int fd, AssetMeta meta);
static Asset ForMemory(const char* data, std::size_t size, AssetMeta meta);
static Asset ForDir(AssetMeta meta);
private:
bool valid_ = false;
AssetMeta meta_;
int fd_ = -1;
const char* data_ = nullptr;
std::size_t size_ = 0;
};
class IAssetProvider {
public:
virtual ~IAssetProvider() = default;
// rel is a guard-normalized relative path (see path_guard.h).
virtual std::optional<Asset> open(std::string_view rel) = 0;
virtual const char* name() const = 0;
};
} // namespace molo::runtime::static_
+84
View File
@@ -0,0 +1,84 @@
// Spec: plans/01-runtime.md 6.7. Jailed disk provider.
#include "disk_provider.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
namespace molo::runtime::static_ {
DiskProvider::DiskProvider(std::string root, bool follow_symlinks,
bool allow_dotfiles)
: root_(std::move(root)),
follow_symlinks_(follow_symlinks),
allow_dotfiles_(allow_dotfiles) {
root_fd_ = ::open(root_.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
}
DiskProvider::~DiskProvider() {
if (root_fd_ >= 0) close(root_fd_);
}
std::optional<Asset> DiskProvider::open(std::string_view rel) {
if (root_fd_ < 0) return std::nullopt;
std::string path(rel);
if (path.empty()) return std::nullopt;
if (path.size() > 4096) return std::nullopt;
if (path[0] == '/') return std::nullopt;
if (path.find('\0') != std::string::npos) return std::nullopt;
if (!allow_dotfiles_) {
// Any dotfile segment denies the request.
std::size_t i = 0;
while (i <= path.size()) {
std::size_t slash = path.find('/', i);
if (slash == std::string::npos) slash = path.size();
if (slash > i && path[i] == '.') return std::nullopt;
i = slash + 1;
}
}
int fd = openat(root_fd_, path.c_str(),
O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
if (fd < 0 && follow_symlinks_) {
// Resolve through realpath and verify containment (v1: small TOCTOU
// window, documented; strict deployments keep follow_symlinks off).
std::string full = root_ + "/" + path;
char* resolved = realpath(full.c_str(), nullptr);
if (resolved == nullptr) return std::nullopt;
std::string r(resolved);
free(resolved);
char* root_resolved = realpath(root_.c_str(), nullptr);
if (root_resolved == nullptr) return std::nullopt;
std::string rr(root_resolved);
free(root_resolved);
if (r != rr && (r.size() <= rr.size() ||
r.compare(0, rr.size(), rr) != 0 ||
r[rr.size()] != '/')) {
return std::nullopt;
}
fd = ::open(r.c_str(), O_RDONLY | O_CLOEXEC);
if (fd < 0) return std::nullopt;
}
if (fd < 0) return std::nullopt;
struct stat st;
if (fstat(fd, &st) != 0) {
close(fd);
return std::nullopt;
}
AssetMeta meta;
meta.mtime = (int64_t)st.st_mtime;
if (S_ISDIR(st.st_mode)) {
close(fd);
meta.is_dir = true;
return Asset::ForDir(meta);
}
if (!S_ISREG(st.st_mode)) {
close(fd);
return std::nullopt;
}
meta.length = (uint64_t)st.st_size;
meta.is_file = true;
return Asset::ForFile(fd, meta);
}
} // namespace molo::runtime::static_
+31
View File
@@ -0,0 +1,31 @@
// Spec: plans/01-runtime.md 6.7. Disk provider: root jailed via openat,
// O_NOFOLLOW unless follow_symlinks is set (then realpath containment is
// verified), dotfiles denied by default.
#pragma once
#include <string>
#include "asset_provider.h"
namespace molo::runtime::static_ {
class DiskProvider : public IAssetProvider {
public:
explicit DiskProvider(std::string root, bool follow_symlinks = false,
bool allow_dotfiles = false);
~DiskProvider() override;
DiskProvider(const DiskProvider&) = delete;
DiskProvider& operator=(const DiskProvider&) = delete;
bool valid() const { return root_fd_ >= 0; }
std::optional<Asset> open(std::string_view rel) override;
const char* name() const override { return "disk"; }
private:
std::string root_;
bool follow_symlinks_;
bool allow_dotfiles_;
int root_fd_ = -1;
};
} // namespace molo::runtime::static_
+36
View File
@@ -0,0 +1,36 @@
// Spec: plans/01-runtime.md 6.7. Embedded provider.
#include "embedded_provider.h"
namespace molo::runtime::static_ {
void EmbeddedProvider::Register(std::string rel, std::vector<char> bytes,
int64_t mtime) {
EmbeddedAsset a;
a.bytes = std::move(bytes);
a.mtime = mtime;
std::lock_guard<std::mutex> lock(mutex_);
assets_[std::move(rel)] = std::move(a);
}
bool EmbeddedProvider::Remove(std::string_view rel) {
std::lock_guard<std::mutex> lock(mutex_);
return assets_.erase(std::string(rel)) > 0;
}
std::optional<Asset> EmbeddedProvider::open(std::string_view rel) {
// NOTE: the returned memory view borrows the registry entry. Callers must
// not Register/Remove concurrently with serving. Registration happens at
// startup (embedder output); serving is read-only after that.
std::lock_guard<std::mutex> lock(mutex_);
auto it = assets_.find(std::string(rel));
if (it == assets_.end()) return std::nullopt;
const EmbeddedAsset& a = it->second;
AssetMeta meta;
meta.length = a.bytes.size();
meta.mtime = a.mtime;
meta.is_file = true;
const char* data = a.bytes.empty() ? "" : a.bytes.data();
return Asset::ForMemory(data, a.bytes.size(), meta);
}
} // namespace molo::runtime::static_
+35
View File
@@ -0,0 +1,35 @@
// Spec: plans/01-runtime.md 6.7. Embedded provider: serves assets registered
// in memory. The 09-cli-ops.md asset embedder generates the registration
// calls; the registry API here is the stable target.
#pragma once
#include <cstdint>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
#include "asset_provider.h"
namespace molo::runtime::static_ {
struct EmbeddedAsset {
std::vector<char> bytes;
int64_t mtime = 0;
};
class EmbeddedProvider : public IAssetProvider {
public:
EmbeddedProvider() = default;
void Register(std::string rel, std::vector<char> bytes, int64_t mtime);
bool Remove(std::string_view rel);
std::optional<Asset> open(std::string_view rel) override;
const char* name() const override { return "embedded"; }
private:
mutable std::mutex mutex_;
std::unordered_map<std::string, EmbeddedAsset> assets_;
};
} // namespace molo::runtime::static_
+129
View File
@@ -0,0 +1,129 @@
// Spec: plans/01-runtime.md 6.7, RFC 9110. Validators.
#include "etag.h"
#include <cstdio>
#include <cstring>
#include <ctime>
namespace molo::runtime::static_ {
std::string Etag::StrongForDisk(uint64_t size, int64_t mtime) {
char buf[64];
snprintf(buf, sizeof(buf), "\"%llx-%llx\"", (unsigned long long)size,
(unsigned long long)mtime);
return std::string(buf);
}
std::string Etag::StrongForEmbedded(uint64_t len, uint64_t hash) {
char buf[64];
snprintf(buf, sizeof(buf), "\"%llx-%llx\"", (unsigned long long)len,
(unsigned long long)hash);
return std::string(buf);
}
uint64_t Etag::Fnva1a(const char* data, std::size_t len) {
uint64_t h = 1469598103934665603ull;
for (std::size_t i = 0; i < len; ++i) {
h ^= (unsigned char)data[i];
h *= 1099511628211ull;
}
return h;
}
bool Etag::Matches(std::string_view if_none_match, std::string_view etag) {
std::size_t pos = 0;
auto trim = [](std::string_view s) {
while (!s.empty() && (s.front() == ' ' || s.front() == '\t')) {
s.remove_prefix(1);
}
while (!s.empty() && (s.back() == ' ' || s.back() == '\t')) {
s.remove_suffix(1);
}
return s;
};
while (pos <= if_none_match.size()) {
std::size_t comma = if_none_match.find(',', pos);
if (comma == std::string_view::npos) comma = if_none_match.size();
std::string_view cand = trim(if_none_match.substr(pos, comma - pos));
pos = comma + 1;
if (cand == "*") return true;
// Weak comparison: strip W/ from both sides.
if (cand.compare(0, 2, "W/") == 0) cand.remove_prefix(2);
std::string_view mine = etag;
if (mine.compare(0, 2, "W/") == 0) mine.remove_prefix(2);
if (!cand.empty() && cand == mine) return true;
}
return false;
}
bool Etag::RangeMatches(std::string_view if_range, std::string_view etag) {
std::string_view v = if_range;
while (!v.empty() && (v.front() == ' ' || v.front() == '\t')) {
v.remove_prefix(1);
}
while (!v.empty() && (v.back() == ' ' || v.back() == '\t')) {
v.remove_suffix(1);
}
if (v.empty() || v.compare(0, 2, "W/") == 0) return false;
return v == etag;
}
std::string Etag::HttpDate(int64_t unix_seconds) {
std::time_t t = (std::time_t)unix_seconds;
struct tm tmv;
gmtime_r(&t, &tmv);
char buf[40];
strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", &tmv);
return std::string(buf);
}
int64_t Etag::ParseHttpDate(std::string_view s) {
// IMF-fixdate only: "Sun, 06 Nov 1994 08:49:37 GMT".
if (s.size() != 29) return -1;
if (s[3] != ',' || s[4] != ' ' || s[7] != ' ' || s[11] != ' ' ||
s[16] != ' ' || s[19] != ':' || s[22] != ':' || s[25] != ' ') {
return -1;
}
if (s.compare(26, 3, "GMT") != 0) return -1;
auto digits = [&](std::size_t at, int n) -> int {
int v = 0;
for (int i = 0; i < n; ++i) {
char c = s[at + (std::size_t)i];
if (c < '0' || c > '9') return -1;
v = v * 10 + (c - '0');
}
return v;
};
int day = digits(5, 2);
int year = digits(12, 4);
int hour = digits(17, 2);
int min = digits(20, 2);
int sec = digits(23, 2);
if (day < 1 || day > 31 || year < 1970 || hour > 23 || min > 59 ||
sec > 60) {
return -1;
}
static const char* kMonths[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int mon = -1;
for (int i = 0; i < 12; ++i) {
if (s.compare(8, 3, kMonths[i]) == 0) {
mon = i;
break;
}
}
if (mon < 0) return -1;
struct tm tmv;
memset(&tmv, 0, sizeof(tmv));
tmv.tm_mday = day;
tmv.tm_mon = mon;
tmv.tm_year = year - 1900;
tmv.tm_hour = hour;
tmv.tm_min = min;
tmv.tm_sec = sec;
std::time_t t = timegm(&tmv);
if (t == (std::time_t)-1) return -1;
return (int64_t)t;
}
} // namespace molo::runtime::static_
+26
View File
@@ -0,0 +1,26 @@
// Spec: plans/01-runtime.md 6.7, RFC 9110 sections 8.8, 13. Validators:
// strong ETags plus Last-Modified, If-None-Match precedence over
// If-Modified-Since, If-Range.
#pragma once
#include <cstdint>
#include <string>
#include <string_view>
namespace molo::runtime::static_ {
class Etag {
public:
static std::string StrongForDisk(uint64_t size, int64_t mtime);
static std::string StrongForEmbedded(uint64_t len, uint64_t hash);
static uint64_t Fnva1a(const char* data, std::size_t len);
// If-None-Match comparison: handles "*", lists, and W/ weak prefixes.
static bool Matches(std::string_view if_none_match, std::string_view etag);
// If-Range: strong comparison only (no weak match, no lists).
static bool RangeMatches(std::string_view if_range, std::string_view etag);
static std::string HttpDate(int64_t unix_seconds);
// Parses IMF-fixdate. Returns -1 when unparseable.
static int64_t ParseHttpDate(std::string_view s);
};
} // namespace molo::runtime::static_
+280
View File
@@ -0,0 +1,280 @@
// Spec: plans/01-runtime.md 6.7. File server.
#include "file_server.h"
#include "etag.h"
#include "path_guard.h"
#include "runtime/compress/negotiate.h"
#include "runtime/observability_hooks.h"
namespace molo::runtime::static_ {
FileServer::FileServer(IAssetProvider& provider, FileServerConfig config)
: provider_(provider), config_(std::move(config)) {}
FileOutcome FileServer::Error(int status) {
FileOutcome o;
o.status = status;
o.head.status = status;
o.head.headers.set("content-type", "text/plain; charset=utf-8");
return o;
}
std::string FileServer::ContentTypeFor(std::string_view rel) {
std::size_t dot = rel.rfind('.');
std::string_view ext =
dot == std::string_view::npos ? std::string_view{} : rel.substr(dot);
if (ext == ".html" || ext == ".htm") return "text/html; charset=utf-8";
if (ext == ".css") return "text/css; charset=utf-8";
if (ext == ".js" || ext == ".mjs") {
return "text/javascript; charset=utf-8";
}
if (ext == ".json" || ext == ".map") return "application/json";
if (ext == ".xml") return "application/xml";
if (ext == ".svg") return "image/svg+xml";
if (ext == ".txt" || ext == ".md" || ext == ".csv") {
return "text/plain; charset=utf-8";
}
if (ext == ".wasm") return "application/wasm";
if (ext == ".png") return "image/png";
if (ext == ".jpg" || ext == ".jpeg") return "image/jpeg";
if (ext == ".gif") return "image/gif";
if (ext == ".webp") return "image/webp";
if (ext == ".ico") return "image/x-icon";
if (ext == ".woff") return "font/woff";
if (ext == ".woff2") return "font/woff2";
if (ext == ".ttf") return "font/ttf";
if (ext == ".pdf") return "application/pdf";
if (ext == ".zip") return "application/zip";
return "application/octet-stream";
}
std::string FileServer::PickCacheControl(std::string_view rel) {
if (rel.size() >= 5 &&
(rel.compare(rel.size() - 5, 5, ".html") == 0 ||
rel.compare(rel.size() - 4, 4, ".htm") == 0)) {
return config_.cache_control_html;
}
return config_.cache_control_versioned;
}
FileOutcome FileServer::Handle(const http::RequestHead& req) {
if (req.method != "GET" && req.method != "HEAD" && req.method != "OPTIONS") {
FileOutcome o = Error(405);
o.head.headers.set("allow", "GET, HEAD, OPTIONS");
return o;
}
if (req.method == "OPTIONS") {
FileOutcome o;
o.status = 204;
o.head.status = 204;
o.head.headers.set("allow", "GET, HEAD, OPTIONS");
o.head_only = true;
return o;
}
std::optional<std::string> rel = NormalizeUrlPath(req.target);
if (!rel.has_value() || rel->empty()) {
obs::OnStaticMiss(req.target);
return Error(404);
}
std::optional<Asset> asset = provider_.open(*rel);
if (!asset.has_value() || !asset->valid()) {
obs::OnStaticMiss(req.target);
return Error(404);
}
if (asset->meta().is_dir) {
// Directory index is off by default.
if (!config_.directory_index) return Error(403);
return Error(403); // indexing on is still 403 in v1 (no renderer)
}
if (!asset->meta().is_file) {
obs::OnStaticMiss(req.target);
return Error(404);
}
obs::OnStaticHit(req.target);
AssetMeta meta = asset->meta();
// Precompressed variants: probe .br/.gz siblings by negotiation.
std::string content_encoding;
std::string etag_suffix;
{
std::string ae = req.headers.get("accept-encoding");
// Probe in preference order without reading bodies.
std::optional<Asset> br_asset;
std::optional<Asset> gz_asset;
if (!ae.empty()) {
br_asset = provider_.open(*rel + ".br");
if (br_asset.has_value() && !br_asset->meta().is_file) {
br_asset.reset();
}
gz_asset = provider_.open(*rel + ".gz");
if (gz_asset.has_value() && !gz_asset->meta().is_file) {
gz_asset.reset();
}
}
compress::Precompressed pre =
compress::Negotiator::SelectPrecompressed(
ae, gz_asset.has_value(), br_asset.has_value());
if (pre.found && pre.suffix == ".br") {
asset = std::move(br_asset);
meta = asset->meta();
content_encoding = "br";
etag_suffix = "-br";
} else if (pre.found && pre.suffix == ".gz") {
asset = std::move(gz_asset);
meta = asset->meta();
content_encoding = "gzip";
etag_suffix = "-gz";
}
}
std::string etag;
if (asset->fd() >= 0) {
etag = Etag::StrongForDisk(meta.length, meta.mtime);
} else {
uint64_t h = Etag::Fnva1a(asset->data(), asset->size());
etag = Etag::StrongForEmbedded(meta.length, h);
}
if (!etag_suffix.empty()) {
// Variant suffix goes inside the quotes.
etag = etag.substr(0, etag.size() - 1) + etag_suffix + "\"";
}
std::string last_mod = Etag::HttpDate(meta.mtime);
// Conditionals: If-None-Match takes precedence over If-Modified-Since.
std::string inm = req.headers.get("if-none-match");
if (!inm.empty()) {
if (Etag::Matches(inm, etag)) {
FileOutcome o;
o.status = 304;
o.head.status = 304;
o.head.headers.set("etag", etag);
o.head.headers.set("last-modified", last_mod);
o.head.headers.set("cache-control", PickCacheControl(*rel));
o.head_only = true;
return o;
}
} else {
std::string ims = req.headers.get("if-modified-since");
if (!ims.empty()) {
int64_t since = Etag::ParseHttpDate(ims);
if (since >= 0 && meta.mtime <= since) {
FileOutcome o;
o.status = 304;
o.head.status = 304;
o.head.headers.set("etag", etag);
o.head.headers.set("last-modified", last_mod);
o.head.headers.set("cache-control", PickCacheControl(*rel));
o.head_only = true;
return o;
}
}
}
FileOutcome o;
o.head.headers.set("etag", etag);
o.head.headers.set("last-modified", last_mod);
o.head.headers.set("cache-control", PickCacheControl(*rel));
o.head.headers.set("accept-ranges", "bytes");
o.head.headers.set("content-type", ContentTypeFor(*rel));
if (!content_encoding.empty()) {
o.head.headers.set("content-encoding", content_encoding);
o.head.headers.set("vary", compress::Negotiator::VaryValue());
}
// Single range support.
std::string range = req.headers.get("range");
uint64_t offset = 0;
uint64_t length = meta.length;
bool partial = false;
if (!range.empty()) {
std::string if_range = req.headers.get("if-range");
bool range_ok = true;
if (!if_range.empty()) {
// Strong ETag compare, else treat as date.
if (if_range.find('"') != std::string::npos ||
if_range.compare(0, 2, "W/") == 0) {
range_ok = Etag::RangeMatches(if_range, etag);
} else {
int64_t d = Etag::ParseHttpDate(if_range);
range_ok = (d >= 0 && meta.mtime <= d);
}
}
if (range_ok) {
// bytes=FIRST-LAST, bytes=FIRST-, bytes=-SUFFIX. Multiple ranges
// give 416 in v1 (documented gap).
if (range.find(',') != std::string::npos) {
FileOutcome e = Error(416);
e.head.headers.set("content-range",
"bytes */" + std::to_string(meta.length));
return e;
}
std::string v = range;
while (!v.empty() && (v.front() == ' ' || v.front() == '\t')) {
v.erase(v.begin());
}
bool bad = true;
if (v.compare(0, 6, "bytes=") == 0) {
std::string spec = v.substr(6);
std::size_t dash = spec.find('-');
if (dash != std::string::npos) {
std::string a = spec.substr(0, dash);
std::string b = spec.substr(dash + 1);
auto parse_num = [](const std::string& s, uint64_t& n) {
if (s.empty() || s.size() > 20) return false;
n = 0;
for (char c : s) {
if (c < '0' || c > '9') return false;
n = n * 10 + (uint64_t)(c - '0');
}
return true;
};
if (a.empty()) {
uint64_t suffix = 0;
if (parse_num(b, suffix) && suffix > 0) {
if (suffix > meta.length) suffix = meta.length;
offset = meta.length - suffix;
length = suffix;
bad = false;
}
} else {
uint64_t first = 0, last = 0;
bool nums_ok = parse_num(a, first);
if (nums_ok) {
if (b.empty()) {
last = meta.length == 0 ? 0 : meta.length - 1;
} else {
nums_ok = parse_num(b, last);
}
}
if (nums_ok && first < meta.length && meta.length > 0) {
if (last >= meta.length) last = meta.length - 1;
if (last >= first) {
offset = first;
length = last - first + 1;
bad = false;
}
}
}
}
}
if (bad) {
FileOutcome e = Error(416);
e.head.headers.set("content-range",
"bytes */" + std::to_string(meta.length));
return e;
}
partial = true;
}
}
o.status = partial ? 206 : 200;
o.head.status = o.status;
if (partial) {
o.head.headers.set("content-range",
"bytes " + std::to_string(offset) + "-" +
std::to_string(offset + length - 1) + "/" +
std::to_string(meta.length));
}
o.head.headers.set("content-length", std::to_string(length));
o.asset = std::move(asset);
o.offset = offset;
o.length = length;
o.head_only = (req.method == "HEAD");
return o;
}
} // namespace molo::runtime::static_
+45
View File
@@ -0,0 +1,45 @@
// Spec: plans/01-runtime.md 6.7. One FileServer over disk and embedded
// providers: traversal guard, conditional requests, single ranges,
// cache headers, precompressed variants. Only GET, HEAD, OPTIONS.
#pragma once
#include <cstdint>
#include <optional>
#include <string>
#include "asset_provider.h"
#include "runtime/http/message.h"
namespace molo::runtime::static_ {
struct FileServerConfig {
std::string cache_control_versioned = "public, max-age=3600";
std::string cache_control_html = "no-cache";
bool directory_index = false; // off: directories give 403
};
struct FileOutcome {
int status = 500;
http::ResponseHead head;
std::optional<Asset> asset; // body source for 200/206
uint64_t offset = 0;
uint64_t length = 0;
bool head_only = false;
};
class FileServer {
public:
FileServer(IAssetProvider& provider, FileServerConfig config = {});
FileOutcome Handle(const http::RequestHead& req);
static std::string ContentTypeFor(std::string_view rel);
private:
FileOutcome Error(int status);
std::string PickCacheControl(std::string_view rel);
IAssetProvider& provider_;
FileServerConfig config_;
};
} // namespace molo::runtime::static_
+69
View File
@@ -0,0 +1,69 @@
// Spec: plans/01-runtime.md 6.7. Traversal guard.
#include "path_guard.h"
#include <vector>
#include "runtime/caps.h"
namespace molo::runtime::static_ {
namespace {
int HexVal(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
} // namespace
std::optional<std::string> NormalizeUrlPath(std::string_view target) {
// Strip query and fragment.
std::size_t q = target.find_first_of("?#");
std::string_view path = target.substr(0, q);
if (path.empty() || path[0] != '/') return std::nullopt;
if (path.size() > caps::kStaticPathMax) return std::nullopt;
// Percent-decode (single pass; %25 stays literal, no double decode).
std::string decoded;
decoded.reserve(path.size());
for (std::size_t i = 0; i < path.size(); ++i) {
char c = path[i];
if (c == '%') {
if (i + 2 >= path.size()) return std::nullopt;
int hi = HexVal(path[i + 1]);
int lo = HexVal(path[i + 2]);
if (hi < 0 || lo < 0) return std::nullopt;
decoded.push_back((char)((hi << 4) | lo));
i += 2;
continue;
}
decoded.push_back(c);
}
for (char c : decoded) {
if (c == '\0') return std::nullopt;
if (c == '\\') return std::nullopt;
}
// Lexical normalize over segments.
std::vector<std::string> segs;
std::size_t i = 0;
while (i <= decoded.size()) {
std::size_t slash = decoded.find('/', i);
if (slash == std::string::npos) slash = decoded.size();
std::string_view seg(decoded.data() + i, slash - i);
i = slash + 1;
if (seg.empty() || seg == ".") continue;
if (seg == "..") {
if (segs.empty()) return std::nullopt; // escape above root
segs.pop_back();
continue;
}
segs.emplace_back(seg);
}
std::string out;
for (std::size_t k = 0; k < segs.size(); ++k) {
if (k > 0) out.push_back('/');
out.append(segs[k]);
}
return out;
}
} // namespace molo::runtime::static_
+16
View File
@@ -0,0 +1,16 @@
// Spec: plans/01-runtime.md 6.7. Traversal guard: percent-decode, lexical
// normalize, reject NUL, backslash, and `..` escapes above root. Symlink
// policy is enforced by DiskProvider at open time.
#pragma once
#include <optional>
#include <string>
#include <string_view>
namespace molo::runtime::static_ {
// Returns the normalized in-root relative path (no leading slash), or
// nullopt when the request escapes or is malformed.
std::optional<std::string> NormalizeUrlPath(std::string_view target);
} // namespace molo::runtime::static_
+56
View File
@@ -0,0 +1,56 @@
// Spec: plans/01-runtime.md section 3. Per-connection serial queue.
#pragma once
#include <deque>
#include <functional>
#include <mutex>
#include "event_loop.h"
namespace molo::runtime {
class Strand {
public:
explicit Strand(EventLoop& loop) : loop_(loop) {}
~Strand() = default;
Strand(const Strand&) = delete;
Strand& operator=(const Strand&) = delete;
void post(std::function<void()> fn) {
if (!fn) return;
bool kick = false;
{
std::lock_guard<std::mutex> lock(mutex_);
queue_.push_back(std::move(fn));
if (!active_) {
active_ = true;
kick = true;
}
}
if (kick) loop_.defer([this] { Drain(); });
}
private:
void Drain() {
for (;;) {
std::function<void()> fn;
{
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.empty()) {
active_ = false;
return;
}
fn = std::move(queue_.front());
queue_.pop_front();
}
fn();
}
}
EventLoop& loop_;
std::mutex mutex_;
std::deque<std::function<void()>> queue_;
bool active_ = false;
};
} // namespace molo::runtime
+216
View File
@@ -0,0 +1,216 @@
// Spec: plans/01-runtime.md 6.1. Lazy coroutine Task<T> with symmetric
// transfer. Handlers are `Task<Response> handle(Request)`. Never hold a lock
// across co_await. Blocking work goes to ThreadPool, never the loop thread.
#pragma once
#include <atomic>
#include <coroutine>
#include <exception>
#include <generator>
#include <memory>
#include <utility>
#include <variant>
namespace molo::runtime {
template <typename T>
using Generator = std::generator<T>;
struct Cancelled : std::exception {
const char* what() const noexcept override { return "cancelled"; }
};
// Checked by handlers at await points. Cancellation is cooperative.
class CancellationToken {
public:
CancellationToken() : state_(std::make_shared<std::atomic<bool>>(false)) {}
void cancel() const { state_->store(true, std::memory_order_relaxed); }
bool is_cancelled() const { return state_->load(std::memory_order_relaxed); }
void throw_if_cancelled() const {
if (is_cancelled()) throw Cancelled{};
}
private:
std::shared_ptr<std::atomic<bool>> state_;
};
template <typename T>
class Task {
public:
struct promise_type {
std::coroutine_handle<> continuation_ = std::noop_coroutine();
std::variant<std::monostate, T, std::exception_ptr> result_;
bool destroy_on_complete_ = false;
Task get_return_object() {
return Task{std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_always initial_suspend() noexcept { return {}; }
struct FinalAwaiter {
bool await_ready() noexcept { return false; }
std::coroutine_handle<> await_suspend(
std::coroutine_handle<promise_type> h) noexcept {
promise_type& p = h.promise();
if (p.destroy_on_complete_) {
h.destroy();
return std::noop_coroutine();
}
return p.continuation_;
}
void await_resume() noexcept {}
};
FinalAwaiter final_suspend() noexcept { return {}; }
void unhandled_exception() noexcept {
result_.template emplace<2>(std::current_exception());
}
template <typename U>
void return_value(U&& v) {
result_.template emplace<1>(std::forward<U>(v));
}
};
using Handle = std::coroutine_handle<promise_type>;
Task() = default;
explicit Task(Handle h) : handle_(h) {}
Task(Task&& other) noexcept : handle_(other.handle_) {
other.handle_ = nullptr;
}
Task& operator=(Task&& other) noexcept {
if (this != &other) {
destroy();
handle_ = other.handle_;
other.handle_ = nullptr;
}
return *this;
}
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
~Task() { destroy(); }
bool valid() const { return handle_ != nullptr; }
bool done() const { return !handle_ || handle_.done(); }
Handle release() {
Handle h = handle_;
handle_ = nullptr;
return h;
}
void start() {
if (handle_ && !handle_.done()) handle_.resume();
}
bool await_ready() const noexcept { return !handle_ || handle_.done(); }
std::coroutine_handle<> await_suspend(
std::coroutine_handle<> awaiting) noexcept {
handle_.promise().continuation_ = awaiting;
return handle_;
}
T await_resume() {
auto& r = handle_.promise().result_;
if (std::holds_alternative<std::exception_ptr>(r)) {
std::rethrow_exception(std::get<2>(r));
}
return std::move(std::get<1>(r));
}
private:
void destroy() {
if (handle_) {
handle_.destroy();
handle_ = nullptr;
}
}
Handle handle_ = nullptr;
};
template <>
class Task<void> {
public:
struct promise_type {
std::coroutine_handle<> continuation_ = std::noop_coroutine();
std::exception_ptr error_;
bool destroy_on_complete_ = false;
Task get_return_object() {
return Task{std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_always initial_suspend() noexcept { return {}; }
struct FinalAwaiter {
bool await_ready() noexcept { return false; }
std::coroutine_handle<> await_suspend(
std::coroutine_handle<promise_type> h) noexcept {
promise_type& p = h.promise();
if (p.destroy_on_complete_) {
h.destroy();
return std::noop_coroutine();
}
return p.continuation_;
}
void await_resume() noexcept {}
};
FinalAwaiter final_suspend() noexcept { return {}; }
void unhandled_exception() noexcept { error_ = std::current_exception(); }
void return_void() noexcept {}
};
using Handle = std::coroutine_handle<promise_type>;
Task() = default;
explicit Task(Handle h) : handle_(h) {}
Task(Task&& other) noexcept : handle_(other.handle_) {
other.handle_ = nullptr;
}
Task& operator=(Task&& other) noexcept {
if (this != &other) {
destroy();
handle_ = other.handle_;
other.handle_ = nullptr;
}
return *this;
}
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
~Task() { destroy(); }
bool valid() const { return handle_ != nullptr; }
bool done() const { return !handle_ || handle_.done(); }
Handle release() {
Handle h = handle_;
handle_ = nullptr;
return h;
}
void start() {
if (handle_ && !handle_.done()) handle_.resume();
}
bool await_ready() const noexcept { return !handle_ || handle_.done(); }
std::coroutine_handle<> await_suspend(
std::coroutine_handle<> awaiting) noexcept {
handle_.promise().continuation_ = awaiting;
return handle_;
}
void await_resume() {
if (handle_.promise().error_) {
std::rethrow_exception(handle_.promise().error_);
}
}
private:
void destroy() {
if (handle_) {
handle_.destroy();
handle_ = nullptr;
}
}
Handle handle_ = nullptr;
};
// Fire and forget. The frame destroys itself on completion.
inline void Detach(Task<void>&& task) {
if (!task.valid()) return;
auto h = task.release();
h.promise().destroy_on_complete_ = true;
h.resume();
}
} // namespace molo::runtime
+132
View File
@@ -0,0 +1,132 @@
// Spec: plans/01-runtime.md 6.1. Hashed timing wheel.
#include "timer_wheel.h"
#include "caps.h"
namespace molo::runtime {
TimerWheel::TimerWheel(int granularity_ms, std::size_t slots)
: granularity_ms_(granularity_ms > 0 ? granularity_ms : 1),
slots_(slots > 0 ? slots : 1),
wheel_(slots_) {
start_tick_ = NowTick();
last_tick_ = start_tick_;
}
int64_t TimerWheel::NowTick() const {
auto now = std::chrono::steady_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(now).count() /
granularity_ms_;
}
TimerWheel::TimerId TimerWheel::add(std::chrono::milliseconds delay,
Callback cb) {
if (!cb) return 0;
if (live_ >= caps::kTimersPerLoopCap) return 0;
int64_t now = NowTick();
int64_t wait_ticks = delay.count() / granularity_ms_;
int64_t at = now + wait_ticks;
auto e = std::make_shared<Entry>();
e->id = next_id_++;
if (next_id_ == 0) next_id_ = 1;
e->tick = at;
e->cb = std::move(cb);
int64_t delta = at - now;
if (delta < (int64_t)slots_) {
wheel_[(std::size_t)(at % (int64_t)slots_)].push_back(e);
} else {
overflow_.push_back(e);
}
++live_;
return e->id;
}
bool TimerWheel::cancel(TimerId id) {
if (id == 0) return false;
for (auto& slot : wheel_) {
for (auto& e : slot) {
if (e->id == id && !e->cancelled) {
e->cancelled = true;
e->cb = nullptr;
--live_;
return true;
}
}
}
for (auto& e : overflow_) {
if (e->id == id && !e->cancelled) {
e->cancelled = true;
e->cb = nullptr;
--live_;
return true;
}
}
return false;
}
void TimerWheel::Cascade(int64_t now) {
for (auto it = overflow_.begin(); it != overflow_.end();) {
EntryPtr e = *it;
if (e->cancelled) {
it = overflow_.erase(it);
continue;
}
int64_t delta = e->tick - now;
if (delta < (int64_t)slots_) {
wheel_[(std::size_t)(e->tick % (int64_t)slots_)].push_back(e);
it = overflow_.erase(it);
} else {
++it;
}
}
}
int TimerWheel::next_expiry_ms() const {
if (live_ == 0) return -1;
int64_t now = NowTick();
int64_t best = -1;
for (const auto& slot : wheel_) {
for (const auto& e : slot) {
if (e->cancelled) continue;
int64_t d = e->tick - now;
if (d < 0) d = 0;
if (best < 0 || d < best) best = d;
}
}
for (const auto& e : overflow_) {
if (e->cancelled) continue;
int64_t d = e->tick - now;
if (d < 0) d = 0;
if (best < 0 || d < best) best = d;
}
if (best < 0) return -1;
return (int)(best * granularity_ms_);
}
void TimerWheel::run_due() {
int64_t now = NowTick();
Cascade(now);
int64_t from = last_tick_ + 1;
if (from < now - (int64_t)slots_) from = now - (int64_t)slots_;
for (int64_t t = from; t <= now; ++t) {
std::size_t idx =
(std::size_t)((t % (int64_t)slots_ + (int64_t)slots_) % (int64_t)slots_);
auto& slot = wheel_[idx];
for (auto it = slot.begin(); it != slot.end();) {
EntryPtr e = *it;
if (e->cancelled || e->tick > now) {
if (e->cancelled) it = slot.erase(it);
else ++it;
continue;
}
it = slot.erase(it);
--live_;
Callback cb = std::move(e->cb);
e->cb = nullptr;
if (cb) cb();
}
}
last_tick_ = now;
}
} // namespace molo::runtime
+56
View File
@@ -0,0 +1,56 @@
// Spec: plans/01-runtime.md 6.1. Hashed timing wheel, 10 ms granularity,
// cap 65536 timers per loop. Single-threaded: only the owning loop touches it.
#pragma once
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <list>
#include <memory>
#include <vector>
namespace molo::runtime {
class TimerWheel {
public:
using TimerId = uint64_t;
using Callback = std::function<void()>;
explicit TimerWheel(int granularity_ms = 10,
std::size_t slots = 1024);
~TimerWheel() = default;
TimerWheel(const TimerWheel&) = delete;
TimerWheel& operator=(const TimerWheel&) = delete;
// Returns 0 when the per-loop cap is hit.
TimerId add(std::chrono::milliseconds delay, Callback cb);
bool cancel(TimerId id);
// Milliseconds until the next expiry, -1 when empty.
int next_expiry_ms() const;
void run_due();
std::size_t size() const { return live_; }
private:
struct Entry {
TimerId id = 0;
int64_t tick = 0;
Callback cb;
bool cancelled = false;
};
using EntryPtr = std::shared_ptr<Entry>;
int64_t NowTick() const;
void Cascade(int64_t now);
int granularity_ms_;
std::size_t slots_;
int64_t start_tick_;
int64_t last_tick_;
std::vector<std::list<EntryPtr>> wheel_;
std::list<EntryPtr> overflow_;
TimerId next_id_ = 1;
std::size_t live_ = 0;
};
} // namespace molo::runtime
+74
View File
@@ -0,0 +1,74 @@
// Spec: plans/01-runtime.md execution step 10, integration with
// 03-security.md (TlsContext/TlsAcceptor over wolfSSL, TLS 1.3 only, ALPN
// h2 and http/1.1, handshake timeout 5 s).
//
// STUB: wolfSSL is not installed in this environment, so this header carries
// the stable interface plus a passthrough StubTlsHandle used by tests and the
// cleartext path. The real handle implementing this interface arrives with
// 03-security.md; Connection code below already routes every byte through it.
#pragma once
#include <cstddef>
#include <cstring>
#include <string>
#include <sys/types.h>
namespace molo::runtime::tls {
enum class HandshakeState { NeedRead, NeedWrite, Done, Failed };
// Non-blocking TLS session bound to one connection. All methods run on the
// owning loop thread.
class ITlsHandle {
public:
virtual ~ITlsHandle() = default;
// Advance the handshake using bytes already pulled from the transport.
// Consumes from `in`, appends handshake output to the caller's out buffer.
virtual HandshakeState handshake_step(const char*& in, std::size_t& in_len,
std::string& out) = 0;
// Negotiated ALPN protocol: "h2" or "http/1.1". Valid after Done.
virtual std::string alpn_protocol() const = 0;
virtual bool is_tls() const = 0;
// Application data. Return bytes produced, or -1 on fatal error.
virtual ssize_t decrypt(const char* in, std::size_t n, char* out,
std::size_t cap, std::size_t& in_used) = 0;
virtual ssize_t encrypt(const char* in, std::size_t n, char* out,
std::size_t cap) = 0;
};
// STUB: cleartext passthrough. handshake completes at once, ALPN is fixed at
// construction ("http/1.1" by default, tests use "h2" for the ALPN path).
class StubTlsHandle : public ITlsHandle {
public:
explicit StubTlsHandle(std::string alpn = "http/1.1", bool is_tls = false)
: alpn_(std::move(alpn)), is_tls_(is_tls) {}
HandshakeState handshake_step(const char*& in, std::size_t& in_len,
std::string& out) override {
(void)in;
(void)in_len;
(void)out;
return HandshakeState::Done;
}
std::string alpn_protocol() const override { return alpn_; }
bool is_tls() const override { return is_tls_; }
ssize_t decrypt(const char* in, std::size_t n, char* out, std::size_t cap,
std::size_t& in_used) override {
std::size_t k = n < cap ? n : cap;
if (k > 0) memcpy(out, in, k);
in_used = k;
return (ssize_t)k;
}
ssize_t encrypt(const char* in, std::size_t n, char* out,
std::size_t cap) override {
std::size_t k = n < cap ? n : cap;
if (k > 0) memcpy(out, in, k);
return (ssize_t)k;
}
private:
std::string alpn_;
bool is_tls_;
};
} // namespace molo::runtime::tls
+27
View File
@@ -0,0 +1,27 @@
// Spec: plans/01-runtime.md 6.8. Transport abstraction: TCP today, a future
// QUIC transport reuses the semantic Request/Response. H2 logic never touches
// sockets directly, only this interface.
#pragma once
#include <cstddef>
#include <string>
#include <sys/types.h>
namespace molo::runtime {
class Transport {
public:
virtual ~Transport() = default;
virtual int fd() const = 0;
// Non-blocking. Returns bytes, 0 on orderly EOF, -1 with errno EAGAIN for
// would-block, -1 with other errno on error.
virtual ssize_t recv(char* buf, std::size_t len) = 0;
virtual ssize_t send(const char* buf, std::size_t len) = 0;
virtual void close() = 0;
virtual bool closed() const = 0;
virtual std::string peer() const = 0;
virtual uint64_t bytes_read() const = 0;
virtual uint64_t bytes_written() const = 0;
};
} // namespace molo::runtime
+40
View File
@@ -0,0 +1,40 @@
// Spec: plans/01-runtime.md 6.8. TCP transport over a non-blocking socket.
#include "transport_tcp.h"
#include <errno.h>
#include <sys/socket.h>
#include <unistd.h>
namespace molo::runtime {
TcpTransport::TcpTransport(int fd, std::string peer)
: fd_(fd), peer_(std::move(peer)) {}
TcpTransport::~TcpTransport() { close(); }
ssize_t TcpTransport::recv(char* buf, std::size_t len) {
if (fd_ < 0) return -1;
ssize_t n = ::recv(fd_, buf, len, 0);
if (n > 0) bytes_read_ += (uint64_t)n;
return n;
}
ssize_t TcpTransport::send(const char* buf, std::size_t len) {
if (fd_ < 0) return -1;
#if defined(MSG_NOSIGNAL)
ssize_t n = ::send(fd_, buf, len, MSG_NOSIGNAL);
#else
ssize_t n = ::send(fd_, buf, len, 0);
#endif
if (n > 0) bytes_written_ += (uint64_t)n;
return n;
}
void TcpTransport::close() {
if (fd_ >= 0) {
::close(fd_);
fd_ = -1;
}
}
} // namespace molo::runtime
+34
View File
@@ -0,0 +1,34 @@
// Spec: plans/01-runtime.md 6.8. TCP transport over a non-blocking socket.
#pragma once
#include <cstdint>
#include <string>
#include "transport.h"
namespace molo::runtime {
class TcpTransport : public Transport {
public:
explicit TcpTransport(int fd, std::string peer = "");
~TcpTransport() override;
TcpTransport(const TcpTransport&) = delete;
TcpTransport& operator=(const TcpTransport&) = delete;
int fd() const override { return fd_; }
ssize_t recv(char* buf, std::size_t len) override;
ssize_t send(const char* buf, std::size_t len) override;
void close() override;
bool closed() const override { return fd_ < 0; }
std::string peer() const override { return peer_; }
uint64_t bytes_read() const override { return bytes_read_; }
uint64_t bytes_written() const override { return bytes_written_; }
private:
int fd_;
std::string peer_;
uint64_t bytes_read_ = 0;
uint64_t bytes_written_ = 0;
};
} // namespace molo::runtime