diff --git a/Cargo.lock b/Cargo.lock index 2ecd271..42b8ae2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -1436,16 +1436,17 @@ dependencies = [ [[package]] name = "mlua" -version = "0.9.9" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d111deb18a9c9bd33e1541309f4742523bfab01d276bfa9a27519f6de9c11dc7" +checksum = "9ea43c3ffac2d0798bd7128815212dd78c98316b299b7a902dabef13dc7b6b8d" dependencies = [ "bstr", + "either", "erased-serde", "futures-util", "mlua-sys", "num-traits", - "once_cell", + "parking_lot", "rustc-hash", "serde", "serde-value", @@ -1453,9 +1454,9 @@ dependencies = [ [[package]] name = "mlua-sys" -version = "0.6.2" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ab7a5b4756b8177a2dfa8e0bbcde63bd4000afbc4ab20cbb68d114a25470f29" +checksum = "63a11d485edf0f3f04a508615d36c7d50d299cf61a7ee6d3e2530651e0a31771" dependencies = [ "cc", "cfg-if 1.0.0", diff --git a/Cargo.toml b/Cargo.toml index 26b4a85..8e7d47a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ exe = "0.5" image = { version = "0.25", features = ["ico"] } urlencoding = "2.1" markdown = "1.0.0-alpha.16" -mlua = { version = "0.9", features = [ +mlua = { version = "0.10", features = [ "lua54", "vendored", "serialize", diff --git a/src/main.rs b/src/main.rs index fa88ebb..d1487df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -184,7 +184,7 @@ async fn file_handler(request: Request) -> impl IntoResponse { .try_call(request) .await .map(|e| e.into_response()) - .map_err(|_| StatusCode::NOT_FOUND); + .map_err(|_| (StatusCode::NOT_FOUND, "404: Not Found".to_string())); }; let lua = Lua::new(); @@ -202,7 +202,7 @@ async fn file_handler(request: Request) -> impl IntoResponse { }) .map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; let fetch_b64 = lua @@ -216,7 +216,7 @@ async fn file_handler(request: Request) -> impl IntoResponse { }) .map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; let dbg = lua @@ -226,27 +226,27 @@ async fn file_handler(request: Request) -> impl IntoResponse { }) .map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; globals.set("fetch", fetch_json).map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; globals.set("fetch_b64", fetch_b64).map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; globals.set("dbg", dbg).map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; let request_table = lua.create_table().map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; let headers_table = lua @@ -258,37 +258,37 @@ async fn file_handler(request: Request) -> impl IntoResponse { ) .map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; request_table.set("headers", headers_table).map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; request_table .set("uri", request.uri().to_string()) .map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; request_table .set("method", request.method().to_string()) .map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; // Inject functions to change headers and such globals.set("request", request_table).map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; let response_table = lua.create_table().map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; response_table @@ -296,83 +296,86 @@ async fn file_handler(request: Request) -> impl IntoResponse { "headers", lua.create_table().map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?, ) .map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; globals.set("response", response_table).map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; let mut path = base_dir.clone(); let uri = urlencoding::decode(&request.uri().path()[1..]).map_err(|e| { println!("{:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()) })?; path.push(&*uri); let full_path = fs::canonicalize(&path).map_err(|e| { println!("{:?}", e); - StatusCode::NOT_FOUND + (StatusCode::NOT_FOUND, "404: Not Found".to_string()) })?; let full_base_path = fs::canonicalize(base_dir).map_err(|e| { println!("{:?}", e); - StatusCode::NOT_FOUND + (StatusCode::NOT_FOUND, "404: Not Found".to_string()) })?; if !full_path.starts_with(full_base_path) { - return Err(StatusCode::BAD_REQUEST); + return Err((StatusCode::BAD_REQUEST, "400: Bad Request".to_string())); } let script = fs::read_to_string(full_path).map_err(|e| { eprintln!("Lua Read Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()) })?; let script = lua.load(script).eval::().map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; let result = lua.load("return response").eval::().map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; let script = if let LuaValue::String(script) = script { script.to_string_lossy().to_string() } else { serde_json::to_string(&script).map_err(|e| { - eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + eprintln!("Serde Error: {:?}", e); + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Serde error: {}", e), + ) })? }; let mut response = Html(script).into_response(); - if let Ok(headers) = result.get::<&str, Table>("headers") { + if let Ok(headers) = result.get::
("headers") { let pairs = headers.pairs::(); for pair in pairs { let (k, v) = pair.map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + to_lua_error(e) })?; response.headers_mut().insert( HeaderName::from_str(&k).map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()) })?, HeaderValue::from_str(&v).map_err(|e| { eprintln!("Lua Error: {:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()) })?, ); } @@ -381,29 +384,42 @@ async fn file_handler(request: Request) -> impl IntoResponse { Ok(response) } -async fn handler(request: Request) -> Result, StatusCode> { +fn to_lua_error(e: mlua::Error) -> (StatusCode, String) { + let e = e + .to_string() + .split(':') + .skip(4) + .collect::>() + .join(":"); + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("LuaError on line {}", e), + ) +} + +async fn handler(request: Request) -> Result, (StatusCode, String)> { let start = Instant::now(); let base_dir = BASE_DIR.get().unwrap(); let mut path = base_dir.clone(); let uri = urlencoding::decode(&request.uri().path()[1..]).map_err(|e| { println!("{:?}", e); - StatusCode::INTERNAL_SERVER_ERROR + (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()) })?; path.push(&*uri); let full_path = fs::canonicalize(&path).map_err(|e| { println!("{:?}", e); - StatusCode::NOT_FOUND + (StatusCode::NOT_FOUND, "404: Not Found".to_string()) })?; let full_base_path = fs::canonicalize(base_dir).map_err(|e| { println!("{:?}", e); - StatusCode::NOT_FOUND + (StatusCode::NOT_FOUND, "404: Not Found".to_string()) })?; if !full_path.starts_with(full_base_path) { - return Err(StatusCode::BAD_REQUEST); + return Err((StatusCode::BAD_REQUEST, "400: Bad Request".to_string())); } let body = match render_dir(&path, start).await { @@ -412,14 +428,14 @@ async fn handler(request: Request) -> Result, StatusCode> { let ioerror: Option<&std::io::Error> = e.downcast_ref(); if let Some(ioerror) = ioerror { if ioerror.kind() == ErrorKind::NotFound { - Err(StatusCode::NOT_FOUND) + Err((StatusCode::NOT_FOUND, "404: Not Found".to_string())) } else { println!("{:?}", ioerror); - Err(StatusCode::INTERNAL_SERVER_ERROR) + Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())) } } else { println!("{:?}", e); - Err(StatusCode::INTERNAL_SERVER_ERROR) + Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())) } } };