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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
//! Implements hyper `Service` adapters that reduce boilerplate. //! //! This module contains adapters for hyper's `Service` trait that make common //! operations easier and require less boilerplate: //! * [`AsyncService`] and [`SyncService`] can be directly passed to a hyper //! server and will decode incoming requests for you and invoke a handler //! closure. They make it very easy to use any type implementing //! [`FromRequest`] as the main entry point of your app. //! * [`ServiceExt`] provides adapter methods on Hyper `Service`s that simplify //! common patterns like catching panics. //! //! [`AsyncService`]: struct.AsyncService.html //! [`SyncService`]: struct.SyncService.html //! [`ServiceExt`]: trait.ServiceExt.html //! [`FromRequest`]: ../trait.FromRequest.html use crate::{BoxedError, DefaultFuture, Error, FromRequest, NoContext}; use futures::{future::FutureResult, Future, IntoFuture}; use hyper::{ service::{MakeService, Service}, Body, Method, Request, Response, }; use std::any::Any; use std::fmt; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::sync::Arc; /// Asynchronous hyper service adapter. /// /// This implements `hyper::service::Service`, decodes incoming requests using /// [`FromRequest`] and passes the result to a provided handler closure. /// /// Using this type takes a bit of boilerplate away from your app. Specifically, /// it handles: /// /// * Suppressing the body of the response when the request used `HEAD`. /// * Turning any [`hyperdrive::Error`] into a proper HTTP response. /// /// This type stores an async request handler `H` and the context needed by the /// [`FromRequest`] implementation. The context is cloned for every request. /// /// # Type Parameters /// /// * **`H`**: The handler closure. Takes a [`FromRequest`] implementor `R`, and /// the original request. Returns a future resolving to the response to return /// to the client. Shared via `Arc`. /// * **`R`**: The request type expected by the handler `H`. Implements /// [`FromRequest`]. /// * **`F`**: The `Future` returned by the handler closure `H`. /// /// # Examples /// /// ``` /// use hyperdrive::{FromRequest, service::AsyncService}; /// use hyper::{Server, Response, Request, Body}; /// use futures::prelude::*; /// use std::sync::Arc; /// /// #[derive(FromRequest)] /// enum Route { /// #[get("/")] /// Index, /// } /// /// let service = AsyncService::new(|route: Route, orig: Arc<Request<()>>| { /// // The closure is called with the `FromRequest`-implementing type and /// // the original request. It has to return any type implementing /// // `Future`. /// match route { /// Route::Index => { /// Ok(Response::new(Body::from("Hello World!"))).into_future() /// } /// } /// }); /// /// // Create the server future: /// let srv = Server::bind(&"127.0.0.1:0".parse().unwrap()) /// .serve(service); /// ``` /// /// [`FromRequest`]: ../trait.FromRequest.html /// [`hyperdrive::Error`]: ../struct.Error.html pub struct AsyncService<H, R, F> where H: Fn(R, Arc<Request<()>>) -> F + Send + Sync + 'static, R: FromRequest, R::Context: Clone, R::Future: 'static, F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static, { handler: Arc<H>, context: R::Context, } impl<H, R, F> AsyncService<H, R, F> where H: Fn(R, Arc<Request<()>>) -> F + Send + Sync + 'static, R: FromRequest<Context = NoContext>, R::Future: 'static, F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static, { /// Creates an `AsyncService` from a handler closure. /// /// This will pass a [`NoContext`] to the [`FromRequest`] implementation, /// which will work fine as long as your type doesn't require a custom /// context. If you need to pass a custom context, refer to /// [`with_context`]. /// /// [`NoContext`]: ../struct.NoContext.html /// [`FromRequest`]: ../trait.FromRequest.html /// [`with_context`]: #method.with_context pub fn new(handler: H) -> Self { Self::with_context(handler, NoContext) } } impl<H, R, F> AsyncService<H, R, F> where H: Fn(R, Arc<Request<()>>) -> F + Send + Sync + 'static, R: FromRequest, R::Context: Clone, R::Future: 'static, F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static, { /// Creates an `AsyncService` that will call `handler` to process incoming /// requests. /// /// # Parameters /// /// * **`handler`**: The handler closure. This is stored in an `Arc` and is /// passed every decoded request `R`. Returns a future `F` resolving to /// the response to return. /// * **`context`**: The context to pass to your [`FromRequest`] /// implementor. /// /// [`FromRequest`]: ../trait.FromRequest.html pub fn with_context(handler: H, context: R::Context) -> Self { Self { handler: Arc::new(handler), context, } } } impl<H, R, F> Clone for AsyncService<H, R, F> where H: Fn(R, Arc<Request<()>>) -> F + Send + Sync + 'static, R: FromRequest, R::Context: Clone, R::Future: 'static, F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static, { fn clone(&self) -> Self { Self { handler: self.handler.clone(), context: self.context.clone(), } } } impl<C, H, R, F> MakeService<C> for AsyncService<H, R, F> where H: Fn(R, Arc<Request<()>>) -> F + Send + Sync + 'static, R: FromRequest, R::Context: Clone, R::Future: 'static, F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static, { type ReqBody = Body; type ResBody = Body; type Error = BoxedError; type Service = Self; type Future = FutureResult<Self, BoxedError>; type MakeError = BoxedError; fn make_service(&mut self, _ctx: C) -> Self::Future { Ok(self.clone()).into_future() } } impl<H, R, F> Service for AsyncService<H, R, F> where H: Fn(R, Arc<Request<()>>) -> F + Send + Sync + 'static, R: FromRequest, R::Context: Clone, R::Future: 'static, F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static, { type ReqBody = Body; type ResBody = Body; type Error = BoxedError; type Future = DefaultFuture<Response<Body>, BoxedError>; fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future { let is_head = req.method() == Method::HEAD; let handler = self.handler.clone(); let (parts, body) = req.into_parts(); let req = Arc::new(Request::from_parts(parts, ())); let fut = R::from_request_and_body(&req, body, self.context.clone()) .and_then(move |r| handler(r, req)) .map(move |response| { if is_head { // Responses to HEAD requests must have an empty body response.map(|_| Body::empty()) } else { response } }) .or_else(|err| { if let Some(our_error) = err.downcast_ref::<Error>() { Ok(our_error.response().map(|()| Body::empty())) } else { Err(err) } }); Box::new(fut) } } impl<H, R, F> fmt::Debug for AsyncService<H, R, F> where H: Fn(R, Arc<Request<()>>) -> F + Send + Sync + 'static, R: FromRequest, R::Context: Clone + fmt::Debug, R::Future: 'static, F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Closures aren't debug-printable, so we print a few Arc stats instead #[derive(Debug)] struct HandlerRef { strong_count: usize, weak_count: usize, } f.debug_struct("AsyncService") .field( "handler", &HandlerRef { strong_count: Arc::strong_count(&self.handler), weak_count: Arc::weak_count(&self.handler), }, ) .field("context", &self.context) .finish() } } /// A hyper `Service` that dispatches requests to a blocking handler. /// /// Just like [`AsyncService`], using this type takes a bit of boilerplate away /// from your app. Specifically, it handles: /// /// * Suppressing the body of the response when the request used `HEAD`. /// * Turning any [`hyperdrive::Error`] into a proper HTTP response. /// /// This is effectively a bridge between async hyper and a synchronous, /// blocking app. Writing sync code is much simpler than writing async code /// (even with async/await syntax), so depending on your app this might be a /// good tradeoff. /// /// # Type Parameters /// /// * **`H`**: The handler closure. It is called with the request type `R` and /// the original request. It has to return the `Response<Body>` to send to the /// client. /// * **`R`**: The request type implementing `FromRequest`. /// /// # Examples /// /// ``` /// use hyperdrive::{FromRequest, service::SyncService}; /// use hyper::{Request, Response, Body, Server}; /// use std::sync::Arc; /// /// #[derive(FromRequest)] /// enum Route { /// #[get("/")] /// Index, /// } /// /// let service = SyncService::new(|route: Route, orig: Arc<Request<()>>| { /// match route { /// Route::Index => Response::new(Body::from("Hello world!")), /// } /// }); /// /// // Create the server future: /// let srv = Server::bind(&"127.0.0.1:0".parse().unwrap()) /// .serve(service); /// ``` /// /// [`AsyncService`]: struct.AsyncService.html /// [`hyperdrive::Error`]: ../struct.Error.html pub struct SyncService<H, R> where H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static, R: FromRequest + Send + 'static, R::Context: Clone, { handler: Arc<H>, context: R::Context, } impl<H, R> SyncService<H, R> where H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static, R: FromRequest<Context = NoContext> + Send + 'static, { /// Creates a `SyncService` that will call `handler` to process incoming /// requests. pub fn new(handler: H) -> Self { Self::with_context(handler, NoContext) } } impl<H, R> SyncService<H, R> where H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static, R: FromRequest + Send + 'static, R::Context: Clone, { /// Creates a `SyncService` that will call `handler` to process incoming /// requests. /// /// # Parameters /// /// * **`handler`**: The handler closure. This is stored in an `Arc` and is /// called with every decoded request `R`. Returns the response to return /// to the client. /// * **`context`**: The context to pass to your [`FromRequest`] /// implementor. If you don't need a special context, [`new()`] should be /// called instead. /// /// [`new()`]: #method.new /// [`FromRequest`]: ../trait.FromRequest.html pub fn with_context(handler: H, context: R::Context) -> Self { Self { handler: Arc::new(handler), context, } } } impl<H, R> Clone for SyncService<H, R> where H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static, R: FromRequest + Send + 'static, R::Context: Clone, { fn clone(&self) -> Self { Self { handler: self.handler.clone(), context: self.context.clone(), } } } impl<C, H, R> MakeService<C> for SyncService<H, R> where H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static, R: FromRequest + Send + 'static, R::Context: Clone, { type ReqBody = Body; type ResBody = Body; type Error = BoxedError; type Service = Self; type Future = FutureResult<Self, BoxedError>; type MakeError = BoxedError; fn make_service(&mut self, _ctx: C) -> Self::Future { Ok(self.clone()).into_future() } } impl<H, R> Service for SyncService<H, R> where H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static, R: FromRequest + Send + 'static, R::Context: Clone, { type ReqBody = Body; type ResBody = Body; type Error = BoxedError; type Future = DefaultFuture<Response<Body>, BoxedError>; fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future { let is_head = req.method() == Method::HEAD; let handler = self.handler.clone(); let (parts, body) = req.into_parts(); let req = Arc::new(Request::from_parts(parts, ())); let fut = R::from_request_and_body(&req, body, self.context.clone()) .and_then(move |route| { // Run the sync handler on the blocking thread pool. crate::blocking(move || Ok(handler(route, req))) }) .map(move |response| { if is_head { // Responses to HEAD requests must have an empty body response.map(|_| Body::empty()) } else { response } }) .or_else(|err| { if let Some(our_error) = err.downcast_ref::<Error>() { Ok(our_error.response().map(|()| Body::empty())) } else { Err(err) } }); Box::new(fut) } } impl<H, R> fmt::Debug for SyncService<H, R> where H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static, R: FromRequest + Send + 'static, R::Context: Clone + fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Closures aren't debug-printable, so we print a few Arc stats instead #[derive(Debug)] struct HandlerRef { strong_count: usize, weak_count: usize, } f.debug_struct("SyncService") .field( "handler", &HandlerRef { strong_count: Arc::strong_count(&self.handler), weak_count: Arc::weak_count(&self.handler), }, ) .field("context", &self.context) .finish() } } /// Extension trait for types implementing Hyper's `Service` trait. /// /// This adds a number of convenience methods that can be used to build robust /// applications with Hyper and Hyperdrive. pub trait ServiceExt: Service + Sized { /// Catches any panics that occur in the service `self`, and calls an /// asynchronous panic handler with the panic payload. /// /// The `handler` can decide if and how the request should be answered. A /// common option is to return a `500 Internal Server Error` response to the /// client. If the handler returns an error, the connection will be dropped /// and no response will be sent, which mirrors the behavior of Hyper. /// /// **Note**: Panics occurring inside of `handler` will not be caught again. /// The behavior in this case depends on the futures executor in use. When /// using tokio, it will catch the panic in the worker thread and recover. /// The connection to the client will be dropped. /// /// **Note**: Like `std::panic::catch_unwind`, this only works when the /// final binary is compiled with `panic = unwind` (the default). Using /// `panic = abort` will *always* abort the whole process on any panic and /// cannot be caught. /// /// **Note**: This mechanism is not very suitable for *logging* panics, /// since no useful backtrace can be constructed and no location information /// is available. The panic hook mechanism in the standard library is better /// suited for that (see `std::panic::set_hook`). /// /// # Examples /// /// ```rust,no_run /// use hyperdrive::{FromRequest, service::*}; /// use hyper::{Body, Server, Response}; /// use futures::Future; /// use http::StatusCode; /// /// #[derive(FromRequest)] /// enum Routes { /// #[get("/")] /// Panic, /// } /// /// let service = SyncService::new(|route: Routes, orig_request| { /// match route { /// Routes::Panic => panic!("Oops, something went wrong!"), /// } /// }).catch_unwind(|panic_payload| { /// // We ignore the payload here. We could also downcast it to `String`/`&'static str` /// // and include it in the response. /// let _ = panic_payload; /// /// let message = r#" /// <!DOCTYPE html> /// <html> /// <body> /// <h1>Internal Server Error</h1> /// <p> /// The server has encountered an internal error and can not process /// your request at this time. Please try again later or contact us /// at <pre>help@example.com</pre>. /// </p> /// </body> /// </html> /// "#; /// /// Ok(Response::builder() /// .status(StatusCode::INTERNAL_SERVER_ERROR) /// .header("Content-Type", "text/html") /// .body(Body::from(message)) /// .expect("couldn't build response")) /// }).make_service_by_cloning(); /// /// let server = Server::bind(&"127.0.0.1:0".parse().unwrap()) /// .serve(service); /// /// tokio::run(server.map_err(|e| { /// panic!("unexpected error: {}", e); /// })); /// ``` fn catch_unwind<H, R>(self, handler: H) -> CatchUnwind<Self, R, H> where Self: Service<ResBody = Body, Error = BoxedError> + Sync, Self::Future: Send, H: Fn(Box<dyn Any + Send>) -> R + Send + Sync + 'static, R: IntoFuture<Item = Response<Body>, Error = BoxedError>, R::Future: Send + 'static; /// Creates a type implementing `MakeService` by cloning `self` for every /// incoming connection. /// /// The result of this can be directly passed to Hyper's `Builder::serve`. fn make_service_by_cloning(self) -> MakeServiceByCloning<Self> where Self: Clone; } impl<T: Service> ServiceExt for T { fn catch_unwind<H, R>(self, handler: H) -> CatchUnwind<Self, R, H> where Self: Service<ResBody = Body, Error = BoxedError> + Sync, Self::Future: Send, H: Fn(Box<dyn Any + Send>) -> R + Send + Sync + 'static, R: IntoFuture<Item = Response<Body>, Error = BoxedError>, R::Future: Send + 'static, { CatchUnwind { inner: self, handler: Arc::new(handler), } } fn make_service_by_cloning(self) -> MakeServiceByCloning<Self> where Self: Clone, { MakeServiceByCloning { service: self } } } /// A `Service` adapter that catches unwinding panics. /// /// Returned by [`ServiceExt::catch_unwind`]. /// /// [`ServiceExt::catch_unwind`]: trait.ServiceExt.html#tymethod.catch_unwind #[derive(Debug)] pub struct CatchUnwind<S, R, H> where S: Service<ResBody = Body, Error = BoxedError> + Sync, S::Future: Send + 'static, R: IntoFuture<Item = Response<Body>, Error = BoxedError>, R::Future: Send + 'static, H: Fn(Box<dyn Any + Send>) -> R + Send + Sync + 'static, { inner: S, handler: Arc<H>, } impl<S, R, H> Service for CatchUnwind<S, R, H> where S: Service<ResBody = Body, Error = BoxedError> + Sync, S::Future: Send + 'static, R: IntoFuture<Item = Response<Body>, Error = BoxedError>, R::Future: Send + 'static, H: Fn(Box<dyn Any + Send>) -> R + Send + Sync + 'static, { type ReqBody = S::ReqBody; type ResBody = Body; type Error = BoxedError; type Future = DefaultFuture<Response<Body>, BoxedError>; fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future { // We need to make sure that we don't just catch panics that happen while *polling* the // inner service's `Future`, but also those that happen when the inner `Future`s are // constructed, which basically means anything happening inside `self.inner.call(..)`. let handler = self.handler.clone(); let inner_future = match catch_unwind(AssertUnwindSafe(move || self.inner.call(req))) { Ok(future) => future, Err(panic_payload) => return Box::new(handler(panic_payload).into_future()), }; Box::new(AssertUnwindSafe(inner_future) .catch_unwind() .then(move |panic_result| -> Box<dyn Future<Item=Response<Body>, Error = BoxedError> + Send> { match panic_result { // FIXME avoid boxing so much here Ok(result) => Box::new(result.into_future()), Err(panic_payload) => Box::new(handler(panic_payload).into_future()), } }), ) } } impl<S, R, H> Clone for CatchUnwind<S, R, H> where S: Service<ResBody = Body, Error = BoxedError> + Clone + Sync, S::Future: Send + 'static, R: IntoFuture<Item = Response<Body>, Error = BoxedError>, R::Future: Send + 'static, H: Fn(Box<dyn Any + Send>) -> R + Send + Sync + 'static, { fn clone(&self) -> Self { CatchUnwind { inner: self.inner.clone(), handler: self.handler.clone(), } } } /// Implements Hyper's `MakeService` trait by cloning a service `S` for every /// incoming connection. /// /// Both [`SyncService`] and [`AsyncService`] already implement `MakeService` /// using the same implementation (cloning themselves), so you don't need this /// if you are using either of those directly. /// /// This type is returned by [`ServiceExt::make_service_by_cloning`]. /// /// [`SyncService`]: struct.SyncService.html /// [`AsyncService`]: struct.AsyncService.html /// [`ServiceExt::make_service_by_cloning`]: trait.ServiceExt.html#tymethod.make_service_by_cloning #[derive(Debug, Copy, Clone)] pub struct MakeServiceByCloning<S: Service + Clone> { service: S, } impl<Ctx, S: Service + Clone> MakeService<Ctx> for MakeServiceByCloning<S> { type ReqBody = S::ReqBody; type ResBody = S::ResBody; type Error = S::Error; type Service = S; type Future = FutureResult<S, Self::MakeError>; type MakeError = BoxedError; fn make_service(&mut self, _ctx: Ctx) -> Self::Future { Ok(self.service.clone()).into_future() } }