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
use std::ops::Deref;

use tokio_core::reactor::Handle;
use typemap::{Key, TypeMap};

use request::{Body, Request};

/// `Context` represents the context of the current HTTP request.
///
/// A `Context` consists of:
///     - The current HTTP [`Request`].
///     - A [`Handle`] referencing the event loop in which this request is being
///       handled.
///
/// [`Handle`]: https://docs.rs/tokio-core/0.1/tokio_core/reactor/struct.Handle.html
/// [`Request`]: ../request/struct.Request.html
pub struct Context {
    state: TypeMap,
    handle: Handle,
    request: Request,
}

impl Context {
    pub(crate) fn new(handle: Handle, request: Request) -> Self {
        Context {
            handle,
            request,
            state: TypeMap::new(),
        }
    }

    /// Return a reference to a handle to the event loop this `Context` is associated with.
    #[inline]
    pub fn handle(&self) -> &Handle {
        &self.handle
    }

    /// Take the request body.
    pub fn body(self) -> Body {
        self.request.body()
    }

    /// Puts a value into the request context.
    pub fn put<K: Key>(&mut self, value: K::Value) {
        self.state.insert::<K>(value);
    }

    /// Gets a value from the request context.
    ///
    /// # Panics
    ///
    /// If there is no value in the request context of the given type.
    pub fn get<K: Key>(&self) -> &K::Value {
        self.state.get::<K>().unwrap()
    }

    /// Gets a value from the request context.
    pub fn try_get<K: Key>(&self) -> Option<&K::Value> {
        self.state.get::<K>()
    }
}

impl Deref for Context {
    type Target = Request;

    fn deref(&self) -> &Self::Target {
        &self.request
    }
}