aboutsummaryrefslogtreecommitdiff
path: root/okhttp-urlconnection
diff options
context:
space:
mode:
authorTobias Thierer <tobiast@google.com>2016-06-24 19:04:17 +0100
committerTobias Thierer <tobiast@google.com>2016-06-29 16:23:09 +0100
commit6c251e20f00c7574b217bd4351ac81666f574380 (patch)
tree2d66a76721f4c8170b990742922675f32ad38122 /okhttp-urlconnection
parent68e16131f12f0174c4c9e5785f6e63297cd02adf (diff)
downloadokhttp-6c251e20f00c7574b217bd4351ac81666f574380.tar.gz
Update OkHttp to 2.7.5 and advance okio by one commit.
This brings OkHttp and okio exactly in line with upstream commits with no local changes. Corresponding upstream commits: okhttp:6e236ce3b80f21369dc544f0e1053ff71be8689b (= parent-2.7.5) okio: 02481cc0cc84bc92e3eab6d5212a226496f56a7e The okio commit differs from the one in the previous pull from Sep 2015 (AOSP commit 71b9f47b26fb57ac3e436a19519c6e3ec70e86eb) only by a single upstream commit, the switch to 8 KiB segments. That commit was previously cherry-picked in AOSP. This CL will temporarily revert the AOSP changes to okio, but those AOSP changes to okio will be reapplied in the subsequent CL. Compilation and tests do not pass after this CL, they will only pass at the end of the chain of 11 CLs going in at the same time. 9 of these 11 CLs are in external/okhttp, the others affect libcore and frameworks/base. Details of behavioural changes introduced by this upgrade are at: https://docs.google.com/document/d/19PF3Exd_q32gAGCiRFWRf0Pq_xrIWs-cRViHkFTxJg8/edit This CL includes files that are not used in Android, such as - top level dot files (.travis.yml etc.) - subdirectories okurl, okhttp-apache, samples, which aren't used - tests in okhttp-hpacktests, okhttp-ws-tests that aren't run or test functionality that we aren't used Test: I've run the following tests *at the end* of the chain of commits, in cts-tradefed: 1.) run cts -p android.core.tests.libcore.package.harmony_java_net 2.) run cts -c libcore.java.net.URLConnectionTest 3.) run cts -p android.core.tests.libcore.package.okhttp 4.) run cts -p android.core.tests.libcore.package.libcore 1.-3.) all passed 4.) had 24 unrelated failures per b/29496407 and b/29744850 Change-Id: Id798d6cf49fa4a7a4ab8ae3b699a38104bf42db3
Diffstat (limited to 'okhttp-urlconnection')
-rw-r--r--okhttp-urlconnection/pom.xml2
-rw-r--r--okhttp-urlconnection/src/main/java/com/squareup/okhttp/OkUrlFactory.java10
-rw-r--r--okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/URLFilter.java32
-rw-r--r--okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpURLConnectionImpl.java58
-rw-r--r--okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpsURLConnectionImpl.java5
-rw-r--r--okhttp-urlconnection/src/test/java/com/squareup/okhttp/OkUrlFactoryTest.java49
-rw-r--r--okhttp-urlconnection/src/test/java/com/squareup/okhttp/UrlConnectionCacheTest.java7
7 files changed, 55 insertions, 108 deletions
diff --git a/okhttp-urlconnection/pom.xml b/okhttp-urlconnection/pom.xml
index be60560..c11c67f 100644
--- a/okhttp-urlconnection/pom.xml
+++ b/okhttp-urlconnection/pom.xml
@@ -6,7 +6,7 @@
<parent>
<groupId>com.squareup.okhttp</groupId>
<artifactId>parent</artifactId>
- <version>2.6.0-SNAPSHOT</version>
+ <version>2.7.5</version>
</parent>
<artifactId>okhttp-urlconnection</artifactId>
diff --git a/okhttp-urlconnection/src/main/java/com/squareup/okhttp/OkUrlFactory.java b/okhttp-urlconnection/src/main/java/com/squareup/okhttp/OkUrlFactory.java
index 30d8b49..4b34559 100644
--- a/okhttp-urlconnection/src/main/java/com/squareup/okhttp/OkUrlFactory.java
+++ b/okhttp-urlconnection/src/main/java/com/squareup/okhttp/OkUrlFactory.java
@@ -15,7 +15,6 @@
*/
package com.squareup.okhttp;
-import com.squareup.okhttp.internal.URLFilter;
import com.squareup.okhttp.internal.huc.HttpURLConnectionImpl;
import com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl;
@@ -28,7 +27,6 @@ import java.net.URLStreamHandlerFactory;
public final class OkUrlFactory implements URLStreamHandlerFactory, Cloneable {
private final OkHttpClient client;
- private URLFilter urlFilter;
public OkUrlFactory(OkHttpClient client) {
this.client = client;
@@ -38,10 +36,6 @@ public final class OkUrlFactory implements URLStreamHandlerFactory, Cloneable {
return client;
}
- void setUrlFilter(URLFilter filter) {
- urlFilter = filter;
- }
-
/**
* Returns a copy of this stream handler factory that includes a shallow copy
* of the internal {@linkplain OkHttpClient HTTP client}.
@@ -59,8 +53,8 @@ public final class OkUrlFactory implements URLStreamHandlerFactory, Cloneable {
OkHttpClient copy = client.copyWithDefaults();
copy.setProxy(proxy);
- if (protocol.equals("http")) return new HttpURLConnectionImpl(url, copy, urlFilter);
- if (protocol.equals("https")) return new HttpsURLConnectionImpl(url, copy, urlFilter);
+ if (protocol.equals("http")) return new HttpURLConnectionImpl(url, copy);
+ if (protocol.equals("https")) return new HttpsURLConnectionImpl(url, copy);
throw new IllegalArgumentException("Unexpected protocol: " + protocol);
}
diff --git a/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/URLFilter.java b/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/URLFilter.java
deleted file mode 100644
index 52745e9..0000000
--- a/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/URLFilter.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (C) 2016 Square, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.squareup.okhttp.internal;
-import java.io.IOException;
-import java.net.URL;
-
-/**
- * Request filter based on the request's URL.
- *
- * @deprecated use {@link okhttp3.Interceptor} for non-HttpURLConnection filtering.
- */
-public interface URLFilter {
- /**
- * Check whether request to the provided URL is permitted to be issued.
- *
- * @throws IOException if the request to the provided URL is not permitted.
- */
- void checkURLPermitted(URL url) throws IOException;
-}
diff --git a/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpURLConnectionImpl.java b/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpURLConnectionImpl.java
index 454f6c3..b7b8c72 100644
--- a/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpURLConnectionImpl.java
+++ b/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpURLConnectionImpl.java
@@ -29,7 +29,6 @@ import com.squareup.okhttp.Response;
import com.squareup.okhttp.Route;
import com.squareup.okhttp.internal.Internal;
import com.squareup.okhttp.internal.Platform;
-import com.squareup.okhttp.internal.URLFilter;
import com.squareup.okhttp.internal.Util;
import com.squareup.okhttp.internal.Version;
import com.squareup.okhttp.internal.http.HttpDate;
@@ -40,6 +39,7 @@ import com.squareup.okhttp.internal.http.RequestException;
import com.squareup.okhttp.internal.http.RetryableSink;
import com.squareup.okhttp.internal.http.RouteException;
import com.squareup.okhttp.internal.http.StatusLine;
+import com.squareup.okhttp.internal.http.StreamAllocation;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -73,7 +73,7 @@ import okio.Sink;
*
* <h3>What does 'connected' mean?</h3>
* This class inherits a {@code connected} field from the superclass. That field
- * is <strong>not</strong> used to indicate not whether this URLConnection is
+ * is <strong>not</strong> used to indicate whether this URLConnection is
* currently connected. Instead, it indicates whether a connection has ever been
* attempted. Once a connection has been attempted, certain properties (request
* header fields, request method, etc.) are immutable.
@@ -107,18 +107,11 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
*/
Handshake handshake;
- private URLFilter urlFilter;
-
public HttpURLConnectionImpl(URL url, OkHttpClient client) {
super(url);
this.client = client;
}
- public HttpURLConnectionImpl(URL url, OkHttpClient client, URLFilter urlFilter) {
- this(url, client);
- this.urlFilter = urlFilter;
- }
-
@Override public final void connect() throws IOException {
initHttpEngine();
boolean success;
@@ -131,7 +124,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
// Calling disconnect() before a connection exists should have no effect.
if (httpEngine == null) return;
- httpEngine.disconnect();
+ httpEngine.cancel();
// This doesn't close the stream because doing so would require all stream
// access to be synchronized. It's expected that the thread using the
@@ -161,9 +154,9 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
if (responseHeaders == null) {
Response response = getResponse().getResponse();
Headers headers = response.headers();
-
responseHeaders = headers.newBuilder()
- .add(Platform.get().getPrefix() + "-Response-Source", responseSourceHeader(response))
+ .add(OkHeaders.SELECTED_PROTOCOL, response.protocol().toString())
+ .add(OkHeaders.RESPONSE_SOURCE, responseSourceHeader(response))
.build();
}
return responseHeaders;
@@ -335,8 +328,9 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
}
}
- private HttpEngine newHttpEngine(String method, Connection connection, RetryableSink requestBody,
- Response priorResponse) throws MalformedURLException, UnknownHostException {
+ private HttpEngine newHttpEngine(String method, StreamAllocation streamAllocation,
+ RetryableSink requestBody, Response priorResponse)
+ throws MalformedURLException, UnknownHostException {
// OkHttp's Call API requires a placeholder body; the real body will be streamed separately.
RequestBody placeholderBody = HttpMethod.requiresRequestBody(method)
? EMPTY_REQUEST_BODY
@@ -380,7 +374,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
engineClient = client.clone().setCache(null);
}
- return new HttpEngine(engineClient, request, bufferRequestBody, true, false, connection, null,
+ return new HttpEngine(engineClient, request, bufferRequestBody, true, false, streamAllocation,
requestBody, priorResponse);
}
@@ -410,7 +404,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
Request followUp = httpEngine.followUpRequest();
if (followUp == null) {
- httpEngine.releaseConnection();
+ httpEngine.releaseStreamAllocation();
return httpEngine;
}
@@ -434,12 +428,13 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
throw new HttpRetryException("Cannot retry streamed HTTP body", responseCode);
}
+ StreamAllocation streamAllocation = httpEngine.close();
if (!httpEngine.sameConnection(followUp.httpUrl())) {
- httpEngine.releaseConnection();
+ streamAllocation.release();
+ streamAllocation = null;
}
- Connection connection = httpEngine.close();
- httpEngine = newHttpEngine(followUp.method(), connection, (RetryableSink) requestBody,
+ httpEngine = newHttpEngine(followUp.method(), streamAllocation, (RetryableSink) requestBody,
response);
}
}
@@ -450,18 +445,21 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
* retried. Throws an exception if the request failed permanently.
*/
private boolean execute(boolean readResponse) throws IOException {
- if (urlFilter != null) {
- urlFilter.checkURLPermitted(httpEngine.getRequest().url());
- }
+ boolean releaseConnection = true;
try {
httpEngine.sendRequest();
- route = httpEngine.getRoute();
- handshake = httpEngine.getConnection() != null
- ? httpEngine.getConnection().getHandshake()
- : null;
+ Connection connection = httpEngine.getConnection();
+ if (connection != null) {
+ route = connection.getRoute();
+ handshake = connection.getHandshake();
+ } else {
+ route = null;
+ handshake = null;
+ }
if (readResponse) {
httpEngine.readResponse();
}
+ releaseConnection = false;
return true;
} catch (RequestException e) {
@@ -473,6 +471,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
// The attempt to connect via a route failed. The request will not have been sent.
HttpEngine retryEngine = httpEngine.recover(e);
if (retryEngine != null) {
+ releaseConnection = false;
httpEngine = retryEngine;
return false;
}
@@ -485,6 +484,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
// An attempt to communicate with a server failed. The request may have been sent.
HttpEngine retryEngine = httpEngine.recover(e);
if (retryEngine != null) {
+ releaseConnection = false;
httpEngine = retryEngine;
return false;
}
@@ -492,6 +492,12 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
// Give up; recovery is not possible.
httpEngineFailure = e;
throw e;
+ } finally {
+ // We're throwing an unchecked exception. Release any resources.
+ if (releaseConnection) {
+ StreamAllocation streamAllocation = httpEngine.close();
+ streamAllocation.release();
+ }
}
}
diff --git a/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpsURLConnectionImpl.java b/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpsURLConnectionImpl.java
index 140b4d2..2aba087 100644
--- a/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpsURLConnectionImpl.java
+++ b/okhttp-urlconnection/src/main/java/com/squareup/okhttp/internal/huc/HttpsURLConnectionImpl.java
@@ -18,7 +18,6 @@ package com.squareup.okhttp.internal.huc;
import com.squareup.okhttp.Handshake;
import com.squareup.okhttp.OkHttpClient;
-import com.squareup.okhttp.internal.URLFilter;
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
@@ -30,10 +29,6 @@ public final class HttpsURLConnectionImpl extends DelegatingHttpsURLConnection {
this(new HttpURLConnectionImpl(url, client));
}
- public HttpsURLConnectionImpl(URL url, OkHttpClient client, URLFilter filter) {
- this(new HttpURLConnectionImpl(url, client, filter));
- }
-
public HttpsURLConnectionImpl(HttpURLConnectionImpl delegate) {
super(delegate);
this.delegate = delegate;
diff --git a/okhttp-urlconnection/src/test/java/com/squareup/okhttp/OkUrlFactoryTest.java b/okhttp-urlconnection/src/test/java/com/squareup/okhttp/OkUrlFactoryTest.java
index 983dd57..0b16929 100644
--- a/okhttp-urlconnection/src/test/java/com/squareup/okhttp/OkUrlFactoryTest.java
+++ b/okhttp-urlconnection/src/test/java/com/squareup/okhttp/OkUrlFactoryTest.java
@@ -1,21 +1,20 @@
package com.squareup.okhttp;
-import com.squareup.okhttp.internal.Platform;
-import com.squareup.okhttp.internal.URLFilter;
-import com.squareup.okhttp.internal.io.FileSystem;
+import com.squareup.okhttp.internal.http.OkHeaders;
import com.squareup.okhttp.internal.io.InMemoryFileSystem;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
-import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
+import okio.BufferedSource;
+import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -28,16 +27,22 @@ import static org.junit.Assert.fail;
public class OkUrlFactoryTest {
@Rule public MockWebServer server = new MockWebServer();
+ @Rule public InMemoryFileSystem fileSystem = new InMemoryFileSystem();
- private FileSystem fileSystem = new InMemoryFileSystem();
private OkUrlFactory factory;
+ private Cache cache;
@Before public void setUp() throws IOException {
OkHttpClient client = new OkHttpClient();
- client.setCache(new Cache(new File("/cache/"), 10 * 1024 * 1024, fileSystem));
+ cache = new Cache(new File("/cache/"), 10 * 1024 * 1024, fileSystem);
+ client.setCache(cache);
factory = new OkUrlFactory(client);
}
+ @After public void tearDown() throws IOException {
+ cache.delete();
+ }
+
/**
* Response code 407 should only come from proxy servers. Android's client
* throws if it is sent by an origin server.
@@ -66,6 +71,7 @@ public class OkUrlFactoryTest {
HttpURLConnection connection = factory.open(server.getUrl("/"));
assertResponseHeader(connection, "NETWORK 404");
+ connection.getErrorStream().close();
}
@Test public void conditionalCacheHitResponseSourceHeaders() throws Exception {
@@ -141,38 +147,15 @@ public class OkUrlFactoryTest {
assertResponseCode(connection, 302);
}
- @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");
- }
-
private void assertResponseBody(HttpURLConnection connection, String expected) throws Exception {
- String actual = buffer(source(connection.getInputStream())).readString(US_ASCII);
+ BufferedSource source = buffer(source(connection.getInputStream()));
+ String actual = source.readString(US_ASCII);
+ source.close();
assertEquals(expected, actual);
}
private void assertResponseHeader(HttpURLConnection connection, String expected) {
- final String headerFieldPrefix = Platform.get().getPrefix();
- assertEquals(expected, connection.getHeaderField(headerFieldPrefix + "-Response-Source"));
+ assertEquals(expected, connection.getHeaderField(OkHeaders.RESPONSE_SOURCE));
}
private void assertResponseCode(HttpURLConnection connection, int expected) throws IOException {
diff --git a/okhttp-urlconnection/src/test/java/com/squareup/okhttp/UrlConnectionCacheTest.java b/okhttp-urlconnection/src/test/java/com/squareup/okhttp/UrlConnectionCacheTest.java
index 0af815b..66bf7c2 100644
--- a/okhttp-urlconnection/src/test/java/com/squareup/okhttp/UrlConnectionCacheTest.java
+++ b/okhttp-urlconnection/src/test/java/com/squareup/okhttp/UrlConnectionCacheTest.java
@@ -19,7 +19,6 @@ package com.squareup.okhttp;
import com.squareup.okhttp.internal.Internal;
import com.squareup.okhttp.internal.SslContextBuilder;
import com.squareup.okhttp.internal.Util;
-import com.squareup.okhttp.internal.io.FileSystem;
import com.squareup.okhttp.internal.io.InMemoryFileSystem;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
@@ -81,9 +80,9 @@ public final class UrlConnectionCacheTest {
@Rule public MockWebServer server = new MockWebServer();
@Rule public MockWebServer server2 = new MockWebServer();
+ @Rule public InMemoryFileSystem fileSystem = new InMemoryFileSystem();
private final SSLContext sslContext = SslContextBuilder.localhost();
- private final FileSystem fileSystem = new InMemoryFileSystem();
private final OkUrlFactory client = new OkUrlFactory(new OkHttpClient());
private Cache cache;
private final CookieManager cookieManager = new CookieManager();
@@ -98,6 +97,7 @@ public final class UrlConnectionCacheTest {
@After public void tearDown() throws Exception {
ResponseCache.setDefault(null);
CookieHandler.setDefault(null);
+ cache.delete();
}
@Test public void responseCacheAccessWithOkHttpMember() throws IOException {
@@ -855,7 +855,7 @@ public final class UrlConnectionCacheTest {
assertEquals("A", readAscii(client.open(server.getUrl("/"))));
assertEquals("A", readAscii(client.open(server.getUrl("/"))));
- assertEquals(1, client.client().getConnectionPool().getConnectionCount());
+ assertEquals(1, client.client().getConnectionPool().getIdleConnectionCount());
}
@Test public void expiresDateBeforeModifiedDate() throws Exception {
@@ -1586,6 +1586,7 @@ public final class UrlConnectionCacheTest {
HttpURLConnection connection = client.open(server.getUrl("/"));
assertEquals("A", connection.getHeaderField(""));
+ assertEquals("body", readAscii(connection));
}
/**