Skip to content
Permalink
Browse files
245: CLI tools do not consider git's http.proxy configuration
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Jan 24, 2020
1 parent b0a9758 commit 1b57804490807c18ac87fc6b125b60597c1f6f65
Showing 1 changed file with 45 additions and 6 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -22,25 +22,64 @@
*/
package org.openjdk.skara.proxy;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class HttpProxy {
private static void setProxyHostAndPortBasedOn(URI uri) {
var scheme = uri.getScheme();
var port = String.valueOf(uri.getPort() == -1 ? 80 : uri.getPort());
if (System.getProperty(scheme + ".proxyHost") == null) {
System.setProperty(scheme + ".proxyHost", uri.getHost());
System.setProperty(scheme + ".proxyPort", port);
}
}

public static void setup() {
try {
var pb = new ProcessBuilder("git", "config", "http.proxy");
pb.redirectOutput(ProcessBuilder.Redirect.PIPE);
pb.redirectError(ProcessBuilder.Redirect.DISCARD);
var p = pb.start();

var output = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8).trim();
var res = p.waitFor();
if (res == 0 && output != null && !output.isEmpty()) {
if (output.startsWith("https://") || output.startsWith("http://")) {
var uri = new URI(output);
setProxyHostAndPortBasedOn(uri);
} else {
for (var scheme : List.of("http", "https")) {
var uri = new URI(scheme + "://" + output);
setProxyHostAndPortBasedOn(uri);
}
}
return;
}
} catch (InterruptedException e) {
// pass
} catch (IOException e) {
// pass
} catch (URISyntaxException e) {
// pass
}

for (var key : List.of("http_proxy", "https_proxy")) {
var value = System.getenv(key);
value = value == null ? System.getenv(key.toUpperCase()) : value;
if (value != null) {
var protocol = key.split("_")[0];
try {
var uri = new URI(value);
if (System.getProperty(protocol + ".proxyHost") == null && uri.getHost() != null) {
System.setProperty(protocol + ".proxyHost", uri.getHost());
System.setProperty(protocol + ".proxyPort", String.valueOf(uri.getPort()));
if (!value.startsWith("http://") && !value.startsWith("https://")) {
var scheme = key.split("_")[0];
value = scheme + "://" + value;
}
var uri = new URI(value);
setProxyHostAndPortBasedOn(uri);
} catch (URISyntaxException e) {
// pass
}

0 comments on commit 1b57804

Please sign in to comment.