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
//! Defines the `GothamService` type which is used to wrap a Gotham application and interface with
//! Hyper.

use std::thread;
use std::net::SocketAddr;
use std::sync::Arc;
use std::panic::AssertUnwindSafe;

use hyper;
use hyper::server::Service;
use hyper::{Request, Response};
use futures::Future;
use tokio_core::reactor::Handle;

use handler::NewHandler;
use state::{request_id, set_request_id, State};
use state::client_addr::put_client_addr;
use http::request::path::RequestPathSegments;

mod timing;
mod trap;

/// Wraps a `NewHandler` which will be used to serve requests. Used in `gotham::os::*` to bind
/// incoming connections to `ConnectedGothamService` values.
pub(crate) struct GothamService<T>
where
    T: NewHandler + 'static,
{
    t: Arc<T>,
    handle: Handle,
}

impl<T> GothamService<T>
where
    T: NewHandler + 'static,
{
    pub(crate) fn new(t: Arc<T>, handle: Handle) -> GothamService<T> {
        GothamService { t, handle }
    }

    pub(crate) fn connect(&self, client_addr: SocketAddr) -> ConnectedGothamService<T> {
        ConnectedGothamService {
            t: self.t.clone(),
            handle: self.handle.clone(),
            client_addr,
        }
    }
}

/// A `GothamService` which has been connected to a client. The major difference is that a
/// `client_addr` has been assigned (as this isn't available from Hyper).
pub(crate) struct ConnectedGothamService<T>
where
    T: NewHandler + 'static,
{
    t: Arc<T>,
    handle: Handle,
    client_addr: SocketAddr,
}

impl<T> Service for ConnectedGothamService<T>
where
    T: NewHandler,
{
    type Request = Request;
    type Response = Response;
    type Error = hyper::Error;
    type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;

    fn call(&self, req: Self::Request) -> Self::Future {
        let mut state = State::new();

        put_client_addr(&mut state, self.client_addr);

        let (method, uri, version, headers, body) = req.deconstruct();

        state.put(self.handle.clone());
        state.put(RequestPathSegments::new(uri.path()));
        state.put(method);
        state.put(uri);
        state.put(version);
        state.put(headers);
        state.put(body);
        set_request_id(&mut state);

        debug!(
            "[DEBUG][{}][Thread][{:?}]",
            request_id(&state),
            thread::current().id(),
        );

        trap::call_handler(self.t.as_ref(), AssertUnwindSafe(state))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use hyper::{Method, StatusCode};
    use tokio_core::reactor::Core;

    use http::response::create_response;
    use router::builder::*;
    use state::State;

    fn handler(state: State) -> (State, Response) {
        let res = create_response(&state, StatusCode::Accepted, None);
        (state, res)
    }

    #[test]
    fn new_handler_closure() {
        let mut core = Core::new().unwrap();
        let service = GothamService::new(Arc::new(|| Ok(handler)), core.handle());

        let req = Request::new(Method::Get, "http://localhost/".parse().unwrap());
        let f = service
            .connect("127.0.0.1:10000".parse().unwrap())
            .call(req);
        let response = core.run(f).unwrap();
        assert_eq!(response.status(), StatusCode::Accepted);
    }

    #[test]
    fn router() {
        let router = build_simple_router(|route| {
            route.get("/").to(handler);
        });

        let mut core = Core::new().unwrap();
        let service = GothamService::new(Arc::new(router), core.handle());

        let req = Request::new(Method::Get, "http://localhost/".parse().unwrap());
        let f = service
            .connect("127.0.0.1:10000".parse().unwrap())
            .call(req);
        let response = core.run(f).unwrap();
        assert_eq!(response.status(), StatusCode::Accepted);
    }
}