forked from hyperium/hyper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_file.rs
65 lines (52 loc) · 1.85 KB
/
send_file.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![deny(warnings)]
use std::net::SocketAddr;
use hyper::server::conn::http1;
use tokio::net::TcpListener;
use bytes::Bytes;
use http_body_util::Full;
use hyper::service::service_fn;
use hyper::{Method, Request, Response, Result, StatusCode};
static INDEX: &str = "examples/send_file_index.html";
static NOTFOUND: &[u8] = b"Not Found";
#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
pretty_env_logger::init();
let addr: SocketAddr = "127.0.0.1:1337".parse().unwrap();
let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);
loop {
let (stream, _) = listener.accept().await?;
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
.serve_connection(stream, service_fn(response_examples))
.await
{
println!("Failed to serve connection: {:?}", err);
}
});
}
}
async fn response_examples(req: Request<hyper::body::Incoming>) -> Result<Response<Full<Bytes>>> {
match (req.method(), req.uri().path()) {
(&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
}
_ => Ok(not_found()),
}
}
/// HTTP status code 404
fn not_found() -> Response<Full<Bytes>> {
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Full::new(NOTFOUND.into()))
.unwrap()
}
async fn simple_file_send(filename: &str) -> Result<Response<Full<Bytes>>> {
if let Ok(contents) = tokio::fs::read(filename).await {
let body = contents.into();
return Ok(Response::new(Full::new(body)));
}
Ok(not_found())
}