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

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: Code review changes 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
« no previous file with comments | « lib/src/async_handler.dart ('k') | lib/src/package_config_handler.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..3605aa99a604a3b6bf1abe7441e6e363f29aae5a
--- /dev/null
+++ b/lib/src/dir_handler.dart
@@ -0,0 +1,34 @@
+// Copyright (c) 2016, 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_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.");
+ }
+}
« no previous file with comments | « lib/src/async_handler.dart ('k') | lib/src/package_config_handler.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698