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."); |
+ } |
+} |