use std::convert::Infallible;
use http_body_util::{BodyExt as _, Empty, combinators::BoxBody};
use hyper::body::Bytes;
use hyper::{Method, Response, StatusCode, header};
use mlua::{AnyUserData, Function, Table as LuaTable, Value as LuaValue};
use futures_util::FutureExt as _;
use std::panic::AssertUnwindSafe;
use std::sync::Arc;
use tokio::sync::Semaphore;
use tracing::Instrument as _;
use crate::app::{self, AppState};
use crate::protect::Protection;
use crate::request::LuaRequest;
use crate::static_files::{self, StaticMount};
use crate::stream;
use nitr_core::{Error, ErrorInfo, Result, Runtime, RuntimeGuard, RuntimePool};
pub(crate) type HttpResponse = Response<BoxBody<Bytes, Infallible>>;
#[cfg(feature = "swagger")]
pub(crate) use error_page::escape_html;
enum Target {
Static(Result<HttpResponse>),
Chain {
chain: Function,
params: Vec<(String, String)>,
error_fn: Option<Function>,
input: Option<(Arc<crate::validation::InputSchemas>, AnyUserData)>,
rate: Option<Arc<crate::protect::RateLimiter>>,
},
NotFound,
Options(Vec<Method>),
MethodNotAllowed(Vec<Method>),
}
pub(crate) async fn handle(
pool: &RuntimePool,
req: LuaRequest,
streams: Arc<Semaphore>,
protection: Arc<Protection>,
) -> Result<HttpResponse> {
let id = req.id.clone();
let dev_mode = protection.dev_mode();
let head = RequestHead::of(&req);
let served = AssertUnwindSafe(handle_inner(pool, req, streams, protection.clone()))
.catch_unwind()
.await;
let mut resp = match served {
Ok(result) => result?,
Err(payload) => {
let err = Error::Panic(panic_message(&payload));
tracing::error!("{err}");
error_response(&err, dev_mode)?
}
};
tracing::Span::current().record("status", resp.status().as_u16());
if let Ok(value) = header::HeaderValue::from_str(&id) {
resp.headers_mut().insert("x-request-id", value);
}
if let Some(cors) = protection.cors() {
cors.apply(head.origin.as_ref(), resp.headers_mut());
}
protection.apply_headers(resp.headers_mut());
let compression = protection.compression();
if compression.negotiates() {
let encoding = compression.negotiate(head.accept_encoding.as_ref());
resp = compression.apply(resp, encoding);
}
if head.method == Method::HEAD {
resp = strip_body(resp);
}
Ok(resp)
}
struct RequestHead {
method: Method,
accept_encoding: Option<header::HeaderValue>,
origin: Option<header::HeaderValue>,
}
impl RequestHead {
fn of(req: &LuaRequest) -> Self {
let headers = req.req.headers();
Self {
method: req.req.method().clone(),
accept_encoding: headers.get(header::ACCEPT_ENCODING).cloned(),
origin: headers.get(header::ORIGIN).cloned(),
}
}
}
fn strip_body(resp: HttpResponse) -> HttpResponse {
use hyper::body::Body as _;
let (mut parts, body) = resp.into_parts();
if !parts.headers.contains_key(header::CONTENT_LENGTH)
&& let Some(len) = body.size_hint().exact()
{
parts.headers.insert(header::CONTENT_LENGTH, len.into());
}
HttpResponse::from_parts(parts, Empty::<Bytes>::new().boxed())
}
fn panic_message(payload: &Box<dyn std::any::Any + Send>) -> String {
if let Some(s) = payload.downcast_ref::<&str>() {
return (*s).to_string();
}
if let Some(s) = payload.downcast_ref::<String>() {
return s.clone();
}
"non-string panic payload".to_string()
}
async fn handle_inner(
pool: &RuntimePool,
mut req: LuaRequest,
streams: Arc<Semaphore>,
protection: Arc<Protection>,
) -> Result<HttpResponse> {
if let Some(rejection) = protection.check(&req) {
return rejection;
}
if let Some(cors) = protection.cors()
&& let Some(resp) = cors.preflight(&req.req)
{
return resp;
}
#[cfg(feature = "openapi")]
if let Some(docs) = protection.docs()
&& let Some(resp) = docs.serve(&req)
{
return resp;
}
let guards = req.guard_body(protection.max_body_bytes(), protection.body_read_timeout());
req.limits = protection.form_limits();
let wants_json = wants_json(req.req.headers());
let routed = match pool.companion::<app::Routing>() {
Some(routing) => match routing.lookup(req.req.method(), req.req.uri().path()) {
app::Lookup::Route { index, params } => {
if let Some(limit) = routing.limits.get(index).and_then(Option::as_ref)
&& let Err(retry_after) = limit.check(&req)
{
tracing::debug!(peer = %req.peer_addr, "request rate limited by its route");
return rate_limited(retry_after, wants_json);
}
Some((routing, index, params))
}
app::Lookup::Options(allowed) => return options_response(&allowed),
app::Lookup::MethodNotAllowed(allowed) => {
let fallback = || method_not_allowed(&allowed, wants_json);
return static_file_or(&routing.statics, &req, protection.compression(), fallback)
.await;
}
app::Lookup::NotFound => {
let fallback = || not_found(wants_json);
return static_or(&routing.statics, &req, protection.compression(), fallback).await;
}
},
None => None,
};
let routed_in_rust = routed.is_some();
let Some(mut rt) = pool.get_timeout(protection.pool_wait()).await else {
tracing::warn!("request shed: no Lua state available within the pool wait budget");
return rejection(
StatusCode::SERVICE_UNAVAILABLE,
"SERVICE_UNAVAILABLE",
Some(1),
wants_json,
);
};
let dev_mode = rt.dev_mode();
nitr_std::reset_outbound_budget(rt.lua());
nitr_std::set_trace_context(rt.lua(), &req.id);
let target = match routed {
Some((routing, index, params)) => app::routed_chain(rt.lua(), routing, index, |chain| {
chain_target(chain, params)
}),
None => Ok(None),
};
let target = match target {
Ok(Some(target)) => Ok(target),
Ok(None) => resolve(&rt, &req, protection.compression()).await,
Err(err) => Err(err),
};
let target = match target {
Ok(target) => target,
Err(err) => {
tracing::error!("failed to resolve the request route: {err}");
return error_response(&err, dev_mode);
}
};
match target {
Target::Static(resp) => resp,
Target::NotFound => not_found(wants_json),
Target::Options(allowed) => options_response(&allowed),
Target::MethodNotAllowed(allowed) => method_not_allowed(&allowed, wants_json),
Target::Chain {
chain,
params,
error_fn,
input,
rate,
} => {
if !routed_in_rust
&& let Some(limit) = rate
&& let Err(retry_after) = limit.check(&req)
{
tracing::debug!(peer = %req.peer_addr, "request rate limited by its route");
return rate_limited(retry_after, wants_json);
}
req.params = params;
let wants_html = dev_mode && accepts_html(req.req.headers());
let content = match &input {
Some((schemas, _)) => match schemas.negotiate(req.req.headers()) {
Ok(content) => content,
Err(accepted) => {
tracing::debug!("request rejected: unsupported media type");
return unsupported_media_type(&accepted);
}
},
None => None,
};
#[cfg(feature = "multipart")]
let _spool = match (&input, &req.limits.upload_root) {
(Some((schemas, _)), Some(root)) if schemas.needs_spool(content) => {
let spool = crate::validation::spool::SpoolDir::new(root);
req.spool_dir = Some(spool.path().to_path_buf());
Some(spool)
}
_ => None,
};
#[cfg(not(feature = "multipart"))]
let _ = content;
let req_ud = rt.lua().create_userdata(req)?;
let span = tracing::debug_span!("lua_handler", elapsed_ms = tracing::field::Empty);
let started = (!span.is_disabled()).then(std::time::Instant::now);
let called = rt
.call_function::<LuaTable>(chain, &req_ud)
.instrument(span.clone())
.await;
if let Some(started) = started {
span.record("elapsed_ms", started.elapsed().as_millis() as u64);
}
let err = match called {
Ok(lua_resp) => {
return finish(rt, lua_resp, &streams, dev_mode, &req_ud, wants_json);
}
Err(err) => err,
};
if guards.oversized() {
tracing::debug!("request rejected: body exceeded max_body_bytes");
discard_body(&req_ud);
return rejection(
StatusCode::PAYLOAD_TOO_LARGE,
"PAYLOAD_TOO_LARGE",
None,
wants_json,
);
}
if guards.stalled() {
tracing::warn!("request rejected: body read stalled beyond [limits] body_read_ms");
discard_body(&req_ud);
let mut resp = rejection(
StatusCode::REQUEST_TIMEOUT,
"REQUEST_TIMEOUT",
None,
wants_json,
)?;
resp.headers_mut().insert(
header::CONNECTION,
header::HeaderValue::from_static("close"),
);
return Ok(resp);
}
#[cfg(feature = "multipart")]
if let Error::Lua(lua_err) = &err
&& let Some(limit) = lua_err.downcast_ref::<crate::multipart::LimitExceeded>()
{
tracing::debug!("request rejected: {limit}");
discard_body(&req_ud);
return rejection(
StatusCode::PAYLOAD_TOO_LARGE,
"PAYLOAD_TOO_LARGE",
None,
wants_json,
);
}
let info = ErrorInfo::from_error(&err);
tracing::error!(
error.kind = info.kind,
error.source = info.source.as_deref(),
error.line = info.line,
error.module = info.module.as_deref(),
"handler failed: {}",
info.message
);
if dev_mode && let Some(traceback) = info.traceback.as_deref() {
tracing::debug!("stack traceback:{traceback}");
}
let mut handled = None;
if let Some(error_fn) = error_fn {
let err_value = nitr_std::error_lua_value(rt.lua(), &info)?;
match rt
.call_function::<LuaTable>(error_fn, (err_value, &req_ud))
.await
{
Ok(lua_resp) => match to_response(lua_resp) {
Ok(resp) => handled = Some(resp),
Err(err) => tracing::error!("invalid error-handler response: {err}"),
},
Err(err) => tracing::error!("the app error handler failed: {err}"),
}
}
discard_body(&req_ud);
match handled {
Some(resp) => Ok(with_failure(resp, info, true)),
None => {
let script = dev_mode.then(|| app::script_path(rt.lua())).flatten();
error_page_with_source(&info, dev_mode, wants_html, script.as_deref())
.map(|resp| with_failure(resp, info, false))
}
}
}
}
}
fn unsupported_media_type(accepted: &str) -> Result<HttpResponse> {
let body = serde_json::json!({
"code": "UNSUPPORTED_MEDIA_TYPE",
"message": format!("unsupported media type; accepted: {accepted}"),
"accepted": accepted.split(", ").collect::<Vec<_>>(),
});
let mut resp = json_response(StatusCode::UNSUPPORTED_MEDIA_TYPE, &body)?;
if let Ok(value) = header::HeaderValue::from_str(accepted) {
resp.headers_mut().insert(header::ACCEPT, value);
}
Ok(resp)
}
fn json_response(status: StatusCode, body: &serde_json::Value) -> Result<HttpResponse> {
use http_body_util::Full;
let bytes = serde_json::to_vec(body).map_err(|err| Error::Script(err.to_string()))?;
Ok(Response::builder()
.status(status)
.header(header::CONTENT_TYPE, "application/json")
.body(Full::new(Bytes::from(bytes)).boxed())?)
}
fn discard_body(req_ud: &AnyUserData) {
match req_ud.borrow_mut::<LuaRequest>() {
Ok(mut req) => req.discard_body(),
Err(err) => tracing::debug!("could not release the request body: {err}"),
}
}
fn finish(
rt: RuntimeGuard,
lua_resp: LuaTable,
streams: &Arc<Semaphore>,
dev_mode: bool,
req_ud: &AnyUserData,
wants_json: bool,
) -> Result<HttpResponse> {
match lua_resp.raw_get::<LuaValue>("body") {
Ok(LuaValue::Function(body_fn)) => {
let permit = match streams.clone().try_acquire_owned() {
Ok(permit) => permit,
Err(_) => {
tracing::warn!("streaming response rejected: max_streams reached");
return rejection(
StatusCode::SERVICE_UNAVAILABLE,
"SERVICE_UNAVAILABLE",
None,
wants_json,
);
}
};
match stream::stream_response(rt, &lua_resp, body_fn, permit) {
Ok(resp) => Ok(resp),
Err(err) => {
tracing::error!("invalid streaming response: {err}");
error_response(&err, dev_mode)
}
}
}
Ok(_) => {
discard_body(req_ud);
match to_response(lua_resp) {
Ok(resp) => Ok(resp),
Err(err) => {
tracing::error!("invalid handler response: {err}");
error_response(&err, dev_mode)
}
}
}
Err(err) => {
discard_body(req_ud);
let err = Error::from(err);
tracing::error!("invalid handler response: {err}");
error_response(&err, dev_mode)
}
}
}
fn not_found(wants_json: bool) -> Result<HttpResponse> {
rejection(StatusCode::NOT_FOUND, "NOT_FOUND", None, wants_json)
}
fn method_not_allowed(allowed: &[Method], wants_json: bool) -> Result<HttpResponse> {
let mut resp = rejection(
StatusCode::METHOD_NOT_ALLOWED,
"METHOD_NOT_ALLOWED",
None,
wants_json,
)?;
resp.headers_mut()
.insert(header::ALLOW, crate::cors::allow_header(allowed));
Ok(resp)
}
fn options_response(allowed: &[Method]) -> Result<HttpResponse> {
let mut resp = empty_response(StatusCode::NO_CONTENT)?;
resp.headers_mut()
.insert(header::ALLOW, crate::cors::allow_header(allowed));
Ok(resp)
}
async fn static_or(
statics: &Arc<Vec<StaticMount>>,
req: &LuaRequest,
compression: &crate::compress::Compression,
fallback: impl FnOnce() -> Result<HttpResponse>,
) -> Result<HttpResponse> {
match static_files::try_serve(statics, req, compression, accepts_html(req.req.headers())).await
{
Some(resp) => resp,
None => fallback(),
}
}
async fn static_file_or(
statics: &Arc<Vec<StaticMount>>,
req: &LuaRequest,
compression: &crate::compress::Compression,
fallback: impl FnOnce() -> Result<HttpResponse>,
) -> Result<HttpResponse> {
match static_files::try_serve(statics, req, compression, false).await {
Some(resp) => resp,
None => fallback(),
}
}
fn chain_target(chain: &app::Chain, params: Vec<(String, String)>) -> Target {
Target::Chain {
chain: chain.fns.clone(),
params,
error_fn: chain.error_fn.clone(),
input: chain.input.clone(),
rate: chain.rate.clone(),
}
}
async fn resolve(
rt: &Runtime,
req: &LuaRequest,
compression: &crate::compress::Compression,
) -> Result<Target> {
let ud = app::state(rt.lua())?;
let (target, statics): (Target, Arc<Vec<StaticMount>>) = {
let state = ud.borrow::<AppState>()?;
let app = &state.dispatch.0;
let target = match app.lookup(req.req.method(), req.req.uri().path()) {
app::Lookup::Route { index, params } => chain_target(&app.chains[index], params),
app::Lookup::Options(allowed) => Target::Options(allowed),
app::Lookup::MethodNotAllowed(allowed) => Target::MethodNotAllowed(allowed),
app::Lookup::NotFound => Target::NotFound,
};
(target, state.statics.clone())
};
let spa_fallback = match target {
Target::NotFound => accepts_html(req.req.headers()),
Target::MethodNotAllowed(_) => false,
other => return Ok(other),
};
Ok(
match static_files::try_serve(&statics, req, compression, spa_fallback).await {
Some(resp) => Target::Static(resp),
None => target,
},
)
}
mod error_page;
mod respond;
#[cfg(test)]
mod tests;
use error_page::{accepts_html, error_page_with_source, error_response, with_failure};
use respond::to_response;
pub(crate) use respond::{
build_response, empty_response, plain_response, rate_limited, rejection, wants_json,
};