Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

245: CLI tools do not consider git's http.proxy configuration #379

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
}