aboutsummaryrefslogtreecommitdiff
path: root/src/sersve.rs
blob: dc5f717fda289c07ef1b0076ae057317aba78db8 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#![feature(plugin, path_ext, path_relative_from)]
#![plugin(serde_macros, docopt_macros)]

extern crate iron;
extern crate regex;
extern crate conduit_mime_types;
extern crate mustache;
extern crate libc;
extern crate num_cpus;
extern crate serde;
#[macro_use]
extern crate lazy_static;
extern crate docopt;
extern crate rustc_serialize;

use std::{ env, fs, process };
use std::path::{ Path, PathBuf };
use std::io::{ Read, Write };
use std::error::Error;
use std::fs::{ File, PathExt };
use std::sync::Arc;
use std::borrow::Borrow;

use regex::Regex;

use conduit_mime_types::Types;

use serde::json::{ self, Value };

use iron::prelude::*;
use iron::status;
use iron::mime::{ Mime, TopLevel, SubLevel };
use iron::headers::ContentType;
use iron::modifiers::Header;

use mustache::{ Template, VecBuilder, MapBuilder };

use constants::*;

pub mod constants;

docopt!(Args derive Debug, "
A minimal static file server, written in Rust with Iron.
Usage: sersve [options]

Options:
    -h, --help                  Show this message.
    -v, --version               Show the version of sersve (duh).
    -c, --config FILE           Provide a configuration file (JSON).
    -a, --address HOST          The address to bind to.
    -p, --port PORT             The port to serve.
    -r, --root ROOT             The uppermost directory to serve.
    -f, --filter REGEX          A regular expression to filter the filenames.
    -s, --size BYTES            The maximum size of a file that will be served.
    -t, --template TEMPLATE     A Mustache template to use for rendering.
    --threads THREADS           Amount of threads to use for serving.
    --fork                      Fork sersve into a background process.",
    flag_help: bool,
    flag_version: bool,
    flag_config: Option<String>,
    flag_address: Option<String>,
    flag_port: Option<u16>,
    flag_root: Option<String>,
    flag_filter: Option<String>,
    flag_size: Option<u64>,
    flag_template: Option<String>,
    flag_threads: Option<usize>,
    flag_fork: bool
);

#[derive(Clone)]
struct State {
    template: Template,
    root: Option<PathBuf>,
    mime_types: Arc<Types>
}

const HOST: &'static str = "0.0.0.0";
const PORT: u16 = 8080;

static UNITS: &'static [&'static str] = &["B", "kB", "MB", "GB", "TB"];

const KEY_TITLE: &'static str = "title";
const KEY_CONTENT: &'static str = "content";
const KEY_URL: &'static str = "url";
const KEY_SIZE: &'static str = "size";
const KEY_NAME: &'static str = "name";

const DEF_LEN: usize = 10000;

lazy_static! {
    static ref ARGS: Args = {
        let mut args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit());

        if let Some(ref flag_config) = args.flag_config {
            let conf = File::open(&flag_config)
                            .and_then(|mut f| {
                                let mut out = String::new();
                                f.read_to_string(&mut out).map(|_| out)
                            }).map_err(|e| error(e.description()))
                            .unwrap();

            // cannot if-let, because typesafe errors are helpful
            let json = match json::from_str(&conf) {
                Ok(Value::Object(o)) => o,
                _ => panic!("Invalid configuration file. Doesn't contain valid top-level object.")
            };
            args.flag_address = args.flag_address.or(match json.get("address") {
                Some(&Value::String(ref s)) => Some((*s).clone()),
                None => None,
                _ => panic!("Invalid configuration file. `address` field must be a string.")
            });
            args.flag_port = args.flag_port.or(match json.get("port") {
                Some(&Value::U64(u)) => Some(u as u16),
                None => None,
                _ => panic!("Invalid configuration file. `port` field must be an unsigned integer.")
            });
            args.flag_root = args.flag_root.or(match json.get("root") {
                Some(&Value::String(ref s)) => Some(s.clone()),
                None => None,
                _ => panic!("Invalid configuration file. `root` field must be a string.")
            });
            args.flag_filter = args.flag_filter.or(match json.get("filter") {
                Some(&Value::String(ref s)) => Some((*s).clone()),
                None => None,
                _ => panic!("Invalid configuration file. `filter` field must be a string.")
            });
            args.flag_size = args.flag_size.or(match json.get("size") {
                Some(&Value::U64(u)) => Some(u),
                None => None,
                _ => panic!("Invalid configuration file. `size` field must be an unsigned integer.")
            });
            args.flag_template = args.flag_template.or(match json.get("template") {
                Some(&Value::String(ref s)) => Some((*s).clone()),
                None => None,
                _ => panic!("Invalid configuration file. `template` field must be a string.")
            });
            args.flag_fork = args.flag_fork || match json.get("fork") {
                Some(&Value::Bool(b)) => b,
                None => false,
                _ => panic!("Invalid configuration file. `fork` field must be a boolean")
            };
            args.flag_threads = args.flag_threads.or(match json.get("threads") {
                Some(&Value::U64(u)) => Some(u as usize),
                None => None,
                _ => panic!("Invalid configuration file. `threads` field must be a string.")
            })
        };

        args
    };

    static ref STATE: State = State {
        template: mustache::compile_str(&ARGS.flag_template.as_ref().unwrap_or(&OPT_TEMPLATE.to_owned())),
        root: ARGS.flag_root.clone().map(|p| Path::new(&p).to_path_buf()),
        mime_types: Arc::new(Types::new().ok().unwrap())
    };
}

