stuff
This commit is contained in:
parent
ea8d6da54c
commit
20d9d9d91c
25
src/main.rs
25
src/main.rs
|
@ -1,8 +1,8 @@
|
||||||
use std::{
|
use std::{
|
||||||
env::current_dir,
|
env::current_dir,
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
fs::DirEntry,
|
fs::{self, read, DirEntry},
|
||||||
io::{BufRead, BufReader, Read, Write},
|
io::{self, BufRead, BufReader, Read, Write},
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
@ -115,7 +115,7 @@ fn handle_connection(mut stream: TcpStream) {
|
||||||
stream
|
stream
|
||||||
.write_all(
|
.write_all(
|
||||||
{
|
{
|
||||||
match std::fs::read_dir("./public/posts/") {
|
match fs::read_dir("./public/posts/") {
|
||||||
Ok(files) => {
|
Ok(files) => {
|
||||||
let mut entries = files
|
let mut entries = files
|
||||||
.filter_map(|x| if x.is_ok() { Some(x.unwrap()) } else { None })
|
.filter_map(|x| if x.is_ok() { Some(x.unwrap()) } else { None })
|
||||||
|
@ -136,23 +136,23 @@ fn handle_connection(mut stream: TcpStream) {
|
||||||
})
|
})
|
||||||
.collect::<Vec<DirEntry>>();
|
.collect::<Vec<DirEntry>>();
|
||||||
entries.sort_by(|a, b| {
|
entries.sort_by(|a, b| {
|
||||||
let mut a = if let Ok(a) = std::fs::File::open(a.path()) {
|
let mut a = if let Ok(a) = fs::File::open(a.path()) {
|
||||||
a
|
a
|
||||||
} else {
|
} else {
|
||||||
let mut path = a.path().clone();
|
let mut path = a.path().clone();
|
||||||
path.push("post.md");
|
path.push("post.md");
|
||||||
if let Ok(a) = std::fs::File::open(path) {
|
if let Ok(a) = fs::File::open(path) {
|
||||||
a
|
a
|
||||||
} else {
|
} else {
|
||||||
return std::cmp::Ordering::Equal;
|
return std::cmp::Ordering::Equal;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut b = if let Ok(b) = std::fs::File::open(b.path()) {
|
let mut b = if let Ok(b) = fs::File::open(b.path()) {
|
||||||
b
|
b
|
||||||
} else {
|
} else {
|
||||||
let mut path = b.path().clone();
|
let mut path = b.path().clone();
|
||||||
path.push("post.md");
|
path.push("post.md");
|
||||||
if let Ok(b) = std::fs::File::open(path) {
|
if let Ok(b) = fs::File::open(path) {
|
||||||
b
|
b
|
||||||
} else {
|
} else {
|
||||||
return std::cmp::Ordering::Equal;
|
return std::cmp::Ordering::Equal;
|
||||||
|
@ -226,7 +226,7 @@ fn handle_connection(mut stream: TcpStream) {
|
||||||
let mut response_content = vec![];
|
let mut response_content = vec![];
|
||||||
|
|
||||||
let response = match req.file() {
|
let response = match req.file() {
|
||||||
Some(path) => match std::fs::read(&path) {
|
Some(path) => match read(&path) {
|
||||||
Ok(content) => {
|
Ok(content) => {
|
||||||
let len = &content.len();
|
let len = &content.len();
|
||||||
response_content = content;
|
response_content = content;
|
||||||
|
@ -237,7 +237,14 @@ fn handle_connection(mut stream: TcpStream) {
|
||||||
get_mime_type(path.extension())
|
get_mime_type(path.extension())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Err(e) => format!("HTTP/1.1 404{0}{0}{e}", CLRF),
|
Err(e) =>
|
||||||
|
match e.kind() {
|
||||||
|
io::ErrorKind::NotFound => format!("HTTP/1.1 404\r\n\r\n{:?} not found", path.to_str()),
|
||||||
|
io::ErrorKind::PermissionDenied => format!("HTTP/1.1 403\r\n\r\n{:?} not readable", path.to_str()),
|
||||||
|
io::ErrorKind::FileTooLarge => format!("HTTP/1.1 413\r\n\r\n{:?} was too large", path.to_str()),
|
||||||
|
_ => format!("HTTP/1.1 500{0}{0}{e}", CLRF),
|
||||||
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
None => format!("HTTP/1.1 404{0}{0}", CLRF),
|
None => format!("HTTP/1.1 404{0}{0}", CLRF),
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue