feat(example): read file by chunks in send_file example (#2193)

This commit is contained in:
Nikolai Kuklin
2020-04-25 01:02:58 +05:00
committed by GitHub
parent e08a271eb9
commit 203621e3be
4 changed files with 11 additions and 26 deletions

View File

@@ -51,6 +51,7 @@ serde_derive = "1.0"
serde_json = "1.0"
tokio = { version = "0.2.2", features = ["fs", "macros", "io-std", "rt-util", "sync", "time", "test-util"] }
tokio-test = "0.2"
tokio-util = { version = "0.3", features = ["codec"] }
tower-util = "0.3"
url = "1.0"

View File

@@ -39,7 +39,7 @@ pretty_env_logger = "0.4"
* [`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 using tokio_fs to read the files asynchronously.
* [`send_file`](send_file.rs) - A server that sends back content of files using tokio-util to read the files asynchronously.
* [`single_threaded`](single_threaded.rs) - A server only running on 1 thread, so it can make use of `!Send` app state (like an `Rc` counter).

View File

@@ -1,13 +1,13 @@
#![deny(warnings)]
use tokio::fs::File;
use tokio::io::AsyncReadExt;
use tokio_util::codec::{BytesCodec, FramedRead};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, Result, Server, StatusCode};
static INDEX: &str = "examples/send_file_index.html";
static INTERNAL_SERVER_ERROR: &[u8] = b"Internal Server Error";
static NOTFOUND: &[u8] = b"Not Found";
#[tokio::main]
@@ -30,9 +30,7 @@ async fn main() {
async fn response_examples(req: Request<Body>) -> Result<Response<Body>> {
match (req.method(), req.uri().path()) {
(&Method::GET, "/") | (&Method::GET, "/index.html") | (&Method::GET, "/big_file.html") => {
simple_file_send(INDEX).await
}
(&Method::GET, "/") | (&Method::GET, "/index.html") => simple_file_send(INDEX).await,
(&Method::GET, "/no_file.html") => {
// Test what happens when file cannot be be found
simple_file_send("this_file_should_not_exist.html").await
@@ -49,26 +47,13 @@ fn not_found() -> Response<Body> {
.unwrap()
}
/// HTTP status code 500
fn internal_server_error() -> Response<Body> {
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(INTERNAL_SERVER_ERROR.into())
.unwrap()
}
async fn simple_file_send(filename: &str) -> Result<Response<Body>> {
// Serve a file by asynchronously reading it entirely into memory.
// Uses tokio_fs to open file asynchronously, then tokio::io::AsyncReadExt
// to read into memory asynchronously.
// Serve a file by asynchronously reading it by chunks using tokio-util crate.
if let Ok(mut file) = File::open(filename).await {
let mut buf = Vec::new();
if let Ok(_) = file.read_to_end(&mut buf).await {
return Ok(Response::new(buf.into()));
}
return Ok(internal_server_error());
if let Ok(file) = File::open(filename).await {
let stream = FramedRead::new(file, BytesCodec::new());
let body = Body::wrap_stream(stream);
return Ok(Response::new(body));
}
Ok(not_found())

View File

@@ -3,9 +3,8 @@
<title>Hyper responding example</title>
</head>
<body>
<h1>Hyper responding example</h1>
<h1>Hyper responding example, streamed in chunks</h1>
<a href="index.html">index.html</a> Top Level<br>
<a href="big_file.html">big_file.html</a> This page, streamed in chunks<br>
<a href="no_file.html">no_file.html</a> A 404 test, the requested file does not exist<br>
</body>
</html>