Attempting a Bare Bones Static File Server in Rust 1.32

I was shopping around recently for a static file server setup recently. In my case, I wanted a server that

  • For debugging’s sake, could be run alongside a CLI
  • Could serve up webassembly (python3 -m http.server hasn’t added support yet, but there is an open ticket)
  • Can be run alongside a file watching thread
  • Is extremely light
  • Analogous to Go’s lovely built in static file server

Alas, it doesn’t seem like there is anything comparable to that last point. I ended up using hyper as my base server after considering rocket.rs.

# Cargo.toml
[dependencies]
futures = "0.1"
hyper = "0.12"
lazy_static = "1.2.0"
maplit = "1.0.1"

Hyper’s echo example got me 80% of the way to a (very) bare bones static file server. All that’s left to get the bare minimum going is mapping file pathing. There is a file reading block of code here that borrows from Steve Klabnik’s project (I found it here). The code linked above doesn’t guard against reading from / or arbitrary files in the directory being served.

Last but not least are MIME types being correctly assigned in the Content-Type header. In fact, there are no headers (other than date and transfer-encoding are set at all out of the box with hyper. And without the Content-Type header being set, Web Assembly being served up will fail to run in Chrome and Firefox. I used the lazy-static and maplit crates to build up the mappings. You can view the code here.

Remaining issues:

  • Chrome requires a cache free refresh to pull in updated files.
  • The lack(?) or incorrect headers seems to not allow mobile iOS to render a page served up from this server.