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