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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use {
    crate::{http_parser, util, Error, Result, TypeCache},
    std::{borrow::Cow, collections::HashMap, fmt, io, mem, rc::Rc, str},
};

#[cfg(feature = "cookies")]
use cookie2::Cookie;

/// A `(start, end)` tuple representing a the location of some part of
/// a Request in a raw buffer, such as the requested URL's path.
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct Span(pub usize, pub usize);

impl Span {
    /// Create a new, empty Span.
    pub fn new() -> Span {
        Span::default()
    }

    /// Is this span empty?
    pub fn is_empty(&self) -> bool {
        self.0 == 0 && self.1 == 0
    }

    /// Find and return the str this span represents from the given
    /// buffer, which should be the raw HTTP request.
    pub fn from_buf<'buf>(&self, buf: &'buf [u8]) -> &'buf str {
        if self.is_empty() {
            ""
        } else if self.1 >= self.0 && self.1 <= buf.len() {
            str::from_utf8(&buf[self.0..self.1]).unwrap_or("?")
        } else {
            "?"
        }
    }
}

/// Contains information about a single request.
pub struct Request {
    /// The raw request.
    buffer: Vec<u8>,

    /// Includes `?query` and starts with `/`.
    /// Calling `request.path()` delivers just the path without any
    /// `?query` - use `request.full_path()` to get the full story.
    path: Span,

    /// HTTP Method
    method: Span,

    /// Sent Headers
    headers: Vec<(Span, Span)>,

    /// Request Body (POST)
    body: Span,

    /// Maps of form and URL args, percent decoded.
    args: HashMap<String, String>,
    form: HashMap<String, String>,

    /// Local request cache.
    cache: Rc<TypeCache>,

    #[cfg(feature = "cookies")]
    cookies: Vec<(String, String)>,
}

impl fmt::Debug for Request {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut headers = HashMap::new();
        for (k, v) in &self.headers {
            headers.insert(k.from_buf(&self.buffer), v.from_buf(&self.buffer));
        }
        f.debug_struct("Request")
            .field("method", &self.method.from_buf(&self.buffer))
            .field("path", &self.path.from_buf(&self.buffer))
            .field("full_path", &self.path.from_buf(&self.buffer))
            .field("headers", &headers)
            .finish()
    }
}

impl Request {
    /// Create a new Request from a raw one. You probably want
    /// `default()` to get an empty `Request`.
    pub fn new(
        method: Span,
        path: Span,
        headers: Vec<(Span, Span)>,
        body: Span,
        buffer: Vec<u8>,
    ) -> Request {
        Request {
            method,
            path,
            headers,
            body,
            buffer,
            ..Request::default()
        }
    }
    /// Produce an empty Request.
    pub fn default() -> Request {
        Request {
            path: Span::new(),
            method: Span::new(),
            body: Span::new(),
            headers: Vec::new(),
            args: HashMap::new(),
            form: HashMap::new(),
            buffer: Vec::new(),
            cache: Rc::new(TypeCache::new()),

            #[cfg(feature = "cookies")]
            cookies: vec![],
        }
    }

    /// Read a raw HTTP request from `reader` and create an
    /// appropriate `Request` to represent it.
    pub fn from_reader<R: io::Read>(mut reader: R) -> Result<Request> {
        let mut buffer = Vec::with_capacity(512);
        let mut read_buf = [0u8; 512];

        let mut req = loop {
            let n = reader.read(&mut read_buf)?;
            if n == 0 {
                return Err(Error::ConnectionClosed);
            }
            buffer.extend_from_slice(&read_buf[..n]);
            match http_parser::parse(mem::replace(&mut buffer, vec![]))? {
                http_parser::Status::Complete(req) => break req,
                http_parser::Status::Partial(b) => {
                    let _ = mem::replace(&mut buffer, b);
                }
            }
        };

        if let Some(size) = req.header("Content-Length") {
            let size = size.parse().unwrap_or(0);
            let start = req.body.0;

            while req.buffer[start..].len() < size {
                let n = reader.read(&mut read_buf)?;
                if n == 0 {
                    break;
                }
                req.buffer.extend_from_slice(&read_buf[..n]);
            }
            req.body.1 = req.body.0 + size;
            req.parse_form();
        }

        #[cfg(feature = "cookies")]
        {
            if let Some(cookie) = req.header("Cookie") {
                let cookie = Cookie::parse(cookie).map_err(|e| Error::Other(e.to_string()))?;
                let name = cookie.name().to_owned();
                let val = util::percent_decode(cookie.value()).unwrap();
                req.cookies.push((name, val));
            }
        }

        Ok(req)
    }

