|
36 | 36 | import java.util.concurrent.Executors; |
37 | 37 | import java.util.concurrent.TimeUnit; |
38 | 38 | import java.util.function.Supplier; |
| 39 | +import java.util.logging.Level; |
| 40 | +import java.util.logging.Logger; |
39 | 41 |
|
40 | 42 | import javax.annotation.Priority; |
41 | 43 | import javax.net.ssl.HostnameVerifier; |
@@ -445,10 +447,42 @@ public RestClientBuilder proxyAddress(String proxyHost, int proxyPort) { |
445 | 447 | if (proxyPort <= 0 || proxyPort > 65535) { |
446 | 448 | throw new IllegalArgumentException("Invalid proxy port"); |
447 | 449 | } |
448 | | - property(ClientProperties.PROXY_URI, proxyHost + ":" + proxyPort); |
| 450 | + |
| 451 | + // If proxyString is something like "localhost:8765" we need to add a scheme since the connectors expect one |
| 452 | + String proxyString = createProxyString(proxyHost, proxyPort); |
| 453 | + |
| 454 | + property(ClientProperties.PROXY_URI, proxyString); |
449 | 455 | return this; |
450 | 456 | } |
451 | 457 |
|
| 458 | + static String createProxyString(String proxyHost, int proxyPort) { |
| 459 | + boolean prependScheme = false; |
| 460 | + String proxyString = proxyHost + ":" + proxyPort; |
| 461 | + |
| 462 | + if (proxyString.split(":").length == 2) { |
| 463 | + // Check if first character is a number to account for if proxyHost is given as an IP rather than a name |
| 464 | + // URI.create("127.0.0.1:8765") will lead to an IllegalArgumentException |
| 465 | + if (proxyString.matches("\\d.*")) { |
| 466 | + prependScheme = true; |
| 467 | + } else { |
| 468 | + // "localhost:8765" will set the scheme as "localhost" and the host as "null" |
| 469 | + URI proxyURI = URI.create(proxyString); |
| 470 | + if (proxyURI.getHost() == null && proxyURI.getScheme().equals(proxyHost)) { |
| 471 | + prependScheme = true; |
| 472 | + } |
| 473 | + } |
| 474 | + } |
| 475 | + |
| 476 | + if (prependScheme) { |
| 477 | + proxyString = "http://" + proxyString; |
| 478 | + Logger.getLogger(RestClientBuilderImpl.class.getName()).log(Level.FINE, |
| 479 | + "No scheme provided with proxyHost: " + proxyHost + ". Defaulting to HTTP, proxy address = " |
| 480 | + + proxyString); |
| 481 | + } |
| 482 | + |
| 483 | + return proxyString; |
| 484 | + } |
| 485 | + |
452 | 486 | @Override |
453 | 487 | public RestClientBuilder queryParamStyle(QueryParamStyle queryParamStyle) { |
454 | 488 | if (queryParamStyle != null) { |
|
0 commit comments