From bfac340626520863aeaeb079dee1dcf3769de2f6 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 10 Oct 2014 21:19:43 -0400 Subject: [PATCH] Forbid setProtocols with HTTP_1_0. Closes https://github.com/square/okhttp/issues/986 --- .../src/test/java/com/squareup/okhttp/CallTest.java | 3 +-- .../java/com/squareup/okhttp/OkHttpClientTest.java | 10 ++++++++++ .../main/java/com/squareup/okhttp/OkHttpClient.java | 10 +++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java index cefb681be..0f0852f4d 100644 --- a/okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java +++ b/okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java @@ -1393,8 +1393,7 @@ public final class CallTest { .matches("okhttp/\\d\\.\\d\\.\\d(-SNAPSHOT)?")); } - @Test - public void setFollowRedirectsFalse() throws Exception { + @Test public void setFollowRedirectsFalse() throws Exception { server.enqueue(new MockResponse() .setResponseCode(302) .addHeader("Location: /b") diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/OkHttpClientTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/OkHttpClientTest.java index 7ecd2a1ff..41cdcf43e 100644 --- a/okhttp-tests/src/test/java/com/squareup/okhttp/OkHttpClientTest.java +++ b/okhttp-tests/src/test/java/com/squareup/okhttp/OkHttpClientTest.java @@ -35,6 +35,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public final class OkHttpClientTest { private static final ProxySelector DEFAULT_PROXY_SELECTOR = ProxySelector.getDefault(); @@ -164,4 +165,13 @@ public final class OkHttpClientTest { Response actualResponse = mockClient.newCall(request).execute(); assertSame(response, actualResponse); } + + @Test public void setProtocolsRejectsHttp10() throws Exception { + OkHttpClient client = new OkHttpClient(); + try { + client.setProtocols(Arrays.asList(Protocol.HTTP_1_0, Protocol.HTTP_1_1)); + fail(); + } catch (IllegalArgumentException expected) { + } + } } diff --git a/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java b/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java index 69029dfa4..d99a961cc 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java +++ b/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java @@ -427,14 +427,22 @@ public class OkHttpClient implements Cloneable { * ALPN * will be used to negotiate a transport. * + *

{@link Protocol#HTTP_1_0} is not supported in this set. Requests are + * initiated with {@code HTTP/1.1} only. If the server responds with {@code + * HTTP/1.0}, that will be exposed by {@link Response#protocol()}. + * * @param protocols the protocols to use, in order of preference. The list - * must contain {@link Protocol#HTTP_1_1}. It must not contain null. + * must contain {@link Protocol#HTTP_1_1}. It must not contain null or + * {@link Protocol#HTTP_1_0}. */ public final OkHttpClient setProtocols(List protocols) { protocols = Util.immutableList(protocols); if (!protocols.contains(Protocol.HTTP_1_1)) { throw new IllegalArgumentException("protocols doesn't contain http/1.1: " + protocols); } + if (protocols.contains(Protocol.HTTP_1_0)) { + throw new IllegalArgumentException("protocols must not contain http/1.0: " + protocols); + } if (protocols.contains(null)) { throw new IllegalArgumentException("protocols must not contain null"); }