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

Side by Side Diff: mojo/public/dart/third_party/test/lib/src/util/path_handler.dart

Issue 1346773002: Stop running pub get at gclient sync time and fix build bugs (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 3 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library test.util.path_handler;
6
7 import 'package:path/path.dart' as p;
8 import 'package:shelf/shelf.dart' as shelf;
9
10 /// A handler that routes to sub-handlers based on exact path prefixes.
11 class PathHandler {
12 /// A trie of path components to handlers.
13 final _paths = new _Node();
14
15 /// The shelf handler.
16 shelf.Handler get handler => _onRequest;
17
18 PathHandler();
19
20 /// Routes requests at or under [path] to [handler].
21 ///
22 /// If [path] is a parent or child directory of another path in this handler,
23 /// the longest matching prefix wins.
24 void add(String path, shelf.Handler handler) {
25 var node = _paths;
26 for (var component in p.url.split(path)) {
27 node = node.children.putIfAbsent(component, () => new _Node());
28 }
29 node.handler = handler;
30 }
31
32 _onRequest(shelf.Request request) {
33 var handler;
34 var handlerIndex;
35 var node = _paths;
36 var components = p.url.split(request.url.path);
37 for (var i = 0; i < components.length; i++ ) {
38 node = node.children[components[i]];
39 if (node == null) break;
40 if (node.handler == null) continue;
41 handler = node.handler;
42 handlerIndex = i;
43 }
44
45 if (handler == null) return new shelf.Response.notFound("Not found.");
46
47 return handler(request.change(
48 path: p.url.joinAll(components.take(handlerIndex + 1))));
49 }
50 }
51
52 /// A trie node.
53 class _Node {
54 shelf.Handler handler;
55 final children = new Map<String, _Node>();
56 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698