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
//! The definition of components for serving an HTTP application by using `App`.

use futures::{self, Async, Future, Poll};
use http::header::HeaderValue;
use http::{header, Request, Response, StatusCode};
use hyper::body::Body;
use hyper::service::{NewService, Service};
use std::mem;
use tokio::executor::{DefaultExecutor, Executor};

use error::{CritError, Error};
use handler::Handle;
use input::upgrade::UpgradeContext;
use input::{Input, InputParts, RequestBody};
use modifier::{AfterHandle, BeforeHandle, Modifier};
use output::{Output, ResponseBody};

use super::{App, RouteData};

impl App {
    /// Creates a new `AppService` to manage a session.
    pub fn new_service(&self) -> AppService {
        AppService { app: self.clone() }
    }
}

impl NewService for App {
    type ReqBody = Body;
    type ResBody = Body;
    type Error = CritError;
    type Service = AppService;
    type InitError = CritError;
    type Future = futures::future::FutureResult<Self::Service, Self::InitError>;

    fn new_service(&self) -> Self::Future {
        futures::future::ok(self.new_service())
    }
}

/// A `Service` representation of the application, created by `App`.
#[derive(Debug)]
pub struct AppService {
    app: App,
}

impl AppService {
    #[allow(missing_docs)]
    pub fn dispatch_request(&mut self, request: Request<RequestBody>) -> AppServiceFuture {
        AppServiceFuture {
            app: self.app.clone(),
            request: Some(request),
            parts: None,
            status: AppServiceFutureStatus::Start,
        }
    }
}

impl Service for AppService {
    type ReqBody = Body;
    type ResBody = Body;
    type Error = CritError;
    type Future = AppServiceFuture;

    #[inline]
    fn call(&mut self, request: Request<Self::ReqBody>) -> Self::Future {
        self.dispatch_request(request.map(RequestBody::from_hyp))
    }
}

/// A future for managing an incoming HTTP request, created by `AppService`.
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct AppServiceFuture {
    app: App,
    request: Option<Request<RequestBody>>,
    parts: Option<InputParts>,
    status: AppServiceFutureStatus,
}

#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
#[derive(Debug)]
enum AppServiceFutureStatus {
    Start,
    BeforeHandle { in_flight: BeforeHandle, pos: usize },
    Handle { in_flight: Handle, pos: usize },
    AfterHandle { in_flight: AfterHandle, pos: usize },
    Done,
}

