Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: pkg/shelf/lib/src/middleware.dart

Issue 219283008: pkg/shelf (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: cl nits Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/shelf/lib/src/message.dart ('k') | pkg/shelf/lib/src/request.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 'util.dart';
11
12 /// A function which creates a new [Handler] by wrapping a [Handler].
13 ///
14 /// You can extend the functions of a [Handler] by wrapping it in
15 /// [Middleware] that can intercept and process a request before it it sent
16 /// to a handler, a response after it is sent by a handler, or both.
17 ///
18 /// Because [Middleware] consumes a [Handler] and returns a new
19 /// [Handler], multiple [Middleware] instances can be composed
20 /// together to offer rich functionality.
21 ///
22 /// Common uses for middleware include caching, logging, and authentication.
23 ///
24 /// A simple [Middleware] can be created using [createMiddleware].
25 typedef Handler Middleware(Handler innerHandler);
26
27 /// Creates a [Middleware] using the provided functions.
28 ///
29 /// If provided, [requestHandler] receives a [Request]. It can respond to
30 /// the request by returning a [Response] or [Future<Response>].
31 /// [requestHandler] can also return `null` for some or all requests in which
32 /// case the request is sent to the inner [Handler].
33 ///
34 /// If provided, [responseHandler] is called with the [Response] generated
35 /// by the inner [Handler]. Responses generated by [requestHandler] are not
36 /// sent to [responseHandler].
37 ///
38 /// [responseHandler] should return either a [Response] or
39 /// [Future<Response>]. It may return the response parameter it receives or
40 /// create a new response object.
41 ///
42 /// If provided, [errorHandler] receives errors thrown by the inner handler. It
43 /// does not receive errors thrown by [requestHandler] or [responseHandler]. It
44 /// can either return a new response or throw an error.
45 Middleware createMiddleware({requestHandler(Request request),
46 responseHandler(Response response),
47 errorHandler(error, StackTrace stackTrace)}) {
48 if (requestHandler == null) requestHandler = (request) => null;
49
50 if (responseHandler == null) responseHandler = (response) => response;
51
52 return (Handler innerHandler) {
53 return (request) {
54 return syncFuture(() => requestHandler(request)).then((response) {
55 if (response != null) return response;
56
57 return syncFuture(() => innerHandler(request))
58 .then((response) => responseHandler(response),
59 onError: errorHandler);
60 });
61 };
62 };
63 }
OLDNEW
« no previous file with comments | « pkg/shelf/lib/src/message.dart ('k') | pkg/shelf/lib/src/request.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698