aboutsummaryrefslogtreecommitdiff
path: root/src/sersve.rs
blob: 1d6e91c715366f691e92afcce3080ff27045a991 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#![feature(globs, slicing_syntax, if_let, unboxed_closures)]

extern crate getopts;
extern crate serialize;
extern crate glob;
extern crate iron;
extern crate persistent;
extern crate error;

use std::{ str, os };
use std::path::{ Path, GenericPath };
use std::io::{ fs, Reader };
use std::io::fs::{ File, PathExtensions };
use std::default::Default;
use std::sync::Mutex;

use getopts::{ optopt, optflag, getopts, usage, OptGroup };

use serialize::json;
use serialize::json::Json;

use iron::prelude::*;
use iron::response::modifiers::*;
use iron::status;
use iron::mime::*;
use iron::mime::SubLevel::Ext as SubExt;
use iron::middleware::ChainBuilder;
use iron::typemap::Assoc;

use persistent::Read;

pub mod constants;

#[deriving(Send, Clone, Default, Encodable, Decodable)]
struct Options {
    host: Option<String>,
    port: Option<u16>,
    root: Option<Path>,
    custom_css: Option<String>,
    filter: Option<String>,
    max_size: Option<u64>,
    not_found: Option<String>
}

struct OptCarrier;
impl Assoc<Mutex<Options>> for OptCarrier {}

static UNITS: &'static [&'static str] = &["B", "kB", "MB", "GB", "TB"];
const BRIEF: &'static str = "A minimal directory server, written in Rust with Iron.";

fn size_with_unit(mut size: u64) -> String {
    let mut frac = 0;
    let mut index = 0;

    while size > 1000 && index + 1 < UNITS.len() {
        frac = size % 1000;
        size /= 1000;
        index += 1;
    }

    format!("{}.{} {}", size, frac, UNITS[index])
}

fn render(root: Path, dir: Path, files: Vec<Path>) -> String {
    fn render_item(url: Path, size: u64, name: &str) -> String {
        format!("<tr><td><a href=\"/{url}\">{name}</a></td><td>{size}</td></tr>\n",
        url = url.display(), size = size_with_unit(size), name = name)
    }

    let mut content = String::new();
    let mut up = dir.clone();
    up.pop();
    if root.is_ancestor_of(&up) {
        content.push_str(render_item(up.path_relative_from(&root).unwrap(), 0, "..")[]);
    }

    for file in files.iter() {
        let relative = file.path_relative_from(&root).unwrap();
        let stat = file.stat().unwrap();
        let filename = file.filename_display().as_maybe_owned().into_string();
        content.push_str(render_item(relative, stat.size, filename.clone()[])[]);
    }

    format!("<!DOCTYPE html>
<html>
    <title>{title}</title>
    <style type=\"text/css\">
    {css}
    </style>
    <body>
        <div id=\"container\">
            <h1>{title}</h1>
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Size</th>
                    </tr>
                </thead>
                <tbody>
                {content}
                </tbody>
            </table>
        </div>
    </body>
</html>",
            title = dir.display().as_maybe_owned(),
            css = constants::CSS,
            content = content)
}

fn plain<B: Bodyable>(content: B) -> IronResult<Response> {
    Ok(Response::new()
       .set(Status(status::Ok))
       .set(Body(content)))
}

fn html<B: Bodyable>(content: B) -> IronResult<Response> {
    plain(content).map(|r| r.set(ContentType(Mime(Text, Html, Vec::new()))))
}

fn binary<B: Bodyable>(content: B) -> IronResult<Response> {
    plain(content).map(
        |r| r.set(ContentType(Mime(Application, SubExt("octet-stream".into_string()), Vec::new()))))
}

fn guess_text(data: &[u8]) -> bool {
    let mut total = 0u;
    let mut text = 0u;
    for (c, _) in data.iter().zip(range(0u, 1000)) {
        let c = *c as char;
        if c.is_alphanumeric() || c.is_whitespace() {
            text += 1;
        }
        total += 1;
    }
    text as f64 / total as f64 > 0.75
}