fn error(e: &str) -> ! {
    println!("Error: {}", e);
    process::exit(-1);
}

fn fork() {
    unsafe {
        let pid = libc::funcs::posix88::unistd::fork();
        if pid == 0 {
            // we are child, now get to work
            return;
        } else if pid > 0 {
            // fork succeeded, die
            process::exit(0);
        } else if pid < 0 {
            // unsuccessful, don't die
            return;
        }
    }
}

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<'a, W: Write>(mut out: W, template: Template, root: PathBuf, dir: PathBuf, files: Vec<PathBuf>, filter: Option<Regex>) {
    let data = MapBuilder::new()
        .insert_str(KEY_TITLE, format!("{}", dir.display()))
        .insert_vec(KEY_CONTENT, |mut vec: VecBuilder| {
            let item = |map: MapBuilder, url: &Path, size: u64, name: String| {
                map.insert(KEY_URL, &format!("{}", url.display())).unwrap()
                   .insert(KEY_SIZE, &size_with_unit(size)).unwrap()
                   .insert_str(KEY_NAME, name)
            };

            // add `..` entry if necessary
            let mut up = dir.to_path_buf();
            up.pop();
            if up.starts_with(&root) {
                vec = vec.push_map(|map: MapBuilder| item(map, &up.relative_from(&root).unwrap(), 0, "..".to_owned()));
            }

            for file in files.iter() {
                let relative = file.relative_from(&root).unwrap();
                let stat = file.metadata().unwrap();
                let filename = file.file_name()
                    .expect("Cannot get filename").to_string_lossy().into_owned();
                if filter.as_ref().map_or(true, |f| f.is_match(&filename)) {
                    vec = vec.push_map(|map| item(map, &relative, stat.len(), filename.clone()));
                }
            }
            vec
        }).build();

    template.render_data(&mut out, &data);
}

fn plain(content: &[u8]) -> IronResult<Response> {
    Ok(Response::with((status::Ok, content)))
}

fn html(content: &[u8]) -> IronResult<Response> {
    plain(content).map(|r| r.set(Header(ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![])))))
}

fn from_path(path: &Path) -> IronResult<Response> {
    Ok(Response::with((status::Ok, path)))
}

fn serve(req: &mut Request) -> IronResult<Response> {
    let (filter_str, max_size) = (
        ARGS.flag_filter.clone(),
        ARGS.flag_size
    );

    let (template, root, mime_types) = (
        STATE.template.clone(),
        STATE.root.clone().unwrap_or_else(|| env::current_dir().ok().unwrap()),
        STATE.mime_types.clone()
    );

    let mut path = root.clone();
    for part in req.url.path.iter() { path.push(part) }
    if !path.exists() { return html(format!("Error, `{}` does not exist.", path.display()).as_bytes()); }

    let filter = filter_str.and_then(|s| Regex::new(&s).ok());

    if path.is_file() && path.starts_with(&root) {
        let stat = path.metadata();
        if stat.as_ref().ok().is_some()
            && max_size.is_some()
            && stat.ok().unwrap().len() > max_size.unwrap() {
            return html(b"I'm afraid, I'm too lazy to serve the requested file. It's pretty big...")
        }

        if filter.as_ref().map_or(false,
                  |f| !f.is_match(path.file_name().unwrap().to_string_lossy().borrow())) {
            return html(b"I don't think you're allowed to do this.");
        }
        let mime: Option<iron::mime::Mime> = path.extension()
            .map(|s| s.to_string_lossy().to_owned())
            .map_or(None, |e| mime_types.get_mime_type(e.borrow()))
            .and_then(|m| m.parse().ok());
        if mime.as_ref().is_some() {
            from_path(&path).map(|r| r.set(Header(ContentType((*mime.as_ref().unwrap()).clone()))))
        } else {
            from_path(path.as_path())
        }
    } else {
        let mut content: Vec<PathBuf> = match fs::read_dir(&path) {
            Ok(s) => s.filter_map(Result::ok).map(|s| s.path()).collect(),
            Err(e) => return html(e.description().as_bytes())
        };
        content.sort_by(|a, b| a.to_string_lossy().cmp(&b.to_string_lossy()));
        let mut out = Vec::with_capacity(DEF_LEN);
        render(&mut out, template, root, path, content, filter);
        html(&out)
    }
}

fn main() {
    let (host, port, threads) = {
        (ARGS.flag_address.clone().unwrap_or(HOST.into()),
         ARGS.flag_port.clone().unwrap_or(PORT),
         ARGS.flag_threads.unwrap_or(num_cpus::get()))
    };

    if ARGS.flag_fork { fork() }

    match Iron::new(serve).listen_with((host.as_ref(), port), threads, iron::Protocol::Http) {
        Ok(_) => (),
        Err(e) => println!("I'm sorry, I failed you.\nError: {:?}", e)
    }
}