| 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 /** |
| 19 * 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 |
| 20 * they've failed. | 20 * they've failed. |
| 21 */ | 21 */ |
| 22 final HTTP_TIMEOUT = 30 * 1000; | 22 final HTTP_TIMEOUT = 30 * 1000; |
| 23 | 23 |
| 24 /// 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 |
| 25 /// user-friendly error messages. | 25 /// user-friendly error messages. |
| 26 class PubHttpClient extends http.BaseClient { | 26 class PubHttpClient extends http.BaseClient { |
| 27 final http.Client _inner; | 27 http.Client inner; |
| 28 | 28 |
| 29 PubHttpClient([http.Client inner]) | 29 PubHttpClient([http.Client inner]) |
| 30 : _inner = inner == null ? new http.Client() : inner; | 30 : this.inner = inner == null ? new http.Client() : inner; |
| 31 | 31 |
| 32 Future<http.StreamedResponse> send(http.BaseRequest request) { | 32 Future<http.StreamedResponse> send(http.BaseRequest request) { |
| 33 // TODO(rnystrom): Log request body when it's available and plaintext, but | 33 // TODO(rnystrom): Log request body when it's available and plaintext, but |
| 34 // not when it contains OAuth2 credentials. | 34 // not when it contains OAuth2 credentials. |
| 35 | 35 |
| 36 // TODO(nweiz): remove this when issue 4061 is fixed. | 36 // TODO(nweiz): remove this when issue 4061 is fixed. |
| 37 var stackTrace; | 37 var stackTrace; |
| 38 try { | 38 try { |
| 39 throw null; | 39 throw null; |
| 40 } catch (_, localStackTrace) { | 40 } catch (_, localStackTrace) { |
| 41 stackTrace = localStackTrace; | 41 stackTrace = localStackTrace; |
| 42 } | 42 } |
| 43 | 43 |
| 44 // TODO(nweiz): Ideally the timeout would extend to reading from the | 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. | 45 // response input stream, but until issue 3657 is fixed that's not feasible. |
| 46 return timeout(_inner.send(request).chain((streamedResponse) { | 46 return timeout(inner.send(request).chain((streamedResponse) { |
| 47 log.fine("Got response ${streamedResponse.statusCode} " | 47 log.fine("Got response ${streamedResponse.statusCode} " |
| 48 "${streamedResponse.reasonPhrase}."); | 48 "${streamedResponse.reasonPhrase}."); |
| 49 | 49 |
| 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 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 * Exception thrown when an HTTP operation fails. | 129 * Exception thrown when an HTTP operation fails. |
| 130 */ | 130 */ |
| 131 class PubHttpException implements Exception { | 131 class PubHttpException implements Exception { |
| 132 final http.Response response; | 132 final http.Response response; |
| 133 | 133 |
| 134 const PubHttpException(this.response); | 134 const PubHttpException(this.response); |
| 135 | 135 |
| 136 String toString() => 'HTTP error ${response.statusCode}: ' | 136 String toString() => 'HTTP error ${response.statusCode}: ' |
| 137 '${response.reasonPhrase}'; | 137 '${response.reasonPhrase}'; |
| 138 } | 138 } |
| OLD | NEW |