OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library shelf.middleware; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'request.dart'; |
| 10 import 'response.dart'; |
| 11 import 'handler.dart'; |
| 12 import 'hijack_exception.dart'; |
| 13 |
| 14 /// A function which creates a new [Handler] by wrapping a [Handler]. |
| 15 /// |
| 16 /// You can extend the functions of a [Handler] by wrapping it in |
| 17 /// [Middleware] that can intercept and process a request before it it sent |
| 18 /// to a handler, a response after it is sent by a handler, or both. |
| 19 /// |
| 20 /// Because [Middleware] consumes a [Handler] and returns a new |
| 21 /// [Handler], multiple [Middleware] instances can be composed |
| 22 /// together to offer rich functionality. |
| 23 /// |
| 24 /// Common uses for middleware include caching, logging, and authentication. |
| 25 /// |
| 26 /// Middleware that captures exceptions should be sure to pass |
| 27 /// [HijackException]s on without modification. |
| 28 /// |
| 29 /// A simple [Middleware] can be created using [createMiddleware]. |
| 30 typedef Handler Middleware(Handler innerHandler); |
| 31 |
| 32 /// Creates a [Middleware] using the provided functions. |
| 33 /// |
| 34 /// If provided, [requestHandler] receives a [Request]. It can respond to |
| 35 /// the request by returning a [Response] or [Future<Response>]. |
| 36 /// [requestHandler] can also return `null` for some or all requests in which |
| 37 /// case the request is sent to the inner [Handler]. |
| 38 /// |
| 39 /// If provided, [responseHandler] is called with the [Response] generated |
| 40 /// by the inner [Handler]. Responses generated by [requestHandler] are not |
| 41 /// sent to [responseHandler]. |
| 42 /// |
| 43 /// [responseHandler] should return either a [Response] or |
| 44 /// [Future<Response>]. It may return the response parameter it receives or |
| 45 /// create a new response object. |
| 46 /// |
| 47 /// If provided, [errorHandler] receives errors thrown by the inner handler. It |
| 48 /// does not receive errors thrown by [requestHandler] or [responseHandler], nor |
| 49 /// does it receive [HijackException]s. It can either return a new response or |
| 50 /// throw an error. |
| 51 Middleware createMiddleware({requestHandler(Request request), |
| 52 responseHandler(Response response), |
| 53 errorHandler(error, StackTrace stackTrace)}) { |
| 54 if (requestHandler == null) requestHandler = (request) => null; |
| 55 |
| 56 if (responseHandler == null) responseHandler = (response) => response; |
| 57 |
| 58 var onError = null; |
| 59 if (errorHandler != null) { |
| 60 onError = (error, stackTrace) { |
| 61 if (error is HijackException) throw error; |
| 62 return errorHandler(error, stackTrace); |
| 63 }; |
| 64 } |
| 65 |
| 66 return (Handler innerHandler) { |
| 67 return (request) { |
| 68 return new Future.sync(() => requestHandler(request)).then((response) { |
| 69 if (response != null) return response; |
| 70 |
| 71 return new Future.sync(() => innerHandler(request)).then( |
| 72 (response) => responseHandler(response), onError: onError); |
| 73 }); |
| 74 }; |
| 75 }; |
| 76 } |
OLD | NEW |