1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-08 23:42:08 +03:00

Add Framed protocol to connections

This commit is contained in:
gkimbwala
2016-01-15 17:58:03 -08:00
parent 9b0706489c
commit c3f8dcb22e
4 changed files with 65 additions and 43 deletions

View File

@@ -51,9 +51,9 @@ public final class HttpLoggingInterceptor implements Interceptor {
* *
* <p>Example: * <p>Example:
* <pre>{@code * <pre>{@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)
* }</pre> * }</pre>
*/ */
BASIC, BASIC,
@@ -62,13 +62,13 @@ public final class HttpLoggingInterceptor implements Interceptor {
* *
* <p>Example: * <p>Example:
* <pre>{@code * <pre>{@code
* --> POST /greeting HTTP/1.1 * --> POST /greeting http/1.1
* Host: example.com * Host: example.com
* Content-Type: plain/text * Content-Type: plain/text
* Content-Length: 3 * Content-Length: 3
* --> END POST * --> END POST
* *
* <-- HTTP/1.1 200 OK (22ms) * <-- 200 OK (22ms)
* Content-Type: plain/text * Content-Type: plain/text
* Content-Length: 6 * Content-Length: 6
* <-- END HTTP * <-- END HTTP
@@ -80,7 +80,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
* *
* <p>Example: * <p>Example:
* <pre>{@code * <pre>{@code
* --> POST /greeting HTTP/1.1 * --> POST /greeting http/1.1
* Host: example.com * Host: example.com
* Content-Type: plain/text * Content-Type: plain/text
* Content-Length: 3 * Content-Length: 3
@@ -88,7 +88,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
* Hi? * Hi?
* --> END GET * --> END GET
* *
* <-- HTTP/1.1 200 OK (22ms) * <-- 200 OK (22ms)
* Content-Type: plain/text * Content-Type: plain/text
* Content-Length: 6 * Content-Length: 6
* *
@@ -149,8 +149,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
Connection connection = chain.connection(); Connection connection = chain.connection();
Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1; Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
String requestStartMessage = String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
"--> " + request.method() + ' ' + request.url() + ' ' + protocol(protocol);
if (!logHeaders && hasRequestBody) { if (!logHeaders && hasRequestBody) {
requestStartMessage += " (" + requestBody.contentLength() + "-byte body)"; requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
} }
@@ -247,8 +246,4 @@ public final class HttpLoggingInterceptor implements Interceptor {
String contentEncoding = headers.get("Content-Encoding"); String contentEncoding = headers.get("Content-Encoding");
return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity"); return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
} }
private static String protocol(Protocol protocol) {
return protocol == Protocol.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1";
}
} }

View File

@@ -113,12 +113,12 @@ public final class HttpLoggingInterceptorTest {
client.newCall(request().build()).execute(); client.newCall(request().build()).execute();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)")
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)")
.assertNoMoreLogs(); .assertNoMoreLogs();
} }
@@ -130,12 +130,12 @@ public final class HttpLoggingInterceptorTest {
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute(); client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
applicationLogs 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\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)")
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs 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\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)")
.assertNoMoreLogs(); .assertNoMoreLogs();
} }
@@ -150,12 +150,12 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 6-byte body\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 6-byte body\\)")
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 6-byte body\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 6-byte body\\)")
.assertNoMoreLogs(); .assertNoMoreLogs();
} }
@@ -170,12 +170,12 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)")
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)")
.assertNoMoreLogs(); .assertNoMoreLogs();
} }
@@ -188,7 +188,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("--> END GET") .assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0") .assertLogEqual("Content-Length: 0")
@@ -198,7 +198,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip") .assertLogEqual("Accept-Encoding: gzip")
@@ -221,7 +221,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("--> POST " + url + " http/1.1")
.assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("Content-Length: 3") .assertLogEqual("Content-Length: 3")
.assertLogEqual("--> END POST") .assertLogEqual("--> END POST")
@@ -233,7 +233,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("--> POST " + url + " http/1.1")
.assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("Content-Length: 3") .assertLogEqual("Content-Length: 3")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
@@ -258,7 +258,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("--> POST " + url + " http/1.1")
.assertLogEqual("Content-Length: 3") .assertLogEqual("Content-Length: 3")
.assertLogEqual("--> END POST") .assertLogEqual("--> END POST")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
@@ -269,7 +269,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("--> POST " + url + " http/1.1")
.assertLogEqual("Content-Length: 3") .assertLogEqual("Content-Length: 3")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Connection: Keep-Alive")
@@ -301,7 +301,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("--> POST " + url + " http/1.1")
.assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("--> END POST") .assertLogEqual("--> END POST")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
@@ -312,7 +312,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("--> POST " + url + " http/1.1")
.assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("Transfer-Encoding: chunked") .assertLogEqual("Transfer-Encoding: chunked")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
@@ -338,7 +338,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("--> END GET") .assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 6") .assertLogEqual("Content-Length: 6")
@@ -349,7 +349,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip") .assertLogEqual("Accept-Encoding: gzip")
@@ -372,7 +372,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("--> END GET") .assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0") .assertLogEqual("Content-Length: 0")
@@ -382,7 +382,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip") .assertLogEqual("Accept-Encoding: gzip")
@@ -413,7 +413,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("--> END GET") .assertLogEqual("--> END GET")
.assertLogMatch("<-- " + code + " No Content " + url + " \\(\\d+ms\\)") .assertLogMatch("<-- " + code + " No Content " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0") .assertLogEqual("Content-Length: 0")
@@ -423,7 +423,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip") .assertLogEqual("Accept-Encoding: gzip")
@@ -446,7 +446,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("--> POST " + url + " http/1.1")
.assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("Content-Length: 3") .assertLogEqual("Content-Length: 3")
.assertLogEqual("") .assertLogEqual("")
@@ -460,7 +460,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("--> POST " + url + " http/1.1")
.assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("Content-Length: 3") .assertLogEqual("Content-Length: 3")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
@@ -488,7 +488,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("--> END GET") .assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 6") .assertLogEqual("Content-Length: 6")
@@ -501,7 +501,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip") .assertLogEqual("Accept-Encoding: gzip")
@@ -528,7 +528,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("--> END GET") .assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Transfer-encoding: chunked") .assertLogEqual("Transfer-encoding: chunked")
@@ -541,7 +541,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip") .assertLogEqual("Accept-Encoding: gzip")
@@ -570,7 +570,7 @@ public final class HttpLoggingInterceptorTest {
response.body().close(); response.body().close();
networkLogs networkLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("Host: " + host) .assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive") .assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip") .assertLogEqual("Accept-Encoding: gzip")
@@ -586,7 +586,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs(); .assertNoMoreLogs();
applicationLogs applicationLogs
.assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("--> END GET") .assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Type: text/plain; charset=utf-8")

View File

@@ -2305,6 +2305,29 @@ public final class CallTest {
assertEquals("password", get.getHeader("Proxy-Authorization")); 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 AtomicReference<Protocol> protocolRef = 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() { private void makeFailingCall() {
RequestBody requestBody = new RequestBody() { RequestBody requestBody = new RequestBody() {
@Override public MediaType contentType() { @Override public MediaType contentType() {

View File

@@ -360,7 +360,11 @@ public final class RealConnection implements Connection {
} }
@Override public Protocol protocol() { @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() { @Override public String toString() {