Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2632 protocol-okhttp proxy authentication

Steven Woodard5f8b72b712c9
diff --git a/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttp.java b/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttp.java
index 9206f81fc..656bd19cd 100755
--- a/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttp.java
+++ b/src/plugin/protocol-okhttp/src/java/org/apache/nutch/protocol/okhttp/OkHttp.java
@@ -43,6 +43,7 @@ import org.apache.nutch.protocol.ProtocolException;
import org.apache.nutch.protocol.http.api.HttpBase;
import org.apache.nutch.util.NutchConfiguration;
+import okhttp3.Authenticator;
import okhttp3.Connection;
import okhttp3.Headers;
import okhttp3.Interceptor;
@@ -95,33 +96,56 @@ public class OkHttp extends HttpBase {
}
if (useProxy) {
- ProxySelector selector = new ProxySelector() {
- @SuppressWarnings("serial")
- private final List<Proxy> noProxy = new ArrayList<Proxy>() {
- {
- add(Proxy.NO_PROXY);
+ Proxy proxy = new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort));
+ String proxyUsername = conf.get("http.proxy.username");
+ if (proxyUsername == null) {
+ ProxySelector selector = new ProxySelector() {
+ @SuppressWarnings("serial")
+ private final List<Proxy> noProxyList = new ArrayList<Proxy>() {
+ {
+ add(Proxy.NO_PROXY);
+ }
+ };
+ @SuppressWarnings("serial")
+ private final List<Proxy> proxyList = new ArrayList<Proxy>() {
+ {
+ add(proxy);
+ }
+ };
+ @Override
+ public List<Proxy> select(URI uri) {
+ if (useProxy(uri)) {
+ return proxyList;
+ }
+ return noProxyList;
}
- };
- @SuppressWarnings("serial")
- private final List<Proxy> proxy = new ArrayList<Proxy>() {
- {
- add(new Proxy(proxyType,
- new InetSocketAddress(proxyHost, proxyPort)));
+ @Override
+ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+ LOG.error("Connection to proxy failed for {}: {}", uri, ioe);
}
};
- @Override
- public List<Proxy> select(URI uri) {
- if (useProxy(uri)) {
- return proxy;
- }
- return noProxy;
- }
- @Override
- public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
- LOG.error("Connection to proxy failed for {}: {}", uri, ioe);
+ builder.proxySelector(selector);
+ } else {
+ // NOTE: the proxy exceptions list does NOT work with proxy username/password
+ // because an okhttp3 bug when using the ProxySelector class with proxy auth.
+ // If a proxy username is present, the configured proxy will be used for ALL
+ // requests.
+ if (proxyException.size() > 0) {
+ LOG.warn("protocol-okhttp does not respect 'http.proxy.exception.list' setting when " +
+ "'http.proxy.username' is set. This is a limitation of the current okhttp3 implementation.");
}
- };
- builder.proxySelector(selector);
+ builder.proxy(proxy);
+ String proxyPassword = conf.get("http.proxy.password");
+ Authenticator proxyAuthenticator = new Authenticator() {
+ @Override public Request authenticate(okhttp3.Route route, okhttp3.Response response) throws IOException {
+ String credential = okhttp3.Credentials.basic(proxyUsername, proxyPassword);
+ return response.request().newBuilder()
+ .header("Proxy-Authorization", credential)
+ .build();
+ }
+ };
+ builder.proxyAuthenticator(proxyAuthenticator);
+ }
}
if (storeIPAddress || storeHttpHeaders || storeHttpRequest) {