Quellcode durchsuchen

Send content-type for static files

Thomas Dy vor 8 Jahren
Ursprung
Commit
7f6a2bf4cf
4 geänderte Dateien mit 16 neuen und 9 gelöschten Zeilen
  1. 1 0
      rust-server/Cargo.lock
  2. 1 0
      rust-server/Cargo.toml
  3. 12 9
      rust-server/src/handler.rs
  4. 2 0
      rust-server/src/main.rs

+ 1 - 0
rust-server/Cargo.lock

@@ -3,6 +3,7 @@ name = "cloudflare-webui"
 version = "0.1.0"
 dependencies = [
  "hyper 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
  "url 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
 ]

+ 1 - 0
rust-server/Cargo.toml

@@ -5,5 +5,6 @@ authors = ["Thomas Dy <thatsmydoing@gmail.com>"]
 
 [dependencies]
 hyper = "0.8.0"
+mime = "0.2.0"
 rustc-serialize = "0.3.0"
 url = "0.5.0"

+ 12 - 9
rust-server/src/handler.rs

@@ -1,4 +1,5 @@
 use config::Config;
+use mime::Mime;
 use std::io;
 use std::io::Read;
 use hyper::{Get, Post};
@@ -46,18 +47,21 @@ fn get_param<'a>(params: &'a Vec<(String, String)>, key: &str) -> &'a str {
     params.into_iter().find(|tuple| tuple.0 == key).map(|tuple| tuple.1.as_ref()).unwrap_or("")
 }
 
+fn serve(mut res: Response, content: &str, mime: Mime) {
+    res.headers_mut().set(ContentType(mime));
+    res.send(content.as_bytes()).unwrap();
+}
+
 impl Handler for SiteHandler {
     fn handle(&self, mut req: Request, mut res: Response) {
         let mut text = String::new();
         req.read_to_string(&mut text).ok().expect("Failed to get request body");
         match req.uri {
             AbsolutePath(ref path) => match (&req.method, &path[..]) {
-                (&Get, "/") => {
-                    res.send(INDEX_HTML.as_bytes()).unwrap();
-                },
-                (&Get, "/assets/bundle.js") => {
-                    res.send(BUNDLE_JS.as_bytes()).unwrap();
-                },
+                (&Get, "/") =>
+                    serve(res, INDEX_HTML, mime!(Text/Html; Charset=Utf8)),
+                (&Get, "/assets/bundle.js") =>
+                    serve(res, BUNDLE_JS, mime!(Application/Javascript; Charset=Utf8)),
                 (&Post, "/api") => {
                     let mut params = form_urlencoded::parse(text.as_bytes());
                     params.push(("email".to_string(), self.cfg.email.clone()));
@@ -111,9 +115,8 @@ impl Handler for SiteHandler {
                         res.send(b"Unauthorized").unwrap();
                     }
                 },
-                (&Get, _) => {
-                    res.send(INDEX_HTML.as_bytes()).unwrap();
-                },
+                (&Get, _) =>
+                    serve(res, INDEX_HTML, mime!(Text/Html; Charset=Utf8)),
                 _ => {
                     *res.status_mut() = NotFound;
                     res.send(b"Not Found").unwrap();

+ 2 - 0
rust-server/src/main.rs

@@ -1,3 +1,5 @@
+#[macro_use]
+extern crate mime;
 extern crate hyper;
 extern crate rustc_serialize;
 extern crate url;