    /// Path requested, starting with `/` and not including `?query`.
    pub fn path(&self) -> &str {
        let span = if let Some(idx) = self.full_path().find('?') {
            Span(self.path.0, self.path.0 + idx)
        } else {
            self.path
        };
        span.from_buf(&self.buffer)
    }

    /// Full path requested, starting with `/` and including `?query`.
    pub fn full_path(&self) -> &str {
        self.path.from_buf(&self.buffer)
    }

    /// Create a request from an arbitrary path. Used in testing.
    pub fn from_path(path: &str) -> Request {
        Request::default().with_path(path)
    }

    /// Give a request an arbitrary `path`. Can be used in tests or
    /// with `filter`.
    pub fn set_path(&mut self, path: &str) {
        self.path = Span(self.buffer.len(), self.buffer.len() + path.len());
        self.buffer.extend(path.as_bytes());
    }

    /// Give a request an arbitrary `path`. Can be used in tests or
    /// with `filter`.
    pub fn with_path(mut self, path: &str) -> Request {
        self.set_path(path);
        self
    }

    /// Raw body of HTTP request. If you are using methods like
    /// `with_path` or `set_arg` this will not accurately represent
    /// the raw HTTP request that was made.
    pub fn body(&self) -> &str {
        self.body.from_buf(&self.buffer)
    }

    /// Give this Request an arbitrary body from a string.
    pub fn set_body<S: AsRef<str>>(&mut self, body: S) {
        self.body = Span(self.buffer.len(), self.buffer.len() + body.as_ref().len());
        self.buffer.extend(body.as_ref().as_bytes());
    }

    /// Give this Request an arbitrary body from a string and return
    /// the new Request.
    pub fn with_body<S: AsRef<str>>(mut self, body: S) -> Request {
        self.set_body(body);
        self
    }

    /// Body of HTTP request deserialized as a JSON value.
    ///
    /// The `json_serde` feature must be enabled in `Cargo.toml`.
    #[cfg(feature = "json_serde")]
    pub fn json<'a, T: serde::Deserialize<'a>>(&'a self) -> serde_json::Result<T> {
        Ok(serde_json::from_str(self.body())?)
    }

    /// HTTP Method
    pub fn method(&self) -> &str {
        self.method.from_buf(&self.buffer)
    }

    /// Give this Request a new HTTP Method.
    pub fn set_method(&mut self, method: &str) {
        self.method = Span(self.buffer.len(), self.buffer.len() + method.len());
        self.buffer.extend(method.as_bytes());
    }

    /// Give this Request a new HTTP Method and return the new Request.
    pub fn with_method(mut self, method: &str) -> Request {
        self.set_method(method);
        self
    }

    /// In a route defined with `routes!` like `"/names/:name"`,
    /// calling `request.arg("name")` will return `Some("peter")` when
    /// the request is `/names/peter`.
    pub fn arg(&self, name: &str) -> Option<&str> {
        self.args.get(name).map(|v| v.as_ref())
    }

    /// Replace or set a new value for an arbitrary URL argument from
    /// a `filter` or in a test.
    pub fn set_arg(&mut self, name: String, value: String) {
        self.args.insert(name, value);
    }

    #[doc(hidden)]
    /// For testing. You should use [`header()`](#method.header) to
    /// get a specific header from this Request.
    pub fn headers(&self) -> &Vec<(Span, Span)> {
        &self.headers
    }

    /// Get a header value. `name` is case insensitive.
    pub fn header(&self, name: &str) -> Option<Cow<str>> {
        let name = name.to_lowercase();
        let mut headers = self
            .headers
            .iter()
            .filter(|(n, _)| n.from_buf(&self.buffer).to_ascii_lowercase() == name)
            .map(|(_, v)| v.from_buf(&self.buffer).trim_end());

        let count = headers.clone().count();
        if count == 0 {
            None
        } else if count == 1 {
            Some(Cow::from(headers.next().unwrap()))
        } else {
            Some(Cow::from(headers.collect::<Vec<_>>().join(", ")))
        }
    }

    /// Was the given form value sent?
    pub fn has_form(&mut self, name: &str) -> bool {
        self.form(name).is_some()
    }

    /// Return a value from the POSTed form data.
    pub fn form(&self, name: &str) -> Option<&str> {
        self.form.get(name).map(|s| s.as_ref())
    }

    /// Replace or set a new value for an arbitrary URL argument from
    /// a `filter` or in a test.
    pub fn set_form(&mut self, name: &str, value: &str) {
        self.form.insert(name.to_string(), value.to_string());
    }

