OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Helpers for dealing with HTTP. |
| 6 library http; |
| 7 |
| 8 import 'dart:io'; |
| 9 import 'dart:json'; |
| 10 |
| 11 // TODO(nweiz): Make this import better. |
| 12 import '../../pkg/http/lib/http.dart' as http; |
| 13 import 'curl_client.dart'; |
| 14 import 'io.dart'; |
| 15 import 'log.dart' as log; |
| 16 |
| 17 // TODO(nweiz): make this configurable |
| 18 /** |
| 19 * The amount of time in milliseconds to allow HTTP requests before assuming |
| 20 * they've failed. |
| 21 */ |
| 22 final HTTP_TIMEOUT = 30 * 1000; |
| 23 |
| 24 /// An HTTP client that transforms 40* errors and socket exceptions into more |
| 25 /// user-friendly error messages. |
| 26 class PubHttpClient extends http.BaseClient { |
| 27 final http.Client _inner; |
| 28 |
| 29 PubHttpClient([http.Client inner]) |
| 30 : _inner = inner == null ? new http.Client() : inner; |
| 31 |
| 32 Future<http.StreamedResponse> send(http.BaseRequest request) { |
| 33 // TODO(rnystrom): Log request body when it's available and plaintext, but |
| 34 // not when it contains OAuth2 credentials. |
| 35 |
| 36 // TODO(nweiz): remove this when issue 4061 is fixed. |
| 37 var stackTrace; |
| 38 try { |
| 39 throw null; |
| 40 } catch (_, localStackTrace) { |
| 41 stackTrace = localStackTrace; |
| 42 } |
| 43 |
| 44 // TODO(nweiz): Ideally the timeout would extend to reading from the |
| 45 // response input stream, but until issue 3657 is fixed that's not feasible. |
| 46 return timeout(_inner.send(request).chain((streamedResponse) { |
| 47 log.fine("Got response ${streamedResponse.statusCode} " |
| 48 "${streamedResponse.reasonPhrase}."); |
| 49 |
| 50 var status = streamedResponse.statusCode; |
| 51 // 401 responses should be handled by the OAuth2 client. It's very |
| 52 // unlikely that they'll be returned by non-OAuth2 requests. |
| 53 if (status < 400 || status == 401) { |
| 54 return new Future.immediate(streamedResponse); |
| 55 } |
| 56 |
| 57 return http.Response.fromStream(streamedResponse).transform((response) { |
| 58 throw new PubHttpException(response); |
| 59 }); |
| 60 }).transformException((e) { |
| 61 if (e is SocketIOException && |
| 62 e.osError != null && |
| 63 (e.osError.errorCode == 8 || |
| 64 e.osError.errorCode == -2 || |
| 65 e.osError.errorCode == -5 || |
| 66 e.osError.errorCode == 11004)) { |
| 67 throw 'Could not resolve URL "${request.url.origin}".'; |
| 68 } |
| 69 throw e; |
| 70 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); |
| 71 } |
| 72 } |
| 73 |
| 74 /// The HTTP client to use for all HTTP requests. |
| 75 final httpClient = new PubHttpClient(); |
| 76 |
| 77 final curlClient = new PubHttpClient(new CurlClient()); |
| 78 |
| 79 /// Handles a successful JSON-formatted response from pub.dartlang.org. |
| 80 /// |
| 81 /// These responses are expected to be of the form `{"success": {"message": |
| 82 /// "some message"}}`. If the format is correct, the message will be printed; |
| 83 /// otherwise an error will be raised. |
| 84 void handleJsonSuccess(http.Response response) { |
| 85 var parsed = parseJsonResponse(response); |
| 86 if (parsed['success'] is! Map || |
| 87 !parsed['success'].containsKey('message') || |
| 88 parsed['success']['message'] is! String) { |
| 89 invalidServerResponse(response); |
| 90 } |
| 91 log.message(parsed['success']['message']); |
| 92 } |
| 93 |
| 94 /// Handles an unsuccessful JSON-formatted response from pub.dartlang.org. |
| 95 /// |
| 96 /// These responses are expected to be of the form `{"error": {"message": "some |
| 97 /// message"}}`. If the format is correct, the message will be raised as an |
| 98 /// error; otherwise an [invalidServerResponse] error will be raised. |
| 99 void handleJsonError(http.Response response) { |
| 100 var errorMap = parseJsonResponse(response); |
| 101 if (errorMap['error'] is! Map || |
| 102 !errorMap['error'].containsKey('message') || |
| 103 errorMap['error']['message'] is! String) { |
| 104 invalidServerResponse(response); |
| 105 } |
| 106 throw errorMap['error']['message']; |
| 107 } |
| 108 |
| 109 /// Parses a response body, assuming it's JSON-formatted. Throws a user-friendly |
| 110 /// error if the response body is invalid JSON, or if it's not a map. |
| 111 Map parseJsonResponse(http.Response response) { |
| 112 var value; |
| 113 try { |
| 114 value = JSON.parse(response.body); |
| 115 } catch (e) { |
| 116 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. |
| 117 invalidServerResponse(response); |
| 118 } |
| 119 if (value is! Map) invalidServerResponse(response); |
| 120 return value; |
| 121 } |
| 122 |
| 123 /// Throws an error describing an invalid response from the server. |
| 124 void invalidServerResponse(http.Response response) { |
| 125 throw 'Invalid server response:\n${response.body}'; |
| 126 } |
| 127 |
| 128 /** |
| 129 * Exception thrown when an HTTP operation fails. |
| 130 */ |
| 131 class PubHttpException implements Exception { |
| 132 final http.Response response; |
| 133 |
| 134 const PubHttpException(this.response); |
| 135 |
| 136 String toString() => 'HTTP error ${response.statusCode}: ' |
| 137 '${response.reasonPhrase}'; |
| 138 } |
OLD | NEW |