| 162 | |
| 163 | |
| 164 | static Try<string> downloadWithNet( |
| 165 | const string& sourceUri, |
| 166 | const string& destinationPath, |
| 167 | const Option<Duration>& stallTimeout) |
| 168 | { |
| 169 | // The net::download function only supports these protocols. |
| 170 | CHECK(strings::startsWith(sourceUri, "http://") || |
| 171 | strings::startsWith(sourceUri, "https://") || |
| 172 | strings::startsWith(sourceUri, "ftp://") || |
| 173 | strings::startsWith(sourceUri, "ftps://")); |
| 174 | |
| 175 | LOG(INFO) << "Downloading resource from '" << sourceUri |
| 176 | << "' to '" << destinationPath << "'"; |
| 177 | |
| 178 | Try<int> code = net::download(sourceUri, destinationPath, stallTimeout); |
| 179 | if (code.isError()) { |
| 180 | return Error("Error downloading resource: " + code.error()); |
| 181 | } else { |
| 182 | // The status code for successful HTTP requests is 200, the status code |
| 183 | // for successful FTP file transfers is 226. |
| 184 | if (strings::startsWith(sourceUri, "ftp://") || |
| 185 | strings::startsWith(sourceUri, "ftps://")) { |
| 186 | if (code.get() != 226) { |
| 187 | return Error("Error downloading resource, received FTP return code " + |
| 188 | stringify(code.get())); |
| 189 | } |
| 190 | } else { |
| 191 | if (code.get() != 200) { |
| 192 | return Error("Error downloading resource, received HTTP return code " + |
| 193 | stringify(code.get())); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return destinationPath; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | // TODO(coffler): Refactor code to eliminate redundant function. |