| 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:io'; | 9 import 'dart:io'; |
| 9 import 'dart:json'; | 10 import 'dart:json'; |
| 10 | 11 |
| 11 // TODO(nweiz): Make this import better. | 12 // TODO(nweiz): Make this import better. |
| 12 import '../../pkg/http/lib/http.dart' as http; | 13 import '../../pkg/http/lib/http.dart' as http; |
| 13 import 'curl_client.dart'; | 14 import 'curl_client.dart'; |
| 14 import 'io.dart'; | 15 import 'io.dart'; |
| 15 import 'log.dart' as log; | 16 import 'log.dart' as log; |
| 16 | 17 |
| 17 // TODO(nweiz): make this configurable | 18 // TODO(nweiz): make this configurable |
| (...skipping 16 matching lines...) Expand all Loading... |
| 34 // TODO(nweiz): remove this when issue 4061 is fixed. | 35 // TODO(nweiz): remove this when issue 4061 is fixed. |
| 35 var stackTrace; | 36 var stackTrace; |
| 36 try { | 37 try { |
| 37 throw null; | 38 throw null; |
| 38 } catch (_, localStackTrace) { | 39 } catch (_, localStackTrace) { |
| 39 stackTrace = localStackTrace; | 40 stackTrace = localStackTrace; |
| 40 } | 41 } |
| 41 | 42 |
| 42 // TODO(nweiz): Ideally the timeout would extend to reading from the | 43 // TODO(nweiz): Ideally the timeout would extend to reading from the |
| 43 // response input stream, but until issue 3657 is fixed that's not feasible. | 44 // response input stream, but until issue 3657 is fixed that's not feasible. |
| 44 return timeout(inner.send(request).chain((streamedResponse) { | 45 return timeout(inner.send(request).then((streamedResponse) { |
| 45 log.fine("Got response ${streamedResponse.statusCode} " | 46 log.fine("Got response ${streamedResponse.statusCode} " |
| 46 "${streamedResponse.reasonPhrase}."); | 47 "${streamedResponse.reasonPhrase}."); |
| 47 | 48 |
| 48 var status = streamedResponse.statusCode; | 49 var status = streamedResponse.statusCode; |
| 49 // 401 responses should be handled by the OAuth2 client. It's very | 50 // 401 responses should be handled by the OAuth2 client. It's very |
| 50 // unlikely that they'll be returned by non-OAuth2 requests. | 51 // unlikely that they'll be returned by non-OAuth2 requests. |
| 51 if (status < 400 || status == 401) { | 52 if (status < 400 || status == 401) { |
| 52 return new Future.immediate(streamedResponse); | 53 return new Future.immediate(streamedResponse); |
| 53 } | 54 } |
| 54 | 55 |
| 55 return http.Response.fromStream(streamedResponse).transform((response) { | 56 return http.Response.fromStream(streamedResponse).then((response) { |
| 56 throw new PubHttpException(response); | 57 throw new PubHttpException(response); |
| 57 }); | 58 }); |
| 58 }).transformException((e) { | 59 }).catchError((e) { |
| 59 if (e is SocketIOException && | 60 if (e is SocketIOException && |
| 60 e.osError != null && | 61 e.osError != null && |
| 61 (e.osError.errorCode == 8 || | 62 (e.osError.errorCode == 8 || |
| 62 e.osError.errorCode == -2 || | 63 e.osError.errorCode == -2 || |
| 63 e.osError.errorCode == -5 || | 64 e.osError.errorCode == -5 || |
| 64 e.osError.errorCode == 11004)) { | 65 e.osError.errorCode == 11004)) { |
| 65 throw 'Could not resolve URL "${request.url.origin}".'; | 66 throw 'Could not resolve URL "${request.url.origin}".'; |
| 66 } | 67 } |
| 67 throw e; | 68 throw e; |
| 68 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); | 69 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 126 |
| 126 /// Exception thrown when an HTTP operation fails. | 127 /// Exception thrown when an HTTP operation fails. |
| 127 class PubHttpException implements Exception { | 128 class PubHttpException implements Exception { |
| 128 final http.Response response; | 129 final http.Response response; |
| 129 | 130 |
| 130 const PubHttpException(this.response); | 131 const PubHttpException(this.response); |
| 131 | 132 |
| 132 String toString() => 'HTTP error ${response.statusCode}: ' | 133 String toString() => 'HTTP error ${response.statusCode}: ' |
| 133 '${response.reasonPhrase}'; | 134 '${response.reasonPhrase}'; |
| 134 } | 135 } |
| OLD | NEW |