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

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

Issue 260933004: Support Request hijacking in Shelf, using a similar API to Rack. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 7 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/hijack_exception.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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library shelf.middleware; 5 library shelf.middleware;
6 6
7 import 'request.dart'; 7 import 'request.dart';
8 import 'response.dart'; 8 import 'response.dart';
9 import 'handler.dart'; 9 import 'handler.dart';
10 import 'hijack_exception.dart';
10 import 'util.dart'; 11 import 'util.dart';
11 12
12 /// A function which creates a new [Handler] by wrapping a [Handler]. 13 /// A function which creates a new [Handler] by wrapping a [Handler].
13 /// 14 ///
14 /// You can extend the functions of a [Handler] by wrapping it in 15 /// 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 /// [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 /// to a handler, a response after it is sent by a handler, or both.
17 /// 18 ///
18 /// Because [Middleware] consumes a [Handler] and returns a new 19 /// Because [Middleware] consumes a [Handler] and returns a new
19 /// [Handler], multiple [Middleware] instances can be composed 20 /// [Handler], multiple [Middleware] instances can be composed
20 /// together to offer rich functionality. 21 /// together to offer rich functionality.
21 /// 22 ///
22 /// Common uses for middleware include caching, logging, and authentication. 23 /// Common uses for middleware include caching, logging, and authentication.
23 /// 24 ///
25 /// Middleware that captures exceptions should be sure to pass
26 /// [HijackException]s on without modification.
27 ///
24 /// A simple [Middleware] can be created using [createMiddleware]. 28 /// A simple [Middleware] can be created using [createMiddleware].
25 typedef Handler Middleware(Handler innerHandler); 29 typedef Handler Middleware(Handler innerHandler);
26 30
27 /// Creates a [Middleware] using the provided functions. 31 /// Creates a [Middleware] using the provided functions.
28 /// 32 ///
29 /// If provided, [requestHandler] receives a [Request]. It can respond to 33 /// If provided, [requestHandler] receives a [Request]. It can respond to
30 /// the request by returning a [Response] or [Future<Response>]. 34 /// the request by returning a [Response] or [Future<Response>].
31 /// [requestHandler] can also return `null` for some or all requests in which 35 /// [requestHandler] can also return `null` for some or all requests in which
32 /// case the request is sent to the inner [Handler]. 36 /// case the request is sent to the inner [Handler].
33 /// 37 ///
34 /// If provided, [responseHandler] is called with the [Response] generated 38 /// If provided, [responseHandler] is called with the [Response] generated
35 /// by the inner [Handler]. Responses generated by [requestHandler] are not 39 /// by the inner [Handler]. Responses generated by [requestHandler] are not
36 /// sent to [responseHandler]. 40 /// sent to [responseHandler].
37 /// 41 ///
38 /// [responseHandler] should return either a [Response] or 42 /// [responseHandler] should return either a [Response] or
39 /// [Future<Response>]. It may return the response parameter it receives or 43 /// [Future<Response>]. It may return the response parameter it receives or
40 /// create a new response object. 44 /// create a new response object.
41 /// 45 ///
42 /// If provided, [errorHandler] receives errors thrown by the inner handler. It 46 /// If provided, [errorHandler] receives errors thrown by the inner handler. It
43 /// does not receive errors thrown by [requestHandler] or [responseHandler]. It 47 /// does not receive errors thrown by [requestHandler] or [responseHandler], nor
44 /// can either return a new response or throw an error. 48 /// does it receive [HijackException]s. It can either return a new response or
49 /// throw an error.
45 Middleware createMiddleware({requestHandler(Request request), 50 Middleware createMiddleware({requestHandler(Request request),
46 responseHandler(Response response), 51 responseHandler(Response response),
47 errorHandler(error, StackTrace stackTrace)}) { 52 errorHandler(error, StackTrace stackTrace)}) {
48 if (requestHandler == null) requestHandler = (request) => null; 53 if (requestHandler == null) requestHandler = (request) => null;
49 54
50 if (responseHandler == null) responseHandler = (response) => response; 55 if (responseHandler == null) responseHandler = (response) => response;
51 56
52 return (Handler innerHandler) { 57 return (Handler innerHandler) {
53 return (request) { 58 return (request) {
54 return syncFuture(() => requestHandler(request)).then((response) { 59 return syncFuture(() => requestHandler(request)).then((response) {
55 if (response != null) return response; 60 if (response != null) return response;
56 61
57 return syncFuture(() => innerHandler(request)) 62 return syncFuture(() => innerHandler(request))
58 .then((response) => responseHandler(response), 63 .then((response) => responseHandler(response),
59 onError: errorHandler); 64 onError: (error, stackTrace) {
65 if (error is HijackException) throw error;
66 return errorHandler(error, stackTrace);
67 });
60 }); 68 });
61 }; 69 };
62 }; 70 };
63 } 71 }
OLDNEW
« no previous file with comments | « pkg/shelf/lib/src/hijack_exception.dart ('k') | pkg/shelf/lib/src/request.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698