Skip to content

Commit

Permalink
Merge pull request #308 from pysaumont/master
Browse files Browse the repository at this point in the history
Added use of proxy as set by env variable 'http_proxy'
  • Loading branch information
cbeust committed Dec 6, 2016
2 parents 7bff267 + 366b635 commit 160d955
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion modules/wrapper/src/main/java/com/beust/kobalt/wrapper/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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.*;
Expand Down Expand Up @@ -364,13 +366,18 @@ private void downloadImpl(File file, String version) throws IOException {
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);
httpConn = (HttpURLConnection) url.openConnection();
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) {
Expand Down Expand Up @@ -425,6 +432,18 @@ private void downloadImpl(File file, String version) throws IOException {
}
}

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;
Expand Down

0 comments on commit 160d955

Please sign in to comment.