| 88 | |
| 89 | |
| 90 | Future<Nothing> CurlFetcherPlugin::fetch( |
| 91 | const URI& uri, |
| 92 | const string& directory, |
| 93 | const Option<string>& data, |
| 94 | const Option<string>& outputFileName) const |
| 95 | { |
| 96 | // TODO(jieyu): Validate the given URI. |
| 97 | |
| 98 | if (!uri.has_path()) { |
| 99 | return Failure("URI path is not specified"); |
| 100 | } |
| 101 | |
| 102 | Try<Nothing> mkdir = os::mkdir(directory); |
| 103 | if (mkdir.isError()) { |
| 104 | return Failure( |
| 105 | "Failed to create directory '" + |
| 106 | directory + "': " + mkdir.error()); |
| 107 | } |
| 108 | |
| 109 | string output; |
| 110 | if (outputFileName.isSome()) { |
| 111 | output = path::join(directory, outputFileName.get()); |
| 112 | } else { |
| 113 | output = path::join(directory, Path(path::from_uri(uri.path())).basename()); |
| 114 | } |
| 115 | |
| 116 | #ifndef __WINDOWS__ |
| 117 | const string curl = "curl"; |
| 118 | #else |
| 119 | const string curl = "curl.exe"; |
| 120 | #endif // __WINDOWS__ |
| 121 | |
| 122 | vector<string> argv = { |
| 123 | curl, |
| 124 | "-s", // Don't show progress meter or error messages. |
| 125 | "-S", // Makes curl show an error message if it fails. |
| 126 | "-L", // Follow HTTP 3xx redirects. |
| 127 | "-w", "%{http_code}", // Display HTTP response code on stdout. |
| 128 | "-o", output, // Write output to the file. |
| 129 | strings::trim(stringify(uri)) |
| 130 | }; |
| 131 | |
| 132 | // Add a timeout for curl to abort when the download speed keeps low |
| 133 | // (1 byte per second by default) for the specified duration. See: |
| 134 | // https://curl.haxx.se/docs/manpage.html#-y |
| 135 | if (flags.curl_stall_timeout.isSome()) { |
| 136 | argv.push_back("-y"); |
| 137 | argv.push_back( |
| 138 | std::to_string(static_cast<long>(flags.curl_stall_timeout->secs()))); |
| 139 | } |
| 140 | |
| 141 | Try<Subprocess> s = subprocess( |
| 142 | curl, |
| 143 | argv, |
| 144 | Subprocess::PATH(os::DEV_NULL), |
| 145 | Subprocess::PIPE(), |
| 146 | Subprocess::PIPE()); |
| 147 |
nothing calls this directly
no test coverage detected