| 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:convert'; | 9 import 'dart:convert'; | 
| 10 import 'dart:io'; | 10 import 'dart:io'; | 
| 11 | 11 | 
| 12 import 'package:http/http.dart' as http; | 12 import 'package:http/http.dart' as http; | 
|  | 13 import 'package:http_throttle/http_throttle.dart'; | 
| 13 | 14 | 
| 14 import 'io.dart'; | 15 import 'io.dart'; | 
| 15 import 'log.dart' as log; | 16 import 'log.dart' as log; | 
| 16 import 'oauth2.dart' as oauth2; | 17 import 'oauth2.dart' as oauth2; | 
| 17 import 'sdk.dart' as sdk; | 18 import 'sdk.dart' as sdk; | 
| 18 import 'utils.dart'; | 19 import 'utils.dart'; | 
| 19 | 20 | 
| 20 // TODO(nweiz): make this configurable | 21 // TODO(nweiz): make this configurable | 
| 21 /// The amount of time in milliseconds to allow HTTP requests before assuming | 22 /// The amount of time in milliseconds to allow HTTP requests before assuming | 
| 22 /// they've failed. | 23 /// they've failed. | 
| 23 final HTTP_TIMEOUT = 30 * 1000; | 24 final HTTP_TIMEOUT = 30 * 1000; | 
| 24 | 25 | 
| 25 /// Headers and field names that should be censored in the log output. | 26 /// Headers and field names that should be censored in the log output. | 
| 26 final _CENSORED_FIELDS = const ['refresh_token', 'authorization']; | 27 final _CENSORED_FIELDS = const ['refresh_token', 'authorization']; | 
| 27 | 28 | 
| 28 /// Headers required for pub.dartlang.org API requests. | 29 /// Headers required for pub.dartlang.org API requests. | 
| 29 /// | 30 /// | 
| 30 /// The Accept header tells pub.dartlang.org which version of the API we're | 31 /// The Accept header tells pub.dartlang.org which version of the API we're | 
| 31 /// expecting, so it can either serve that version or give us a 406 error if | 32 /// expecting, so it can either serve that version or give us a 406 error if | 
| 32 /// it's not supported. | 33 /// it's not supported. | 
| 33 final PUB_API_HEADERS = const {'Accept': 'application/vnd.pub.v2+json'}; | 34 final PUB_API_HEADERS = const {'Accept': 'application/vnd.pub.v2+json'}; | 
| 34 | 35 | 
| 35 /// An HTTP client that transforms 40* errors and socket exceptions into more | 36 /// An HTTP client that transforms 40* errors and socket exceptions into more | 
| 36 /// user-friendly error messages. | 37 /// user-friendly error messages. | 
| 37 /// | 38 /// | 
| 38 /// This also adds a 30-second timeout to every request. This can be configured | 39 /// This also adds a 30-second timeout to every request. This can be configured | 
| 39 /// on a per-request basis by setting the 'Pub-Request-Timeout' header to the | 40 /// on a per-request basis by setting the 'Pub-Request-Timeout' header to the | 
| 40 /// desired number of milliseconds, or to "None" to disable the timeout. | 41 /// desired number of milliseconds, or to "None" to disable the timeout. | 
| 41 class PubHttpClient extends http.BaseClient { | 42 class _PubHttpClient extends http.BaseClient { | 
| 42   final _requestStopwatches = new Map<http.BaseRequest, Stopwatch>(); | 43   final _requestStopwatches = new Map<http.BaseRequest, Stopwatch>(); | 
| 43 | 44 | 
| 44   http.Client inner; | 45   http.Client _inner; | 
| 45 | 46 | 
| 46   PubHttpClient([http.Client inner]) | 47   _PubHttpClient([http.Client inner]) | 
| 47       : this.inner = inner == null ? new http.Client() : inner; | 48       : this._inner = inner == null ? new http.Client() : inner; | 
| 48 | 49 | 
| 49   Future<http.StreamedResponse> send(http.BaseRequest request) { | 50   Future<http.StreamedResponse> send(http.BaseRequest request) { | 
| 50     _requestStopwatches[request] = new Stopwatch()..start(); | 51     _requestStopwatches[request] = new Stopwatch()..start(); | 
| 51     request.headers[HttpHeaders.USER_AGENT] = "Dart pub ${sdk.version}"; | 52     request.headers[HttpHeaders.USER_AGENT] = "Dart pub ${sdk.version}"; | 
| 52     _logRequest(request); | 53     _logRequest(request); | 
| 53 | 54 | 
| 54     // TODO(nweiz): remove this when issue 4061 is fixed. | 55     // TODO(nweiz): remove this when issue 4061 is fixed. | 
| 55     var stackTrace; | 56     var stackTrace; | 
| 56     try { | 57     try { | 
| 57       throw null; | 58       throw null; | 
| 58     } catch (_, localStackTrace) { | 59     } catch (_, localStackTrace) { | 
| 59       stackTrace = localStackTrace; | 60       stackTrace = localStackTrace; | 
| 60     } | 61     } | 
| 61 | 62 | 
| 62     var timeoutLength = HTTP_TIMEOUT; | 63     var timeoutLength = HTTP_TIMEOUT; | 
| 63     var timeoutString = request.headers.remove('Pub-Request-Timeout'); | 64     var timeoutString = request.headers.remove('Pub-Request-Timeout'); | 
| 64     if (timeoutString == 'None') { | 65     if (timeoutString == 'None') { | 
| 65       timeoutLength = null; | 66       timeoutLength = null; | 
| 66     } else if (timeoutString != null) { | 67     } else if (timeoutString != null) { | 
| 67       timeoutLength = int.parse(timeoutString); | 68       timeoutLength = int.parse(timeoutString); | 
| 68     } | 69     } | 
| 69 | 70 | 
| 70     var future = inner.send(request).then((streamedResponse) { | 71     var future = _inner.send(request).then((streamedResponse) { | 
| 71       _logResponse(streamedResponse); | 72       _logResponse(streamedResponse); | 
| 72 | 73 | 
| 73       var status = streamedResponse.statusCode; | 74       var status = streamedResponse.statusCode; | 
| 74       // 401 responses should be handled by the OAuth2 client. It's very | 75       // 401 responses should be handled by the OAuth2 client. It's very | 
| 75       // unlikely that they'll be returned by non-OAuth2 requests. We also want | 76       // unlikely that they'll be returned by non-OAuth2 requests. We also want | 
| 76       // to pass along 400 responses from the token endpoint. | 77       // to pass along 400 responses from the token endpoint. | 
| 77       var tokenRequest = urisEqual( | 78       var tokenRequest = urisEqual( | 
| 78           streamedResponse.request.url, oauth2.tokenEndpoint); | 79           streamedResponse.request.url, oauth2.tokenEndpoint); | 
| 79       if (status < 400 || status == 401 || (status == 400 && tokenRequest)) { | 80       if (status < 400 || status == 401 || (status == 400 && tokenRequest)) { | 
| 80         return streamedResponse; | 81         return streamedResponse; | 
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 184   /// [name] and [value]. | 185   /// [name] and [value]. | 
| 185   String _logField(String name, String value) { | 186   String _logField(String name, String value) { | 
| 186     if (_CENSORED_FIELDS.contains(name.toLowerCase())) { | 187     if (_CENSORED_FIELDS.contains(name.toLowerCase())) { | 
| 187       return "$name: <censored>"; | 188       return "$name: <censored>"; | 
| 188     } else { | 189     } else { | 
| 189       return "$name: $value"; | 190       return "$name: $value"; | 
| 190     } | 191     } | 
| 191   } | 192   } | 
| 192 } | 193 } | 
| 193 | 194 | 
|  | 195 /// The [_PubHttpClient] wrapped by [httpClient]. | 
|  | 196 final _pubClient = new _PubHttpClient(); | 
|  | 197 | 
| 194 /// The HTTP client to use for all HTTP requests. | 198 /// The HTTP client to use for all HTTP requests. | 
| 195 final httpClient = new PubHttpClient(); | 199 final httpClient = new ThrottleClient(16, _pubClient); | 
|  | 200 | 
|  | 201 /// The underlying HTTP client wrapped by [httpClient]. | 
|  | 202 http.Client get innerHttpClient => _pubClient._inner; | 
|  | 203 set innerHttpClient(http.Client client) => _pubClient._inner = client; | 
| 196 | 204 | 
| 197 /// Handles a successful JSON-formatted response from pub.dartlang.org. | 205 /// Handles a successful JSON-formatted response from pub.dartlang.org. | 
| 198 /// | 206 /// | 
| 199 /// These responses are expected to be of the form `{"success": {"message": | 207 /// These responses are expected to be of the form `{"success": {"message": | 
| 200 /// "some message"}}`. If the format is correct, the message will be printed; | 208 /// "some message"}}`. If the format is correct, the message will be printed; | 
| 201 /// otherwise an error will be raised. | 209 /// otherwise an error will be raised. | 
| 202 void handleJsonSuccess(http.Response response) { | 210 void handleJsonSuccess(http.Response response) { | 
| 203   var parsed = parseJsonResponse(response); | 211   var parsed = parseJsonResponse(response); | 
| 204   if (parsed['success'] is! Map || | 212   if (parsed['success'] is! Map || | 
| 205       !parsed['success'].containsKey('message') || | 213       !parsed['success'].containsKey('message') || | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 245 | 253 | 
| 246 /// Exception thrown when an HTTP operation fails. | 254 /// Exception thrown when an HTTP operation fails. | 
| 247 class PubHttpException implements Exception { | 255 class PubHttpException implements Exception { | 
| 248   final http.Response response; | 256   final http.Response response; | 
| 249 | 257 | 
| 250   const PubHttpException(this.response); | 258   const PubHttpException(this.response); | 
| 251 | 259 | 
| 252   String toString() => 'HTTP error ${response.statusCode}: ' | 260   String toString() => 'HTTP error ${response.statusCode}: ' | 
| 253       '${response.reasonPhrase}'; | 261       '${response.reasonPhrase}'; | 
| 254 } | 262 } | 
| OLD | NEW | 
|---|