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
use std::io;
use std::time::SystemTime;
use std::fs::{File};
use std::path::Path;
use std::ffi::OsString;
use std::sync::Arc;

use accept_encoding::{AcceptEncoding, AcceptEncodingParser};
use accept_encoding::{Iter as EncodingIter, Encoding};
use config::{Config, EncodingSupport};
use conditionals::{ModifiedParser, NoneMatchParser};
use etag::Etag;
use output::{Head, FileWrapper};
use range::{Range, RangeParser};
use mime_guess::get_mime_type_str;
use {Output};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    Head,
    Get,
    InvalidMethod,
    InvalidRange,
}

pub fn is_text_file(val: &str) -> bool {
    return val.starts_with("text/") || val == "application/javascript"
}

/// The structure represents parsed input headers
///
/// Create it with `Input::from_headers`, and make output structure
/// using `Input::probe_file`. Note: the latter should be run in disk
/// thread.
#[derive(Debug, Clone)]
pub struct Input {
    pub(crate) config: Arc<Config>,
    pub(crate) mode: Mode,
    pub(crate) accept_encoding: AcceptEncoding,
    pub(crate) range: Option<Range>,
    pub(crate) if_range: Option<Result<SystemTime, Etag>>,
    pub(crate) if_match: Vec<Etag>,
    pub(crate) if_none: Vec<Etag>,
    pub(crate) if_unmodified: Option<SystemTime>,
    pub(crate) if_modified: Option<SystemTime>,
}

