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' as 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'; | |
15 import 'io.dart'; | 14 import 'io.dart'; |
16 import 'log.dart' as log; | 15 import 'log.dart' as log; |
17 import 'utils.dart'; | 16 import 'utils.dart'; |
18 | 17 |
19 // TODO(nweiz): make this configurable | 18 // TODO(nweiz): make this configurable |
20 /// 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 |
21 /// they've failed. | 20 /// they've failed. |
22 final HTTP_TIMEOUT = 30 * 1000; | 21 final HTTP_TIMEOUT = 30 * 1000; |
23 | 22 |
24 /// An HTTP client that transforms 40* errors and socket exceptions into more | 23 /// An HTTP client that transforms 40* errors and socket exceptions into more |
(...skipping 25 matching lines...) Expand all Loading... |
50 var status = streamedResponse.statusCode; | 49 var status = streamedResponse.statusCode; |
51 // 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 |
52 // unlikely that they'll be returned by non-OAuth2 requests. | 51 // unlikely that they'll be returned by non-OAuth2 requests. |
53 if (status < 400 || status == 401) return streamedResponse; | 52 if (status < 400 || status == 401) return streamedResponse; |
54 | 53 |
55 return http.Response.fromStream(streamedResponse).then((response) { | 54 return http.Response.fromStream(streamedResponse).then((response) { |
56 throw new PubHttpException(response); | 55 throw new PubHttpException(response); |
57 }); | 56 }); |
58 }).catchError((asyncError) { | 57 }).catchError((asyncError) { |
59 if (asyncError.error is SocketIOException && | 58 if (asyncError.error is SocketIOException && |
60 asyncError.error.osError != null && | 59 asyncError.error.osError != null) { |
61 (asyncError.error.osError.errorCode == 8 || | 60 if (asyncError.error.osError.errorCode == 8 || |
62 asyncError.error.osError.errorCode == -2 || | 61 asyncError.error.osError.errorCode == -2 || |
63 asyncError.error.osError.errorCode == -5 || | 62 asyncError.error.osError.errorCode == -5 || |
64 asyncError.error.osError.errorCode == 11004)) { | 63 asyncError.error.osError.errorCode == 11004) { |
65 throw 'Could not resolve URL "${request.url.origin}".'; | 64 throw 'Could not resolve URL "${request.url.origin}".'; |
| 65 } else if (asyncError.error.osError.errorCode == -12276) { |
| 66 throw 'Unable to validate SSL certificate for ' |
| 67 '"${request.url.origin}".'; |
| 68 } |
66 } | 69 } |
67 throw asyncError; | 70 throw asyncError; |
68 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); | 71 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); |
69 } | 72 } |
70 } | 73 } |
71 | 74 |
72 /// The HTTP client to use for all HTTP requests. | 75 /// The HTTP client to use for all HTTP requests. |
73 final httpClient = new PubHttpClient(); | 76 final httpClient = new PubHttpClient(); |
74 | 77 |
75 final curlClient = new PubHttpClient(new CurlClient()); | |
76 | |
77 /// Handles a successful JSON-formatted response from pub.dartlang.org. | 78 /// Handles a successful JSON-formatted response from pub.dartlang.org. |
78 /// | 79 /// |
79 /// These responses are expected to be of the form `{"success": {"message": | 80 /// These responses are expected to be of the form `{"success": {"message": |
80 /// "some message"}}`. If the format is correct, the message will be printed; | 81 /// "some message"}}`. If the format is correct, the message will be printed; |
81 /// otherwise an error will be raised. | 82 /// otherwise an error will be raised. |
82 void handleJsonSuccess(http.Response response) { | 83 void handleJsonSuccess(http.Response response) { |
83 var parsed = parseJsonResponse(response); | 84 var parsed = parseJsonResponse(response); |
84 if (parsed['success'] is! Map || | 85 if (parsed['success'] is! Map || |
85 !parsed['success'].containsKey('message') || | 86 !parsed['success'].containsKey('message') || |
86 parsed['success']['message'] is! String) { | 87 parsed['success']['message'] is! String) { |
(...skipping 38 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 |