Rust helpers for serving HTTP GET and HEAD responses with hyper 0.13.x and tokio.
This crate supplies two ways to respond to HTTP GET and HEAD requests:
serve
function can be used to serve an Entity
, a trait representing
reusable, byte-rangeable HTTP entities. Entity
must 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.streaming_body
function can be used to add a body to an
otherwise-complete response. If a body is needed, it returns a
BodyWriter
(which implements std::io::Writer
). The caller should
produce the complete body or call BodyWriter::abort
, causing the HTTP
stream to terminate abruptly.They have pros and cons. This chart shows some of them:
serve | streaming_body | |
---|---|---|
automatic byte range serving | yes | no (always sends full body) |
backpressure | yes | no |
conditional GET | yes | unimplemented (always sends body) |
sends first byte before length known | no | yes |
automatic gzip content encoding | no | yes |
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:
include_bytes!
..mp4
files 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
See the AUTHORS file for details.
Your choice of MIT or Apache; see LICENSE-MIT.txt or LICENSE-APACHE, respectively.