153 lines
5.1 KiB
C++
153 lines
5.1 KiB
C++
// 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
|