Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
| 6 * Helper functionality to make working with IO easier. | 6 * Helper functionality to make working with IO easier. |
| 7 */ | 7 */ |
| 8 library io; | 8 library io; |
| 9 | 9 |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| 11 import 'dart:isolate'; | 11 import 'dart:isolate'; |
| 12 import 'dart:json'; | |
| 12 import 'dart:uri'; | 13 import 'dart:uri'; |
| 13 | 14 |
| 14 // TODO(nweiz): Make this import better. | 15 // TODO(nweiz): Make this import better. |
| 15 import '../../pkg/http/lib/http.dart' as http; | 16 import '../../pkg/http/lib/http.dart' as http; |
| 16 import 'curl_client.dart'; | 17 import 'curl_client.dart'; |
| 17 import 'log.dart' as log; | 18 import 'log.dart' as log; |
| 18 import 'path.dart' as path; | 19 import 'path.dart' as path; |
| 19 import 'utils.dart'; | 20 import 'utils.dart'; |
| 20 | 21 |
| 21 bool _isGitInstalledCache; | 22 bool _isGitInstalledCache; |
| (...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 558 throw e; | 559 throw e; |
| 559 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); | 560 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); |
| 560 } | 561 } |
| 561 } | 562 } |
| 562 | 563 |
| 563 /// The HTTP client to use for all HTTP requests. | 564 /// The HTTP client to use for all HTTP requests. |
| 564 final httpClient = new PubHttpClient(); | 565 final httpClient = new PubHttpClient(); |
| 565 | 566 |
| 566 final curlClient = new PubHttpClient(new CurlClient()); | 567 final curlClient = new PubHttpClient(new CurlClient()); |
| 567 | 568 |
| 569 /// Handles a successful JSON-formatted response from pub.dartlang.org. | |
|
Bob Nystrom
2012/12/17 18:41:58
io.dart is becoming a bit of a dumping ground. How
nweiz
2012/12/17 21:18:48
Done.
| |
| 570 /// | |
| 571 /// These responses are expected to be of the form `{"success": {"message": | |
| 572 /// "some message"}}`. If the format is correct, the message will be printed; | |
| 573 /// otherwise an error will be raised. | |
| 574 void handleJsonSuccess(http.Response response) { | |
| 575 var parsed = parseJsonResponse(response); | |
| 576 if (parsed['success'] is! Map || | |
| 577 !parsed['success'].containsKey('message') || | |
| 578 parsed['success']['message'] is! String) { | |
| 579 invalidServerResponse(response); | |
| 580 } | |
| 581 log.message(parsed['success']['message']); | |
| 582 } | |
| 583 | |
| 584 /// Handles an unsuccessful JSON-formatted response from pub.dartlang.org. | |
| 585 /// | |
| 586 /// These responses are expected to be of the form `{"error": {"message": "some | |
| 587 /// message"}}`. If the format is correct, the message will be raised as an | |
| 588 /// error; otherwise an [invalidServerResponse] error will be raised. | |
| 589 void handleJsonError(http.Response response) { | |
| 590 var errorMap = parseJsonResponse(response); | |
| 591 if (errorMap['error'] is! Map || | |
| 592 !errorMap['error'].containsKey('message') || | |
| 593 errorMap['error']['message'] is! String) { | |
| 594 invalidServerResponse(response); | |
| 595 } | |
| 596 throw errorMap['error']['message']; | |
| 597 } | |
| 598 | |
| 599 /// Parses a response body, assuming it's JSON-formatted. Throws a user-friendly | |
| 600 /// error if the response body is invalid JSON, or if it's not a map. | |
| 601 Map parseJsonResponse(http.Response response) { | |
| 602 var value; | |
| 603 try { | |
| 604 value = JSON.parse(response.body); | |
| 605 } catch (e) { | |
| 606 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. | |
| 607 invalidServerResponse(response); | |
| 608 } | |
| 609 if (value is! Map) invalidServerResponse(response); | |
| 610 return value; | |
| 611 } | |
| 612 | |
| 613 /// Throws an error describing an invalid response from the server. | |
| 614 void invalidServerResponse(http.Response response) { | |
| 615 throw 'Invalid server response:\n${response.body}'; | |
| 616 } | |
| 617 | |
| 568 /** | 618 /** |
| 569 * Takes all input from [source] and writes it to [sink]. | 619 * Takes all input from [source] and writes it to [sink]. |
| 570 * | 620 * |
| 571 * Returns a future that completes when [source] is closed. | 621 * Returns a future that completes when [source] is closed. |
| 572 */ | 622 */ |
| 573 Future pipeInputToInput(InputStream source, ListInputStream sink) { | 623 Future pipeInputToInput(InputStream source, ListInputStream sink) { |
| 574 var completer = new Completer(); | 624 var completer = new Completer(); |
| 575 source.onClosed = () { | 625 source.onClosed = () { |
| 576 sink.markEndOfStream(); | 626 sink.markEndOfStream(); |
| 577 completer.complete(null); | 627 completer.complete(null); |
| (...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1070 return new Directory(entry); | 1120 return new Directory(entry); |
| 1071 } | 1121 } |
| 1072 | 1122 |
| 1073 /** | 1123 /** |
| 1074 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 1124 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
| 1075 */ | 1125 */ |
| 1076 Uri _getUri(uri) { | 1126 Uri _getUri(uri) { |
| 1077 if (uri is Uri) return uri; | 1127 if (uri is Uri) return uri; |
| 1078 return new Uri.fromString(uri); | 1128 return new Uri.fromString(uri); |
| 1079 } | 1129 } |
| OLD | NEW |