Search in sources :

Example 1 with ConnectFailed

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);
}
Also used : ProxySelector(java.net.ProxySelector) MockResponse(okhttp3.mockwebserver.MockResponse) ArrayList(java.util.ArrayList) MockWebServer(okhttp3.mockwebserver.MockWebServer) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) URI(java.net.URI) Test(org.junit.Test)

Example 2 with ConnectFailed

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) URI(java.net.URI) ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 3 with ConnectFailed

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) ProxySelector(java.net.ProxySelector) Response(org.apache.nutch.net.protocols.Response) Proxy(java.net.Proxy) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Protocol(okhttp3.Protocol) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Authenticator(okhttp3.Authenticator)

Example 4 with ConnectFailed

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());
}
Also used : ProxySelector(java.net.ProxySelector) RecordingProxySelector(okhttp3.internal.http.RecordingProxySelector) Proxy(java.net.Proxy) SocketAddress(java.net.SocketAddress) Address(okhttp3.Address) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) Test(org.junit.Test)

Example 5 with ConnectFailed

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();
}
Also used : ConnectFailed(okhttp3.CallEvent.ConnectFailed) MockResponse(mockwebserver3.MockResponse) InetSocketAddress(java.net.InetSocketAddress) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InetAddress(java.net.InetAddress) SecureConnectStart(okhttp3.CallEvent.SecureConnectStart) ConnectStart(okhttp3.CallEvent.ConnectStart) Test(org.junit.jupiter.api.Test)

Aggregations

IOException (java.io.IOException)6 InetSocketAddress (java.net.InetSocketAddress)4 ProxySelector (java.net.ProxySelector)4 SocketAddress (java.net.SocketAddress)4 URI (java.net.URI)4 InetAddress (java.net.InetAddress)3 Proxy (java.net.Proxy)3 OkHttpClient (okhttp3.OkHttpClient)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MockResponse (mockwebserver3.MockResponse)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 InterruptedIOException (java.io.InterruptedIOException)1 UnknownHostException (java.net.UnknownHostException)1 LinkedList (java.util.LinkedList)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1 SSLSession (javax.net.ssl.SSLSession)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 Address (okhttp3.Address)1