Search in sources :

Example 1 with URLFilter

use of okhttp3.internal.URLFilter in project okhttp by square.

the class OkUrlFactory method open.

HttpURLConnection open(URL url, Proxy proxy) {
    String protocol = url.getProtocol();
    OkHttpClient copy = client.newBuilder().proxy(proxy).build();
    if (protocol.equals("http"))
        return new OkHttpURLConnection(url, copy, urlFilter);
    if (protocol.equals("https"))
        return new OkHttpsURLConnection(url, copy, urlFilter);
    throw new IllegalArgumentException("Unexpected protocol: " + protocol);
}
Also used : OkHttpsURLConnection(okhttp3.internal.huc.OkHttpsURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection)

Example 2 with URLFilter

use of okhttp3.internal.URLFilter in project okhttp by square.

the class OkHttpURLConnection method buildCall.

private Call buildCall() throws IOException {
    if (call != null) {
        return call;
    }
    connected = true;
    if (doOutput) {
        if (method.equals("GET")) {
            // they are requesting a stream to write to. This implies a POST method
            method = "POST";
        } else if (!HttpMethod.permitsRequestBody(method)) {
            throw new ProtocolException(method + " does not support writing");
        }
    }
    if (requestHeaders.get("User-Agent") == null) {
        requestHeaders.add("User-Agent", defaultUserAgent());
    }
    OutputStreamRequestBody requestBody = null;
    if (HttpMethod.permitsRequestBody(method)) {
        // Add a content type for the request body, if one isn't already present.
        String contentType = requestHeaders.get("Content-Type");
        if (contentType == null) {
            contentType = "application/x-www-form-urlencoded";
            requestHeaders.add("Content-Type", contentType);
        }
        boolean stream = fixedContentLength != -1L || chunkLength > 0;
        long contentLength = -1L;
        String contentLengthString = requestHeaders.get("Content-Length");
        if (fixedContentLength != -1L) {
            contentLength = fixedContentLength;
        } else if (contentLengthString != null) {
            contentLength = Long.parseLong(contentLengthString);
        }
        requestBody = stream ? new StreamedRequestBody(contentLength) : new BufferedRequestBody(contentLength);
        requestBody.timeout().timeout(client.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
    }
    Request request = new Request.Builder().url(Internal.instance.getHttpUrlChecked(getURL().toString())).headers(requestHeaders.build()).method(method, requestBody).build();
    if (urlFilter != null) {
        urlFilter.checkURLPermitted(request.url().url());
    }
    OkHttpClient.Builder clientBuilder = client.newBuilder();
    clientBuilder.interceptors().clear();
    clientBuilder.interceptors().add(UnexpectedException.INTERCEPTOR);
    clientBuilder.networkInterceptors().clear();
    clientBuilder.networkInterceptors().add(networkInterceptor);
    // Use a separate dispatcher so that limits aren't impacted. But use the same executor service!
    clientBuilder.dispatcher(new Dispatcher(client.dispatcher().executorService()));
    // If we're currently not using caches, make sure the engine's client doesn't have one.
    if (!getUseCaches()) {
        clientBuilder.cache(null);
    }
    return call = clientBuilder.build().newCall(request);
}
Also used : ProtocolException(java.net.ProtocolException) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Dispatcher(okhttp3.Dispatcher)

Example 3 with URLFilter

use of okhttp3.internal.URLFilter in project okhttp by square.

the class OkUrlFactoryTest method testURLFilterRedirect.

@Test
public void testURLFilterRedirect() throws Exception {
    MockWebServer cleartextServer = new MockWebServer();
    cleartextServer.enqueue(new MockResponse().setBody("Blocked!"));
    final URL blockedURL = cleartextServer.url("/").url();
    SslClient contextBuilder = SslClient.localhost();
    server.useHttps(contextBuilder.socketFactory, false);
    factory.setClient(factory.client().newBuilder().sslSocketFactory(contextBuilder.socketFactory, contextBuilder.trustManager).followSslRedirects(true).build());
    factory.setUrlFilter(new URLFilter() {

        @Override
        public void checkURLPermitted(URL url) throws IOException {
            if (blockedURL.equals(url)) {
                throw new IOException("Blocked");
            }
        }
    });
    server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: " + blockedURL).setBody("This page has moved"));
    URL destination = server.url("/").url();
    try {
        HttpsURLConnection httpsConnection = (HttpsURLConnection) factory.open(destination);
        httpsConnection.getInputStream();
        fail("Connection was successful");
    } catch (IOException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) URLFilter(okhttp3.internal.URLFilter) MockWebServer(okhttp3.mockwebserver.MockWebServer) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 4 with URLFilter

use of okhttp3.internal.URLFilter in project okhttp by square.

the class OkUrlFactoryTest method testURLFilter.

@Test
public void testURLFilter() throws Exception {
    server.enqueue(new MockResponse().setBody("B"));
    final URL blockedURL = server.url("/a").url();
    factory.setUrlFilter(new URLFilter() {

        @Override
        public void checkURLPermitted(URL url) throws IOException {
            if (blockedURL.equals(url)) {
                throw new IOException("Blocked");
            }
        }
    });
    try {
        HttpURLConnection connection = factory.open(server.url("/a").url());
        connection.getInputStream();
        fail("Connection was successful");
    } catch (IOException e) {
        assertEquals("Blocked", e.getMessage());
    }
    HttpURLConnection connection = factory.open(server.url("/b").url());
    assertResponseBody(connection, "B");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URLFilter(okhttp3.internal.URLFilter) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 URL (java.net.URL)2 URLFilter (okhttp3.internal.URLFilter)2 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Test (org.junit.Test)2 HttpURLConnection (java.net.HttpURLConnection)1 ProtocolException (java.net.ProtocolException)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 Dispatcher (okhttp3.Dispatcher)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 OkHttpsURLConnection (okhttp3.internal.huc.OkHttpsURLConnection)1 SslClient (okhttp3.internal.tls.SslClient)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1