| 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 /// Helpers for dealing with HTTP. | 5 /// Helpers for dealing with HTTP. |
| 6 library pub.http; | 6 library pub.http; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:json'; | 10 import 'dart:json' as json; |
| 11 | 11 |
| 12 // TODO(nweiz): Make this import better. | 12 // TODO(nweiz): Make this import better. |
| 13 import '../../pkg/http/lib/http.dart' as http; | 13 import '../../pkg/http/lib/http.dart' as http; |
| 14 import 'curl_client.dart'; | 14 import 'curl_client.dart'; |
| 15 import 'io.dart'; | 15 import 'io.dart'; |
| 16 import 'log.dart' as log; | 16 import 'log.dart' as log; |
| 17 import 'utils.dart'; | 17 import 'utils.dart'; |
| 18 | 18 |
| 19 // TODO(nweiz): make this configurable | 19 // TODO(nweiz): make this configurable |
| 20 /// The amount of time in milliseconds to allow HTTP requests before assuming | 20 /// The amount of time in milliseconds to allow HTTP requests before assuming |
| (...skipping 29 matching lines...) Expand all Loading... |
| 50 var status = streamedResponse.statusCode; | 50 var status = streamedResponse.statusCode; |
| 51 // 401 responses should be handled by the OAuth2 client. It's very | 51 // 401 responses should be handled by the OAuth2 client. It's very |
| 52 // unlikely that they'll be returned by non-OAuth2 requests. | 52 // unlikely that they'll be returned by non-OAuth2 requests. |
| 53 if (status < 400 || status == 401) { | 53 if (status < 400 || status == 401) { |
| 54 return new Future.immediate(streamedResponse); | 54 return new Future.immediate(streamedResponse); |
| 55 } | 55 } |
| 56 | 56 |
| 57 return http.Response.fromStream(streamedResponse).then((response) { | 57 return http.Response.fromStream(streamedResponse).then((response) { |
| 58 throw new PubHttpException(response); | 58 throw new PubHttpException(response); |
| 59 }); | 59 }); |
| 60 }).catchError((e) { | 60 }).catchError((asyncError) { |
| 61 e = getRealError(e); | 61 var e = getRealError(asyncError); |
| 62 if (e is SocketIOException && | 62 if (e is SocketIOException && |
| 63 e.osError != null && | 63 e.osError != null && |
| 64 (e.osError.errorCode == 8 || | 64 (e.osError.errorCode == 8 || |
| 65 e.osError.errorCode == -2 || | 65 e.osError.errorCode == -2 || |
| 66 e.osError.errorCode == -5 || | 66 e.osError.errorCode == -5 || |
| 67 e.osError.errorCode == 11004)) { | 67 e.osError.errorCode == 11004)) { |
| 68 throw 'Could not resolve URL "${request.url.origin}".'; | 68 throw 'Could not resolve URL "${request.url.origin}".'; |
| 69 } | 69 } |
| 70 throw e; | 70 throw asyncError; |
| 71 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); | 71 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 | 74 |
| 75 /// The HTTP client to use for all HTTP requests. | 75 /// The HTTP client to use for all HTTP requests. |
| 76 final httpClient = new PubHttpClient(); | 76 final httpClient = new PubHttpClient(); |
| 77 | 77 |
| 78 final curlClient = new PubHttpClient(new CurlClient()); | 78 final curlClient = new PubHttpClient(new CurlClient()); |
| 79 | 79 |
| 80 /// Handles a successful JSON-formatted response from pub.dartlang.org. | 80 /// Handles a successful JSON-formatted response from pub.dartlang.org. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 105 invalidServerResponse(response); | 105 invalidServerResponse(response); |
| 106 } | 106 } |
| 107 throw errorMap['error']['message']; | 107 throw errorMap['error']['message']; |
| 108 } | 108 } |
| 109 | 109 |
| 110 /// Parses a response body, assuming it's JSON-formatted. Throws a user-friendly | 110 /// Parses a response body, assuming it's JSON-formatted. Throws a user-friendly |
| 111 /// error if the response body is invalid JSON, or if it's not a map. | 111 /// error if the response body is invalid JSON, or if it's not a map. |
| 112 Map parseJsonResponse(http.Response response) { | 112 Map parseJsonResponse(http.Response response) { |
| 113 var value; | 113 var value; |
| 114 try { | 114 try { |
| 115 value = JSON.parse(response.body); | 115 value = json.parse(response.body); |
| 116 } catch (e) { | 116 } catch (e) { |
| 117 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. | 117 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. |
| 118 invalidServerResponse(response); | 118 invalidServerResponse(response); |
| 119 } | 119 } |
| 120 if (value is! Map) invalidServerResponse(response); | 120 if (value is! Map) invalidServerResponse(response); |
| 121 return value; | 121 return value; |
| 122 } | 122 } |
| 123 | 123 |
| 124 /// Throws an error describing an invalid response from the server. | 124 /// Throws an error describing an invalid response from the server. |
| 125 void invalidServerResponse(http.Response response) { | 125 void invalidServerResponse(http.Response response) { |
| 126 throw 'Invalid server response:\n${response.body}'; | 126 throw 'Invalid server response:\n${response.body}'; |
| 127 } | 127 } |
| 128 | 128 |
| 129 /// Exception thrown when an HTTP operation fails. | 129 /// Exception thrown when an HTTP operation fails. |
| 130 class PubHttpException implements Exception { | 130 class PubHttpException implements Exception { |
| 131 final http.Response response; | 131 final http.Response response; |
| 132 | 132 |
| 133 const PubHttpException(this.response); | 133 const PubHttpException(this.response); |
| 134 | 134 |
| 135 String toString() => 'HTTP error ${response.statusCode}: ' | 135 String toString() => 'HTTP error ${response.statusCode}: ' |
| 136 '${response.reasonPhrase}'; | 136 '${response.reasonPhrase}'; |
| 137 } | 137 } |
| OLD | NEW |