1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00

Added use of proxy as set by env variable 'http_proxy'

This commit is contained in:
pysaumont 2016-12-06 22:41:05 +01:00
parent 7bff267b9f
commit 366b635c11

View file

@ -2,6 +2,8 @@ package com.beust.kobalt.wrapper;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.*;
@ -364,13 +366,18 @@ public class Main {
log(2, "Downloading " + fileUrl);
boolean done = false;
Proxy proxy = getProxy();
HttpURLConnection httpConn = null;
try {
int responseCode = 0;
URL url = null;
while (!done) {
url = new URL(fileUrl);
if (proxy != null) {
httpConn = (HttpURLConnection) url.openConnection(proxy);
} else {
httpConn = (HttpURLConnection) url.openConnection();
}
responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
responseCode == HttpURLConnection.HTTP_MOVED_PERM) {
@ -425,6 +432,18 @@ public class Main {
}
}
private Proxy getProxy() {
String http_proxy = System.getenv("http_proxy");
try {
String host = http_proxy == null || http_proxy.indexOf(':') == -1 ? "localhost" : http_proxy.substring(0, http_proxy.indexOf(':'));
int port = http_proxy == null || http_proxy.indexOf(':') == -1 ? 0 : Integer.parseInt(http_proxy.substring(http_proxy.indexOf(':') + 1));
return http_proxy == null ? null : new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
} catch (NumberFormatException e) {
log(1, "Warning: invalid proxy port: " + http_proxy);;
return null;
}
}
private void copyToStreamWithProgress(InputStream inputStream, OutputStream outputStream, long contentLength,
String url) throws IOException {
int bytesRead;