feat(web,agent): hosting H1–H4 — proxy, invoke, MCP stdio, cross-module db

Close GAP-01–05 with ext/web streaming reverse proxy and HTTP→## invoke,
transitive object method lookup, corpus_search plugin ensure, and agent MCP
server stdio so downstream apps can drop Python side relays.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-07 22:14:51 +08:00
1 parent 2e068e66d4
commit fc543f1ca5
31 files changed
+2765 -102

No files matched your search

+6
View File
@@ -281,6 +281,8 @@ pub fn listen(
middleware: &Middleware,
mut site_head: Vec<crate::assets::HeadLink>,
mut icon_routes: Vec<crate::assets::IconRoute>,
proxy_routes: Vec<(String, crate::proxy::ProxyRoute)>,
invoke_routes: Vec<(String, crate::invoke::InvokeRoute)>,
) -> Result<Value, String> {
let mut form_owners = HashMap::new();
collect_page_forms(page, &mut forms, &mut form_owners);
@@ -601,6 +603,10 @@ pub fn listen(
);
}
// Streaming proxy + HTTP→## invoke (before fallback so paths are explicit).
app = crate::proxy::mount_all(app, &proxy_routes);
app = crate::invoke::mount_all(app, &invoke_routes);
app = app.fallback(get(fallback_404));
let app = crate::middleware::apply(app, middleware);
+332
View File
@@ -0,0 +1,332 @@
//! HTTP → user `##` invoke routes for `app.invoke` / `configure invoke=`.
use std::sync::Arc;
use axum::extract::{Request, State};
use axum::http::{header, HeaderValue, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::Router;
use serde_json::{json, Map, Value};
use crate::middleware::{cell_text, JsonBody};
use crate::table;
use crate::call_lib_with_args;
/// One declared invoke mount.
#[derive(Clone, Debug)]
pub struct InvokeRoute {
pub method: String,
pub fn_path: String,
pub body_mode: String,
pub return_mode: String,
}
impl InvokeRoute {
pub fn from_json(v: &Value) -> Option<Self> {
let obj = v.as_object()?;
let fn_path = obj
.get("fn")
.or_else(|| obj.get("function"))
.and_then(|x| x.as_str())?
.trim()
.to_string();
if fn_path.is_empty() {
return None;
}
let method = obj
.get("method")
.and_then(|x| x.as_str())
.unwrap_or("POST")
.to_uppercase();
let body_mode = obj
.get("body")
.and_then(|x| x.as_str())
.unwrap_or("json")
.to_ascii_lowercase();
let return_mode = obj
.get("return")
.and_then(|x| x.as_str())
.unwrap_or("json")
.to_ascii_lowercase();
Some(Self {
method,
fn_path,
body_mode,
return_mode,
})
}
}
/// Normalize invoke-routes GFM table into `app.invoke_routes` map.
pub fn invoke_routes_from_table(table_v: &Value) -> Value {
let rows = table::as_rows(table_v);
let mut routes = Map::new();
if let Some(arr) = rows.as_array() {
for row in arr {
let col = |keys: &[&str]| -> String {
keys.iter()
.find_map(|k| row.get(*k).map(cell_text))
.unwrap_or_default()
};
let path = col(&["路径", "path", "Path"])
.trim()
.trim_start_matches('/')
.to_string();
if path.is_empty() {
continue;
}
let fn_path = col(&["函数", "fn", "function", "Function"]);
if fn_path.is_empty() {
continue;
}
let method = {
let m = col(&["方法", "method", "Method"]).to_uppercase();
if m.is_empty() {
"POST".into()
} else {
m
}
};
let body = {
let b = col(&["正文", "body", "Body"]).to_ascii_lowercase();
if b.is_empty() {
"json".into()
} else {
b
}
};
let ret = {
let r = col(&["返回", "return", "Return"]).to_ascii_lowercase();
if r.is_empty() {
"json".into()
} else {
r
}
};
routes.insert(
path,
json!({
"method": method,
"fn": fn_path,
"body": body,
"return": ret,
}),
);
}
}
Value::Object(routes)
}
pub fn mount_all(
mut app: Router<Arc<crate::http::AppState>>,
routes: &[(String, InvokeRoute)],
) -> Router<Arc<crate::http::AppState>> {
for (path, route) in routes {
app = mount_one(app, path, route.clone());
}
app
}
fn mount_one(
app: Router<Arc<crate::http::AppState>>,
path: &str,
route: InvokeRoute,
) -> Router<Arc<crate::http::AppState>> {
let r = route.clone();
match route.method.as_str() {
"GET" => app.route(
path,
get(move |State(st): State<Arc<crate::http::AppState>>, req: Request| {
let r = r.clone();
async move { invoke_handler(st, r, req).await }
}),
),
_ => app.route(
path,
post(move |State(st): State<Arc<crate::http::AppState>>, req: Request| {
let r = r.clone();
async move { invoke_handler(st, r, req).await }
}),
),
}
}
async fn invoke_handler(
_st: Arc<crate::http::AppState>,
route: InvokeRoute,
req: Request,
) -> Response {
let (parts, body) = req.into_parts();
let query_map = query_to_map(parts.uri.query());
let payload = match route.body_mode.as_str() {
"query" => Value::Object(query_map.clone()),
"raw" => {
let bytes = match axum::body::to_bytes(body, 8 * 1024 * 1024).await {
Ok(b) => b,
Err(e) => {
return err_json(StatusCode::BAD_REQUEST, &format!("body: {e}"));
}
};
json!({ "payload": String::from_utf8_lossy(&bytes).to_string() })
}
"form" => {
let bytes = match axum::body::to_bytes(body, 8 * 1024 * 1024).await {
Ok(b) => b,
Err(e) => {
return err_json(StatusCode::BAD_REQUEST, &format!("body: {e}"));
}
};
form_to_map(&bytes)
}
_ => {
// json (default)
let bytes = match axum::body::to_bytes(body, 8 * 1024 * 1024).await {
Ok(b) => b,
Err(e) => {
return err_json(StatusCode::BAD_REQUEST, &format!("body: {e}"));
}
};
if bytes.is_empty() {
Value::Object(query_map.clone())
} else {
match serde_json::from_slice::<Value>(&bytes) {
Ok(Value::Object(mut m)) => {
for (k, v) in query_map {
m.entry(k).or_insert(v);
}
Value::Object(m)
}
Ok(other) => json!({ "payload": other }),
Err(e) => {
return err_json(StatusCode::BAD_REQUEST, &format!("json: {e}"));
}
}
}
}
};
// Bind JSON object keys as named args; always offer `payload` too.
let mut args = Map::new();
if let Value::Object(ref m) = payload {
for (k, v) in m {
args.insert(k.clone(), v.clone());
}
}
if !args.contains_key("payload") {
args.insert("payload".to_string(), payload.clone());
}
let fn_path = route.fn_path.clone();
let result = tokio::task::spawn_blocking(move || call_lib_with_args(&fn_path, &Value::Object(args)))
.await;
let value = match result {
Ok(Ok(v)) => v,
Ok(Err(e)) => {
let status = if e.contains("unknown library")
|| e.contains("call_lib_path: need")
|| e.contains("not allowed")
|| e.contains("no site")
{
StatusCode::BAD_REQUEST
} else {
StatusCode::INTERNAL_SERVER_ERROR
};
return err_json(status, &e);
}
Err(e) => {
return err_json(StatusCode::INTERNAL_SERVER_ERROR, &format!("join: {e}"));
}
};
match route.return_mode.as_str() {
"text" => {
let text = match &value {
Value::String(s) => s.clone(),
other => other.to_string(),
};
(
StatusCode::OK,
[(
header::CONTENT_TYPE,
HeaderValue::from_static("text/plain; charset=utf-8"),
)],
text,
)
.into_response()
}
"status" => StatusCode::NO_CONTENT.into_response(),
_ => {
// Wrap non-objects so clients always get JSON object when possible.
let body = match value {
Value::Null => json!({ "ok": true }),
Value::Object(_) => value,
other => json!({ "ok": true, "result": other }),
};
JsonBody(body).into_response()
}
}
}
fn query_to_map(q: Option<&str>) -> Map<String, Value> {
let mut m = Map::new();
let Some(q) = q else {
return m;
};
for pair in q.split('&') {
if pair.is_empty() {
continue;
}
let (k, v) = pair.split_once('=').unwrap_or((pair, ""));
let k = urlencoding_decode(k);
let v = urlencoding_decode(v);
if !k.is_empty() {
m.insert(k, json!(v));
}
}
m
}
fn form_to_map(bytes: &[u8]) -> Value {
let s = String::from_utf8_lossy(bytes);
Value::Object(query_to_map(Some(&s)))
}
fn urlencoding_decode(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let b = s.as_bytes();
let mut i = 0;
while i < b.len() {
match b[i] {
b'+' => {
out.push(' ');
i += 1;
}
b'%' if i + 2 < b.len() => {
let h = u8::from_str_radix(std::str::from_utf8(&b[i + 1..i + 3]).unwrap_or(""), 16);
if let Ok(c) = h {
out.push(c as char);
i += 3;
} else {
out.push('%');
i += 1;
}
}
c => {
out.push(c as char);
i += 1;
}
}
}
out
}
fn err_json(status: StatusCode, msg: &str) -> Response {
(
status,
JsonBody(json!({ "ok": false, "error": msg })),
)
.into_response()
}
+292 -62
View File
@@ -8,9 +8,11 @@ mod driver;
mod form;
mod http;
mod cache;
mod invoke;
mod markdown;
mod middleware;
mod password;
mod proxy;
mod rate_limit;
mod render;
mod rss;
@@ -156,6 +158,17 @@ fn call_lib(path: &str) -> Result<Value, String> {
host_query_json("call_lib_path", &json!({ "path": path }))
}
/// Call `lib.member` with a JSON object of named arguments (for HTTP invoke).
pub(crate) fn call_lib_with_args(path: &str, args: &Value) -> Result<Value, String> {
host_query_json(
"call_lib_path",
&json!({
"path": path,
"args": args,
}),
)
}
/// Absolute directory of the entry `.mq.md` (falls back to process cwd).
fn entry_dir() -> PathBuf {
host_query_json("entry_dir", &json!({}))
@@ -1507,74 +1520,245 @@ web_ffi!(web_app_static, |args: &Value| {
// backed by db queries (method GET/POST, `表`=table name).
web_ffi!(web_app_middleware, |args: &Value| {
let mut app = args.get("app").cloned().unwrap_or(json!({}));
{
let obj = app
.as_object_mut()
.ok_or_else(|| "app must be a map".to_string())?;
let mw = obj
.entry("middleware".to_string())
.or_insert_with(|| json!({}));
let m = mw
.as_object_mut()
.ok_or_else(|| "app.middleware must be a map".to_string())?;
if let Some(cors) = args.get("cors") {
m.insert("cors".into(), middleware::cors_from_table(cors));
}
if let Some(security) = args.get("security") {
m.insert(
"security".into(),
middleware::security_from_table(security),
);
}
if let Some(compress) = args.get("compress") {
let on = match compress {
Value::Bool(b) => *b,
Value::String(s) => matches!(s.as_str(), "true" | "True" | "1" | "yes" | "on"),
Value::Number(n) => n.as_i64().unwrap_or(0) != 0,
_ => false,
};
m.insert("compress".into(), json!(on));
}
if let Some(al) = args.get("access_log").or_else(|| args.get("访问日志")) {
let on = match al {
Value::Bool(b) => *b,
Value::String(s) => {
matches!(s.as_str(), "true" | "True" | "1" | "yes" | "on" | "真")
}
Value::Number(n) => n.as_i64().unwrap_or(0) != 0,
_ => false,
};
m.insert("access_log".into(), json!(on));
}
if let Some(cc) = args
.get("cache_control")
.or_else(|| args.get("缓存控制"))
.and_then(|v| v.as_str())
{
let s = cc.trim();
if !s.is_empty() {
m.insert("cache_control".into(), json!(s));
}
}
if let Some(bl) = args.get("body_limit") {
let n = bl
.as_u64()
.or_else(|| bl.as_i64().map(|i| i as u64))
.unwrap_or(0);
if n > 0 {
m.insert("body_limit".into(), json!(n));
}
}
if let Some(routes) = args.get("json_routes") {
m.insert(
"json_routes".into(),
middleware::json_routes_from_table(routes),
);
}
}
if let Some(proxy) = args
.get("proxy")
.or_else(|| args.get("proxy_routes"))
.or_else(|| args.get("代理"))
.filter(|v| !v.is_null())
{
let incoming = proxy::proxy_routes_from_table(proxy);
let obj_app = app
.as_object_mut()
.ok_or_else(|| "app must be a map".to_string())?;
let mut existing = obj_app
.get("proxy_routes")
.and_then(|v| v.as_object())
.cloned()
.unwrap_or_default();
if let Some(map) = incoming.as_object() {
for (k, v) in map {
existing.insert(k.clone(), v.clone());
}
}
obj_app.insert("proxy_routes".into(), Value::Object(existing));
}
if let Some(inv) = args
.get("invoke")
.or_else(|| args.get("invoke_routes"))
.or_else(|| args.get("调用"))
.filter(|v| !v.is_null())
{
let incoming = invoke::invoke_routes_from_table(inv);
let obj_app = app
.as_object_mut()
.ok_or_else(|| "app must be a map".to_string())?;
let mut existing = obj_app
.get("invoke_routes")
.and_then(|v| v.as_object())
.cloned()
.unwrap_or_default();
if let Some(map) = incoming.as_object() {
for (k, v) in map {
existing.insert(k.clone(), v.clone());
}
}
obj_app.insert("invoke_routes".into(), Value::Object(existing));
}
Ok(app)
});
web_ffi!(web_app_proxy, |args: &Value| {
let mut app = args.get("app").cloned().unwrap_or(json!({}));
let path = normalize_route_path_in_app(
arg_str(args, "path").or_else(|_| arg_str(args, "路径"))?,
&app,
)?;
let upstream = arg_str(args, "upstream")
.or_else(|_| arg_str(args, "上游"))?
.to_string();
if upstream.trim().is_empty() {
return Err("proxy requires `upstream`".into());
}
let stream = match args
.get("stream")
.or_else(|| args.get("流式"))
.cloned()
.unwrap_or(json!(true))
{
Value::Bool(b) => b,
Value::String(s) => matches!(
s.as_str(),
"true" | "True" | "1" | "yes" | "on" | "真"
),
Value::Number(n) => n.as_i64().unwrap_or(0) != 0,
_ => true,
};
let strip_prefix = arg_str_opt(args, "strip_prefix")
.or_else(|| arg_str_opt(args, "去前缀"))
.unwrap_or("")
.to_string();
let methods = match args.get("methods").or_else(|| args.get("方法")) {
Some(Value::Array(arr)) => arr
.iter()
.filter_map(|v| v.as_str().map(|s| s.to_uppercase()))
.collect::<Vec<_>>(),
Some(Value::String(s)) => s
.split(',')
.map(|m| m.trim().to_uppercase())
.filter(|m| !m.is_empty())
.collect(),
_ => vec!["POST".into()],
};
let headers_from_env = arg_str_opt(args, "headers_from_env")
.or_else(|| arg_str_opt(args, "环境头"))
.unwrap_or("")
.to_string();
let timeout_ms = args
.get("timeout_ms")
.or_else(|| args.get("超时"))
.and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64)))
.unwrap_or(120_000);
let obj = app
.as_object_mut()
.ok_or_else(|| "app must be a map".to_string())?;
let mw = obj
.entry("middleware".to_string())
.or_insert_with(|| json!({}));
let m = mw
.as_object_mut()
.ok_or_else(|| "app.middleware must be a map".to_string())?;
let mut routes = obj
.get("proxy_routes")
.and_then(|v| v.as_object())
.cloned()
.unwrap_or_default();
let key = path.trim_start_matches('/').to_string();
routes.insert(
key,
json!({
"upstream": upstream,
"stream": stream,
"strip_prefix": strip_prefix,
"methods": methods,
"headers_from_env": headers_from_env,
"timeout_ms": timeout_ms,
}),
);
obj.insert("proxy_routes".into(), Value::Object(routes));
Ok(app)
});
if let Some(cors) = args.get("cors") {
m.insert(
"cors".into(),
middleware::cors_from_table(cors),
);
web_ffi!(web_app_invoke, |args: &Value| {
let mut app = args.get("app").cloned().unwrap_or(json!({}));
let path = normalize_route_path_in_app(
arg_str(args, "path").or_else(|_| arg_str(args, "路径"))?,
&app,
)?;
let fn_path = arg_str(args, "fn")
.or_else(|_| arg_str(args, "function"))
.or_else(|_| arg_str(args, "函数"))?
.to_string();
if fn_path.trim().is_empty() {
return Err("invoke requires `fn`".into());
}
if let Some(security) = args.get("security") {
m.insert(
"security".into(),
middleware::security_from_table(security),
);
}
if let Some(compress) = args.get("compress") {
let on = match compress {
Value::Bool(b) => *b,
Value::String(s) => matches!(s.as_str(), "true" | "True" | "1" | "yes" | "on"),
Value::Number(n) => n.as_i64().unwrap_or(0) != 0,
_ => false,
};
m.insert("compress".into(), json!(on));
}
if let Some(al) = args
.get("access_log")
.or_else(|| args.get("访问日志"))
{
let on = match al {
Value::Bool(b) => *b,
Value::String(s) => matches!(s.as_str(), "true" | "True" | "1" | "yes" | "on" | "真"),
Value::Number(n) => n.as_i64().unwrap_or(0) != 0,
_ => false,
};
m.insert("access_log".into(), json!(on));
}
if let Some(cc) = args
.get("cache_control")
.or_else(|| args.get("缓存控制"))
.and_then(|v| v.as_str())
{
let s = cc.trim();
if !s.is_empty() {
m.insert("cache_control".into(), json!(s));
}
}
if let Some(bl) = args.get("body_limit") {
let n = bl
.as_u64()
.or_else(|| bl.as_i64().map(|i| i as u64))
.unwrap_or(0);
if n > 0 {
m.insert("body_limit".into(), json!(n));
}
}
if let Some(routes) = args.get("json_routes") {
m.insert(
"json_routes".into(),
middleware::json_routes_from_table(routes),
// Optional allowlist: only `lib.member` form for now (no arbitrary file paths).
if !fn_path.contains('.') || fn_path.contains('/') || fn_path.contains('#') {
return Err(
"invoke `fn` must be `lib.member` (imported on the entry module)".into(),
);
}
let method = arg_str_opt(args, "method")
.or_else(|| arg_str_opt(args, "方法"))
.unwrap_or("POST")
.to_uppercase();
let body = arg_str_opt(args, "body")
.or_else(|| arg_str_opt(args, "正文"))
.unwrap_or("json")
.to_ascii_lowercase();
let ret = arg_str_opt(args, "return")
.or_else(|| arg_str_opt(args, "返回"))
.unwrap_or("json")
.to_ascii_lowercase();
let obj = app
.as_object_mut()
.ok_or_else(|| "app must be a map".to_string())?;
let mut routes = obj
.get("invoke_routes")
.and_then(|v| v.as_object())
.cloned()
.unwrap_or_default();
let key = path.trim_start_matches('/').to_string();
routes.insert(
key,
json!({
"method": method,
"fn": fn_path,
"body": body,
"return": ret,
}),
);
obj.insert("invoke_routes".into(), Value::Object(routes));
Ok(app)
});
@@ -1737,6 +1921,8 @@ web_ffi!(web_listen, |args: &Value| {
page_500,
gates,
gallery_routes,
proxy_routes,
invoke_routes,
middleware,
cookie_secure,
site_head,
@@ -1793,6 +1979,8 @@ web_ffi!(web_listen, |args: &Value| {
None,
Vec::<http::Gate>::new(),
HashMap::new(),
Vec::<(String, crate::proxy::ProxyRoute)>::new(),
Vec::<(String, crate::invoke::InvokeRoute)>::new(),
middleware::Middleware::default(),
cookie_secure,
Vec::<crate::assets::HeadLink>::new(),
@@ -1943,6 +2131,34 @@ web_ffi!(web_listen, |args: &Value| {
gallery_routes.insert(k.clone(), v.clone());
}
}
let mut proxy_routes: Vec<(String, proxy::ProxyRoute)> = Vec::new();
if let Some(obj) = app.get("proxy_routes").and_then(|v| v.as_object()) {
for (k, v) in obj {
if let Some(r) = proxy::ProxyRoute::from_json(v) {
let path = if k.starts_with('/') {
k.clone()
} else {
format!("/{k}")
};
proxy_routes.push((path, r));
}
}
proxy_routes.sort_by(|a, b| a.0.cmp(&b.0));
}
let mut invoke_routes: Vec<(String, invoke::InvokeRoute)> = Vec::new();
if let Some(obj) = app.get("invoke_routes").and_then(|v| v.as_object()) {
for (k, v) in obj {
if let Some(r) = invoke::InvokeRoute::from_json(v) {
let path = if k.starts_with('/') {
k.clone()
} else {
format!("/{k}")
};
invoke_routes.push((path, r));
}
}
invoke_routes.sort_by(|a, b| a.0.cmp(&b.0));
}
if app.get("tls_cert").is_some() || app.get("tls_key").is_some() {
eprintln!(
"marqdo web: in-process TLS is not enabled; terminate HTTPS at a reverse proxy (nginx/caddy) and set cookie_secure=True"
@@ -2026,6 +2242,8 @@ web_ffi!(web_listen, |args: &Value| {
page_500,
gates,
gallery_routes,
proxy_routes,
invoke_routes,
middleware,
cookie_secure,
site_head,
@@ -2141,6 +2359,8 @@ web_ffi!(web_listen, |args: &Value| {
&middleware,
site_head,
icon_routes,
proxy_routes,
invoke_routes,
)
});
@@ -2369,9 +2589,19 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
),
(
"web_app_middleware",
"app,cors,security,compress,body_limit,json_routes,access_log,cache_control",
"app,cors,security,compress,body_limit,json_routes,access_log,cache_control,proxy,invoke",
web_app_middleware as PluginFn,
),
(
"web_app_proxy",
"app,path,upstream,stream,strip_prefix,methods,headers_from_env,timeout_ms",
web_app_proxy as PluginFn,
),
(
"web_app_invoke",
"app,path,method,fn,body,return",
web_app_invoke as PluginFn,
),
("web_form_new", "table,action,id", web_form_new as PluginFn),
("web_form_fields", "form,fields", web_form_fields as PluginFn),
("web_form_rules", "form,rules", web_form_rules as PluginFn),
+1 -1
View File
@@ -24,7 +24,7 @@ use tower_http::set_header::SetResponseHeaderLayer;
use crate::db;
/// Render a cell value as text (strings stay, numbers/bools stringify).
fn cell_text(v: &Value) -> String {
pub fn cell_text(v: &Value) -> String {
match v {
Value::Null => String::new(),
Value::String(s) => s.clone(),
+462
View File
@@ -0,0 +1,462 @@
//! Streaming reverse proxy routes for `app.proxy` / `configure proxy=`.
//!
//! Browser → Marqdo (same origin) → upstream, piping response bodies
//! (including `text/event-stream`) without buffering the full stream.
use std::sync::Arc;
use std::time::Duration;
use axum::body::Body;
use axum::extract::{Request, State};
use axum::http::{header, HeaderMap, HeaderName, HeaderValue, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::routing::{MethodFilter, MethodRouter};
use axum::Router;
use futures_util::TryStreamExt;
use serde_json::{json, Map, Value};
use crate::middleware::cell_text;
use crate::table;
/// One declared proxy mount.
#[derive(Clone, Debug)]
pub struct ProxyRoute {
pub upstream: String,
pub stream: bool,
pub strip_prefix: String,
pub methods: Vec<String>,
/// `ENV_NAME=Header-Name` pairs injected on the upstream request.
pub headers_from_env: Vec<(String, String)>,
pub timeout_ms: u64,
}
impl ProxyRoute {
pub fn from_json(v: &Value) -> Option<Self> {
let obj = v.as_object()?;
let upstream = obj.get("upstream")?.as_str()?.trim().to_string();
if upstream.is_empty() {
return None;
}
let stream = obj
.get("stream")
.and_then(|x| x.as_bool())
.unwrap_or(true);
let strip_prefix = obj
.get("strip_prefix")
.and_then(|x| x.as_str())
.unwrap_or("")
.to_string();
let methods = obj
.get("methods")
.and_then(|x| x.as_array())
.map(|arr| {
arr.iter()
.filter_map(|m| m.as_str().map(|s| s.to_uppercase()))
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
})
.filter(|v| !v.is_empty())
.unwrap_or_else(|| vec!["POST".into()]);
let headers_from_env = parse_headers_from_env_value(obj.get("headers_from_env"));
let timeout_ms = obj
.get("timeout_ms")
.and_then(|x| x.as_u64().or_else(|| x.as_i64().map(|i| i as u64)))
.unwrap_or(120_000)
.max(1);
Some(Self {
upstream,
stream,
strip_prefix,
methods,
headers_from_env,
timeout_ms,
})
}
}
fn parse_headers_from_env_value(v: Option<&Value>) -> Vec<(String, String)> {
let Some(v) = v else {
return Vec::new();
};
match v {
Value::String(s) => parse_headers_from_env_str(s),
Value::Array(arr) => arr
.iter()
.filter_map(|x| x.as_str())
.flat_map(parse_headers_from_env_str)
.collect(),
_ => Vec::new(),
}
}
fn parse_headers_from_env_str(s: &str) -> Vec<(String, String)> {
s.split(|c| c == ',' || c == ';')
.filter_map(|part| {
let part = part.trim();
if part.is_empty() {
return None;
}
let (env, hdr) = part.split_once('=')?;
let env = env.trim();
let hdr = hdr.trim();
if env.is_empty() || hdr.is_empty() {
return None;
}
Some((env.to_string(), hdr.to_string()))
})
.collect()
}
/// Expand `$VAR` / `${VAR}` in upstream URL strings.
pub fn expand_env(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let bytes = s.as_bytes();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'$' {
if i + 1 < bytes.len() && bytes[i + 1] == b'{' {
if let Some(end) = s[i + 2..].find('}') {
let name = &s[i + 2..i + 2 + end];
out.push_str(&std::env::var(name).unwrap_or_default());
i += 3 + end;
continue;
}
} else {
let start = i + 1;
let mut end = start;
while end < bytes.len()
&& (bytes[end].is_ascii_alphanumeric() || bytes[end] == b'_')
{
end += 1;
}
if end > start {
let name = &s[start..end];
out.push_str(&std::env::var(name).unwrap_or_default());
i = end;
continue;
}
}
}
out.push(bytes[i] as char);
i += 1;
}
out
}
/// Normalize a proxy-routes GFM table into `app.proxy_routes` map shape.
pub fn proxy_routes_from_table(table_v: &Value) -> Value {
let rows = table::as_rows(table_v);
let mut routes = Map::new();
if let Some(arr) = rows.as_array() {
for row in arr {
let col = |keys: &[&str]| -> String {
keys.iter()
.find_map(|k| row.get(*k).map(cell_text))
.unwrap_or_default()
};
let path = normalize_path(&col(&["路径", "path", "Path"]));
if path.is_empty() || path == "/" {
continue;
}
let upstream = col(&["上游", "upstream", "Upstream"]);
if upstream.is_empty() {
continue;
}
let stream = {
let s = col(&["流式", "stream", "Stream"]);
if s.is_empty() {
true
} else {
matches!(
s.to_ascii_lowercase().as_str(),
"true" | "1" | "yes" | "on" | "真"
)
}
};
let strip_prefix = col(&["去前缀", "strip_prefix", "Strip-Prefix"]);
let methods_raw = col(&["方法", "methods", "Methods"]);
let methods: Vec<String> = if methods_raw.is_empty() {
vec!["POST".into()]
} else {
methods_raw
.split(',')
.map(|m| m.trim().to_uppercase())
.filter(|m| !m.is_empty())
.collect()
};
let headers_from_env = col(&[
"环境头",
"headers_from_env",
"Headers-From-Env",
]);
let timeout_ms = col(&["超时", "timeout_ms", "Timeout"])
.parse::<u64>()
.unwrap_or(120_000);
routes.insert(
path.trim_start_matches('/').to_string(),
json!({
"upstream": upstream,
"stream": stream,
"strip_prefix": strip_prefix,
"methods": methods,
"headers_from_env": headers_from_env,
"timeout_ms": timeout_ms,
}),
);
}
}
Value::Object(routes)
}
fn normalize_path(raw: &str) -> String {
let s = raw.trim();
if s.is_empty() {
return String::new();
}
if s.starts_with('/') {
s.to_string()
} else {
format!("/{s}")
}
}
/// Mount all proxy routes onto the router.
pub fn mount_all(
mut app: Router<Arc<crate::http::AppState>>,
routes: &[(String, ProxyRoute)],
) -> Router<Arc<crate::http::AppState>> {
for (path, route) in routes {
app = mount_one(app, path, route.clone());
}
app
}
fn mount_one(
app: Router<Arc<crate::http::AppState>>,
path: &str,
route: ProxyRoute,
) -> Router<Arc<crate::http::AppState>> {
let mut filter: Option<MethodFilter> = None;
for m in &route.methods {
let f = match m.as_str() {
"GET" => MethodFilter::GET,
"POST" => MethodFilter::POST,
"PUT" => MethodFilter::PUT,
"PATCH" => MethodFilter::PATCH,
"DELETE" => MethodFilter::DELETE,
"HEAD" => MethodFilter::HEAD,
"OPTIONS" => MethodFilter::OPTIONS,
_ => continue,
};
filter = Some(match filter {
None => f,
Some(acc) => acc.or(f),
});
}
let filter = filter.unwrap_or(MethodFilter::POST);
let r = route.clone();
let handler = MethodRouter::new().on(filter, move |State(_st): State<Arc<crate::http::AppState>>, req: Request| {
let r = r.clone();
async move { proxy_handler(r, req).await }
});
app.route(path, handler)
}
async fn proxy_handler(route: ProxyRoute, req: Request) -> Response {
let method = req.method().clone();
let uri = req.uri().clone();
let in_headers = req.headers().clone();
let body_bytes = match axum::body::to_bytes(req.into_body(), 64 * 1024 * 1024).await {
Ok(b) => b,
Err(e) => {
return (
StatusCode::BAD_REQUEST,
format!("read body: {e}"),
)
.into_response();
}
};
let target = match build_upstream_url(&route, uri.path(), uri.query()) {
Ok(u) => u,
Err(e) => return (StatusCode::BAD_GATEWAY, e).into_response(),
};
let client = match reqwest::Client::builder()
.timeout(Duration::from_millis(route.timeout_ms))
.build()
{
Ok(c) => c,
Err(e) => {
return (
StatusCode::INTERNAL_SERVER_ERROR,
format!("client: {e}"),
)
.into_response();
}
};
let mut builder = client.request(
reqwest::Method::from_bytes(method.as_str().as_bytes()).unwrap_or(reqwest::Method::POST),
&target,
);
// Forward selected inbound headers (skip hop-by-hop).
for (name, value) in in_headers.iter() {
let lname = name.as_str().to_ascii_lowercase();
if is_hop_by_hop(&lname) || lname == "host" {
continue;
}
if let Ok(v) = value.to_str() {
builder = builder.header(name.as_str(), v);
}
}
// Inject secrets from env (never from the browser).
for (env_name, header_name) in &route.headers_from_env {
if let Ok(val) = std::env::var(env_name) {
if !val.is_empty() {
// Common pattern: Authorization Bearer
let hdr_val = if header_name.eq_ignore_ascii_case("authorization")
&& !val.to_ascii_lowercase().starts_with("bearer ")
{
format!("Bearer {val}")
} else {
val
};
builder = builder.header(header_name.as_str(), hdr_val);
}
}
}
if !body_bytes.is_empty() {
builder = builder.body(body_bytes);
}
let upstream = match builder.send().await {
Ok(r) => r,
Err(e) => {
return (StatusCode::BAD_GATEWAY, format!("upstream: {e}")).into_response();
}
};
let status = StatusCode::from_u16(upstream.status().as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);
let mut out_headers = HeaderMap::new();
for (name, value) in upstream.headers().iter() {
let lname = name.as_str().to_ascii_lowercase();
if is_hop_by_hop(&lname) {
continue;
}
if let (Ok(n), Ok(v)) = (
HeaderName::from_bytes(name.as_str().as_bytes()),
HeaderValue::from_bytes(value.as_bytes()),
) {
out_headers.append(n, v);
}
}
// Ensure SSE content-type is preserved if present.
if !out_headers.contains_key(header::CONTENT_TYPE) {
if let Some(ct) = upstream.headers().get(reqwest::header::CONTENT_TYPE) {
if let Ok(v) = HeaderValue::from_bytes(ct.as_bytes()) {
out_headers.insert(header::CONTENT_TYPE, v);
}
}
}
if route.stream {
let stream = upstream.bytes_stream().map_err(|e| {
std::io::Error::new(std::io::ErrorKind::Other, e)
});
let body = Body::from_stream(stream);
let mut resp = Response::new(body);
*resp.status_mut() = status;
*resp.headers_mut() = out_headers;
resp
} else {
let bytes = match upstream.bytes().await {
Ok(b) => b,
Err(e) => {
return (StatusCode::BAD_GATEWAY, format!("upstream body: {e}")).into_response();
}
};
let mut resp = Response::new(Body::from(bytes));
*resp.status_mut() = status;
*resp.headers_mut() = out_headers;
resp
}
}
fn build_upstream_url(
route: &ProxyRoute,
req_path: &str,
query: Option<&str>,
) -> Result<String, String> {
let base = expand_env(&route.upstream);
if base.is_empty() {
return Err("proxy upstream empty after env expand".into());
}
// If upstream already looks like a full URL with path (contains scheme and
// more than origin), and strip_prefix empties the remainder, use base as-is.
let mut path = req_path.to_string();
let strip = route.strip_prefix.trim();
if !strip.is_empty() {
let strip_n = normalize_path(strip);
if path.starts_with(&strip_n) {
path = path[strip_n.len()..].to_string();
if path.is_empty() {
path = "/".into();
} else if !path.starts_with('/') {
path = format!("/{path}");
}
}
}
let url = if base.contains("://") {
let trimmed = base.trim_end_matches('/');
// If base has no path beyond origin, append path; if base includes a
// path suffix, append remaining path.
if let Some(idx) = trimmed.find("://") {
let after = &trimmed[idx + 3..];
if after.contains('/') {
// base includes path — append only if remaining path is not "/"
if path == "/" {
trimmed.to_string()
} else {
format!("{trimmed}{path}")
}
} else {
format!("{trimmed}{path}")
}
} else {
format!("{trimmed}{path}")
}
} else {
return Err(format!("proxy upstream must be absolute URL, got `{base}`"));
};
Ok(match query {
Some(q) if !q.is_empty() => {
if url.contains('?') {
format!("{url}&{q}")
} else {
format!("{url}?{q}")
}
}
_ => url,
})
}
fn is_hop_by_hop(name: &str) -> bool {
matches!(
name,
"connection"
| "keep-alive"
| "proxy-authenticate"
| "proxy-authorization"
| "te"
| "trailers"
| "transfer-encoding"
| "upgrade"
| "content-length"
)
}