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

Unified Diff: mojo/public/dart/third_party/shelf/lib/src/middleware.dart

Issue 1346773002: Stop running pub get at gclient sync time and fix build bugs (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/dart/third_party/shelf/lib/src/middleware.dart
diff --git a/mojo/public/dart/third_party/shelf/lib/src/middleware.dart b/mojo/public/dart/third_party/shelf/lib/src/middleware.dart
new file mode 100644
index 0000000000000000000000000000000000000000..1d503d0845c9d173a11410f493587a24b960da11
--- /dev/null
+++ b/mojo/public/dart/third_party/shelf/lib/src/middleware.dart
@@ -0,0 +1,76 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library shelf.middleware;
+
+import 'dart:async';
+
+import 'request.dart';
+import 'response.dart';
+import 'handler.dart';
+import 'hijack_exception.dart';
+
+/// A function which creates a new [Handler] by wrapping a [Handler].
+///
+/// You can extend the functions of a [Handler] by wrapping it in
+/// [Middleware] that can intercept and process a request before it it sent
+/// to a handler, a response after it is sent by a handler, or both.
+///
+/// Because [Middleware] consumes a [Handler] and returns a new
+/// [Handler], multiple [Middleware] instances can be composed
+/// together to offer rich functionality.
+///
+/// Common uses for middleware include caching, logging, and authentication.
+///
+/// Middleware that captures exceptions should be sure to pass
+/// [HijackException]s on without modification.
+///
+/// A simple [Middleware] can be created using [createMiddleware].
+typedef Handler Middleware(Handler innerHandler);
+
+/// Creates a [Middleware] using the provided functions.
+///
+/// If provided, [requestHandler] receives a [Request]. It can respond to
+/// the request by returning a [Response] or [Future<Response>].
+/// [requestHandler] can also return `null` for some or all requests in which
+/// case the request is sent to the inner [Handler].
+///
+/// If provided, [responseHandler] is called with the [Response] generated
+/// by the inner [Handler]. Responses generated by [requestHandler] are not
+/// sent to [responseHandler].
+///
+/// [responseHandler] should return either a [Response] or
+/// [Future<Response>]. It may return the response parameter it receives or
+/// create a new response object.
+///
+/// If provided, [errorHandler] receives errors thrown by the inner handler. It
+/// does not receive errors thrown by [requestHandler] or [responseHandler], nor
+/// does it receive [HijackException]s. It can either return a new response or
+/// throw an error.
+Middleware createMiddleware({requestHandler(Request request),
+ responseHandler(Response response),
+ errorHandler(error, StackTrace stackTrace)}) {
+ if (requestHandler == null) requestHandler = (request) => null;
+
+ if (responseHandler == null) responseHandler = (response) => response;
+
+ var onError = null;
+ if (errorHandler != null) {
+ onError = (error, stackTrace) {
+ if (error is HijackException) throw error;
+ return errorHandler(error, stackTrace);
+ };
+ }
+
+ return (Handler innerHandler) {
+ return (request) {
+ return new Future.sync(() => requestHandler(request)).then((response) {
+ if (response != null) return response;
+
+ return new Future.sync(() => innerHandler(request)).then(
+ (response) => responseHandler(response), onError: onError);
+ });
+ };
+ };
+}
« no previous file with comments | « mojo/public/dart/third_party/shelf/lib/src/message.dart ('k') | mojo/public/dart/third_party/shelf/lib/src/pipeline.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698