| 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 library curl_client; | 5 library curl_client; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import '../../pkg/http/lib/http.dart' as http; | 9 import '../../pkg/http/lib/http.dart' as http; |
| 10 import 'io.dart'; | 10 import 'io.dart'; |
| 11 import 'log.dart' as log; |
| 11 import 'utils.dart'; | 12 import 'utils.dart'; |
| 12 | 13 |
| 13 /// A drop-in replacement for [http.Client] that uses the `curl` command-line | 14 /// A drop-in replacement for [http.Client] that uses the `curl` command-line |
| 14 /// utility rather than [dart:io] to make requests. This class will only exist | 15 /// utility rather than [dart:io] to make requests. This class will only exist |
| 15 /// temporarily until [dart:io] natively supports requests over HTTPS. | 16 /// temporarily until [dart:io] natively supports requests over HTTPS. |
| 16 class CurlClient extends http.BaseClient { | 17 class CurlClient extends http.BaseClient { |
| 17 /// The path to the `curl` executable to run. | 18 /// The path to the `curl` executable to run. |
| 18 /// | 19 /// |
| 19 /// By default on Unix-like operating systems, this will look up `curl` on the | 20 /// By default on Unix-like operating systems, this will look up `curl` on the |
| 20 /// system path. On Windows, it will use the bundled `curl.exe`. | 21 /// system path. On Windows, it will use the bundled `curl.exe`. |
| 21 final String executable; | 22 final String executable; |
| 22 | 23 |
| 23 /// Creates a new [CurlClient] with [executable] as the path to the `curl` | 24 /// Creates a new [CurlClient] with [executable] as the path to the `curl` |
| 24 /// executable. | 25 /// executable. |
| 25 /// | 26 /// |
| 26 /// By default on Unix-like operating systems, this will look up `curl` on the | 27 /// By default on Unix-like operating systems, this will look up `curl` on the |
| 27 /// system path. On Windows, it will use the bundled `curl.exe`. | 28 /// system path. On Windows, it will use the bundled `curl.exe`. |
| 28 CurlClient([String executable]) | 29 CurlClient([String executable]) |
| 29 : executable = executable == null ? _defaultExecutable : executable; | 30 : executable = executable == null ? _defaultExecutable : executable; |
| 30 | 31 |
| 31 /// Sends a request via `curl` and returns the response. | 32 /// Sends a request via `curl` and returns the response. |
| 32 Future<http.StreamedResponse> send(http.BaseRequest request) { | 33 Future<http.StreamedResponse> send(http.BaseRequest request) { |
| 34 log.fine("Sending Curl request $request"); |
| 35 |
| 33 var requestStream = request.finalize(); | 36 var requestStream = request.finalize(); |
| 34 return withTempDir((tempDir) { | 37 return withTempDir((tempDir) { |
| 35 var headerFile = new Path(tempDir).append("curl-headers").toNativePath(); | 38 var headerFile = new Path(tempDir).append("curl-headers").toNativePath(); |
| 36 var arguments = _argumentsForRequest(request, headerFile); | 39 var arguments = _argumentsForRequest(request, headerFile); |
| 40 log.process(executable, arguments); |
| 37 var process; | 41 var process; |
| 38 return Process.start(executable, arguments).chain((process_) { | 42 return Process.start(executable, arguments).chain((process_) { |
| 39 process = process_; | 43 process = process_; |
| 40 if (requestStream.closed) { | 44 if (requestStream.closed) { |
| 41 process.stdin.close(); | 45 process.stdin.close(); |
| 42 } else { | 46 } else { |
| 43 requestStream.pipe(process.stdin); | 47 requestStream.pipe(process.stdin); |
| 44 } | 48 } |
| 45 | 49 |
| 46 return _waitForHeaders(process, expectBody: request.method != "HEAD"); | 50 return _waitForHeaders(process, expectBody: request.method != "HEAD"); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 /// requests). | 111 /// requests). |
| 108 /// | 112 /// |
| 109 /// Curl prints the headers to a file and then prints the body to stdout. So, | 113 /// Curl prints the headers to a file and then prints the body to stdout. So, |
| 110 /// in theory, we could read the headers as soon as we see anything appear | 114 /// in theory, we could read the headers as soon as we see anything appear |
| 111 /// in stdout. However, that seems to be too early to successfully read the | 115 /// in stdout. However, that seems to be too early to successfully read the |
| 112 /// file (at least on Mac). Instead, this just waits until the entire process | 116 /// file (at least on Mac). Instead, this just waits until the entire process |
| 113 /// has completed. | 117 /// has completed. |
| 114 Future _waitForHeaders(Process process, {bool expectBody}) { | 118 Future _waitForHeaders(Process process, {bool expectBody}) { |
| 115 var completer = new Completer(); | 119 var completer = new Completer(); |
| 116 process.onExit = (exitCode) { | 120 process.onExit = (exitCode) { |
| 121 log.io("Curl process exited with code $exitCode."); |
| 122 |
| 117 if (exitCode == 0) { | 123 if (exitCode == 0) { |
| 118 completer.complete(null); | 124 completer.complete(null); |
| 119 return; | 125 return; |
| 120 } | 126 } |
| 121 | 127 |
| 122 chainToCompleter(consumeInputStream(process.stderr) | 128 chainToCompleter(consumeInputStream(process.stderr) |
| 123 .transform((stderrBytes) { | 129 .transform((stderrBytes) { |
| 124 var message = new String.fromCharCodes(stderrBytes); | 130 var message = new String.fromCharCodes(stderrBytes); |
| 131 log.fine('Got error reading headers from curl: $message'); |
| 125 if (exitCode == 47) { | 132 if (exitCode == 47) { |
| 126 throw new RedirectLimitExceededException([]); | 133 throw new RedirectLimitExceededException([]); |
| 127 } else { | 134 } else { |
| 128 throw new HttpException(message); | 135 throw new HttpException(message); |
| 129 } | 136 } |
| 130 }), completer); | 137 }), completer); |
| 131 }; | 138 }; |
| 132 | 139 |
| 133 // If there's not going to be a response body (e.g. for HEAD requests), curl | 140 // If there's not going to be a response body (e.g. for HEAD requests), curl |
| 134 // prints the headers to stdout instead of the body. We want to wait until | 141 // prints the headers to stdout instead of the body. We want to wait until |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 /// path to the bundled `curl.exe`; elsewhere, this is just "curl", and we | 193 /// path to the bundled `curl.exe`; elsewhere, this is just "curl", and we |
| 187 /// assume it to be installed and on the user's PATH. | 194 /// assume it to be installed and on the user's PATH. |
| 188 static String get _defaultExecutable { | 195 static String get _defaultExecutable { |
| 189 if (Platform.operatingSystem != 'windows') return 'curl'; | 196 if (Platform.operatingSystem != 'windows') return 'curl'; |
| 190 // Note: This line of code gets munged by create_sdk.py to be the correct | 197 // Note: This line of code gets munged by create_sdk.py to be the correct |
| 191 // relative path to curl in the SDK. | 198 // relative path to curl in the SDK. |
| 192 var pathToCurl = "../../third_party/curl/curl.exe"; | 199 var pathToCurl = "../../third_party/curl/curl.exe"; |
| 193 return relativeToPub(pathToCurl); | 200 return relativeToPub(pathToCurl); |
| 194 } | 201 } |
| 195 } | 202 } |
| OLD | NEW |