| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'package:path/path.dart' as p; | 5 import 'package:path/path.dart' as p; |
| 6 import 'package:shelf/shelf.dart' as shelf; | 6 import 'package:shelf/shelf.dart' as shelf; |
| 7 | 7 |
| 8 /// A handler that routes to sub-handlers based on exact path prefixes. | 8 /// A handler that routes to sub-handlers based on exact path prefixes. |
| 9 class PathHandler { | 9 class PathHandler { |
| 10 /// A trie of path components to handlers. | 10 /// A trie of path components to handlers. |
| 11 final _paths = new _Node(); | 11 final _paths = new _Node(); |
| 12 | 12 |
| 13 /// The shelf handler. | 13 /// The shelf handler. |
| 14 shelf.Handler get handler => _onRequest; | 14 shelf.Handler get handler => _onRequest; |
| 15 | 15 |
| 16 PathHandler(); | 16 /// Returns middleware that nests all requests beneath the URL prefix |
| 17 /// [beneath]. |
| 18 static shelf.Middleware nestedIn(String beneath) { |
| 19 return (handler) { |
| 20 var pathHandler = new PathHandler()..add(beneath, handler); |
| 21 return pathHandler.handler; |
| 22 }; |
| 23 } |
| 17 | 24 |
| 18 /// Routes requests at or under [path] to [handler]. | 25 /// Routes requests at or under [path] to [handler]. |
| 19 /// | 26 /// |
| 20 /// If [path] is a parent or child directory of another path in this handler, | 27 /// If [path] is a parent or child directory of another path in this handler, |
| 21 /// the longest matching prefix wins. | 28 /// the longest matching prefix wins. |
| 22 void add(String path, shelf.Handler handler) { | 29 void add(String path, shelf.Handler handler) { |
| 23 var node = _paths; | 30 var node = _paths; |
| 24 for (var component in p.url.split(path)) { | 31 for (var component in p.url.split(path)) { |
| 25 node = node.children.putIfAbsent(component, () => new _Node()); | 32 node = node.children.putIfAbsent(component, () => new _Node()); |
| 26 } | 33 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 40 handlerIndex = i; | 47 handlerIndex = i; |
| 41 } | 48 } |
| 42 | 49 |
| 43 if (handler == null) return new shelf.Response.notFound("Not found."); | 50 if (handler == null) return new shelf.Response.notFound("Not found."); |
| 44 | 51 |
| 45 return handler(request.change( | 52 return handler(request.change( |
| 46 path: p.url.joinAll(components.take(handlerIndex + 1)))); | 53 path: p.url.joinAll(components.take(handlerIndex + 1)))); |
| 47 } | 54 } |
| 48 } | 55 } |
| 49 | 56 |
| 50 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. | |
| 51 shelf.Middleware nestingMiddleware(String beneath) { | |
| 52 return (handler) { | |
| 53 var pathHandler = new PathHandler()..add(beneath, handler); | |
| 54 return pathHandler.handler; | |
| 55 }; | |
| 56 } | |
| 57 | |
| 58 /// A trie node. | 57 /// A trie node. |
| 59 class _Node { | 58 class _Node { |
| 60 shelf.Handler handler; | 59 shelf.Handler handler; |
| 61 final children = new Map<String, _Node>(); | 60 final children = new Map<String, _Node>(); |
| 62 } | 61 } |
| OLD | NEW |