impl AppServiceFuture {
    fn get_modifier<'a>(&self, pos: usize, app: &'a App) -> Option<&'a (dyn Modifier + Send + Sync + 'static)> {
        app.modifier(*self.get_route(&self.app)?.modifier_ids.get(pos)?)
    }

    fn get_route<'a>(&self, app: &'a App) -> Option<&'a RouteData> {
        app.route(self.parts.as_ref()?.route)
    }

    fn poll_in_flight(&mut self) -> Poll<Output, Error> {
        use self::AppServiceFutureStatus::*;

        macro_rules! input {
            () => {
                Input {
                    request: self.request
                        .as_mut()
                        .expect("This future has already polled"),
                    parts: self.parts.as_mut().expect("This future has already polled"),
                    app: &self.app,
                }
            };
        }

        macro_rules! ready {
            ($e:expr) => {
                match $e {
                    Ok(::futures::Async::Ready(x)) => Ok(x),
                    Ok(::futures::Async::NotReady) => return Ok(::futures::Async::NotReady),
                    Err(e) => Err(e),
                }
            };
        }

        #[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
        enum Polled {
            BeforeHandle(Option<Result<Output, Error>>),
            Handle(Result<Output, Error>),
            AfterHandle(Result<Output, Error>),
            Empty,
        }

        let result = loop {
            let polled = match self.status {
                Start => Polled::Empty,
                BeforeHandle { ref mut in_flight, .. } => {
                    // FIXME: use result.transpose()
                    Polled::BeforeHandle(match ready!(in_flight.poll_ready(&mut input!())) {
                        Ok(Some(x)) => Some(Ok(x)),
                        Ok(None) => None,
                        Err(e) => Some(Err(e)),
                    })
                }
                Handle { ref mut in_flight, .. } => Polled::Handle(ready!(in_flight.poll_ready(&mut input!()))),
                AfterHandle { ref mut in_flight, .. } => {
                    Polled::AfterHandle(ready!(in_flight.poll_ready(&mut input!())))
                }
                Done => panic!("unexpected state"),
            };

            self.status = match (mem::replace(&mut self.status, Done), polled) {
                (Start, Polled::Empty) => {
                    {
                        let request = self.request.as_ref().expect("This future has already polled");
                        let (pos, params) = match self.app.recognize(request.uri().path(), request.method()) {
                            Ok(r) => r,
                            Err(e) => break Err(e),
                        };
                        let route_id = self.app.inner.routes[pos].id;
                        debug_assert_eq!(route_id.1, pos);
                        self.parts = Some(InputParts::new(route_id, params));
                    }

                    match self.get_modifier(0, &self.app) {
                        Some(modifier) => BeforeHandle {
                            in_flight: modifier.before_handle(&mut input!()),
                            pos: 0,
                        },
                        None => match self.get_route(&self.app) {
                            Some(endpoint) => Handle {
                                in_flight: endpoint.handler.handle(&mut input!()),
                                pos: 0,
                            },
                            None => panic!(""),
                        },
                    }
                }

                (BeforeHandle { pos, .. }, Polled::BeforeHandle(result)) => match result {
                    Some(result) => match pos.checked_sub(1) {
                        Some(pos) => match self.get_modifier(pos, &self.app) {
                            Some(modifier) => AfterHandle {
                                in_flight: modifier.after_handle(&mut input!(), result),
                                pos,
                            },
                            None => break result,
                        },
                        None => break result,
                    },
                    None => match self.get_modifier(pos + 1, &self.app) {
                        Some(modifier) => BeforeHandle {
                            in_flight: modifier.before_handle(&mut input!()),
                            pos: pos + 1,
                        },
                        None => match self.get_route(&self.app) {
                            Some(endpoint) => Handle {
                                in_flight: endpoint.handler.handle(&mut input!()),
                                pos: pos + 1,
                            },
                            None => panic!(""),
                        },
                    },
                },

                (Handle { pos, .. }, Polled::Handle(result)) => match pos.checked_sub(1) {
                    Some(pos) => match self.get_modifier(pos, &self.app) {
                        Some(modifier) => AfterHandle {
                            in_flight: modifier.after_handle(&mut input!(), result),
                            pos,
                        },
                        None => break result,
                    },
                    None => break result,
                },

                (AfterHandle { pos, .. }, Polled::AfterHandle(result)) => match pos.checked_sub(1) {
                    Some(pos) => match self.get_modifier(pos, &self.app) {
                        Some(modifier) => AfterHandle {
                            in_flight: modifier.after_handle(&mut input!(), result),
                            pos,
                        },
                        None => break result,
                    },
                    None => break result,
                },

                _ => panic!("unexpected state"),
            }
        };

        result.map(Async::Ready)
    }

    #[allow(missing_docs)]
    pub fn poll_ready(&mut self, exec: &mut impl Executor) -> Poll<Response<ResponseBody>, CritError> {
        match self.poll_in_flight() {
            Ok(Async::Ready(output)) => self.handle_response(output, exec).map(Async::Ready),
            Ok(Async::NotReady) => Ok(Async::NotReady),
            Err(err) => {
                self.status = AppServiceFutureStatus::Done;
                self.handle_error(err).map(Async::Ready)
            }
        }
    }

    fn handle_response(
        &mut self,
        mut output: Output,
        exec: &mut impl Executor,
    ) -> Result<Response<ResponseBody>, CritError> {
        let (request, body) = {
            let request = self.request.take().expect("This future has already polled.");
            let (parts, body) = request.into_parts();
            (Request::from_parts(parts, ()), body)
        };
        let InputParts {
            cookies,
            locals,
            route,
            params,
            ..
        } = self.parts.take().expect("This future has already polled");

        // append Cookie entries.
        cookies.append_to(output.headers_mut());

        // append the value of Content-Length to the response header if missing.
        if let Some(len) = output.body().content_length() {
            output.headers_mut().entry(header::CONTENT_LENGTH)?.or_insert_with(|| {
                // safety: '0'-'9' is ascci.
                // TODO: more efficient
                unsafe { HeaderValue::from_shared_unchecked(len.to_string().into()) }
            });
        }

        // spawn the upgrade task.
        if let (Some(body), Some(upgrade)) = body.deconstruct() {
            if output.status() == StatusCode::SWITCHING_PROTOCOLS {
                let app = self.app.clone();
                exec.spawn(Box::new(
                    body.on_upgrade()
                        .map_err(|e| error!("upgrade error: {}", e))
                        .and_then(move |io| {
                            let cx = UpgradeContext {
                                request,
                                locals,
                                route,
                                params,
                                app,
                            };
                            upgrade.upgrade(io, cx)
                        }),
                )).map_err(|_| format_err!("failed spawn the upgrade task").compat())?;
            }
        }

        Ok(output)
    }

    fn handle_error(&mut self, err: Error) -> Result<Response<ResponseBody>, CritError> {
        let request = self.request
            .take()
            .expect("This future has already polled")
            .map(mem::drop);
        drop(self.parts.take());

        if let Some(err) = err.as_http_error() {
            let response = self.app.error_handler().handle_error(err, &request)?;
            return Ok(response);
        }

        Err(err.into_critical().unwrap())
    }
}

impl futures::Future for AppServiceFuture {
    type Item = Response<Body>;
    type Error = CritError;

    fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
        // FIXME: use futures::task::Context::executor() instead.
        let mut exec = DefaultExecutor::current();
        self.poll_ready(&mut exec)
            .map(|x| x.map(|response| response.map(ResponseBody::into_hyp)))
    }
}