Skip to content

Commit 3646138

Browse files
authored
Don't summarize the request and response headers. (square#3576)
Just pass the Request and Response objects in directly.
1 parent 6cddc89 commit 3646138

File tree

6 files changed

+54
-42
lines changed

6 files changed

+54
-42
lines changed

okhttp-tests/src/test/java/okhttp3/EventListenerTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,19 +933,19 @@ private void requestBodyFail() throws IOException {
933933
enableTlsWithTunnel(false);
934934
server.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
935935
requestBodySuccess(RequestBody.create(MediaType.parse("text/plain"), "Hello"), equalTo(5L),
936-
equalTo(15L));
936+
equalTo(19L));
937937
}
938938

939939
@Test public void requestBodySuccessHttp2OverHttps() throws IOException {
940940
enableTlsWithTunnel(false);
941941
server.setProtocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1));
942942
requestBodySuccess(RequestBody.create(MediaType.parse("text/plain"), "Hello"), equalTo(5L),
943-
equalTo(15L));
943+
equalTo(19L));
944944
}
945945

946946
@Test public void requestBodySuccessHttp() throws IOException {
947947
requestBodySuccess(RequestBody.create(MediaType.parse("text/plain"), "Hello"), equalTo(5L),
948-
equalTo(15L));
948+
equalTo(19L));
949949
}
950950

951951
@Test public void requestBodySuccessStreaming() throws IOException {
@@ -960,12 +960,12 @@ private void requestBodyFail() throws IOException {
960960
}
961961
};
962962

963-
requestBodySuccess(requestBody, equalTo(8192L), equalTo(15L));
963+
requestBodySuccess(requestBody, equalTo(8192L), equalTo(19L));
964964
}
965965

966966
@Test public void requestBodySuccessEmpty() throws IOException {
967967
requestBodySuccess(RequestBody.create(MediaType.parse("text/plain"), ""), equalTo(0L),
968-
equalTo(15L));
968+
equalTo(19L));
969969
}
970970

971971
private void requestBodySuccess(RequestBody body, Matcher<Long> requestBodyBytes,

okhttp-tests/src/test/java/okhttp3/HeadersTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,17 @@ public final class HeadersTest {
458458
.build();
459459
assertEquals(Collections.emptyList(), HttpHeaders.parseChallenges(headers, "WWW-Authenticate"));
460460
}
461+
462+
@Test public void byteCount() {
463+
assertEquals(0L, new Headers.Builder().build().byteCount());
464+
assertEquals(10L, new Headers.Builder()
465+
.add("abc", "def")
466+
.build()
467+
.byteCount());
468+
assertEquals(20L, new Headers.Builder()
469+
.add("abc", "def")
470+
.add("ghi", "jkl")
471+
.build()
472+
.byteCount());
473+
}
461474
}

okhttp-tests/src/test/java/okhttp3/RecordingEventListener.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,32 +126,32 @@ private void logEvent(CallEvent e) {
126126
logEvent(new RequestHeadersStart(call));
127127
}
128128

