http-serve
Rust helpers for serving HTTP GET and HEAD responses with hyper 0.14.x and tokio.
This crate supplies two ways to respond to HTTP GET and HEAD requests:
- the
servefunction can be used to serve anEntity, a trait representing reusable, byte-rangeable HTTP entities.Entitymust be able to produce exactly the same data on every call, know its size in advance, and be able to produce portions of the data on demand. - the
streaming_bodyfunction can be used to add a body to an otherwise-complete response. If a body is needed, it returns aBodyWriter(which implementsstd::io::Writer). The caller should produce the complete body or callBodyWriter::abort, causing the HTTP stream to terminate abruptly.
Why two ways?
They have pros and cons. This chart shows some of them:
There's also a built-in Entity implementation, ChunkedReadFile. It serves
static files from the local filesystem, reading chunks in a separate thread
pool to avoid blocking the tokio reactor thread.
You're not limited to the built-in entity type(s), though. You could supply your own that do anything you desire:
- bytes built into the binary via
include_bytes!. - bytes retrieved from another HTTP server or network filesystem.
- memcached-based caching of another entity.
- anything else for which it's cheaper to compute the etag, size, and a byte
range than the entirety of the data. (See
moonfire-nvr's logic for
generating
.mp4files to represent arbitrary time ranges.)
http_serve::serve is similar to golang's
http.ServeContent. It was
extracted from moonfire-nvr's
.mp4 file serving.
Try the example:
$ cargo run --example serve_file /usr/share/dict/words
Authors
See the AUTHORS file for details.
License
Your choice of MIT or Apache; see LICENSE-MIT.txt or LICENSE-APACHE, respectively.