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

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

Issue 1076803003: Change the URL-space exposed by the browser server. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: Changelog + pubspec Created 5 years, 8 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 import '../utils.dart';
11
12 /// A handler that routes to sub-handlers based on exact path prefixes.
13 class PathHandler {
14 /// A trie of path components to handlers.
15 final _paths = new _Node();
16
17 /// The shelf handler.
18 shelf.Handler get handler => _onRequest;
19
20 PathHandler();
21
22 /// Routes requests at or under [path] to [handler].
23 ///
24 /// If [path] is a parent or child directory of another path in this handler,
25 /// the longest matching prefix wins.
26 void add(String path, shelf.Handler handler) {
27 var node = _paths;
28 for (var component in p.url.split(path)) {
29 node = node.children.putIfAbsent(component, () => new _Node());
30 }
31 node.handler = handler;
32 }
33
34 _onRequest(shelf.Request request) {
35 var handler;
36 var handlerIndex;
37 var node = _paths;
38 var components = p.url.split(shelfUrl(request).path);
39 for (var i = 0; i < components.length; i++ ) {
40 node = node.children[components[i]];
41 if (node == null) break;
42 if (node.handler == null) continue;
43 handler = node.handler;
44 handlerIndex = i;
45 }
46
47 if (handler == null) return new shelf.Response.notFound("Not found.");
48
49 return handler(shelfChange(request,
50 path: p.joinAll(components.take(handlerIndex + 1))));
51 }
52 }
53
54 /// A trie node.
55 class _Node {
56 shelf.Handler handler;
57 final children = new Map<String, _Node>();
58 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698