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
//! Defines types for handlers, the primary building block of a Gotham application.
//!
//! A function can be used directly as a handler using one of the default implementations of
//! `Handler`, but the traits can also be implemented directly for greater control. See the
//! `Handler` trait for some examples of valid handlers.
use std::borrow::Cow;
use std::panic::RefUnwindSafe;
use bytes::Bytes;
use futures::{future, Future};
use hyper::{Body, Chunk, Response, StatusCode};
use mime::{self, Mime};
use crate::helpers::http::response;
use crate::state::State;
mod error;
use crate::error::*;
/// Defines handlers for serving static assets.
pub mod assets;
pub use self::error::{HandlerError, IntoHandlerError};
/// A type alias for the trait objects returned by `HandlerService`.
///
/// When the `Future` resolves to an error, the `(State, HandlerError)` value is used to generate
/// an appropriate HTTP error response.
pub type HandlerFuture =
dyn Future<Item = (State, Response<Body>), Error = (State, HandlerError)> + Send;
/// A `Handler` is an asynchronous function, taking a `State` value which represents the request
/// and related runtime state, and returns a future which resolves to a response.
///
/// This represents the common entry point for the parts of a Gotham application, and is used with
/// the `Router` API to describe how a request should be dispatched and handled.
///
/// The `Handler` is created and consumed by each request. In the most common case (a bare function
/// acting as a `Handler`) the `Handler + Copy` traits allow the `Handler` to be copied for each
/// request, and the copy consumed. For a closure or a custom handler, the `NewHandler`
/// implementation creates a `Handler` value for each request.
///
/// # Examples
///
/// The simplest kind of handler is a bare function which returns a synchronous response. This is
/// useful when we don't need to do any I/O before generating a response.
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// #
/// # use hyper::{Body, Response};
/// # use gotham::handler::Handler;
/// # use gotham::state::State;
/// #
/// # fn main() {
/// fn my_handler(_state: State) -> (State, Response<Body>) {
/// // Implementation elided.
/// # unimplemented!()
/// }
/// #
/// # fn assert_type<H>(_h: H) where H: Handler + Copy {}
/// # assert_type(my_handler);
/// # }
/// ```
///
/// An asynchronous handler returns a `HandlerFuture` that will resolve to the response. For
/// example, this allows I/O work to begin, and for the Gotham app to continue generating a
/// response once the work completes.
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// #
/// # use gotham::handler::{Handler, HandlerFuture};
/// # use gotham::state::State;
/// #
/// # fn main() {
/// fn async_handler(_state: State) -> Box<HandlerFuture> {
/// // Implementation elided.
/// # unimplemented!()
/// }
/// #
/// # fn assert_type<H>(_h: H) where H: Handler + Copy {}
/// # assert_type(async_handler);
/// # }
/// ```
///
/// A closure can implement `Handler` automatically, in the same way as a bare function. When
/// constructing a `Handler` in this way, a wrapping closure must also be used to implement the
/// `NewHandler` trait.
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// # extern crate futures;
/// #
/// # use gotham::handler::{HandlerFuture, NewHandler};
/// # use gotham::state::State;
/// # use futures::future;
/// #
/// # fn main() {
/// let new_handler = || {
/// let handler = |_state: State| {
/// // Implementation elided.
/// # Box::new(future::empty()) as Box<HandlerFuture>
/// };
/// Ok(handler)
/// };
///
/// // Pass `new_handler` to the router, using the `to_new_handler` API.
/// #
/// # fn assert_type<H>(_h: H) where H: NewHandler {}
/// # assert_type(new_handler);
/// # }
/// ```
///
/// A custom handler, which implements the `NewHandler` and `Handler` traits directly for greater
/// control. See the `NewHandler` trait for more examples of custom handlers.
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// #
/// # use gotham::handler::{Handler, HandlerFuture, NewHandler};
/// # use gotham::state::State;
/// # use gotham::error::*;
/// #
/// # fn main() {
/// #[derive(Copy, Clone)]
/// struct MyCustomHandler;
///
/// impl NewHandler for MyCustomHandler {
/// type Instance = Self;
///
/// fn new_handler(&self) -> Result<Self::Instance> {
/// Ok(*self)
/// }
/// }
///
/// impl Handler for MyCustomHandler {
/// fn handle(self, _state: State) -> Box<HandlerFuture> {
/// // Implementation elided.
/// # unimplemented!()
/// }
/// }
/// #
/// # fn assert_type<H>(_h: H) where H: NewHandler {}
/// # assert_type(MyCustomHandler);
/// # }
/// ```
pub trait Handler: Send {
/// Handles the request, returning a boxed future which resolves to a response.
fn handle(self, state: State) -> Box<HandlerFuture>;
}
impl<F, R> Handler for F
where
F: FnOnce(State) -> R + Send,
R: IntoHandlerFuture,
{
fn handle(self, state: State) -> Box<HandlerFuture> {
self(state).into_handler_future()
}
}
/// A type which is used to spawn new `Handler` values. When implementing a custom `Handler` type,
/// this is used to define how instances of the `Handler` are created.
///
/// The `Instance` associated type is usually `Self` in the simple case, but can be a different
/// type where greater control is needed over lifetimes.
///
/// # Examples
///
/// A custom handler which implements `NewHandler` by copying itself.
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// #
/// # use gotham::handler::{Handler, HandlerFuture, NewHandler};
/// # use gotham::state::State;
/// # use gotham::error::*;
/// #
/// # fn main() {
/// #[derive(Copy, Clone)]
/// struct MyCustomHandler;
///
/// impl NewHandler for MyCustomHandler {
/// type Instance = Self;
///
/// fn new_handler(&self) -> Result<Self::Instance> {
/// Ok(*self)
/// }
/// }
///
/// impl Handler for MyCustomHandler {
/// fn handle(self, _state: State) -> Box<HandlerFuture> {
/// // Implementation elided.
/// # unimplemented!()
/// }
/// }
/// #
/// # fn assert_type<H>(_h: H) where H: NewHandler {}
/// # assert_type(MyCustomHandler);
/// # }
/// ```
///
/// A custom handler which implements `NewHandler` using a specific `Instance` type.
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// #
/// # use gotham::handler::{Handler, HandlerFuture, NewHandler};
/// # use gotham::state::State;
/// # use gotham::error::*;
/// #
/// # fn main() {
/// #[derive(Copy, Clone)]
/// struct MyValueInstantiatingHandler;
///
/// impl NewHandler for MyValueInstantiatingHandler {
/// type Instance = MyHandler;
///
/// fn new_handler(&self) -> Result<Self::Instance> {
/// Ok(MyHandler)
/// }
/// }
///
/// struct MyHandler;
///
/// impl Handler for MyHandler {
/// fn handle(self, _state: State) -> Box<HandlerFuture> {
/// // Implementation elided.
/// # unimplemented!()
/// }
/// }
/// #
/// # fn assert_type<H>(_h: H) where H: NewHandler {}
/// # assert_type(MyValueInstantiatingHandler);
/// # }
/// ```
pub trait NewHandler: Send + Sync + RefUnwindSafe {
/// The type of `Handler` created by the `NewHandler`.
type Instance: Handler + Send;
/// Create and return a new `Handler` value.
fn new_handler(&self) -> Result<Self::Instance>;
}
impl<F, H> NewHandler for F
where
F: Fn() -> Result<H> + Send + Sync + RefUnwindSafe,
H: Handler + Send,
{
type Instance = H;
fn new_handler(&self) -> Result<H> {
self()
}
}
/// Represents a type which can be converted into the future type returned by a `Handler`.
///
/// This is used to allow functions with different return types to satisfy the `Handler` trait
/// bound via the generic function implementation.
pub trait IntoHandlerFuture {
/// Converts this value into a boxed future resolving to a state and response.
fn into_handler_future(self) -> Box<HandlerFuture>;
}
impl<T> IntoHandlerFuture for (State, T)
where
T: IntoResponse,
{
fn into_handler_future(self) -> Box<HandlerFuture> {
let (state, t) = self;
let response = t.into_response(&state);
Box::new(future::ok((state, response)))
}
}
impl IntoHandlerFuture for Box<HandlerFuture> {
fn into_handler_future(self) -> Box<HandlerFuture> {
self
}
}
/// Represents a type which can be converted to a response. This trait is used in converting the
/// return type of a function into a response.
///
/// # Examples
///
/// ```rust
/// # #![allow(deprecated)] // TODO: Refactor this.
/// #
/// # extern crate gotham;
/// # extern crate hyper;
/// #
/// # use gotham::state::State;
/// # use gotham::pipeline::set::*;
/// # use gotham::router::Router;
/// # use gotham::router::route::{RouteImpl, Extractors, Delegation};
/// # use gotham::router::tree::Tree;
/// # use gotham::router::route::matcher::MethodOnlyRouteMatcher;
/// # use gotham::router::route::dispatch::DispatcherImpl;
/// # use gotham::handler::IntoResponse;
/// # use gotham::extractor::{NoopPathExtractor, NoopQueryStringExtractor};
/// # use gotham::router::response::finalizer::ResponseFinalizerBuilder;
/// # use hyper::Method;
/// # use hyper::StatusCode;
/// # use hyper::{Body, Response};
/// #
/// struct MyStruct {
/// value: String
/// }
///
/// impl MyStruct {
/// fn new() -> MyStruct {
/// // ...
/// # MyStruct { value: "".to_owned() }
/// }
/// }
///
/// impl IntoResponse for MyStruct {
/// fn into_response(self, _state: &State) -> Response<Body> {
/// Response::builder()
/// .status(StatusCode::OK)
/// .body(self.value.into())
/// .unwrap()
/// }
/// }
///
/// fn handler(state: State) -> (State, MyStruct) {
/// (state, MyStruct::new())
/// }
///
/// # fn main() {
/// # let mut tree = Tree::new();
/// # let pipeline_set = finalize_pipeline_set(new_pipeline_set());
/// # let finalizer = ResponseFinalizerBuilder::new().finalize();
/// # let matcher = MethodOnlyRouteMatcher::new(vec![Method::GET]);
/// # let dispatcher = DispatcherImpl::new(|| Ok(handler), (), pipeline_set);
/// # let extractors: Extractors<NoopPathExtractor, NoopQueryStringExtractor> = Extractors::new();
/// # let route = RouteImpl::new(matcher, Box::new(dispatcher), extractors, Delegation::Internal);
/// tree.add_route(Box::new(route));
/// Router::new(tree, finalizer);
/// # }
/// ```
pub trait IntoResponse {
/// Converts this value into a `hyper::Response`
fn into_response(self, state: &State) -> Response<Body>;
}
impl IntoResponse for Response<Body> {
fn into_response(self, _state: &State) -> Response<Body> {
self
}
}
impl<T, E> IntoResponse for ::std::result::Result<T, E>
where
T: IntoResponse,
E: IntoResponse,
{
fn into_response(self, state: &State) -> Response<Body> {
match self {
Ok(res) => res.into_response(state),
Err(e) => e.into_response(state),
}
}
}
impl<B> IntoResponse for (Mime, B)
where
B: Into<Body>,
{
fn into_response(self, state: &State) -> Response<Body> {
(StatusCode::OK, self.0, self.1).into_response(state)
}
}
impl<B> IntoResponse for (StatusCode, Mime, B)
where
B: Into<Body>,
{
fn into_response(self, state: &State) -> Response<Body> {
response::create_response(state, self.0, self.1, self.2)
}
}
// derive IntoResponse for Into<Body> types
macro_rules! derive_into_response {
($type:ty) => {
impl IntoResponse for $type {
fn into_response(self, state: &State) -> Response<Body> {
(StatusCode::OK, mime::TEXT_PLAIN, self).into_response(state)
}
}
};
}
// derive Into<Body> types - this is required because we
// can't impl IntoResponse for Into<Body> due to Response<T>
// and the potential it will add Into<Body> in the future
derive_into_response!(Bytes);
derive_into_response!(Chunk);
derive_into_response!(String);
derive_into_response!(Vec<u8>);
derive_into_response!(&'static str);
derive_into_response!(&'static [u8]);
derive_into_response!(Cow<'static, str>);
derive_into_response!(Cow<'static, [u8]>);