use of okhttp3.CallEvent.ConnectFailed in project okhttp by square.
the class URLConnectionTest method redirectWithProxySelector.
@Test
public void redirectWithProxySelector() throws Exception {
final List<URI> proxySelectionRequests = new ArrayList<>();
urlFactory.setClient(urlFactory.client().newBuilder().proxySelector(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
proxySelectionRequests.add(uri);
MockWebServer proxyServer = (uri.getPort() == server.getPort()) ? server : server2;
return Arrays.asList(proxyServer.toProxyAddress());
}
@Override
public void connectFailed(URI uri, SocketAddress address, IOException failure) {
throw new AssertionError();
}
}).build());
server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.url("/b").url().toString()).setBody("This page has moved!"));
assertContent("This is the 2nd server!", urlFactory.open(server.url("/a").url()));
assertEquals(Arrays.asList(server.url("/").url().toURI(), server2.url("/").url().toURI()), proxySelectionRequests);
}
use of okhttp3.CallEvent.ConnectFailed in project graylog2-server by Graylog2.
the class OkHttpClientProvider method get.
@Override
public OkHttpClient get() {
final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().retryOnConnectionFailure(true).connectTimeout(connectTimeout.getQuantity(), connectTimeout.getUnit()).writeTimeout(writeTimeout.getQuantity(), writeTimeout.getUnit()).readTimeout(readTimeout.getQuantity(), readTimeout.getUnit());
if (httpProxyUri != null) {
final ProxySelector proxySelector = new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
final String host = uri.getHost();
if (nonProxyHostsPattern != null && nonProxyHostsPattern.matches(host)) {
LOG.debug("Bypassing proxy server for {}", host);
return ImmutableList.of(Proxy.NO_PROXY);
}
try {
final InetAddress targetAddress = InetAddress.getByName(host);
if (targetAddress.isLoopbackAddress()) {
return ImmutableList.of(Proxy.NO_PROXY);
} else if (nonProxyHostsPattern != null && nonProxyHostsPattern.matches(targetAddress.getHostAddress())) {
LOG.debug("Bypassing proxy server for {}", targetAddress.getHostAddress());
return ImmutableList.of(Proxy.NO_PROXY);
}
} catch (UnknownHostException e) {
LOG.debug("Unable to resolve host name for proxy selection: ", e);
}
final Proxy proxy = new Proxy(Proxy.Type.HTTP, getProxyAddress());
return ImmutableList.of(proxy);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
LOG.warn("Unable to connect to proxy: ", ioe);
}
};
clientBuilder.proxySelector(proxySelector);
if (!isNullOrEmpty(httpProxyUri.getUserInfo())) {
final List<String> list = Splitter.on(":").limit(2).splitToList(httpProxyUri.getUserInfo());
if (list.size() == 2) {
clientBuilder.proxyAuthenticator(new ProxyAuthenticator(list.get(0), list.get(1)));
}
}
}
return clientBuilder.build();
}
use of okhttp3.CallEvent.ConnectFailed in project nutch by apache.
the class OkHttp method setConf.
@Override
public void setConf(Configuration conf) {
super.setConf(conf);
// protocols in order of preference
List<okhttp3.Protocol> protocols = new ArrayList<>();
if (useHttp2) {
protocols.add(okhttp3.Protocol.HTTP_2);
}
protocols.add(okhttp3.Protocol.HTTP_1_1);
okhttp3.OkHttpClient.Builder builder = new OkHttpClient.Builder().protocols(//
protocols).retryOnConnectionFailure(//
true).followRedirects(//
false).connectTimeout(timeout, TimeUnit.MILLISECONDS).writeTimeout(timeout, TimeUnit.MILLISECONDS).readTimeout(timeout, TimeUnit.MILLISECONDS);
if (!tlsCheckCertificate) {
builder.sslSocketFactory(trustAllSslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
}
if (!accept.isEmpty()) {
getCustomRequestHeaders().add(new String[] { "Accept", accept });
}
if (!acceptLanguage.isEmpty()) {
getCustomRequestHeaders().add(new String[] { "Accept-Language", acceptLanguage });
}
if (!acceptCharset.isEmpty()) {
getCustomRequestHeaders().add(new String[] { "Accept-Charset", acceptCharset });
}
if (useProxy) {
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;
}
@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
* (https://github.com/square/okhttp/issues/3995) 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, see NUTCH-2636");
}
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) {
builder.addNetworkInterceptor(new HTTPHeadersInterceptor());
}
// enable support for Brotli compression (Content-Encoding)
builder.addInterceptor(BrotliInterceptor.INSTANCE);
client = builder.build();
}
use of okhttp3.CallEvent.ConnectFailed in project okhttp by square.
the class RouteSelectorTest method proxySelectorReturnsNull.
@Test
public void proxySelectorReturnsNull() throws Exception {
ProxySelector nullProxySelector = new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
assertEquals(uriHost, uri.getHost());
return null;
}
@Override
public void connectFailed(URI uri, SocketAddress socketAddress, IOException e) {
throw new AssertionError();
}
};
Address address = new Address(uriHost, uriPort, dns, socketFactory, null, null, null, authenticator, null, protocols, connectionSpecs, nullProxySelector);
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
assertTrue(routeSelector.hasNext());
dns.set(uriHost, dns.allocate(1));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
dns.assertRequests(uriHost);
assertFalse(routeSelector.hasNext());
}
use of okhttp3.CallEvent.ConnectFailed in project okhttp by square.
the class EventListenerTest method failedConnect.
@Test
public void failedConnect() throws UnknownHostException {
enableTlsWithTunnel(false);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
try {
call.execute();
fail();
} catch (IOException expected) {
}
InetAddress address = client.dns().lookup(server.getHostName()).get(0);
InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());
ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
assertThat(connectStart.getCall()).isSameAs(call);
assertThat(connectStart.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectStart.getProxy()).isEqualTo(Proxy.NO_PROXY);
ConnectFailed connectFailed = listener.removeUpToEvent(ConnectFailed.class);
assertThat(connectFailed.getCall()).isSameAs(call);
assertThat(connectFailed.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectFailed.getProtocol()).isNull();
assertThat(connectFailed.getIoe()).isNotNull();
}
Aggregations