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

Unified Diff: lib/src/dir_handler.dart

Issue 2171743003: Add the package implementation. (Closed) Base URL: git@github.com:dart-lang/shelf_packages_handler.git@master
Patch Set: Created 4 years, 5 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: lib/src/dir_handler.dart
diff --git a/lib/src/dir_handler.dart b/lib/src/dir_handler.dart
new file mode 100644
index 0000000000000000000000000000000000000000..028f38e8f5e99a66e46f03b0e4d80cdff435504b
--- /dev/null
+++ b/lib/src/dir_handler.dart
@@ -0,0 +1,34 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
jakemac 2016/07/21 21:45:57 nit, 2016
nweiz 2016/07/21 23:07:24 Done.
+// 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_packages_handler.dir_handler;
+
+import 'package:path/path.dart' as p;
+import 'package:shelf/shelf.dart';
+
+/// A utility handler that mounts a sub-handler beneath a directory name,
+/// wherever that directory name appears in a URL.
+///
+/// In practice, this is used to mount a [PackagesHandler] underneath
+/// `packages/` directories.
+class DirHandler {
+ /// The directory name to look for.
+ final String _name;
+
+ /// The inner handler to mount.
+ final Handler _inner;
+
+ DirHandler(this._name, this._inner);
+
+ /// The callback for handling a single request.
+ call(Request request) {
+ var segments = request.url.pathSegments;
+ for (var i = 0; i < segments.length; i++) {
+ if (segments[i] != _name) continue;
+ return _inner(request.change(path: p.url.joinAll(segments.take(i + 1))));
+ }
+
+ return new Response.notFound("Not found.");
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698