Skip to content

Commit

Permalink
crates/examples/sel4cp/http-server: Move mime logic into own module
Browse files Browse the repository at this point in the history
  • Loading branch information
nspin committed Aug 31, 2023
1 parent bda21f0 commit 2c70165
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions crates/examples/sel4cp/http-server/pds/server/core/src/mime.rs
Original file line number Diff line number Diff line change
@@ -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"),
];
30 changes: 2 additions & 28 deletions crates/examples/sel4cp/http-server/pds/server/core/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
index: cpiofs::Index<T>,
}
Expand Down Expand Up @@ -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"),
];

0 comments on commit 2c70165

Please sign in to comment.