docs(examples): Update send_file example to use tokio-fs

This commit is contained in:
Aaron Riekenberg
2018-06-04 11:59:42 -05:00
committed by Sean McArthur
parent 14d9246de2
commit 7eca445ff9
3 changed files with 26 additions and 89 deletions

View File

@@ -43,6 +43,7 @@ num_cpus = "1.0"
pretty_env_logger = "0.2.0" pretty_env_logger = "0.2.0"
spmc = "0.2" spmc = "0.2"
url = "1.0" url = "1.0"
tokio-fs = "0.1"
tokio-mockstream = "1.1.0" tokio-mockstream = "1.1.0"
[features] [features]

View File

@@ -14,6 +14,6 @@ Run examples with `cargo run --example example_name`.
* [`params`](params.rs) - A webserver that accept a form, with a name and a number, checks the parameters are presents and validates the input. * [`params`](params.rs) - A webserver that accept a form, with a name and a number, checks the parameters are presents and validates the input.
* [`send_file`](send_file.rs) - A server that sends back content of files either simply or streaming the response. * [`send_file`](send_file.rs) - A server that sends back content of files using tokio_fs to read the files asynchronously.
* [`web_api`](web_api.rs) - A server consisting in a service that returns incoming POST request's content in the response in uppercase and a service that call that call the first service and includes the first service response in its own response. * [`web_api`](web_api.rs) - A server consisting in a service that returns incoming POST request's content in the response in uppercase and a service that call that call the first service and includes the first service response in its own response.

View File

@@ -2,16 +2,15 @@
extern crate futures; extern crate futures;
extern crate hyper; extern crate hyper;
extern crate pretty_env_logger; extern crate pretty_env_logger;
extern crate tokio_fs;
extern crate tokio_io;
use futures::{future, Future}; use futures::{future, Future};
use futures::sync::oneshot;
use hyper::{Body, Method, Request, Response, Server, StatusCode}; use hyper::{Body, Method, Request, Response, Server, StatusCode};
use hyper::service::service_fn; use hyper::service::service_fn;
use std::fs::File; use std::io;
use std::io::{self, copy/*, Read*/};
use std::thread;
static NOTFOUND: &[u8] = b"Not Found"; static NOTFOUND: &[u8] = b"Not Found";
static INDEX: &str = "examples/send_file_index.html"; static INDEX: &str = "examples/send_file_index.html";
@@ -35,58 +34,9 @@ type ResponseFuture = Box<Future<Item=Response<Body>, Error=io::Error> + Send>;
fn response_examples(req: Request<Body>) -> ResponseFuture { fn response_examples(req: Request<Body>) -> ResponseFuture {
match (req.method(), req.uri().path()) { match (req.method(), req.uri().path()) {
(&Method::GET, "/") | (&Method::GET, "/index.html") => { (&Method::GET, "/") | (&Method::GET, "/index.html") | (&Method::GET, "/big_file.html") => {
simple_file_send(INDEX) simple_file_send(INDEX)
}, },
(&Method::GET, "/big_file.html") => {
// Stream a large file in chunks. This requires a
// little more overhead with two channels, (one for
// the response future, and a second for the response
// body), but can handle arbitrarily large files.
//
// We use an artificially small buffer, since we have
// a small test file.
let (tx, rx) = oneshot::channel();
thread::spawn(move || {
let _file = match File::open(INDEX) {
Ok(f) => f,
Err(_) => {
tx.send(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(NOTFOUND.into())
.unwrap())
.expect("Send error on open");
return;
},
};
let (_tx_body, rx_body) = Body::channel();
let res = Response::new(rx_body.into());
tx.send(res).expect("Send error on successful file read");
/* TODO: fix once we have futures 0.2 Sink working
let mut buf = [0u8; 16];
loop {
match file.read(&mut buf) {
Ok(n) => {
if n == 0 {
// eof
tx_body.close().expect("panic closing");
break;
} else {
let chunk: Chunk = buf[0..n].to_vec().into();
match tx_body.send_data(chunk).wait() {
Ok(t) => { tx_body = t; },
Err(_) => { break; }
};
}
},
Err(_) => { break; }
}
}
*/
});
Box::new(rx.map_err(|e| io::Error::new(io::ErrorKind::Other, e)))
},
(&Method::GET, "/no_file.html") => { (&Method::GET, "/no_file.html") => {
// Test what happens when file cannot be be found // Test what happens when file cannot be be found
simple_file_send("this_file_should_not_exist.html") simple_file_send("this_file_should_not_exist.html")
@@ -102,42 +52,28 @@ fn response_examples(req: Request<Body>) -> ResponseFuture {
} }
fn simple_file_send(f: &str) -> ResponseFuture { fn simple_file_send(f: &str) -> ResponseFuture {
// Serve a file by reading it entirely into memory. As a result // Serve a file by asynchronously reading it entirely into memory.
// this is limited to serving small files, but it is somewhat // Uses tokio_fs to open file asynchronously, then tokio_io to read into
// simpler with a little less overhead. // memory asynchronously.
//
// On channel errors, we panic with the expect method. The thread
// ends at that point in any case.
let filename = f.to_string(); // we need to copy for lifetime issues let filename = f.to_string(); // we need to copy for lifetime issues
let (tx, rx) = oneshot::channel(); Box::new(tokio_fs::file::File::open(filename)
thread::spawn(move || { .and_then(|file| {
let mut file = match File::open(filename) { let buf: Vec<u8> = Vec::new();
Ok(f) => f, tokio_io::io::read_to_end(file, buf)
Err(_) => { .and_then(|item| {
tx.send(Response::builder() Ok(Response::new(item.1.into()))
.status(StatusCode::NOT_FOUND) })
.body(NOTFOUND.into()) .or_else(|_| {
.unwrap()) Ok(Response::builder()
.expect("Send error on open");
return;
},
};
let mut buf: Vec<u8> = Vec::new();
match copy(&mut file, &mut buf) {
Ok(_) => {
let res = Response::new(buf.into());
tx.send(res).expect("Send error on successful file read");
},
Err(_) => {
tx.send(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR) .status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::empty()) .body(Body::empty())
.unwrap()) .unwrap())
.expect("Send error on error reading file"); })
}, })
}; .or_else(|_| {
}); Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
Box::new(rx.map_err(|e| io::Error::new(io::ErrorKind::Other, e))) .body(NOTFOUND.into())
.unwrap())
}))
} }