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
//! Web Request.

use {
    crate::{asset, render, routes::route, util},
    ascii::AsciiString,
    etag::EntityTag,
    std::{
        collections::HashMap,
        io::{self},
        str,
    },
    tiny_http::{
        Header,
        Method::{self, Get, Post},
        Request as TinyRequest, Response,
    },
};

pub struct Request {
    // raw TinyHTTP request
    tiny_req: TinyRequest,
    // POST/GET params
    params: HashMap<String, String>,
}

impl Request {
    /// Make a new Request.
    pub fn new(req: TinyRequest) -> Request {
        Request {
            tiny_req: req,
            params: HashMap::new(),
        }
    }

    /// HTTP Method
    pub fn method(&self) -> &Method {
        self.tiny_req.method()
    }

    /// URL requested
    pub fn url(&self) -> &str {
        self.tiny_req.url()
    }

    /// Headers for this request.
    pub fn headers(&self) -> &[Header] {
        self.tiny_req.headers()
    }

    /// Return a value in a POST <form> or ?querystring=
    /// Always gives a string. Will be empty if param wasn't sent.
    /// Use `has_param()` to check if it exists.
    pub fn param(&mut self, name: &str) -> &str {
        self.parse_params();
        if let Some(s) = self.params.get(name) {
            s
        } else {
            ""
        }
    }

    /// Has the given param been set?
    pub fn has_param(&mut self, name: &str) -> bool {
        self.parse_params();
        self.params.contains_key(name)
    }

    /// Turn a query string or POST body into a nice and tidy HashMap.
    fn parse_params(&mut self) {
        if !self.params.is_empty() {
            return;
        }

        // temp value
        let mut map = HashMap::new();

        // parse url
        let url = self.url();
        if let Some(start) = url.find('?') {
            parse_query_into_map(&url[start + 1..], &mut map);
        }

        // parse POST body
        if self.method() == &Post {
            let mut content = String::new();
            if let Ok(size) = self.as_reader().read_to_string(&mut content) {
                if size > 0 {
                    parse_query_into_map(&content, &mut map);
                }
            }
        }

        if !map.is_empty() {
            self.params = map;
        }
    }

    /// Provide io::Read
    pub fn as_reader(&mut self) -> &mut dyn io::Read {
        self.tiny_req.as_reader()
    }

    /// Respond with 404.
    fn respond_404(self) -> Result<(), io::Error> {
        self.respond(Response::from_string("404 Not Found").with_status_code(404))
    }

    /// Respond to this request. Consumes.
    pub fn respond<R>(self, res: Response<R>) -> Result<(), io::Error>
    where
        R: io::Read,
    {
        self.tiny_req.respond(res)
    }

    /// All the wiki pages, in alphabetical order.
    pub fn page_names(&self) -> Vec<String> {
        let mut dirs = vec![];

        for entry in walkdir::WalkDir::new("./")
            .into_iter()
            .filter_map(|e| e.ok())
        {
            if !entry.file_type().is_dir()
                && entry.file_name().to_str().unwrap_or("").ends_with(".md")
            {
                let dir = entry.path().display().to_string();
                let dir = dir.trim_start_matches("./").trim_end_matches(".md");
                if !dir.is_empty() {
                    dirs.push(format!("{}", dir));
                }
            }
        }

        dirs.sort();
        dirs
    }

    /// Request handler. Consumes.
    pub fn handle(mut self) -> Result<(), io::Error> {
        // static files
        if self.method() == &Get && self.url().contains('.') {
            let path = strip_query(self.url()).to_string();
            if asset::exists(&path) {
                return self.serve_static_file(&path);
            }
        }

        let (status, body, content_type) = match route(&mut self) {
            Ok(res) => res,
            Err(e) => {
                eprintln!("{}", e);
                (
                    500,
                    format!("<h1>500 Internal Error</h1><pre>{}</pre>", e),
                    "text/html",
                )
            }
        };

        let response = if status == 302 {
            Response::from_data(format!("Redirected to {}", body))
                .with_status_code(status)
                .with_header(header("Location", &body))
        } else {
            Response::from_data(body)
                .with_status_code(status)
                .with_header(header("Content-Type", content_type))
        };

        println!("-> {} {} {}", status, self.method(), self.url());
        self.respond(response)
    }

    /// Serve a static file, doing the header dance with ETag and whatnot.
    fn serve_static_file(self, path: &str) -> Result<(), io::Error> {
        if let Some(file) = asset::Asset::get(&render::pathify(path)) {
            let etag = EntityTag::from_hash(&file);
            if self
                .headers()
                .iter()
                .any(|h| h.field.equiv("If-None-Match") && h.value == etag.tag())
            {
                println!("-> {} {} {}", 304, self.method(), self.url());
                return self.respond(Response::from_data("").with_status_code(304));
            } else {
                println!("-> {} {} {}", 200, self.method(), self.url());
                return self.respond(
                    Response::from_data(file)
                        .with_header(header("ETag", etag.tag()))
                        .with_header(header("Content-Type", util::get_content_type(&path))),
                );
            }
        }

        self.respond_404()
    }
}

/// Generate a Header for tiny_http.
fn header(field: &str, value: &str) -> tiny_http::Header {
    tiny_http::Header {
        field: field.parse().unwrap(),
        value: AsciiString::from_ascii(value).unwrap_or_else(|_| AsciiString::new()),
    }
}

/// Parses a query string like "name=jimbo&other_data=sure" into a
/// HashMap.
fn parse_query_into_map(params: &str, map: &mut HashMap<String, String>) {
    for kv in params.split('&') {
        let mut parts = kv.splitn(2, '=');
        if let Some(key) = parts.next() {
            if let Some(val) = parts.next() {
                map.insert(key.to_string(), util::decode_form_value(val));
            } else {
                map.insert(key.to_string(), "".to_string());
            }
        }
    }
}

/// Strip the ?querystring from a URL.
fn strip_query(url: &str) -> &str {
    if let Some(idx) = url.find('?') {
        &url[..idx]
    } else {
        url
    }
    .trim_start_matches("static/")
    .trim_start_matches("/static/")
    .trim_start_matches("./static/")
}