mirror of
https://github.com/square/okhttp.git
synced 2025-11-27 18:21:14 +03:00
Don't log gzipped data. It isn't human readable.
This commit is contained in:
@@ -178,8 +178,11 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String endMessage = "--> END " + request.method();
|
if (!logBody || !hasRequestBody) {
|
||||||
if (logBody && hasRequestBody) {
|
logger.log("--> END " + request.method());
|
||||||
|
} else if (bodyEncoded(request.headers())) {
|
||||||
|
logger.log("--> END " + request.method() + " (encoded body omitted)");
|
||||||
|
} else {
|
||||||
Buffer buffer = new Buffer();
|
Buffer buffer = new Buffer();
|
||||||
requestBody.writeTo(buffer);
|
requestBody.writeTo(buffer);
|
||||||
|
|
||||||
@@ -192,9 +195,9 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
|||||||
logger.log("");
|
logger.log("");
|
||||||
logger.log(buffer.readString(charset));
|
logger.log(buffer.readString(charset));
|
||||||
|
|
||||||
endMessage += " (" + requestBody.contentLength() + "-byte body)";
|
logger.log("--> END " + request.method()
|
||||||
|
+ " (" + requestBody.contentLength() + "-byte body)");
|
||||||
}
|
}
|
||||||
logger.log(endMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long startNs = System.nanoTime();
|
long startNs = System.nanoTime();
|
||||||
@@ -212,8 +215,11 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
|||||||
logger.log(headers.name(i) + ": " + headers.value(i));
|
logger.log(headers.name(i) + ": " + headers.value(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
String endMessage = "<-- END HTTP";
|
if (!logBody || !HttpEngine.hasBody(response)) {
|
||||||
if (logBody && HttpEngine.hasBody(response)) {
|
logger.log("<-- END HTTP");
|
||||||
|
} else if (bodyEncoded(response.headers())) {
|
||||||
|
logger.log("<-- END HTTP (encoded body omitted)");
|
||||||
|
} else {
|
||||||
BufferedSource source = responseBody.source();
|
BufferedSource source = responseBody.source();
|
||||||
source.request(Long.MAX_VALUE); // Buffer the entire body.
|
source.request(Long.MAX_VALUE); // Buffer the entire body.
|
||||||
Buffer buffer = source.buffer();
|
Buffer buffer = source.buffer();
|
||||||
@@ -229,14 +235,18 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
|||||||
logger.log(buffer.clone().readString(charset));
|
logger.log(buffer.clone().readString(charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
endMessage += " (" + buffer.size() + "-byte body)";
|
logger.log("<-- END HTTP (" + buffer.size() + "-byte body)");
|
||||||
}
|
}
|
||||||
logger.log(endMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean bodyEncoded(Headers headers) {
|
||||||
|
String contentEncoding = headers.get("Content-Encoding");
|
||||||
|
return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
|
||||||
|
}
|
||||||
|
|
||||||
private static String protocol(Protocol protocol) {
|
private static String protocol(Protocol protocol) {
|
||||||
return protocol == Protocol.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1";
|
return protocol == Protocol.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,9 @@ import java.io.IOException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
import okio.Buffer;
|
||||||
import okio.BufferedSink;
|
import okio.BufferedSink;
|
||||||
|
import okio.ByteString;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -519,6 +521,45 @@ public final class HttpLoggingInterceptorTest {
|
|||||||
.assertNoMoreLogs();
|
.assertNoMoreLogs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void bodyResponseNotIdentityEncoded() throws IOException {
|
||||||
|
setLevel(Level.BODY);
|
||||||
|
|
||||||
|
server.enqueue(new MockResponse()
|
||||||
|
.setHeader("Content-Encoding", "gzip")
|
||||||
|
.setHeader("Content-Type", PLAIN)
|
||||||
|
.setBody(new Buffer().write(ByteString.decodeBase64(
|
||||||
|
"H4sIAAAAAAAAAPNIzcnJ11HwQKIAdyO+9hMAAAA="))));
|
||||||
|
client.newCall(request().build()).execute();
|
||||||
|
|
||||||
|
networkLogs
|
||||||
|
.assertLogEqual("--> GET " + url + " HTTP/1.1")
|
||||||
|
.assertLogEqual("Host: " + host)
|
||||||
|
.assertLogEqual("Connection: Keep-Alive")
|
||||||
|
.assertLogEqual("Accept-Encoding: gzip")
|
||||||
|
.assertLogMatch("User-Agent: okhttp/.+")
|
||||||
|
.assertLogEqual("--> END GET")
|
||||||
|
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
|
||||||
|
.assertLogEqual("Content-Encoding: gzip")
|
||||||
|
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
|
||||||
|
.assertLogMatch("Content-Length: \\d+")
|
||||||
|
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||||
|
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||||
|
.assertLogEqual("<-- END HTTP (encoded body omitted)")
|
||||||
|
.assertNoMoreLogs();
|
||||||
|
|
||||||
|
applicationLogs
|
||||||
|
.assertLogEqual("--> GET " + url + " HTTP/1.1")
|
||||||
|
.assertLogEqual("--> END GET")
|
||||||
|
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
|
||||||
|
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
|
||||||
|
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||||
|
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||||
|
.assertLogEqual("")
|
||||||
|
.assertLogEqual("Hello, Hello, Hello")
|
||||||
|
.assertLogEqual("<-- END HTTP (19-byte body)")
|
||||||
|
.assertNoMoreLogs();
|
||||||
|
}
|
||||||
|
|
||||||
private Request.Builder request() {
|
private Request.Builder request() {
|
||||||
return new Request.Builder().url(url);
|
return new Request.Builder().url(url);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user