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

Merge pull request #308 from pysaumont/master

Added use of proxy as set by env variable 'http_proxy'
This commit is contained in:
Cedric Beust 2016-12-06 14:41:10 -08:00 committed by GitHub
commit 160d955a32

View file

@ -2,6 +2,8 @@ package com.beust.kobalt.wrapper;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.*; import java.nio.file.*;
@ -364,13 +366,18 @@ public class Main {
log(2, "Downloading " + fileUrl); log(2, "Downloading " + fileUrl);
boolean done = false; boolean done = false;
Proxy proxy = getProxy();
HttpURLConnection httpConn = null; HttpURLConnection httpConn = null;
try { try {
int responseCode = 0; int responseCode = 0;
URL url = null; URL url = null;
while (!done) { while (!done) {
url = new URL(fileUrl); url = new URL(fileUrl);
httpConn = (HttpURLConnection) url.openConnection(); if (proxy != null) {
httpConn = (HttpURLConnection) url.openConnection(proxy);
} else {
httpConn = (HttpURLConnection) url.openConnection();
}
responseCode = httpConn.getResponseCode(); responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
responseCode == HttpURLConnection.HTTP_MOVED_PERM) { 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, private void copyToStreamWithProgress(InputStream inputStream, OutputStream outputStream, long contentLength,
String url) throws IOException { String url) throws IOException {
int bytesRead; int bytesRead;