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:io'; | 8 import 'dart:io'; |
9 import 'dart:json'; | 9 import 'dart:json'; |
10 | 10 |
11 // TODO(nweiz): Make this import better. | 11 // TODO(nweiz): Make this import better. |
12 import '../../pkg/http/lib/http.dart' as http; | 12 import '../../pkg/http/lib/http.dart' as http; |
13 import 'curl_client.dart'; | 13 import 'curl_client.dart'; |
14 import 'io.dart'; | 14 import 'io.dart'; |
15 import 'log.dart' as log; | 15 import 'log.dart' as log; |
16 | 16 |
17 // TODO(nweiz): make this configurable | 17 // TODO(nweiz): make this configurable |
18 /** | 18 /// The amount of time in milliseconds to allow HTTP requests before assuming |
19 * The amount of time in milliseconds to allow HTTP requests before assuming | 19 /// they've failed. |
20 * they've failed. | |
21 */ | |
22 final HTTP_TIMEOUT = 30 * 1000; | 20 final HTTP_TIMEOUT = 30 * 1000; |
23 | 21 |
24 /// An HTTP client that transforms 40* errors and socket exceptions into more | 22 /// An HTTP client that transforms 40* errors and socket exceptions into more |
25 /// user-friendly error messages. | 23 /// user-friendly error messages. |
26 class PubHttpClient extends http.BaseClient { | 24 class PubHttpClient extends http.BaseClient { |
27 final http.Client _inner; | 25 final http.Client _inner; |
28 | 26 |
29 PubHttpClient([http.Client inner]) | 27 PubHttpClient([http.Client inner]) |
30 : _inner = inner == null ? new http.Client() : inner; | 28 : _inner = inner == null ? new http.Client() : inner; |
31 | 29 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 116 } |
119 if (value is! Map) invalidServerResponse(response); | 117 if (value is! Map) invalidServerResponse(response); |
120 return value; | 118 return value; |
121 } | 119 } |
122 | 120 |
123 /// Throws an error describing an invalid response from the server. | 121 /// Throws an error describing an invalid response from the server. |
124 void invalidServerResponse(http.Response response) { | 122 void invalidServerResponse(http.Response response) { |
125 throw 'Invalid server response:\n${response.body}'; | 123 throw 'Invalid server response:\n${response.body}'; |
126 } | 124 } |
127 | 125 |
128 /** | 126 /// Exception thrown when an HTTP operation fails. |
129 * Exception thrown when an HTTP operation fails. | |
130 */ | |
131 class PubHttpException implements Exception { | 127 class PubHttpException implements Exception { |
132 final http.Response response; | 128 final http.Response response; |
133 | 129 |
134 const PubHttpException(this.response); | 130 const PubHttpException(this.response); |
135 | 131 |
136 String toString() => 'HTTP error ${response.statusCode}: ' | 132 String toString() => 'HTTP error ${response.statusCode}: ' |
137 '${response.reasonPhrase}'; | 133 '${response.reasonPhrase}'; |
138 } | 134 } |
OLD | NEW |