///|
#derive(Debug, Eq)
pub(all) struct NativeServerOptions {
host : String
port : Int32
max_connections : Int32
reuse_addr : Bool
/// 是否注册进程全局 SIGINT/SIGTERM 信号取消钩子(默认 true;并行单测传 false 并直接通过 server_task.cancel() 触发无干扰优雅排空)
install_signal_handlers : Bool
request_timeout_ms : Int32?
shutdown_timeout_ms : Int32
/// URL 补全优先级:public_origin > (trust_proxy=true 时 Forwarded / X-Forwarded-Proto+Host) > 校验后的 Host 头 > http://{listen_addr}
public_origin : String?
trust_proxy : Bool
}
///|
pub fn NativeServerOptions::NativeServerOptions(
host~ : String = "127.0.0.1",
port~ : Int32 = 8080,
max_connections~ : Int32 = 1024,
reuse_addr~ : Bool = true,
install_signal_handlers~ : Bool = true,
request_timeout_ms? : Int32,
shutdown_timeout_ms~ : Int32 = 5000,
public_origin? : String,
trust_proxy~ : Bool = false,
) -> NativeServerOptions {
{
host,
port,
max_connections,
reuse_addr,
install_signal_handlers,
request_timeout_ms,
shutdown_timeout_ms,
public_origin,
trust_proxy,
}
}
///|
priv struct ServerState {
mut shutting_down : Bool
mut force_cancelled : Bool
mut in_flight : Int32
drain_cond : @async.Cond
shutdown_cond : @async.Cond
idle_conns : Map[Int32, @std_http.ServerConnection]
mut next_conn_id : Int32
active_request_cancels : Map[Int64, () -> Unit]
mut next_req_id : Int64
}
///|
fn cancel_all_active_requests(state : ServerState) -> Unit {
state.force_cancelled = true
if state.active_request_cancels.is_empty() {
return
}
let cancels : Array[() -> Unit] = []
state.active_request_cancels.each((_id, cancel_fn) => cancels.push(cancel_fn))
state.active_request_cancels.clear()
for cancel_fn in cancels {
cancel_fn()
}
}
///|
fn std_method_to_webstd(m : @std_http.RequestMethod) -> @http.Method {
match m {
Get => @http.Method::Get
Head => @http.Method::Head
Post => @http.Method::Post
Put => @http.Method::Put
Delete => @http.Method::Delete
Connect => @http.Method::Connect
Options => @http.Method::Options
Trace => @http.Method::Trace
Patch => @http.Method::Patch
Extension(s) => @http.Method::Custom(s)
}
}
///|
fn is_valid_host_header(h : String) -> Bool {
if h.is_empty() {
return false
}
for ch in h {
let c = ch.to_int()
if c <= 32 || c == 127 || ch == '/' || ch == '\\' || ch == '@' {
return false
}
}
true
}
///|
fn strip_trailing_slash(origin : String) -> String {
let mut end = origin.length()
while end > 0 && origin.code_unit_at(end - 1) == '/' {
end = end - 1
}
origin[:end].to_owned()
}
///|
fn parse_forwarded_proto_host(header : String) -> (String?, String?) {
let first = match header.find(",") {
Some(idx) => header[:idx].to_owned()
None => header
}
let mut proto : String? = None
let mut host : String? = None
for part in first.split(";") {
let trimmed = part.trim().to_owned()
match trimmed.find("=") {
Some(eq_idx) => {
let k = trimmed[:eq_idx].trim().to_lower().to_owned()
let mut v = trimmed[eq_idx + 1:].trim().to_owned()
if v.length() >= 2 && v.has_prefix("\"") && v.has_suffix("\"") {
v = v[1:v.length() - 1].to_owned()
}
if k == "proto" && (v == "http" || v == "https") {
proto = Some(v)
} else if k == "host" && is_valid_host_header(v) {
host = Some(v)
}
}
None => ()
}
}
(proto, host)
}
///|
fn build_full_url(
raw_path : String,
headers : @http.Headers,
options : NativeServerOptions,
listen_addr : String,
) -> String {
if raw_path.has_prefix("http://") || raw_path.has_prefix("https://") {
return raw_path
}
let path_part = if raw_path.has_prefix("/") {
raw_path
} else {
"/\{raw_path}"
}
let origin = match options.public_origin {
Some(o) => strip_trailing_slash(o)
None => {
let mut resolved : String? = None
if options.trust_proxy {
match headers.get("forwarded") {
Some(fwd) => {
let (p, h) = parse_forwarded_proto_host(fwd)
match h {
Some(host) => {
let scheme = p.unwrap_or("http")
resolved = Some("\{scheme}://\{host}")
}
None => ()
}
}
None => ()
}
if resolved is None {
match headers.get("x-forwarded-host") {
Some(xfh) => {
let host_candidate = match xfh.find(",") {
Some(idx) => xfh[:idx].trim().to_owned()
None => xfh.trim().to_owned()
}
if is_valid_host_header(host_candidate) {
let xfp = match headers.get("x-forwarded-proto") {
Some(p) => {
let first_p = match p.find(",") {
Some(idx) => p[:idx].trim().to_lower().to_owned()
None => p.trim().to_lower().to_owned()
}
if first_p == "https" {
"https"
} else {
"http"
}
}
None => "http"
}
resolved = Some("\{xfp}://\{host_candidate}")
}
}
None => ()
}
}
}
match resolved {
Some(o) => o
None =>
match headers.get("host") {
Some(h) if is_valid_host_header(h.trim().to_owned()) =>
"http://\{h.trim().to_owned()}"
_ => "http://\{listen_addr}"
}
}
}
}
"\{origin}\{path_part}"
}
///|
fn webstd_cookie_to_std(cookie : @http.Cookie) -> @std_http.Cookie? {
// Strictly reject any cookie containing CR/LF/NUL/; header-injection characters
if cookie.to_set_cookie_header().is_empty() {
return None
}
let extensions : Array[String] = []
match cookie.same_site {
Some(@http.SameSite::Lax) => extensions.push("SameSite=Lax")
Some(@http.SameSite::Strict) => extensions.push("SameSite=Strict")
Some(@http.SameSite::None) => extensions.push("SameSite=None")
None => ()
}
if cookie.partitioned {
extensions.push("Partitioned")
}
Some(
@std_http.Cookie::Cookie(
cookie.name,
cookie.value,
path?=cookie.path,
expires_raw?=cookie.expires,
max_age?=cookie.max_age,
domain?=cookie.domain,
secure=cookie.secure,
http_only=cookie.http_only,
extensions~,
),
)
}
///|
fn request_may_have_body(headers : @http.Headers) -> Bool {
if headers.has("transfer-encoding") {
return true
}
match headers.get("content-length") {
Some(raw) =>
match (Some(@string.parse_int64(raw.trim())) catch { _ => None }) {
Some(n) => n > 0L
None => true
}
None => false
}
}
///|
fn is_safe_ip_token(s : String) -> Bool {
if s.is_empty() || s.length() > 128 {
return false
}
for ch in s {
let c = ch.to_int()
if c <= 32 || c >= 127 {
return false
}
}
true
}
///|
fn normalize_forwarded_for_value(raw : String) -> String? {
let mut v = raw.trim().to_owned()
if v.length() >= 2 && v.has_prefix("\"") && v.has_suffix("\"") {
v = v[1:v.length() - 1].to_owned()
}
let lower = v.to_lower()
if v.is_empty() || lower == "unknown" || v.has_prefix("_") {
return None
}
if v.has_prefix("[") {
// [::1]:12345 or [::1] -> ::1
match v.find("]") {
Some(close_idx) if close_idx > 1 => {
let inner = v[1:close_idx].to_owned()
if is_safe_ip_token(inner) {
Some(inner)
} else {
None
}
}
_ => None
}
} else {
// Strip optional :port for IPv4 (only if single colon)
let stripped = match v.find(":") {
Some(idx) => {
let rest = v[idx + 1:].to_owned()
if rest.contains(":") {
// Bare IPv6 address
v
} else {
v[:idx].to_owned()
}
}
None => v
}
if is_safe_ip_token(stripped) {
Some(stripped)
} else {
None
}
}
}
///|
fn extract_client_ip(
headers : @http.Headers,
socket_addr : String,
trust_proxy : Bool,
) -> String {
if trust_proxy {
// 1. RFC 7239 Forwarded: for=...
match headers.get("forwarded") {
Some(fwd) => {
let first_hop = match fwd.find(",") {
Some(idx) => fwd[:idx].to_owned()
None => fwd
}
for part in first_hop.split(";") {
let trimmed = part.trim().to_owned()
match trimmed.find("=") {
Some(eq_idx) => {
let k = trimmed[:eq_idx].trim().to_lower()
if k == "for" {
match
normalize_forwarded_for_value(trimmed[eq_idx + 1:].to_owned()) {
Some(ip) => return ip
None => ()
}
}
}
None => ()
}
}
}
None => ()
}
// 2. X-Forwarded-For first hop
match headers.get("x-forwarded-for") {
Some(xff) => {
let first_hop = match xff.find(",") {
Some(idx) => xff[:idx].to_owned()
None => xff
}
match normalize_forwarded_for_value(first_hop) {
Some(ip) => return ip
None => ()
}
}
None => ()
}
}
socket_addr
}
///|
fn is_valid_non_neg_int_str(s : String) -> Bool {
if s.is_empty() {
return false
}
for ch in s {
let c = ch.to_int()
if c < 48 || c > 57 {
return false
}
}
true
}
///|
fn is_error_force_close_status(status : Int32) -> Bool {
status == 400 ||
status == 408 ||
status == 413 ||
status == 431 ||
status >= 500
}
///|
async fn handle_single_request(
app : @core.App,
conn : @std_http.ServerConnection,
req_hdr : @std_http.RequestHeader,
base_env : @runtime.Env,
options : NativeServerOptions,
listen_addr : String,
state : ServerState,
req_group : @async.TaskGroup,
) -> Bool {
let req_headers = @http.Headers::Headers()
req_hdr.headers.each((k, v) => req_headers.append(k.0, v))
let full_url = build_full_url(req_hdr.path, req_headers, options, listen_addr)
let raw_socket_addr = conn.remote_addr().to_string()
let socket_ip = normalize_forwarded_for_value(raw_socket_addr).unwrap_or(
raw_socket_addr,
)
let remote_str = extract_client_ip(
req_headers,
socket_ip,
options.trust_proxy,
)
let ws_slot = @runtime.WsUpgradeSlot::WsUpgradeSlot()
let req_env = base_env
.with_binding(@core.remote_addr_key, remote_str)
.with_binding(@runtime.ws_upgrade_slot_key, ws_slot)
let has_body = request_may_have_body(req_headers)
let body_eof : Ref[Bool] = { val: !has_body, }
let body_aborted : Ref[Bool] = { val: false, }
let req_body : @http.Body = if has_body {
@http.Body::Stream(
@http.ByteStream::ByteStream(
next_chunk=() => {
if body_eof.val || body_aborted.val {
return None
}
match conn.read() {
Some(chunk) => Some(chunk)
None => {
body_eof.val = true
None
}
}
},
cancel=_ => body_aborted.val = true,
),
)
} else {
@http.Body::Empty
}
let web_req_opt = Some(
@http.Request::Request(
meth=std_method_to_webstd(req_hdr.meth),
url=full_url,
headers=req_headers,
body=req_body,
),
) catch {
_ => None
}
let mut force_close = state.shutting_down
try {
let exec_ctx = @runtime.ExecutionContext::ExecutionContext(schedule_fn=task => {
req_group.spawn_bg(no_wait=true, allow_failure=true, task)
})
let resp = match web_req_opt {
None =>
@core.HttpError::BadRequest("invalid request URL", loc=None).to_response()
Some(web_req) =>
match options.request_timeout_ms {
Some(ms) =>
@async.with_timeout(ms, () => {
app.fetch(web_req, env=req_env, exec_ctx~)
}) catch {
@async.TimeoutError =>
@core.HttpError::Custom(
status=408,
code="REQUEST_TIMEOUT",
message="Request timed out",
extensions=None,
loc=None,
).to_response()
err => raise err
}
None => app.fetch(web_req, env=req_env, exec_ctx~)
}
}
// First-class App::ws upgrade path: route passed all pipeline guards and returned 101
if resp.status == 101 {
match ws_slot.take() {
Some(ws_cb) => {
let ws = @std_ws.Conn::from_http_server(req_hdr, conn)
let wrapped : NativeWsConn = { conn: ws, }
defer wrapped.close()
ws_cb(wrapped as &@runtime.WsPeer)
return true
}
None => ()
}
}
// Early-abort unread-body DoS defense + error/shutdown close policy
let unread_body = has_body && !body_eof.val
if state.shutting_down ||
is_error_force_close_status(resp.status) ||
unread_body {
force_close = true
}
// Filter untrusted Content-Length / Transfer-Encoding and fold headers
let forbid_body_and_cl = (resp.status >= 100 && resp.status < 200) ||
resp.status == 204 ||
resp.status == 304
let is_head = req_hdr.meth == @std_http.RequestMethod::Head
let mut head_preserved_cl : String? = None
let extra_headers : Map[@std_http.CaseInsensitiveString, String] = Map([])
let std_cookies : Array[@std_http.Cookie] = []
let grouped : Map[String, Array[String]] = Map([])
for pair in resp.headers.entries() {
let k = pair.0
let v = pair.1
if k == "content-length" {
if is_head && !forbid_body_and_cl {
let trimmed = v.trim().to_owned()
if is_valid_non_neg_int_str(trimmed) {
head_preserved_cl = Some(trimmed)
}
}
continue
}
if k == "transfer-encoding" {
continue
}
if k == "set-cookie" {
match @http.Cookie::parse_set_cookie(v) {
Some(parsed_c) =>
match webstd_cookie_to_std(parsed_c) {
Some(sc) => std_cookies.push(sc)
None => ()
}
None => ()
}
continue
}
match grouped.get(k) {
Some(arr) => arr.push(v)
None => grouped[k] = [v]
}
}
grouped.each((k, vals) => {
extra_headers[@std_http.CaseInsensitiveString(k)] = vals.join(", ")
})
// 1:1 zero-reparse structured Cookie bridge
for c in resp.cookies {
match webstd_cookie_to_std(c) {
Some(sc) => std_cookies.push(sc)
None => ()
}
}
// RFC 9110: forbid Content-Length on 1xx/204/304; preserve Content-Length on HEAD while omitting body
let pre_encoded_body : Bytes? = if forbid_body_and_cl {
None
} else if is_head {
match resp.body {
@http.Body::Empty =>
match head_preserved_cl {
Some(cl) =>
extra_headers[@std_http.CaseInsensitiveString("content-length")] = cl
None => ()
}
@http.Body::Bytes(b) =>
extra_headers[@std_http.CaseInsensitiveString("content-length")] = b
.length()
.to_string()
@http.Body::Text(s) =>
extra_headers[@std_http.CaseInsensitiveString("content-length")] = @utf8.encode(
s,
)
.length()
.to_string()
@http.Body::Stream(_) => ()
}
None
} else {
match resp.body {
@http.Body::Empty => {
extra_headers[@std_http.CaseInsensitiveString("content-length")] = "0"
None
}
@http.Body::Bytes(b) => {
extra_headers[@std_http.CaseInsensitiveString("content-length")] = b
.length()
.to_string()
Some(b)
}
@http.Body::Text(s) => {
let utf8_bytes = @utf8.encode(s)
extra_headers[@std_http.CaseInsensitiveString("content-length")] = utf8_bytes
.length()
.to_string()
Some(utf8_bytes)
}
@http.Body::Stream(_) => None
}
}
if force_close {
extra_headers[@std_http.CaseInsensitiveString("connection")] = "close"
} else if req_headers.get("connection").map(s => s.to_lower()) ==
Some("close") {
force_close = true
extra_headers[@std_http.CaseInsensitiveString("connection")] = "close"
}
let reason = @http.status_reason(resp.status)
conn.send_response(resp.status, reason, extra_headers~, cookies=std_cookies)
if !forbid_body_and_cl && !is_head {
match resp.body {
@http.Body::Empty => ()
@http.Body::Bytes(_) | @http.Body::Text(_) =>
match pre_encoded_body {
Some(bytes) => if !bytes.is_empty() { conn.write(bytes) }
None => ()
}
@http.Body::Stream(stream) => {
defer (stream.cancel)("stream_closed")
for ;; {
match (stream.next_chunk)() {
Some(chunk) =>
if !chunk.is_empty() {
conn.write(chunk)
conn.flush_partial_data()
}
None => break
}
}
}
}
}
conn.end_response()
// Drain any pending wait_until tasks within this request scope
exec_ctx.drain_background()
} catch {
_ => force_close = true
}
force_close
}
///|
async fn handle_connection(
app : @core.App,
ws_routes : Map[String, NativeWsHandler],
conn : @std_http.ServerConnection,
base_env : @runtime.Env,
options : NativeServerOptions,
listen_addr : String,
state : ServerState,
) -> Unit {
let conn_id = state.next_conn_id
state.next_conn_id = conn_id + 1
defer {
state.idle_conns.remove(conn_id)
conn.close()
}
for ;; {
if state.shutting_down {
break
}
// Stage A: idle Keep-Alive wait (NEVER shielded by protect_from_cancel)
state.idle_conns[conn_id] = conn
let req_hdr = conn.read_request() catch {
_ => {
state.idle_conns.remove(conn_id)
break
}
}
state.idle_conns.remove(conn_id)
if state.shutting_down {
break
}
// Check first-class WebSocket upgrade before HTTP dispatch
if is_ws_upgrade_request(req_hdr) {
let path_only = request_path_only(req_hdr.path)
match ws_routes.get(path_only) {
Some(ws_handler) => {
state.in_flight = state.in_flight + 1
defer {
state.in_flight = state.in_flight - 1
if state.in_flight == 0 {
state.drain_cond.broadcast()
}
}
handle_ws_upgrade(conn, req_hdr, ws_handler) catch {
_ => ()
}
break
}
None => ()
}
}
// Stage B: in-flight shielded execution (ADR-012 Iron Rule ③: single TaskGroup + single
// non-shielded child coroutine so `request_timeout_ms` and SSE `: ping` keepalive work,
// while server-level `active_request_cancels` drives shutdown deadline cancellation with
// zero per-request watcher coroutines).
state.in_flight = state.in_flight + 1
let should_close = @async.protect_from_cancel(() => {
defer {
state.in_flight = state.in_flight - 1
if state.in_flight == 0 {
state.drain_cond.broadcast()
}
}
if state.shutting_down || state.force_cancelled {
return true
}
let mut res = true
@async.with_task_group(req_group => {
let req_task = req_group.spawn(allow_failure=true, () => {
handle_single_request(
app, conn, req_hdr, base_env, options, listen_addr, state, req_group,
)
})
let req_id = state.next_req_id
state.next_req_id = req_id + 1L
state.active_request_cancels[req_id] = () => {
req_task.cancel()
conn.close()
}
defer state.active_request_cancels.remove(req_id)
res = req_task.wait() catch { _ => true }
}) catch {
_ => res = true
}
res
})
if should_close || state.shutting_down {
break
}
}
}
///|
pub async fn serve(
app : @core.App,
options~ : NativeServerOptions = NativeServerOptions::NativeServerOptions(),
env~ : @runtime.Env = @runtime.Env::Env(),
on_listen? : (Int32) -> Unit,
) -> Unit {
serve_with_ws(app, ws_routes=Map([]), options~, env~, on_listen?)
}
///|
/// Serves an HTTP `@core.App` alongside first-class WebSocket upgrade routes
/// powered by `moonbitlang/core/async/websocket`.
pub async fn serve_with_ws(
app : @core.App,
ws_routes~ : Map[String, NativeWsHandler],
options~ : NativeServerOptions = NativeServerOptions::NativeServerOptions(),
env~ : @runtime.Env = @runtime.Env::Env(),
on_listen? : (Int32) -> Unit,
) -> Unit {
ignore(app.freeze())
app.start_lifecycles()
if options.install_signal_handlers {
@signal.set_global_cancellation_signals([
@signal.Signal::SIGINT,
@signal.Signal::SIGTERM,
])
}
let bind_addr = @socket.Addr::parse("\{options.host}:\{options.port}")
let server = @std_http.Server::Server(
bind_addr,
reuse_addr=options.reuse_addr,
)
let actual_addr = server.local_addr()
let listen_addr_str = actual_addr.to_string()
match on_listen {
Some(cb) => cb(actual_addr.port.to_int())
None => ()
}
let state : ServerState = {
shutting_down: false,
force_cancelled: false,
in_flight: 0,
drain_cond: @async.Cond::Cond(),
shutdown_cond: @async.Cond::Cond(),
idle_conns: Map([]),
next_conn_id: 1,
active_request_cancels: Map([]),
next_req_id: 1L,
}
let sem = @async.Semaphore::Semaphore(options.max_connections)
// Top-level shutdown defer: close idle_conns in <1ms, then spawn NON-SHIELDED child coroutines
// for independent bounded drain and bounded reverse stop_lifecycles (breaking protect_from_cancel shield!)
defer @async.protect_from_cancel(() => {
state.shutting_down = true
state.shutdown_cond.broadcast()
server.close()
let to_close : Array[@std_http.ServerConnection] = []
state.idle_conns.each((_id, c) => to_close.push(c))
state.idle_conns.clear()
for c in to_close {
c.close()
}
@async.with_task_group(drain_group => {
let t = drain_group.spawn(allow_failure=true, () => {
if options.shutdown_timeout_ms > 0 {
ignore(
@async.with_timeout_opt(options.shutdown_timeout_ms, () => {
while state.in_flight > 0 {
state.drain_cond.wait()
}
}),
)
}
})
ignore(t.wait())
}) catch {
_ => ()
}
cancel_all_active_requests(state)
@async.with_task_group(stop_group => {
let t = stop_group.spawn(allow_failure=true, () => {
ignore(
@async.with_timeout_opt(options.shutdown_timeout_ms, () => {
app.stop_lifecycles()
}),
)
})
ignore(t.wait())
}) catch {
_ => ()
}
})
@async.with_task_group(conn_group => {
// Single server-wide shutdown reaper (replaces per-request watcher coroutines):
// waits for shutdown, grants `shutdown_timeout_ms` for graceful drain, then cancels
// any remaining in-flight requests via `state.active_request_cancels`.
conn_group.spawn_bg(no_wait=true, allow_failure=true, () => {
defer @async.protect_from_cancel(() => {
state.shutting_down = true
if state.in_flight > 0 {
if options.shutdown_timeout_ms > 0 {
@async.with_task_group(reaper_group => {
let t = reaper_group.spawn(allow_failure=true, () => {
ignore(
@async.with_timeout_opt(options.shutdown_timeout_ms, () => {
while state.in_flight > 0 {
state.drain_cond.wait()
}
}),
)
})
ignore(t.wait())
}) catch {
_ => ()
}
}
cancel_all_active_requests(state)
}
})
while !state.shutting_down {
state.shutdown_cond.wait()
}
})
defer {
state.shutting_down = true
state.shutdown_cond.broadcast()
let to_close : Array[@std_http.ServerConnection] = []
state.idle_conns.each((_id, c) => to_close.push(c))
state.idle_conns.clear()
for c in to_close {
c.close()
}
}
for ;; {
if state.shutting_down {
break
}
let conn = server.accept() catch { _ => break }
sem.acquire()
conn_group.spawn_bg(no_wait=true, allow_failure=true, () => {
defer sem.release()
handle_connection(
app, ws_routes, conn, env, options, listen_addr_str, state,
)
})
}
})
}