Skip to content

Latest commit

 

History

History
88 lines (66 loc) · 3.36 KB

README.md

File metadata and controls

88 lines (66 loc) · 3.36 KB

httpserv

A tiny, zero-dependency HTTP fileserver, meant for local development of HTML and embedding low-stakes webservers in things. It's not the fastest, not the most secure, and not the most featureful, but it's so little that it can fit anywhere and starts instantly -- making it perfect to host a local webserver for testing your static site, or embedding into something that makes static sites.

Specifically, all it does is:

  • Parse the URL to find the local filepath
  • Make sure that URL doesn't contain ..s
  • If that filepath points to a directory, add /index.html
  • Use the extension to figure out the Content-Type
  • Send the file back

That simplicity is what makes it so quick to cargo install, launch, and respond.

There are a few planned features, but no timeline to make them:

  • Some multithreading, just to make sure large files don't take too long
  • A config file for easier reuse and arguments
  • More convenient embedding

Planned non-features include:

  • Any dependencies besides the stdlib
  • Maximizing response speed where it would cost setup speed
  • Anything that gives httpserv a noticeable startup or response delay
  • Anything specifically for better production suitability

Install

cargo install httpserv

That's it. Assuming the Cargo bin directory is on your path, you can now call httpserv from your command line. For directions on installing Cargo, please see rustup.rs.

On WSL, you may need to call cargo.exe and httpserv.exe instead, depending on if you've got Rust installed on the Windows or WSL side of things.

Usage

All arguments are optional -- if you want to serve your current directory on localhost:8080 with the default mappings, you can just type httpserv and hit enter. Otherwise:

httpserv [directory] [listen] [mappings...]
  • directory: Where to look for files to serve. Defaults to .
  • listen: The host/port to listen on, as expected by the FromStr impl for SocketAddr. Defaults to localhost:8080
  • mappings...: Any additional mappings from file extension to MIME types, besides the defaults. Anything specified here which matches the same extension as a default will override the default MIME type. The format is extension=MIME, with no leading . on the extension.

Known issues

Because this is meant for local development and not production use, there are some issues which I haven't bothered to fix. In general, the reason why boils down to httpserv being meant to aid local development. If you're using it in any situation where you can't restart it at will, you're doing it very, very wrong.

  • Requests with absurdly long URLs or absurd numbers of headers can cause the process to hang or crash
  • Requests are processed serially, so if enough are received at once, some may not get responses for a while; however, the time to parse any single request is so fast that it's not an issue normally
  • If a file is changed between when the HTTP headers are sent and when the rest of the body is sent, the reported Content-Length will be incorrect, so the browser may truncate the content or display an error.
  • A malicious actor could send a partial request (e.g. never ending the header) and lock up the server.