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:
* <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>
*/
BASIC,
@@ -62,13 +62,13 @@ public final class HttpLoggingInterceptor implements Interceptor {
*
* <p>Example:
* <pre>{@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 {
*
* <p>Example:
* <pre>{@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";
}
}

View File

@@ -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")

View File

@@ -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 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() {
RequestBody requestBody = new RequestBody() {
@Override public MediaType contentType() {

View File

@@ -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() {