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'; |
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 | 18 |
18 // TODO(nweiz): make this configurable | 19 // TODO(nweiz): make this configurable |
19 /// 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 |
20 /// they've failed. | 21 /// they've failed. |
21 final HTTP_TIMEOUT = 30 * 1000; | 22 final HTTP_TIMEOUT = 30 * 1000; |
22 | 23 |
23 /// An HTTP client that transforms 40* errors and socket exceptions into more | 24 /// An HTTP client that transforms 40* errors and socket exceptions into more |
24 /// user-friendly error messages. | 25 /// user-friendly error messages. |
25 class PubHttpClient extends http.BaseClient { | 26 class PubHttpClient extends http.BaseClient { |
26 http.Client inner; | 27 http.Client inner; |
(...skipping 23 matching lines...) Expand all Loading... |
50 // 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 |
51 // unlikely that they'll be returned by non-OAuth2 requests. | 52 // unlikely that they'll be returned by non-OAuth2 requests. |
52 if (status < 400 || status == 401) { | 53 if (status < 400 || status == 401) { |
53 return new Future.immediate(streamedResponse); | 54 return new Future.immediate(streamedResponse); |
54 } | 55 } |
55 | 56 |
56 return http.Response.fromStream(streamedResponse).then((response) { | 57 return http.Response.fromStream(streamedResponse).then((response) { |
57 throw new PubHttpException(response); | 58 throw new PubHttpException(response); |
58 }); | 59 }); |
59 }).catchError((e) { | 60 }).catchError((e) { |
| 61 e = getRealError(e); |
60 if (e is SocketIOException && | 62 if (e is SocketIOException && |
61 e.osError != null && | 63 e.osError != null && |
62 (e.osError.errorCode == 8 || | 64 (e.osError.errorCode == 8 || |
63 e.osError.errorCode == -2 || | 65 e.osError.errorCode == -2 || |
64 e.osError.errorCode == -5 || | 66 e.osError.errorCode == -5 || |
65 e.osError.errorCode == 11004)) { | 67 e.osError.errorCode == 11004)) { |
66 throw 'Could not resolve URL "${request.url.origin}".'; | 68 throw 'Could not resolve URL "${request.url.origin}".'; |
67 } | 69 } |
68 throw e; | 70 throw e; |
69 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); | 71 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 128 |
127 /// Exception thrown when an HTTP operation fails. | 129 /// Exception thrown when an HTTP operation fails. |
128 class PubHttpException implements Exception { | 130 class PubHttpException implements Exception { |
129 final http.Response response; | 131 final http.Response response; |
130 | 132 |
131 const PubHttpException(this.response); | 133 const PubHttpException(this.response); |
132 | 134 |
133 String toString() => 'HTTP error ${response.statusCode}: ' | 135 String toString() => 'HTTP error ${response.statusCode}: ' |
134 '${response.reasonPhrase}'; | 136 '${response.reasonPhrase}'; |
135 } | 137 } |
OLD | NEW |