faithea 0.1.9

A HTTP framework
Documentation
use std::{ffi::OsStr, path::Path};

use bytes::Bytes;
use http::{
    HeaderValue,
    header::{CONTENT_LENGTH, CONTENT_TYPE},
};
use serde::Serialize;

use crate::{
    data::Json,
    handler::types::HttpHandlerError,
    map_fu,
    response::{HttpResponseModifier, HttpResponseModifierFuture, ResponseBody},
};
impl<T: Serialize> TryFrom<&Json<T>> for ResponseBody {
    type Error = HttpHandlerError;
    fn try_from(value: &Json<T>) -> Result<Self, Self::Error> {
        let res = serde_json::to_vec(value)?;
        Ok(Self::Simple(Bytes::from(res)))
    }
}
impl<T: Serialize> TryFrom<&mut Json<T>> for ResponseBody {
    type Error = HttpHandlerError;
    fn try_from(value: &mut Json<T>) -> Result<Self, Self::Error> {
        let im_ref = &(*value);
        im_ref.try_into()
    }
}

impl<T: Serialize + Send + Sync> HttpResponseModifier for Json<T> {
    fn modify<'a>(
        &'a mut self,
        res: &'a mut crate::response::HttpResponse,
    ) -> HttpResponseModifierFuture<'a> {
        Box::pin(async move {
            use ResponseBody::*;
            res.add_header(
                CONTENT_TYPE,
                HeaderValue::from_maybe_shared("application/json").map_err(map_fu!())?,
            );
            let body = self.try_into()?;
            if let Simple(ref b) = body {
                res.add_header(
                    CONTENT_LENGTH,
                    HeaderValue::from_maybe_shared(b.len().to_string())?,
                );
            } else {
                return Err(HttpHandlerError::after_handler_incompatible_body_type());
            }
            res.set_body(body);
            Ok(())
        })
    }
}

impl HttpResponseModifier for &str {
    fn modify<'a>(
        &'a mut self,
        res: &'a mut crate::response::HttpResponse,
    ) -> HttpResponseModifierFuture<'a> {
        Box::pin(async move {
            // res.add_header(("content-type".to_string(), "text/plain".to_string()));
            res.add_header(
                CONTENT_TYPE,
                HeaderValue::from_maybe_shared("text/plain").map_err(map_fu!())?,
            );
            // res.add_header(("content-length".to_string(), self.len().to_string()));
            res.add_header(
                CONTENT_LENGTH,
                HeaderValue::from_maybe_shared(self.len().to_string()).map_err(map_fu!())?,
            );
            let b: Bytes = Bytes::from_iter(self.as_bytes().iter().copied());
            res.set_body(ResponseBody::Simple(b));
            Ok(())
        })
    }
}
impl HttpResponseModifier for String {
    fn modify<'a>(
        &'a mut self,
        res: &'a mut crate::response::HttpResponse,
    ) -> HttpResponseModifierFuture<'a> {
        Box::pin(async move {
            // res.add_header(("content-type".to_string(), "text/plain".to_string()));
            res.add_header(
                CONTENT_TYPE,
                HeaderValue::from_maybe_shared("text/plain").map_err(map_fu!())?,
            );
            // res.add_header(("content-length".to_string(), self.len().to_string()));
            res.add_header(
                CONTENT_LENGTH,
                HeaderValue::from_maybe_shared(self.len().to_string()).map_err(map_fu!())?,
            );
            let b: Bytes = Bytes::from_iter(self.as_bytes().iter().copied());
            res.set_body(ResponseBody::Simple(b));
            Ok(())
        })
    }
}

pub struct StaticFile<T: AsRef<Path>>(pub T);

impl<T: AsRef<Path> + Send + Sync> HttpResponseModifier for StaticFile<T> {
    fn modify<'a>(
        &'a mut self,
        res: &'a mut crate::response::HttpResponse,
    ) -> HttpResponseModifierFuture<'a> {
        Box::pin(async move {
            let f = tokio::fs::File::open(self.0.as_ref()).await?;
            let meta = f.metadata().await?;
            if !meta.is_file() {
                let err_msg = format!("{:?} is not a File!!", self.0.as_ref());
                return Err(HttpHandlerError::after_handler_file_not_exists(err_msg));
            }

            let len = meta.len();
            res.add_header(
                CONTENT_LENGTH,
                HeaderValue::from_maybe_shared(len.to_string()).map_err(map_fu!())?,
            );
            res.add_header(
                CONTENT_TYPE,
                HeaderValue::from_static(mime_type(self.0.as_ref())),
            );
            res.set_body(ResponseBody::File(f));
            Ok(())
        })
    }
}
fn mime_type(path: &Path) -> &'static str {
    match path.extension() {
        Some(e) => get_mime_type_by_extention(e),
        None => "application/octet-stream",
    }
}
fn get_mime_type_by_extention(e: &OsStr) -> &'static str {
    if let Some(e) = e.to_str() {
        match e {
            "html" => "text/html",
            "htm" => "text/html",
            "css" => "text/css",
            "js" => "text/javascript",
            "json" => "application/json",
            "map" => "application/json",
            // image
            "png" => "image/png",
            "jpg" => "image/jpeg",
            "jpeg" => "image/jpeg",
            "gif" => "image/gif",
            "webp" => "image/webp",
            "svg" => "image/svg+xml",
            "ico" => "image/x-icon",
            //file
            "txt" => "text/plain",
            "md" => "text/markdown",
            "csv" => "text/csv",
            "xml" => "application/xml",
            "pdf" => "application/pdf",
            // video
            "mp3" => "audio/mpeg",
            "wav" => "audio/wav",
            "ogg" => "audio/ogg",
            "mp4" => "video/mp4",
            "webm" => "video/webm",
            // zips
            "zip" => "application/zip",
            "tar" => "application/x-tar",
            "gz" => "application/gzip",
            "7z" => "application/x-7z-compressed",
            // font
            "woff" => "font/woff",
            "woff2" => "font/woff2",
            "ttf" => "font/ttf",
            "otf" => "font/otf",
            _ => "application/octet-stream",
        }
    } else {
        "application/octet-stream"
    }
}