129-
@Override public void requestHeadersEnd(Call call, long headerLength) {
130-
logEvent(new RequestHeadersEnd(call, headerLength));
129+
@Override public void requestHeadersEnd(Call call, Request request) {
130+
logEvent(new RequestHeadersEnd(call, request.headers.byteCount()));
131131
}
132132

133133
@Override public void requestBodyStart(Call call) {
134134
logEvent(new RequestBodyStart(call));
135135
}
136136

137-
@Override public void requestBodyEnd(Call call, long bytesWritten) {
138-
logEvent(new RequestBodyEnd(call, bytesWritten));
137+
@Override public void requestBodyEnd(Call call, long byteCount) {
138+
logEvent(new RequestBodyEnd(call, byteCount));
139139
}
140140

141141
@Override public void responseHeadersStart(Call call) {
142142
logEvent(new ResponseHeadersStart(call));
143143
}
144144

145-
@Override public void responseHeadersEnd(Call call, long headerLength) {
146-
logEvent(new ResponseHeadersEnd(call, headerLength));
145+
@Override public void responseHeadersEnd(Call call, Response response) {
146+
logEvent(new ResponseHeadersEnd(call, response.headers.byteCount()));
147147
}
148148

149149
@Override public void responseBodyStart(Call call) {
150150
logEvent(new ResponseBodyStart(call));
151151
}
152152

153-
@Override public void responseBodyEnd(Call call, long bytesRead) {
154-
logEvent(new ResponseBodyEnd(call, bytesRead));
153+
@Override public void responseBodyEnd(Call call, long byteCount) {
154+
logEvent(new ResponseBodyEnd(call, byteCount));
155155
}
156156

157157
@Override public void callEnd(Call call) {

okhttp/src/main/java/okhttp3/EventListener.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ public void requestHeadersStart(Call call) {
185185
*
186186
* <p>This method is always invoked after {@link #requestHeadersStart(Call)}.
187187
*
188-
* @param headerLength the length in java characters of headers to be written.
188+
* @param request the request sent over the network. It is an error to access the body of this
189+
* request.
189190
*/
190-
public void requestHeadersEnd(Call call, long headerLength) {
191+
public void requestHeadersEnd(Call call, Request request) {
191192
}
192193

193194
/**
@@ -207,10 +208,8 @@ public void requestBodyStart(Call call) {
207208
* Invoked immediately after sending a request body.
208209
*
209210
* <p>This method is always invoked after {@link #requestBodyStart(Call)}.
210-
*
211-
* @param bytesWritten the length in bytes of body written, including partial success.
212211
*/
213-
public void requestBodyEnd(Call call, long bytesWritten) {
212+
public void requestBodyEnd(Call call, long byteCount) {
214213
}
215214

216215
/**
@@ -228,11 +227,12 @@ public void responseHeadersStart(Call call) {
228227
/**
229228
* Invoked immediately after receiving response headers.
230229
*
231-
* <p>This method is always invoked after {@link #responseHeadersStart(Call)}.
230+
* <p>This method is always invoked after {@link #responseHeadersStart}.
232231
*
233-
* @param headerLength the length in bytes of headers read, or -1 if failed to read.
232+
* @param response the response received over the network. It is an error to access the body of
233+
* this response.
234234
*/
235-
public void responseHeadersEnd(Call call, long headerLength) {
235+
public void responseHeadersEnd(Call call, Response response) {
236236
}
237237

238238
/**
@@ -254,21 +254,15 @@ public void responseBodyStart(Call call) {
254254
* websocket upgrade.
255255
*
256256
* <p>This method is always invoked after {@link #requestBodyStart(Call)}.
257-
*
258-
* @param bytesRead the length in bytes of the body read, including partial success.
259257
*/
260-
public void responseBodyEnd(Call call, long bytesRead) {
258+
public void responseBodyEnd(Call call, long byteCount) {
261259
}
262260

263261
/**
264262
* Invoked immediately after a call has completely ended. This includes delayed consumption
265263
* of response body by the caller.
266264
*
267265
* <p>This method is always invoked after {@link #callStart(Call)}.
268-
*
269-
* <p>{@code ioe} will be null in the case of a successful attempt to execute the call.
270-
*
271-
* <p>{@code ioe} will be non-null in the case of a failed attempt to execute the call.
272266
*/
273267
public void callEnd(Call call) {
274268
}

okhttp/src/main/java/okhttp3/Headers.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ public List<String> values(String name) {
110110
: Collections.<String>emptyList();
111111
}
112112

113+
/**
114+
* Returns the number of bytes required to encode these headers using HTTP/1.1. This is also the
115+
* approximate size of HTTP/2 headers before they are compressed with HPACK. This value is
116+
* intended to be used as a metric: smaller headers are more efficient to encode and transmit.
117+
*/
118+
public long byteCount() {
119+
// Each header name has 2 bytes of overhead for ': ' and every header value has 2 bytes of
120+
// overhead for '\r\n'.
121+
long result = namesAndValues.length * 2;
122+
123+
for (int i = 0, size = namesAndValues.length; i < size; i++) {
124+
result += namesAndValues[i].length();
125+
}
126+
127+
return result;
128+
}
129+
113130
public Builder newBuilder() {
114131
Builder result = new Builder();
115132
Collections.addAll(result.namesAndValues, namesAndValues);

okhttp/src/main/java/okhttp3/internal/http/CallServerInterceptor.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.io.IOException;
1919
import java.net.ProtocolException;
20-
import okhttp3.Headers;
2120
import okhttp3.Interceptor;
2221
import okhttp3.Request;
2322
import okhttp3.Response;
@@ -49,7 +48,7 @@ public CallServerInterceptor(boolean forWebSocket) {
4948

5049
realChain.eventListener().requestHeadersStart(realChain.call());
5150
httpCodec.writeRequestHeaders(request);
52-
realChain.eventListener().requestHeadersEnd(realChain.call(), headerLength(request.headers()));
51+
realChain.eventListener().requestHeadersEnd(realChain.call(), request);
5352

5453
Response.Builder responseBuilder = null;
5554
if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
@@ -97,7 +96,7 @@ public CallServerInterceptor(boolean forWebSocket) {
9796
.build();
9897

9998
realChain.eventListener()
100-
.responseHeadersEnd(realChain.call(), headerLength(response.headers()));
99+
.responseHeadersEnd(realChain.call(), response);
101100

102101
int code = response.code();
103102
if (forWebSocket && code == 101) {
@@ -124,17 +123,6 @@ public CallServerInterceptor(boolean forWebSocket) {
124123
return response;
125124
}
126125

127-
private long headerLength(Headers headers) {
128-
long length = 0;
129-
130-
for (int i = 0, size = headers.size(); i < size; i++) {
131-
length += headers.name(i).length();
132-
length += headers.value(i).length();
133-
}
134-
135-
return length;
136-
}
137-
138126
static final class CountingSink extends ForwardingSink {
139127
long successfulCount;
140128

0 commit comments

Comments
 (0)