    /// Parse and decode form POST data into a Hash. Should be called
    /// when this Request is created.
    #[doc(Hidden)]
    pub fn parse_form(&mut self) {
        let mut map = HashMap::new();
        for kv in self.body().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(), String::new());
                };
            }
        }
        self.form = map;
    }

    /// Was the given query value sent?
    pub fn has_query(&self, name: &str) -> bool {
        self.query(name).is_some()
    }

    /// Return a value from the ?querystring=
    pub fn query(&self, name: &str) -> Option<&str> {
        let idx = self.full_path().find('?')?;
        self.full_path()[idx + 1..]
            .split('&')
            .filter_map(|s| {
                if s.starts_with(name) && s[name.len()..].starts_with('=') {
                    Some(&s[name.len() + 1..])
                } else {
                    None
                }
            })
            .next()
    }

    /// Request's `cache()` lives for only a single Request, but can
    /// nonethenevertheless be useful to prevent looking up the same
    /// data over and over. The cache is based on the return type of
    /// the function or closure you pass to `cache()` using
    /// [`TypeCache`](struct.TypeCache.html), so make sure to create
    /// little wrapper structs if you want different functions to
    /// return the same common types, like `Vec<String>`:
    ///
    /// ```rust
    /// struct PageNames(Vec<String>);
    /// struct UserNames(Vec<String>);
    /// ```
    ///
    /// Here's an example:
    ///
    /// ```no_run
    /// use vial::prelude::*;
    /// # mod page { pub struct Page { pub name: String } }
    /// use page::Page;
    /// # mod db { use super::Page; pub fn lookup(query: &str) -> Vec<Page> { vec![] } }
    ///
    /// routes! {
    ///     GET "/" => list;
    /// }
    ///
    /// struct PageNames(Vec<String>);
    ///
    /// fn all_pages(_: &Request) -> Vec<Page> {
    ///     db::lookup("select * from pages")
    /// }
    ///
    /// fn page_names(req: &Request) -> PageNames {
    ///     PageNames(req.cache(all_pages)
    ///         .iter()
    ///         .map(|page| page.name.clone())
    ///         .collect::<Vec<_>>())
    /// }
    ///
    /// fn list_of_names(req: &Request) -> String {
    ///     req.cache(page_names)
    ///         .0
    ///         .iter()
    ///         .map(|name| format!("<li>{}</li>", name))
    ///         .collect::<Vec<_>>()
    ///         .join("\n")
    /// }
    ///
    /// fn list(req: Request) -> impl Responder {
    ///     format!(
    ///         "<html>
    ///             <head><title>{title}</title></head>
    ///             <body>
    ///                 <h1>{title}</h1>
    ///                 <h3>There are {page_count} pages:</h3>
    ///                 <ul>
    ///                     {pages}
    ///                 </ul>
    ///             </body>
    ///         </html>",
    ///         title = "List Pages",
    ///         page_count = req.cache(all_pages).len(),
    ///         pages = req.cache(list_of_names),
    ///     )
    /// }
    ///
    /// fn main() {
    ///     run!().unwrap();
    /// }
    /// ```
    pub fn cache<T, F>(&self, fun: F) -> &T
    where
        F: FnOnce(&Request) -> T,
        T: Send + Sync + 'static,
    {
        self.cache.get().unwrap_or_else(|| {
            self.cache.set(fun(&self));
            self.cache.get().unwrap()
        })
    }

    /// Access to global shared state defined with the
    /// [`vial::use_state!`](macro.use_state.html) macro before
    /// starting your application with [`vial::run!`](macro.run.html).
    ///
    /// ```ignore
    /// use std::sync::atomic::{AtomicUsize, Ordering};
    /// use vial::prelude::*;
    ///
    /// routes! {
    ///     #[filter(count)]
    ///     GET "/" => |req|
    ///         format!("Hits: {}", req.state::<Counter>.hits());
    /// }
    ///
    /// fn count(req: &mut Request) -> Option<Response> {
    ///     req.state::<Counter>().incr();
    ///     None
    /// }
    ///
    /// #[derive(Debug, Default)]
    /// struct Counter(AtomicUsize);
    ///
    /// impl Counter {
    ///     fn hits(&self) -> usize {
    ///         self.0.load(Ordering::Relaxed)
    ///     }
    ///     fn incr(&self) {
    ///         self.0.fetch_add(1, Ordering::Relaxed);
    ///     }
    /// }
    ///
    /// fn main() {
    ///     use_state!(Counter::default());
    ///     run!();
    /// }
    /// ```
    pub fn state<T: Send + Sync + 'static>(&self) -> &T {
        crate::storage::get::<T>()
    }

    #[cfg(feature = "cookies")]
    /// Get the value of a cookie sent by the client.
    pub fn cookie(&self, name: &str) -> Option<&str> {
        self.cookies
            .iter()
            .find_map(|(k, v)| if k == name { Some(v.as_ref()) } else { None })
    }
}