-
Notifications
You must be signed in to change notification settings - Fork 85
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
add proxy #473
Open
wsargent
wants to merge
1
commit into
playframework:main
Choose a base branch
from
wsargent:add-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add proxy #473
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
play-ahc-ws-standalone/src/main/java/play/libs/ws/ahc/DefaultWSProxyServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package play.libs.ws.ahc; | ||
|
||
import play.libs.ws.WSProxyServer; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class DefaultWSProxyServer implements WSProxyServer { | ||
private final String host; | ||
private final int port; | ||
private final String protocol; | ||
private final String proxyType; | ||
private final String principal; | ||
private final String password; | ||
private final String ntlmDomain; | ||
private final List<String> nonProxyHosts; | ||
private final String encoding; | ||
|
||
DefaultWSProxyServer(String host, | ||
Integer port, | ||
String protocol, | ||
String proxyType, | ||
String principal, | ||
String password, | ||
String ntlmDomain, | ||
List<String> nonProxyHosts, | ||
String encoding) { | ||
this.host = Objects.requireNonNull(host, "host cannot be null!"); | ||
this.port = Objects.requireNonNull(port, "port cannot be null"); | ||
this.protocol = protocol; | ||
this.proxyType = proxyType; | ||
this.principal = principal; | ||
this.password = password; | ||
this.ntlmDomain = ntlmDomain; | ||
this.nonProxyHosts = nonProxyHosts; | ||
this.encoding = encoding; | ||
} | ||
|
||
@Override | ||
public String getHost() { | ||
return this.host; | ||
} | ||
|
||
@Override | ||
public int getPort() { | ||
return this.port; | ||
} | ||
|
||
@Override | ||
public Optional<String> getProtocol() { | ||
return Optional.ofNullable(this.protocol); | ||
} | ||
|
||
@Override | ||
public Optional<String> getProxyType() { | ||
return Optional.ofNullable(this.proxyType); | ||
} | ||
|
||
@Override | ||
public Optional<String> getPrincipal() { | ||
return Optional.ofNullable(this.principal); | ||
} | ||
|
||
@Override | ||
public Optional<String> getPassword() { | ||
return Optional.ofNullable(this.password); | ||
} | ||
|
||
@Override | ||
public Optional<String> getNtlmDomain() { | ||
return Optional.ofNullable(this.ntlmDomain); | ||
} | ||
|
||
@Override | ||
public Optional<String> getEncoding() { | ||
return Optional.ofNullable(this.encoding); | ||
} | ||
|
||
@Override | ||
public Optional<List<String>> getNonProxyHosts() { | ||
return Optional.ofNullable(this.nonProxyHosts); | ||
} | ||
|
||
static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
static class Builder { | ||
private String host; | ||
private Integer port; | ||
private String protocol; | ||
private String proxyType; | ||
private String principal; | ||
private String password; | ||
private String ntlmDomain; | ||
private List<String> nonProxyHosts; | ||
private String encoding; | ||
|
||
public Builder withHost(String host) { | ||
this.host = host; | ||
return this; | ||
} | ||
|
||
public Builder withPort(int port) { | ||
this.port = port; | ||
return this; | ||
} | ||
|
||
public Builder withProtocol(String protocol) { | ||
this.protocol = protocol; | ||
return this; | ||
} | ||
|
||
public Builder withProxyType(String proxyType) { | ||
this.proxyType = proxyType; | ||
return this; | ||
} | ||
|
||
public Builder withPrincipal(String principal) { | ||
this.principal = principal; | ||
return this; | ||
} | ||
|
||
public Builder withPassword(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
public Builder withNtlmDomain(String ntlmDomain) { | ||
this.ntlmDomain = ntlmDomain; | ||
return this; | ||
} | ||
|
||
public Builder withNonProxyHosts(List<String> nonProxyHosts) { | ||
this.nonProxyHosts = nonProxyHosts; | ||
return this; | ||
} | ||
|
||
public Builder withEncoding(String encoding) { | ||
this.encoding = encoding; | ||
return this; | ||
} | ||
|
||
public WSProxyServer build() { | ||
return new DefaultWSProxyServer(host, | ||
port, | ||
protocol, | ||
proxyType, | ||
principal, | ||
password, | ||
ntlmDomain, | ||
nonProxyHosts, | ||
encoding); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,22 +26,17 @@ | |
import play.shaded.ahc.org.asynchttpclient.RequestBuilder; | ||
import play.shaded.ahc.org.asynchttpclient.SignatureCalculator; | ||
|
||
import play.shaded.ahc.org.asynchttpclient.proxy.ProxyServer; | ||
import play.shaded.ahc.org.asynchttpclient.proxy.ProxyType; | ||
import play.shaded.ahc.org.asynchttpclient.util.HttpUtils; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.Proxy; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.*; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
@@ -67,6 +62,8 @@ public class StandaloneAhcWSRequest implements StandaloneWSRequest { | |
|
||
private WSAuthInfo auth; | ||
private WSSignatureCalculator calculator; | ||
private WSProxyServer proxyServer; | ||
|
||
private final StandaloneAhcWSClient client; | ||
|
||
private final Materializer materializer; | ||
|
@@ -243,6 +240,15 @@ public StandaloneAhcWSRequest setContentType(String contentType) { | |
return addHeader(CONTENT_TYPE.toString(), contentType); | ||
} | ||
|
||
@Override | ||
public StandaloneWSRequest setProxyServer(WSProxyServer proxyServer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set proxy server |
||
if (proxyServer == null) { | ||
throw new IllegalArgumentException("proxyServer must not be null."); | ||
} | ||
this.proxyServer = proxyServer; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Optional<String> getContentType() { | ||
return getHeader(CONTENT_TYPE.toString()); | ||
|
@@ -324,6 +330,11 @@ public Optional<WSAuthInfo> getAuth() { | |
return Optional.ofNullable(this.auth); | ||
} | ||
|
||
@Override | ||
public Optional<WSProxyServer> getProxyServer() { | ||
return Optional.ofNullable(this.proxyServer); | ||
} | ||
|
||
@Override | ||
public Optional<WSSignatureCalculator> getCalculator() { | ||
return Optional.ofNullable(this.calculator); | ||
|
@@ -527,9 +538,52 @@ Request buildRequest() { | |
builder.addCookie(ahcCookie); | ||
}); | ||
|
||
getProxyServer().ifPresent(ps -> builder.setProxyServer(createProxy(ps))); | ||
|
||
return builder.build(); | ||
} | ||
|
||
private ProxyServer createProxy(WSProxyServer proxyServer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create java proxy |
||
String host = proxyServer.getHost(); | ||
int port = proxyServer.getPort(); | ||
ProxyServer.Builder proxyBuilder = new ProxyServer.Builder(host, port); | ||
|
||
proxyServer.getPrincipal().ifPresent(principal -> { | ||
Realm.Builder realmBuilder = new Realm.Builder(principal, proxyServer.getPassword().orElse(null)); | ||
String protocol = proxyServer.getProtocol().orElse("http").toLowerCase(Locale.ENGLISH); | ||
switch (protocol) { | ||
case "http": | ||
case "https": | ||
realmBuilder.setScheme(Realm.AuthScheme.BASIC); | ||
case "kerberos": | ||
realmBuilder.setScheme(Realm.AuthScheme.KERBEROS); | ||
case "ntlm": | ||
realmBuilder.setScheme(Realm.AuthScheme.NTLM); | ||
case "spnego": | ||
realmBuilder.setScheme(Realm.AuthScheme.SPNEGO); | ||
default: | ||
// Default to BASIC rather than throwing an error. | ||
realmBuilder.setScheme(Realm.AuthScheme.BASIC); | ||
} | ||
proxyServer.getEncoding().ifPresent(enc -> realmBuilder.setCharset(Charset.forName(enc))); | ||
proxyServer.getNtlmDomain().ifPresent(realmBuilder::setNtlmDomain); | ||
proxyBuilder.setRealm(realmBuilder); | ||
}); | ||
|
||
String proxyType = proxyServer.getProxyType().orElse("http").toLowerCase(Locale.ENGLISH); | ||
switch (proxyType) { | ||
case "http": | ||
proxyBuilder.setProxyType(ProxyType.HTTP); | ||
case "socksv4": | ||
proxyBuilder.setProxyType(ProxyType.SOCKS_V4); | ||
case "socksv5": | ||
proxyBuilder.setProxyType(ProxyType.SOCKS_V5); | ||
} | ||
|
||
proxyServer.getNonProxyHosts().ifPresent(proxyBuilder::setNonProxyHosts); | ||
return proxyBuilder.build(); | ||
} | ||
|
||
private static void addValueTo(Map<String, List<String>> map, String name, String value) { | ||
final Optional<String> existing = map.keySet().stream().filter(s -> s.equalsIgnoreCase(name)).findAny(); | ||
if (existing.isPresent()) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a proxy server implementation