summaryrefslogtreecommitdiff
path: root/src/handler
diff options
context:
space:
mode:
Diffstat (limited to 'src/handler')
-rw-r--r--src/handler/lua.rs75
-rw-r--r--src/handler/staticfile.rs75
2 files changed, 0 insertions, 150 deletions
diff --git a/src/handler/lua.rs b/src/handler/lua.rs
deleted file mode 100644
index 377111f..0000000
--- a/src/handler/lua.rs
+++ /dev/null
@@ -1,75 +0,0 @@
-use std::{collections::HashMap, str::FromStr};
-
-use mlua::{FromLua, Lua, Value};
-
-use crate::{
- handler::{self, Handle},
- request::Request,
- response::{Body, Response, Status},
-};
-
-#[derive(Debug, Clone)]
-pub struct LuaResponse {
- content: Option<Vec<u8>>,
- status: Status,
- headers: HashMap<String, Vec<u8>>,
-}
-
-impl Handle for LuaResponse {
- async fn handle(self, _: Request) -> Result<Response, handler::Error> {
- Ok(Response::builder()
- .status(self.status)
- .headers(self.headers)
- .body(match self.content {
- Some(content) => Body::Buffer(content),
- None => Body::Empty,
- }))
- }
-}
-
-impl FromLua for LuaResponse {
- fn from_lua(value: Value, _: &Lua) -> mlua::Result<Self> {
- match value {
- Value::Table(table) => {
- let content = match table.get("content")? {
- Value::String(string) => Some(string.as_bytes().to_vec()),
- _ => None,
- };
-
- let status = match table.get("status")? {
- Value::String(string) => Status::from_str(&string.to_str()?).map_err(|e| {
- mlua::Error::RuntimeError(format!(
- "failed to parse status from string: {e}"
- ))
- })?,
- Value::Integer(i) => Status::from_repr(i as usize).ok_or_else(|| {
- mlua::Error::runtime(format!("failed to parse status from integer: {i}"))
- })?,
- _ => return Err(mlua::Error::runtime("invalid type when reading status")),
- };
-
- let headers: HashMap<String, Vec<u8>> = match table.get::<Value>("headers")? {
- Value::Table(table) => {
- let mut hashmap = HashMap::<String, Vec<u8>>::new();
-
- for result in table.pairs() {
- let (key, value): (String, mlua::BString) = result?;
-
- hashmap.insert(key, value.to_vec());
- }
-
- hashmap
- }
- _ => HashMap::new(),
- };
-
- Ok(Self {
- content,
- status,
- headers,
- })
- }
- _ => Err(mlua::Error::runtime("expected table")),
- }
- }
-}
diff --git a/src/handler/staticfile.rs b/src/handler/staticfile.rs
deleted file mode 100644
index f208528..0000000
--- a/src/handler/staticfile.rs
+++ /dev/null
@@ -1,75 +0,0 @@
-use crate::{
- handler::{self, Handle},
- request::{Method, Request},
- response::{Body, Response, Status},
-};
-
-use std::{ffi::OsString, os::unix::ffi::OsStringExt, path::PathBuf};
-
-use mlua::{FromLua, Value};
-
-use tokio::{
- fs::File,
- io::{self},
-};
-
-#[derive(Debug, thiserror::Error)]
-pub enum Error {
- #[error("io error: {0}")]
- Io(#[from] io::Error),
-}
-
-#[derive(Debug, Clone)]
-pub struct StaticFile {
- path: PathBuf,
- mime: String,
-}
-
-impl Handle for StaticFile {
- async fn handle(self, request: Request) -> Result<Response, handler::Error> {
- match request.method() {
- Method::Get | Method::Head => match File::open(&self.path).await {
- Ok(file) => {
- let metadata = file.metadata().await.map_err(Error::Io)?;
-
- if metadata.is_file() {
- Ok(Response::builder()
- .status(Status::Ok)
- .headers([
- ("content-length", format!("{}", metadata.len())),
- ("content-type", self.mime),
- ])
- .body(match request.method() {
- Method::Get => Body::File(file),
- Method::Head => Body::Empty,
- }))
- } else {
- Ok(Response::builder()
- .status(Status::NotFound)
- .headers([("content-length", "0")])
- .body(Body::Empty))
- }
- }
- Err(e) if matches!(e.kind(), io::ErrorKind::NotFound) => Ok(Response::builder()
- .status(Status::NotFound)
- .headers([("content-length", "0")])
- .body(Body::Empty)),
- Err(e) => Err(Error::Io(e))?,
- },
- }
- }
-}
-
-impl FromLua for StaticFile {
- fn from_lua(value: mlua::Value, _: &mlua::Lua) -> mlua::Result<Self> {
- match value {
- Value::Table(table) => Ok(Self {
- path: PathBuf::from(OsString::from_vec(
- table.get::<mlua::String>("path")?.as_bytes().to_vec(),
- )),
- mime: table.get::<String>("mime")?,
- }),
- _ => Err(mlua::Error::runtime("expected table")),
- }
- }
-}