impl Input {
    /// A constructor for `Input` object
    pub fn from_headers<'x, I>(cfg: &Arc<Config>, method: &str, headers: I)
        -> Input
        where I: Iterator<Item=(&'x str, &'x[u8])>
    {
        let mode = match method {
            "HEAD" => Mode::Head,
            "GET" => Mode::Get,
            _ => return Input {
                config: cfg.clone(),
                mode: Mode::InvalidMethod,
                accept_encoding: AcceptEncoding::identity(),
                range: None,
                if_range: None,
                if_match: Vec::new(),
                if_none: Vec::new(),
                if_unmodified: None,
                if_modified: None,
            },
        };
        let mut ae_parser = AcceptEncodingParser::new();
        let mut range_parser = RangeParser::new();
        let mut modified_parser = ModifiedParser::new();
        let mut none_match_parser = NoneMatchParser::new();
        for (key, val) in headers {
            if cfg.encoding_support != EncodingSupport::Never &&
               key.eq_ignore_ascii_case("accept-encoding")
            {
                ae_parser.add_header(val);
            } else if key.eq_ignore_ascii_case("range") {
                range_parser.add_header(val);
            } else if cfg.last_modified &&
                      key.eq_ignore_ascii_case("if-modified-since")
            {
                modified_parser.add_header(val);
            } else if cfg.etag &&
                      key.eq_ignore_ascii_case("if-none-match")
            {
                none_match_parser.add_header(val);
            }
        }
        let range = match range_parser.done() {
            Ok(range) => range,
            Err(()) => return Input {
                config: cfg.clone(),
                mode: Mode::InvalidRange,
                accept_encoding: AcceptEncoding::identity(),
                range: None,
                if_range: None,
                if_match: Vec::new(),
                if_none: Vec::new(),
                if_unmodified: None,
                if_modified: None,
            },
        };
        Input {
            config: cfg.clone(),
            mode: mode,
            accept_encoding: ae_parser.done(),
            range: range,
            if_range: None,
            if_match: Vec::new(),
            if_none: none_match_parser.done(),
            if_unmodified: None,
            if_modified: modified_parser.done(),
        }
    }
    /// Iterate over encodings accepted by user-agent in preferred order
    pub fn encodings(&self) -> EncodingIter {
        self.accept_encoding.iter()
    }
    /// Open files from filesystem
    ///
    /// **Must be run in disk thread**
    pub fn probe_file<P: AsRef<Path>>(&self, base_path: P)
        -> Result<Output, io::Error>
    {
        match self.mode {
            Mode::Head | Mode::Get => {}
            Mode::InvalidMethod => return Ok(Output::InvalidMethod),
            Mode::InvalidRange => return Ok(Output::InvalidRange),
        }
        let base_path = base_path.as_ref();
        match base_path.metadata() {
            Ok(ref m) if m.is_dir() => self.try_dir(base_path),
            Ok(_) => self.try_file(base_path),
            Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
                return Ok(Output::NotFound);
            }
            Err(e) => return Err(e),
        }
    }
    fn try_dir(&self, base_path: &Path) -> Result<Output, io::Error> {
        let mut buf = base_path.to_path_buf();
        for name in &self.config.index_files {
            buf.push(name);
            if buf.exists() {
                return self.try_file(&buf);
            }
            buf.pop();
        }
        Ok(Output::Directory)
    }
    fn try_file(&self, base_path: &Path) -> Result<Output, io::Error> {
        use config::EncodingSupport as E;
        let ctype = base_path.extension()
            .and_then(|x| x.to_str())
            .and_then(|x| get_mime_type_str(x))
            .unwrap_or("application/octed-stream");
        let encodings = match self.config.encoding_support {
            E::Never => false,
            E::TextFiles => is_text_file(ctype),
            E::AllFiles => true,
        };
        if encodings {
            return self.try_encodings(base_path, ctype);
        } else {
            return self.try_path(base_path, Encoding::Identity, ctype);
        }
    }

    fn try_path(&self, path: &Path, enc: Encoding, ctype: &'static str)
        -> Result<Output, io::Error>
    {
        let f = File::open(path)?;
        let meta = f.metadata()?;
        if !meta.is_file() {
            return Err(io::ErrorKind::PermissionDenied.into());
        }
        let head = match Head::from_meta(self, enc, &meta, ctype) {
            Err(output) => return Ok(output),
            Ok(head) => head,
        };
        match self.mode {
            Mode::InvalidMethod => unreachable!(),
            Mode::InvalidRange => unreachable!(),
            Mode::Head => Ok(Output::FileHead(head)),
            Mode::Get => Ok(Output::File(FileWrapper::new(head, f)?)),
        }
    }

    fn try_encodings(&self, base_path: &Path, ctype: &'static str)
        -> Result<Output, io::Error>
    {
        let path = base_path.as_os_str();
        let mut buf = OsString::with_capacity(path.len() + 3);
        for enc in self.encodings() {
            buf.clear();
            buf.push(path);
            buf.push(enc.suffix());
            let path = Path::new(&buf);
            match self.try_path(&path, enc, ctype) {
                Ok(x) => return Ok(x),
                Err(ref e) if e.kind() == io::ErrorKind::NotFound
                => continue,
                Err(e) => return Err(e),
            }
        }
        // Tecnically it can happen only if file was removed while
        // we are looking for encodings
        Ok(Output::NotFound)
    }
}

#[cfg(test)]
mod test {
    use std::mem::size_of;
    use accept_encoding::{AcceptEncodingParser};
    use super::*;

    fn send<T: Send>(_: &T) {}
    fn self_contained<T: 'static>(_: &T) {}

    #[test]
    fn traits() {
        let v = Input {
            config: Config::new().done(),
            mode: Mode::Get,
            accept_encoding: AcceptEncodingParser::new().done(),
            range: None,
            if_range: None,
            if_match: Vec::new(),
            if_none: Vec::new(),
            if_unmodified: None,
            if_modified: None,
        };
        send(&v);
        self_contained(&v);
    }

    #[cfg(all(target_arch="x86_64", target_os="linux"))]
    #[test]
    fn size() {
        assert!(size_of::<Range>() <= 24);
        assert!(size_of::<Input>() <= 176);
    }
}