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.serverhasn’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
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
maplit crates to build up the mappings. You can view the code here.
- 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.