fn serve(req: &mut Request) -> IronResult<Response> {
    let root = {
        let o = req.get::<Read<OptCarrier, Mutex<Options>>>().unwrap();
        let mutex = o.lock();
        mutex.root.clone().unwrap_or_else(|| os::getcwd().ok().unwrap())
    };

    let mut path = root.clone();
    for part in req.url.path.iter() { path.push(part[]) }
    if !path.exists() { return html("Well, no... We don't have that today."); }

    if path.is_file() && root.is_ancestor_of(&path) {
        let content = match File::open(&path).read_to_end() {
            Ok(s) => s,
            Err(e) => return html(e.desc)
        };
        if guess_text(content[]) { plain(content[]) } else { binary(content[]) }
    } else {
        let mut content = match fs::readdir(&path) {
            Ok(s) => s,
            Err(e) => return html(e.desc)
        };
        content.sort_by(|a, b| a.filename_str().unwrap().cmp(b.filename_str().unwrap()));
        html(render(root, path, content)[])
    }
}

fn print_usage(program: &str, opts: &[OptGroup]) {
    println!("Usage: {} [options]\n", program);
    println!("{}", usage(BRIEF, opts));
}

fn main() {
    let args: Vec<String> = os::args();
    let program = args[0].clone();
    let opts = &[
        optopt("c", "config", "set config file name", "NAME"),
        optopt("a", "address", "the address to bind to", "HOST"),
        optopt("p", "port", "the port to serve", "PORT"),
        optopt("r", "root", "the uppermost directory to serve", "ROOT"),
        optflag("h", "help", "print this help menu")
    ];
    let matches = match getopts(args.tail(), opts) {
        Ok(m) => { m }
        Err(f) => {
            println!("{}", f.to_string());
            return;
        }
    };

    if matches.opt_present("h") {
        print_usage(program[], opts);
        return;
    }

    let options: Mutex<Options> = Mutex::new(Default::default());

    matches.opt_str("c").map(|conf_file| {
        let conf_file = File::open(&Path::new(conf_file));
        //conf_file.map_err(|e| panic!("{}", e.desc));
        let json = match json::from_reader(&mut conf_file.ok().unwrap()) {
            Ok(Json::Object(o)) => o,
            _ => panic!("Invalid configuration file. Doesn't contain top-level object.")
        };
        let mut o = options.lock();
        o.host = match json.get("address") {
            Some(&Json::String(ref s)) => Some((*s).clone()),
            None => None,
            _ => panic!("Invalid configuration file. `address` field must be a string.")
        };
        o.port = match json.get("port") {
            Some(&Json::U64(u)) => Some(u as u16),
            None => None,
            _ => panic!("Invalid configuration file. `port` field must be an unsigned integer.")
        };
        o.root = match json.get("root") {
            Some(&Json::String(ref s)) => Some(Path::new((*s).clone())),
            None => None,
            _ => panic!("Invalid configuration file. `root` field must be a string.")
        };
    });

    let (host, port) = {
        let mut o = options.lock();
        o.host = o.host.clone().or(matches.opt_str("a"));
        o.port = o.port.or(matches.opt_str("p").and_then(|p| str::from_str(p[])));
        o.root = o.root.clone().or(matches.opt_str("r").and_then(|p| Path::new_opt(p)));
        (o.host.clone().unwrap_or("0.0.0.0".into_string()),
         o.port.clone().unwrap_or(8080))
    };

    let mut chain = ChainBuilder::new(serve);
    chain.link(Read::<OptCarrier, Mutex<Options>>::both(options));
    match Iron::new(chain).listen((host[], port)) {
        Ok(_) => (),
        Err(e) => println!("I'm sorry, I failed you. {}", e)
    }
}