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

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

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

Powered by Google App Engine
This is Rietveld 408576698