diff --git a/okhttp-logging-interceptor/src/main/java/okhttp3/logging/HttpLoggingInterceptor.java b/okhttp-logging-interceptor/src/main/java/okhttp3/logging/HttpLoggingInterceptor.java index a77bc5639..4c663f341 100644 --- a/okhttp-logging-interceptor/src/main/java/okhttp3/logging/HttpLoggingInterceptor.java +++ b/okhttp-logging-interceptor/src/main/java/okhttp3/logging/HttpLoggingInterceptor.java @@ -51,9 +51,9 @@ public final class HttpLoggingInterceptor implements Interceptor { * *
Example: *
{@code - * --> POST /greeting HTTP/1.1 (3-byte body) + * --> POST /greeting http/1.1 (3-byte body) * - * <-- HTTP/1.1 200 OK (22ms, 6-byte body) + * <-- 200 OK (22ms, 6-byte body) * }*/ BASIC, @@ -62,13 +62,13 @@ public final class HttpLoggingInterceptor implements Interceptor { * *
Example: *
{@code - * --> POST /greeting HTTP/1.1 + * --> POST /greeting http/1.1 * Host: example.com * Content-Type: plain/text * Content-Length: 3 * --> END POST * - * <-- HTTP/1.1 200 OK (22ms) + * <-- 200 OK (22ms) * Content-Type: plain/text * Content-Length: 6 * <-- END HTTP @@ -80,7 +80,7 @@ public final class HttpLoggingInterceptor implements Interceptor { * *Example: *
{@code - * --> POST /greeting HTTP/1.1 + * --> POST /greeting http/1.1 * Host: example.com * Content-Type: plain/text * Content-Length: 3 @@ -88,7 +88,7 @@ public final class HttpLoggingInterceptor implements Interceptor { * Hi? * --> END GET * - * <-- HTTP/1.1 200 OK (22ms) + * <-- 200 OK (22ms) * Content-Type: plain/text * Content-Length: 6 * @@ -149,8 +149,7 @@ public final class HttpLoggingInterceptor implements Interceptor { Connection connection = chain.connection(); Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1; - String requestStartMessage = - "--> " + request.method() + ' ' + request.url() + ' ' + protocol(protocol); + String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol; if (!logHeaders && hasRequestBody) { requestStartMessage += " (" + requestBody.contentLength() + "-byte body)"; } @@ -247,8 +246,4 @@ public final class HttpLoggingInterceptor implements Interceptor { String contentEncoding = headers.get("Content-Encoding"); return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity"); } - - private static String protocol(Protocol protocol) { - return protocol == Protocol.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1"; - } } diff --git a/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java b/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java index 42ac9d214..c1c0df1be 100644 --- a/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java +++ b/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java @@ -113,12 +113,12 @@ public final class HttpLoggingInterceptorTest { client.newCall(request().build()).execute(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)") .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)") .assertNoMoreLogs(); } @@ -130,12 +130,12 @@ public final class HttpLoggingInterceptorTest { client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute(); applicationLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1 (3-byte body)") + .assertLogEqual("--> POST " + url + " http/1.1 (3-byte body)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)") .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1 (3-byte body)") + .assertLogEqual("--> POST " + url + " http/1.1 (3-byte body)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)") .assertNoMoreLogs(); } @@ -150,12 +150,12 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 6-byte body\\)") .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 6-byte body\\)") .assertNoMoreLogs(); } @@ -170,12 +170,12 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)") .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)") .assertNoMoreLogs(); } @@ -188,7 +188,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 0") @@ -198,7 +198,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("Host: " + host) .assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Accept-Encoding: gzip") @@ -221,7 +221,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1") + .assertLogEqual("--> POST " + url + " http/1.1") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Length: 3") .assertLogEqual("--> END POST") @@ -233,7 +233,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1") + .assertLogEqual("--> POST " + url + " http/1.1") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Length: 3") .assertLogEqual("Host: " + host) @@ -258,7 +258,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1") + .assertLogEqual("--> POST " + url + " http/1.1") .assertLogEqual("Content-Length: 3") .assertLogEqual("--> END POST") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") @@ -269,7 +269,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1") + .assertLogEqual("--> POST " + url + " http/1.1") .assertLogEqual("Content-Length: 3") .assertLogEqual("Host: " + host) .assertLogEqual("Connection: Keep-Alive") @@ -301,7 +301,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1") + .assertLogEqual("--> POST " + url + " http/1.1") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("--> END POST") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") @@ -312,7 +312,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1") + .assertLogEqual("--> POST " + url + " http/1.1") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Transfer-Encoding: chunked") .assertLogEqual("Host: " + host) @@ -338,7 +338,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 6") @@ -349,7 +349,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("Host: " + host) .assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Accept-Encoding: gzip") @@ -372,7 +372,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 0") @@ -382,7 +382,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("Host: " + host) .assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Accept-Encoding: gzip") @@ -413,7 +413,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- " + code + " No Content " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 0") @@ -423,7 +423,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("Host: " + host) .assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Accept-Encoding: gzip") @@ -446,7 +446,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1") + .assertLogEqual("--> POST " + url + " http/1.1") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Length: 3") .assertLogEqual("") @@ -460,7 +460,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> POST " + url + " HTTP/1.1") + .assertLogEqual("--> POST " + url + " http/1.1") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Length: 3") .assertLogEqual("Host: " + host) @@ -488,7 +488,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 6") @@ -501,7 +501,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("Host: " + host) .assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Accept-Encoding: gzip") @@ -528,7 +528,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Transfer-encoding: chunked") @@ -541,7 +541,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("Host: " + host) .assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Accept-Encoding: gzip") @@ -570,7 +570,7 @@ public final class HttpLoggingInterceptorTest { response.body().close(); networkLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("Host: " + host) .assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Accept-Encoding: gzip") @@ -586,7 +586,7 @@ public final class HttpLoggingInterceptorTest { .assertNoMoreLogs(); applicationLogs - .assertLogEqual("--> GET " + url + " HTTP/1.1") + .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Type: text/plain; charset=utf-8") diff --git a/okhttp-tests/src/test/java/okhttp3/CallTest.java b/okhttp-tests/src/test/java/okhttp3/CallTest.java index e5d46d88a..12bf5db21 100644 --- a/okhttp-tests/src/test/java/okhttp3/CallTest.java +++ b/okhttp-tests/src/test/java/okhttp3/CallTest.java @@ -2305,6 +2305,29 @@ public final class CallTest { assertEquals("password", get.getHeader("Proxy-Authorization")); } + @Test public void interceptorGetsFramedProtocol() throws Exception { + enableProtocol(Protocol.HTTP_2); + + // Capture the protocol as it is observed by the interceptor. + final AtomicReferenceprotocolRef = new AtomicReference<>(); + Interceptor interceptor = new Interceptor() { + @Override public Response intercept(Chain chain) throws IOException { + protocolRef.set(chain.connection().protocol()); + return chain.proceed(chain.request()); + } + }; + client = client.newBuilder() + .addNetworkInterceptor(interceptor) + .build(); + + // Make an HTTP/2 request and confirm that the protocol matches. + server.enqueue(new MockResponse()); + executeSynchronously(new Request.Builder() + .url(server.url("/")) + .build()); + assertEquals(Protocol.HTTP_2, protocolRef.get()); + } + private void makeFailingCall() { RequestBody requestBody = new RequestBody() { @Override public MediaType contentType() { diff --git a/okhttp/src/main/java/okhttp3/internal/io/RealConnection.java b/okhttp/src/main/java/okhttp3/internal/io/RealConnection.java index 3077a7717..a0509a08d 100644 --- a/okhttp/src/main/java/okhttp3/internal/io/RealConnection.java +++ b/okhttp/src/main/java/okhttp3/internal/io/RealConnection.java @@ -360,7 +360,11 @@ public final class RealConnection implements Connection { } @Override public Protocol protocol() { - return protocol != null ? protocol : Protocol.HTTP_1_1; + if (framedConnection == null) { + return protocol != null ? protocol : Protocol.HTTP_1_1; + } else { + return framedConnection.getProtocol(); + } } @Override public String toString() {