| 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import '../../pkg/http/lib/http.dart' as http; | 10 import '../../pkg/http/lib/http.dart' as http; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 log.fine("Sending Curl request $request"); | 35 log.fine("Sending Curl request $request"); |
| 36 | 36 |
| 37 var requestStream = request.finalize(); | 37 var requestStream = request.finalize(); |
| 38 return withTempDir((tempDir) { | 38 return withTempDir((tempDir) { |
| 39 var headerFile = join(tempDir, "curl-headers"); | 39 var headerFile = join(tempDir, "curl-headers"); |
| 40 var arguments = _argumentsForRequest(request, headerFile); | 40 var arguments = _argumentsForRequest(request, headerFile); |
| 41 log.process(executable, arguments); | 41 log.process(executable, arguments); |
| 42 var process; | 42 var process; |
| 43 return startProcess(executable, arguments).then((process_) { | 43 return startProcess(executable, arguments).then((process_) { |
| 44 process = process_; | 44 process = process_; |
| 45 if (requestStream.closed) { | 45 return requestStream.pipe(wrapOutputStream(process.stdin)); |
| 46 process.stdin.close(); | 46 }).then((_) { |
| 47 } else { | |
| 48 requestStream.pipe(process.stdin); | |
| 49 } | |
| 50 | |
| 51 return _waitForHeaders(process, expectBody: request.method != "HEAD"); | 47 return _waitForHeaders(process, expectBody: request.method != "HEAD"); |
| 52 }).then((_) => new File(headerFile).readAsLines()) | 48 }).then((_) => new File(headerFile).readAsLines()) |
| 53 .then((lines) => _buildResponse(request, process, lines)); | 49 .then((lines) => _buildResponse(request, process, lines)); |
| 54 }); | 50 }); |
| 55 } | 51 } |
| 56 | 52 |
| 57 /// Returns the list of arguments to `curl` necessary for performing | 53 /// Returns the list of arguments to `curl` necessary for performing |
| 58 /// [request]. [headerFile] is the path to the file where the response headers | 54 /// [request]. [headerFile] is the path to the file where the response headers |
| 59 /// should be stored. | 55 /// should be stored. |
| 60 List<String> _argumentsForRequest( | 56 List<String> _argumentsForRequest( |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 Future _waitForHeaders(Process process, {bool expectBody}) { | 115 Future _waitForHeaders(Process process, {bool expectBody}) { |
| 120 var completer = new Completer(); | 116 var completer = new Completer(); |
| 121 process.onExit = (exitCode) { | 117 process.onExit = (exitCode) { |
| 122 log.io("Curl process exited with code $exitCode."); | 118 log.io("Curl process exited with code $exitCode."); |
| 123 | 119 |
| 124 if (exitCode == 0) { | 120 if (exitCode == 0) { |
| 125 completer.complete(null); | 121 completer.complete(null); |
| 126 return; | 122 return; |
| 127 } | 123 } |
| 128 | 124 |
| 129 chainToCompleter(consumeInputStream(process.stderr) | 125 chainToCompleter(consumeInputStream(process.stderr).then((stderrBytes) { |
| 130 .then((stderrBytes) { | |
| 131 var message = new String.fromCharCodes(stderrBytes); | 126 var message = new String.fromCharCodes(stderrBytes); |
| 132 log.fine('Got error reading headers from curl: $message'); | 127 log.fine('Got error reading headers from curl: $message'); |
| 133 if (exitCode == 47) { | 128 if (exitCode == 47) { |
| 134 throw new RedirectLimitExceededException([]); | 129 throw new RedirectLimitExceededException([]); |
| 135 } else { | 130 } else { |
| 136 throw new HttpException(message); | 131 throw new HttpException(message); |
| 137 } | 132 } |
| 138 }), completer); | 133 }), completer); |
| 139 }; | 134 }; |
| 140 | 135 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 var responseStream = process.stdout; | 171 var responseStream = process.stdout; |
| 177 if (responseStream.closed) { | 172 if (responseStream.closed) { |
| 178 responseStream = new ListInputStream(); | 173 responseStream = new ListInputStream(); |
| 179 responseStream.markEndOfStream(); | 174 responseStream.markEndOfStream(); |
| 180 } | 175 } |
| 181 var contentLength = -1; | 176 var contentLength = -1; |
| 182 if (headers.containsKey('content-length')) { | 177 if (headers.containsKey('content-length')) { |
| 183 contentLength = int.parse(headers['content-length']); | 178 contentLength = int.parse(headers['content-length']); |
| 184 } | 179 } |
| 185 | 180 |
| 186 return new http.StreamedResponse(responseStream, status, contentLength, | 181 return new http.StreamedResponse( |
| 182 wrapInputStream(responseStream), status, contentLength, |
| 187 request: request, | 183 request: request, |
| 188 headers: headers, | 184 headers: headers, |
| 189 isRedirect: isRedirect, | 185 isRedirect: isRedirect, |
| 190 reasonPhrase: reasonPhrase); | 186 reasonPhrase: reasonPhrase); |
| 191 } | 187 } |
| 192 | 188 |
| 193 /// The default executable to use for running curl. On Windows, this is the | 189 /// The default executable to use for running curl. On Windows, this is the |
| 194 /// path to the bundled `curl.exe`; elsewhere, this is just "curl", and we | 190 /// path to the bundled `curl.exe`; elsewhere, this is just "curl", and we |
| 195 /// assume it to be installed and on the user's PATH. | 191 /// assume it to be installed and on the user's PATH. |
| 196 static String get _defaultExecutable { | 192 static String get _defaultExecutable { |
| 197 if (Platform.operatingSystem != 'windows') return 'curl'; | 193 if (Platform.operatingSystem != 'windows') return 'curl'; |
| 198 // Note: This line of code gets munged by create_sdk.py to be the correct | 194 // Note: This line of code gets munged by create_sdk.py to be the correct |
| 199 // relative path to curl in the SDK. | 195 // relative path to curl in the SDK. |
| 200 var pathToCurl = "../../third_party/curl/curl.exe"; | 196 var pathToCurl = "../../third_party/curl/curl.exe"; |
| 201 return relativeToPub(pathToCurl); | 197 return relativeToPub(pathToCurl); |
| 202 } | 198 } |
| 203 } | 199 } |
| OLD | NEW |