OLD | NEW |
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.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 /// A Shelf handler that provides support for one-time handlers. | 10 /// A Shelf handler that provides support for one-time handlers. |
(...skipping 18 matching lines...) Expand all Loading... |
29 /// [handler] will be unmounted as soon as it receives a request. | 29 /// [handler] will be unmounted as soon as it receives a request. |
30 String create(shelf.Handler handler) { | 30 String create(shelf.Handler handler) { |
31 var path = _counter.toString(); | 31 var path = _counter.toString(); |
32 _handlers[path] = handler; | 32 _handlers[path] = handler; |
33 _counter++; | 33 _counter++; |
34 return path; | 34 return path; |
35 } | 35 } |
36 | 36 |
37 /// Dispatches [request] to the appropriate handler. | 37 /// Dispatches [request] to the appropriate handler. |
38 _onRequest(shelf.Request request) { | 38 _onRequest(shelf.Request request) { |
39 // Skip the first component because it's always "/". | 39 var components = p.url.split(request.url.path); |
40 var components = p.url.split(request.url.path).skip(1).toList(); | 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 |
41 if (components.isEmpty) return new shelf.Response.notFound(null); | 47 if (components.isEmpty) return new shelf.Response.notFound(null); |
42 | 48 |
43 var handler = _handlers.remove(components.removeAt(0)); | 49 var handler = _handlers.remove(components.removeAt(0)); |
44 if (handler == null) return new shelf.Response.notFound(null); | 50 if (handler == null) return new shelf.Response.notFound(null); |
45 return handler(request); | 51 return handler(request); |
46 } | 52 } |
47 } | 53 } |
OLD | NEW |