aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 36699af7f3ab4a3d00dc114204372b09a69a76ea (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#[macro_use]
extern crate lazy_static;

mod http;
mod peekable_bufreader;
mod response;
mod chunked_bufreader;

use std::env;
use std::path::PathBuf;
use std::path::Path;

use futures::stream;
use futures::stream::StreamExt;

use async_std::prelude::*;
use async_std::task;
use async_std::net::{TcpStream, TcpListener};
use async_std::io::{BufReader, BufWriter};
use async_std::fs::File;
use async_std::fs;

use chrono::offset::Local;
use chrono::DateTime;

use http::{Parser, Method};
use peekable_bufreader::PeekableBufReader;
use chunked_bufreader::ChunkedBufReader;
use response::{Response, BadRequest, NotFound, NotImplemented, InternalServerError};

#[async_std::main]
async fn main() {
    let port = env::args().skip(1).next().unwrap_or("8000".to_owned()).parse::<u16>().unwrap_or(8000);
    let listener = TcpListener::bind(format!("0.0.0.0:{}", port)).await;
    match listener {
        Err(e) => {
            eprintln!("Failed to bind TCP Listener: {}", e);
            return;
        },
        _ => {},
    }
    let listener = listener.unwrap();
    listener
        .incoming()
        .for_each_concurrent(None, |stream| async move {
            if let Ok(valid) = stream {
                task::spawn(handle_connection(valid));
            }
        })
        .await;
}

async fn handle_connection(mut stream: TcpStream) {
    let response = generate_response(&stream).await.response_bytes();
    let writer = BufWriter::new(&mut stream);
    let mut writer = response.fold(writer, |mut writer, bytes| async move {
        writer.write(&bytes).await.unwrap();
        writer
    }).await;
    writer.flush().await.unwrap();
    stream.flush().await.unwrap();
}

async fn generate_response(stream: &TcpStream) -> Box<dyn Response> {
    let reader = PeekableBufReader::new(BufReader::new(stream));
    let request = match Parser::new(reader).parse().await {
        Some(success) => success,
        None => return Box::new(BadRequest{}),
    };
    match request.method {
        Method::GET => {
            if request.requested_path.iter().filter(|segment| segment.contains("/")).count() != 0 {
                return Box::new(BadRequest{});
            } else {
                let path = match PathBuf::from("./".to_owned() + &request.requested_path.join("/")).canonicalize() {
                    Ok(canonical_path) => canonical_path,
                    Err(_) => return Box::new(NotFound{}),
                };
                let current_dir = match env::current_dir() {
                    Ok(dir) => match dir.canonicalize() {
                        Ok(canonical_path) => canonical_path,
                        Err(_) => return Box::new(InternalServerError{}),
                    },
                    Err(_) => return Box::new(InternalServerError{}),
                };
                if !is_path_ancestor_of(&current_dir, &path) {
                    return Box::new(NotFound{});
                }
                let metadata = match fs::metadata(&path).await {
                    Ok(data) => data,
                    Err(_) => return Box::new(NotFound{}),
                };
                if metadata.is_dir() {
                    let friendly_name = match path.strip_prefix(&current_dir) {
                        Ok(result) => {
                            match result.to_str() {
                                Some(result) => result,
                                None => return Box::new(InternalServerError{}),
                            }
                        },
                        Err(_) => return Box::new(InternalServerError{}),
                    };
                    let mut listings = Vec::new();
                    if path != current_dir {
                        listings.push(format!(include_str!("../res/listing_entry.html"), path.parent().unwrap().strip_prefix(&current_dir).unwrap().to_str().unwrap(), "..", "-", "-"));
                    }
                    for file in path.read_dir().unwrap() {
                        let file_path = file.unwrap().path();
                        let file_metadata = fs::metadata(&file_path).await.unwrap();
                        let created_time = match file_metadata.created() {
                            Ok(birth_time) => {
                                let formatted_time: DateTime<Local> = birth_time.into();
                                formatted_time.format("%d-%b-%Y %H:%M").to_string()
                            },
                            Err(_) => "-".to_owned(),
                        };
                        let file_size = if file_metadata.is_dir() {
                            "-".to_owned()
                        } else {
                            file_metadata.len().to_string()
                        };
                        listings.push(format!(include_str!("../res/listing_entry.html"),
                            file_path.strip_prefix(&current_dir).unwrap().to_str().unwrap(),
                            file_path.file_name().unwrap().to_str().unwrap(),
                            created_time,
                            file_size,
                        ))
                    }
                    return Box::new(response::Ok {
                        file_stream: Box::new(stream::iter(vec![Vec::from(
                                format!(
                                    include_str!("../res/listing.html"),
                                    friendly_name,
                                    friendly_name,
                                    listings.join("\n"),
                                ).as_bytes()
                            )].into_iter().map(|entry| entry.to_owned())))
                    });
                } else {
                    return Box::new(response::Ok{ file_stream: Box::new(ChunkedBufReader::new(BufReader::new(File::open(&path).await.unwrap()))) })
                }
            }
        },
        _ => return Box::new(NotImplemented{}),
    }
}

fn is_path_ancestor_of(ancestor: &Path, child: &Path) -> bool {
    let mut ancestors = child.ancestors();
    loop {
        if let Some(parent) = ancestors.next() {
            if parent == ancestor {
                return true;
            }
        } else {
            return false;
        }
    }
}