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

Side by Side Diff: lib/src/util/path_handler.dart

Issue 2232533002: Rename nestingMiddleware to PathHandler.nestedIn. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: Code review changes Created 4 years, 4 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 unified diff | Download patch
« no previous file with comments | « lib/src/runner/browser/platform.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
OLDNEW
« no previous file with comments | « lib/src/runner/browser/platform.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698