OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library shelf_packages_handler.dir_handler; |
| 6 |
| 7 import 'package:path/path.dart' as p; |
| 8 import 'package:shelf/shelf.dart'; |
| 9 |
| 10 /// A utility handler that mounts a sub-handler beneath a directory name, |
| 11 /// wherever that directory name appears in a URL. |
| 12 /// |
| 13 /// In practice, this is used to mount a [PackagesHandler] underneath |
| 14 /// `packages/` directories. |
| 15 class DirHandler { |
| 16 /// The directory name to look for. |
| 17 final String _name; |
| 18 |
| 19 /// The inner handler to mount. |
| 20 final Handler _inner; |
| 21 |
| 22 DirHandler(this._name, this._inner); |
| 23 |
| 24 /// The callback for handling a single request. |
| 25 call(Request request) { |
| 26 var segments = request.url.pathSegments; |
| 27 for (var i = 0; i < segments.length; i++) { |
| 28 if (segments[i] != _name) continue; |
| 29 return _inner(request.change(path: p.url.joinAll(segments.take(i + 1)))); |
| 30 } |
| 31 |
| 32 return new Response.notFound("Not found."); |
| 33 } |
| 34 } |
OLD | NEW |