OLD | NEW |
---|---|
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:args/args.dart'; | 9 import 'package:args/args.dart'; |
10 | 10 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 | 57 |
58 class HttpException implements Exception { | 58 class HttpException implements Exception { |
59 final Uri uri; | 59 final Uri uri; |
60 final int statusCode; | 60 final int statusCode; |
61 | 61 |
62 HttpException(this.uri, this.statusCode); | 62 HttpException(this.uri, this.statusCode); |
63 | 63 |
64 String toString() => '$uri: $statusCode'; | 64 String toString() => '$uri: $statusCode'; |
65 } | 65 } |
66 | 66 |
67 class TimeoutException implements Exception { | |
floitsch
2017/08/15 11:26:35
I would have used the builtin TimeoutException cla
Johnni Winther
2017/08/15 12:51:27
Done.
| |
68 final Uri uri; | |
69 | |
70 TimeoutException(this.uri); | |
71 | |
72 String toString() => '$uri timeout'; | |
73 } | |
74 | |
67 /// Reads the content of [uri] as text. | 75 /// Reads the content of [uri] as text. |
68 Future<String> readUriAsText(HttpClient client, Uri uri) async { | 76 Future<String> readUriAsText( |
77 HttpClient client, Uri uri, Duration timeout) async { | |
69 HttpClientRequest request = await client.getUrl(uri); | 78 HttpClientRequest request = await client.getUrl(uri); |
70 HttpClientResponse response = await request.close(); | 79 HttpClientResponse response = await request.close(); |
71 if (response.statusCode != 200) { | 80 if (response.statusCode != 200) { |
72 response.drain(); | 81 response.drain(); |
73 throw new HttpException(uri, response.statusCode); | 82 throw new HttpException(uri, response.statusCode); |
74 } | 83 } |
75 return response.transform(UTF8.decoder).join(); | 84 List<int> data = <int>[]; |
85 Timer timer; | |
86 Completer<String> completer = new Completer<String>(); | |
87 StreamSubscription subscription = response.listen((List<int> buffer) { | |
floitsch
2017/08/15 11:26:35
You could consider using .timeout from the Stream
Johnni Winther
2017/08/15 12:51:27
Done.
| |
88 data.addAll(buffer); | |
89 }, onDone: () { | |
90 timer?.cancel(); | |
91 completer.complete(UTF8.decode(data)); | |
92 }, cancelOnError: true); | |
93 if (timeout != null) { | |
94 timer = new Timer(timeout, () { | |
95 subscription.cancel(); | |
96 completer.completeError(new TimeoutException(uri)); | |
97 }); | |
98 } | |
99 return completer.future; | |
76 } | 100 } |
77 | 101 |
78 ArgParser createArgParser() { | 102 ArgParser createArgParser() { |
79 ArgParser argParser = new ArgParser(allowTrailingOptions: true); | 103 ArgParser argParser = new ArgParser(allowTrailingOptions: true); |
80 argParser.addFlag('help', help: "Help"); | 104 argParser.addFlag('help', help: "Help"); |
81 argParser.addFlag('verbose', | 105 argParser.addFlag('verbose', |
82 abbr: 'v', negatable: false, help: "Turn on logging output."); | 106 abbr: 'v', negatable: false, help: "Turn on logging output."); |
83 argParser.addFlag('no-cache', help: "Disable caching."); | 107 argParser.addFlag('no-cache', help: "Disable caching."); |
84 argParser.addOption('cache', | 108 argParser.addOption('cache', |
85 help: "Use <dir> for caching test output.\n" | 109 help: "Use <dir> for caching test output.\n" |
86 "Defaults to 'temp/gardening-cache/'."); | 110 "Defaults to 'temp/gardening-cache/'."); |
87 argParser.addFlag('logdog', | 111 argParser.addFlag('logdog', |
88 negatable: false, help: "Pull test results from logdog."); | 112 negatable: false, help: "Pull test results from logdog."); |
89 return argParser; | 113 return argParser; |
90 } | 114 } |
91 | 115 |
92 void processArgResults(ArgResults argResults) { | 116 void processArgResults(ArgResults argResults) { |
93 if (argResults['verbose']) { | 117 if (argResults['verbose']) { |
94 LOG = true; | 118 LOG = true; |
95 } | 119 } |
96 if (argResults['cache'] != null) { | 120 if (argResults['cache'] != null) { |
97 cache.base = Uri.base.resolve('${argResults['cache']}/'); | 121 cache.base = Uri.base.resolve('${argResults['cache']}/'); |
98 } | 122 } |
99 if (argResults['no-cache']) { | 123 if (argResults['no-cache']) { |
100 cache.base = null; | 124 cache.base = null; |
101 } | 125 } |
102 } | 126 } |
OLD | NEW |