changed to simple file server

This commit is contained in:
2020-04-13 01:34:24 +08:00
parent d94c391999
commit 73806c0ca4
2 changed files with 62 additions and 19 deletions

View File

@@ -8,3 +8,5 @@ edition = "2018"
[dependencies]
simple-server = "0.4.0"
rust_util = "0.2.2"

View File

@@ -1,31 +1,72 @@
use simple_server::Server;
use rust_util::{
XResult,
util_msg::{
MessageType,
print_message,
},
};
use std::str::FromStr;
use std::path::PathBuf;
fn main() {
fn main() -> XResult<()> {
let host = "127.0.0.1";
let port = "8080";
let base_path = std::env::var("HOME")?;
print_message(MessageType::INFO, &format!("Base path: {}", &base_path));
println!("Starting server @{}:{}...", host, port);
let server = Server::new(|request, mut response| {
let server = Server::new(move |request, mut response| {
// let uri = request.uri();
let uri_str = request.uri().to_string();
let mut buff = String::with_capacity(1024);
buff.push_str(&format!("Method: {}\n", request.method()));
buff.push_str(&format!("URI: {}\n", request.uri()));
buff.push_str(&format!("Version: {:?}\n", &request.version()));
// buff.push_str(&format!("Headers: {:?}\n", request.headers()));
for header in request.headers() {
if let Ok(val) = header.1.to_str() {
buff.push_str(&format!("{}:{}\n", header.0, val));
} else {
// TODO, INGORE ?
let mut local_path_buff = match PathBuf::from_str(&base_path) {
Ok(p) => p, Err(e) => {
return Ok(response.status(500).body(format!("{}", e).as_bytes().to_vec())?);
}
}
buff.push_str("\n");
buff.push_str(&format!("Body: {:?}\n", request.body()));
};
local_path_buff.push(uri_str.chars().skip(1).collect::<String>());
let local_path = local_path_buff.as_path();
Ok(response
.status(200)
.header("content-type", "text/plain; charset=UTF-8")
.body(buff.as_bytes().to_vec())?)
println!("{:?}", &base_path);
println!("{:?} {}", local_path, local_path.exists());
if !local_path.exists() {
return Ok(response.status(404).body("not found!".as_bytes().to_vec())?);
}
if local_path.is_dir() && !uri_str.ends_with('/') {
return Ok(response.status(302).header("location", &format!("{}/", &uri_str)).body("".as_bytes().to_vec())?);
}
if local_path.is_dir() {
return Ok(response.status(200).body("dir!".as_bytes().to_vec())?);
} else {
return Ok(response.status(200).body("file!".as_bytes().to_vec())?);
}
// let mut buff = String::with_capacity(1024);
// buff.push_str(&format!("Method: {}\n", request.method()));
// buff.push_str(&format!("URI: {}\n", request.uri()));
// buff.push_str(&format!("Version: {:?}\n", &request.version()));
// // buff.push_str(&format!("Headers: {:?}\n", request.headers()));
// for header in request.headers() {
// if let Ok(val) = header.1.to_str() {
// buff.push_str(&format!("{}:{}\n", header.0, val));
// } else {
// // TODO, INGORE ?
// }
// }
// buff.push_str("\n");
// buff.push_str(&format!("Body: {:?}\n", request.body()));
// Ok(response
// .status(200)
// .header("content-type", "text/plain; charset=UTF-8")
// .body(buff.as_bytes().to_vec())?)
});
server.listen(host, port);
// Ok(())
}