From 2c7016561041a8622928bf7656f5a36ecf2d1740 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Thu, 31 Aug 2023 03:28:40 +0000 Subject: [PATCH] crates/examples/sel4cp/http-server: Move mime logic into own module --- .../http-server/pds/server/core/src/lib.rs | 1 + .../http-server/pds/server/core/src/mime.rs | 29 ++++++++++++++++++ .../http-server/pds/server/core/src/server.rs | 30 ++----------------- 3 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 crates/examples/sel4cp/http-server/pds/server/core/src/mime.rs diff --git a/crates/examples/sel4cp/http-server/pds/server/core/src/lib.rs b/crates/examples/sel4cp/http-server/pds/server/core/src/lib.rs index 240b04dba..4472305fd 100644 --- a/crates/examples/sel4cp/http-server/pds/server/core/src/lib.rs +++ b/crates/examples/sel4cp/http-server/pds/server/core/src/lib.rs @@ -21,6 +21,7 @@ use sel4_async_network_mbedtls::{ use sel4_async_single_threaded_executor::LocalSpawner; use sel4_async_timers::SharedTimers; +mod mime; mod server; use server::Server; diff --git a/crates/examples/sel4cp/http-server/pds/server/core/src/mime.rs b/crates/examples/sel4cp/http-server/pds/server/core/src/mime.rs new file mode 100644 index 000000000..ca5d07daf --- /dev/null +++ b/crates/examples/sel4cp/http-server/pds/server/core/src/mime.rs @@ -0,0 +1,29 @@ +use core::str::pattern::Pattern; + +pub(crate) fn content_type_from_name(name: &str) -> &'static str { + for (ext, ty) in MIME_ASSOCS { + if ext.is_suffix_of(name) { + return ty; + } + } + DEFAULT_MIME_TYPE +} + +const DEFAULT_MIME_TYPE: &str = "application/octet-stream"; + +const MIME_ASSOCS: &[(&str, &str)] = &[ + (".css", "text/css"), + (".html", "text/html; charset=utf-8"), + (".ico", "image/vnd.microsoft.icon"), + (".jpg", "image/jpeg"), + (".js", "text/javascript; charset=utf-8"), + (".mp4", "video/mp4"), + (".pdf", "application/pdf"), + (".png", "image/png"), + (".svg", "image/svg+xml"), + (".ttf", "font/ttf"), + (".txt", "text/plain; charset=utf-8"), + (".woff", "font/woff"), + (".woff2", "font/woff2"), + (".zip", "application/zip"), +]; diff --git a/crates/examples/sel4cp/http-server/pds/server/core/src/server.rs b/crates/examples/sel4cp/http-server/pds/server/core/src/server.rs index b5c6b9aa0..ee829b139 100644 --- a/crates/examples/sel4cp/http-server/pds/server/core/src/server.rs +++ b/crates/examples/sel4cp/http-server/pds/server/core/src/server.rs @@ -10,6 +10,8 @@ use sel4_async_block_io::BytesIO; use sel4_async_block_io_cpiofs as cpiofs; use sel4_async_network_mbedtls::mbedtls; +use crate::mime::content_type_from_name; + pub(crate) struct Server { index: cpiofs::Index, } @@ -257,31 +259,3 @@ fn should_keep_alive(req: &httparse::Request) -> bool { } default } - -fn content_type_from_name(name: &str) -> &'static str { - for (ext, ty) in MIME_ASSOCS { - if ext.is_suffix_of(name) { - return ty; - } - } - DEFAULT_MIME_TYPE -} - -const DEFAULT_MIME_TYPE: &str = "application/octet-stream"; - -const MIME_ASSOCS: &[(&str, &str)] = &[ - (".css", "text/css"), - (".html", "text/html; charset=utf-8"), - (".ico", "image/vnd.microsoft.icon"), - (".jpg", "image/jpeg"), - (".js", "text/javascript; charset=utf-8"), - (".mp4", "video/mp4"), - (".pdf", "application/pdf"), - (".png", "image/png"), - (".svg", "image/svg+xml"), - (".ttf", "font/ttf"), - (".txt", "text/plain; charset=utf-8"), - (".woff", "font/woff"), - (".woff2", "font/woff2"), - (".zip", "application/zip"), -];