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);
}
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);
}
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) {
}
}
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");
}
Aggregations