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

Side by Side Diff: lib/src/util/one_off_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
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.one_off_handler; 5 library test.util.one_off_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
10 /// A Shelf handler that provides support for one-time handlers. 12 /// A Shelf handler that provides support for one-time handlers.
11 /// 13 ///
12 /// This is useful for handlers that only expect to be hit once before becoming 14 /// This is useful for handlers that only expect to be hit once before becoming
13 /// invalid and don't need to have a persistent URL. 15 /// invalid and don't need to have a persistent URL.
14 class OneOffHandler { 16 class OneOffHandler {
15 /// A map from URL paths to handlers. 17 /// A map from URL paths to handlers.
16 final _handlers = new Map<String, shelf.Handler>(); 18 final _handlers = new Map<String, shelf.Handler>();
17 19
18 /// The counter of handlers that have been activated. 20 /// The counter of handlers that have been activated.
19 var _counter = 0; 21 var _counter = 0;
20 22
21 /// The actual [shelf.Handler] that dispatches requests. 23 /// The actual [shelf.Handler] that dispatches requests.
22 shelf.Handler get handler => _onRequest; 24 shelf.Handler get handler => _onRequest;
23 25
24 /// Creates a new one-off handler that forwards to [handler]. 26 /// Creates a new one-off handler that forwards to [handler].
25 /// 27 ///
26 /// Returns a string that's the URL path for hitting this handler, relative to 28 /// Returns a string that's the URL path for hitting this handler, relative to
27 /// the URL for the one-off handler itself. 29 /// the URL for the one-off handler itself.
28 /// 30 ///
29 /// [handler] will be unmounted as soon as it receives a request. 31 /// [handler] will be unmounted as soon as it receives a request.
30 String create(shelf.Handler handler) { 32 String create(shelf.Handler handler) {
31 var path = _counter.toString(); 33 var path = _counter.toString();
32 _handlers[path] = handler; 34 _handlers[path] = handler;
33 _counter++; 35 _counter++;
34 return path; 36 return path;
35 } 37 }
36 38
37 /// Dispatches [request] to the appropriate handler. 39 /// Dispatches [request] to the appropriate handler.
38 _onRequest(shelf.Request request) { 40 _onRequest(shelf.Request request) {
39 var components = p.url.split(request.url.path); 41 var components = p.url.split(shelfUrl(request).path);
40
41 // For shelf < 0.6.0, the first component of the path is always "/". We can
42 // safely skip it.
43 if (components.isNotEmpty && components.first == "/") {
44 components.removeAt(0);
45 }
46
47 if (components.isEmpty) return new shelf.Response.notFound(null); 42 if (components.isEmpty) return new shelf.Response.notFound(null);
48 43
49 var handler = _handlers.remove(components.removeAt(0)); 44 var path = components.removeAt(0);
45 var handler = _handlers.remove(path);
50 if (handler == null) return new shelf.Response.notFound(null); 46 if (handler == null) return new shelf.Response.notFound(null);
51 return handler(request); 47 return handler(shelfChange(request, path: path));
52 } 48 }
53 } 49 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698