Skip to content

Commit

Permalink
Merge commit '1f7e796e6e658df34a98276b2092a81de118937d'
Browse files Browse the repository at this point in the history
* commit '1f7e796e6e658df34a98276b2092a81de118937d':
  Cleanup HttpLoggingInterceptor (square#4346)
  Add a LoggingEventListener and use it in okcurl (square#4353)
  Preemptive auth for proxy CONNECT
  Relax handling of Cache-Control: immutable
  Add some docs for Cache class (square#4375)
  Fix connection leaks on failed web socket upgrades.
  Don't specify a crypto provider in HeldCertificate.
  Confirm that call timeouts don't apply to SSE or web sockets.
  Add APIs to configure the client's default call timeout. (square#4369)
  Recover from executor shutdowns gracefully. (square#4365)
  Make the nested BasicAuthInterceptor static (square#4368)
  Add basic auth interceptor recipe (square#4336)
  Whole operation timeouts
  Make scheme names case-sensitive again.
  Remove colon when scheme missing in builder toString (square#4361)
  CipherSuite init speedup (square#4340)
  Limit the use of regexes in the RFC 7235 challenge parser.
  Allow incomplete url builder toString usage (square#4357)
  APIs to set date headers
  Upgrade Conscrypt to 1.4.0 (was 1.3.0)
  • Loading branch information
SeniorZhai committed Nov 15, 2018
2 parents 099b3f4 + 1f7e796 commit d0fd9e2
Show file tree
Hide file tree
Showing 47 changed files with 2,077 additions and 675 deletions.
5 changes: 5 additions & 0 deletions okcurl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<artifactId>okhttp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>logging-interceptor</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>okhttp-testing-support</artifactId>
Expand Down
17 changes: 17 additions & 0 deletions okcurl/src/main/java/okhttp3/curl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import okhttp3.internal.http.StatusLine;
import okhttp3.internal.http2.Http2;
import okhttp3.internal.platform.Platform;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.LoggingEventListener;
import okio.BufferedSource;
import okio.Okio;
import okio.Sink;
Expand Down Expand Up @@ -121,6 +123,11 @@ private static String protocols() {
@Option(name = {"-V", "--version"}, description = "Show version number and quit")
public boolean version;

@Option(
name = {"-v", "--verbose"},
description = "Makes " + NAME + " verbose during the operation")
public boolean verbose;

@Arguments(title = "url", description = "Remote resource URL")
public String url;

Expand Down Expand Up @@ -184,6 +191,16 @@ private OkHttpClient createClient() {
builder.sslSocketFactory(sslSocketFactory, trustManager);
builder.hostnameVerifier(createInsecureHostnameVerifier());
}
if (verbose) {
HttpLoggingInterceptor.Logger logger =
new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
System.out.println(message);
}
};
builder.eventListenerFactory(new LoggingEventListener.Factory(logger));
}
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ static boolean isPlaintext(Buffer buffer) {
}
}

private boolean bodyHasUnknownEncoding(Headers headers) {
private static boolean bodyHasUnknownEncoding(Headers headers) {
String contentEncoding = headers.get("Content-Encoding");
return contentEncoding != null
&& !contentEncoding.equalsIgnoreCase("identity")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Copyright (C) 2018 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.logging;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import okhttp3.Call;
import okhttp3.Connection;
import okhttp3.EventListener;
import okhttp3.Handshake;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;

/**
* An OkHttp EventListener, which logs call events. Can be applied as an
* {@linkplain OkHttpClient#eventListenerFactory() event listener factory}.
*
* <p>The format of the logs created by this class should not be considered stable and may change
* slightly between releases. If you need a stable logging format, use your own event listener.
*/
public final class LoggingEventListener extends EventListener {
private final HttpLoggingInterceptor.Logger logger;
private long startNs;

private LoggingEventListener(HttpLoggingInterceptor.Logger logger) {
this.logger = logger;
}

@Override
public void callStart(Call call) {
startNs = System.nanoTime();

logWithTime("callStart: " + call.request());
}

@Override
public void dnsStart(Call call, String domainName) {
logWithTime("dnsStart: " + domainName);
}

@Override
public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) {
logWithTime("dnsEnd: " + inetAddressList);
}

@Override
public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
logWithTime("connectStart: " + inetSocketAddress + " " + proxy);
}

@Override
public void secureConnectStart(Call call) {
logWithTime("secureConnectStart");
}

@Override
public void secureConnectEnd(Call call, @Nullable Handshake handshake) {
logWithTime("secureConnectEnd");
}

@Override
public void connectEnd(
Call call, InetSocketAddress inetSocketAddress, Proxy proxy, @Nullable Protocol protocol) {
logWithTime("connectEnd: " + protocol);
}

@Override
public void connectFailed(
Call call,
InetSocketAddress inetSocketAddress,
Proxy proxy,
@Nullable Protocol protocol,
IOException ioe) {
logWithTime("connectFailed: " + protocol + " " + ioe);
}

@Override
public void connectionAcquired(Call call, Connection connection) {
logWithTime("connectionAcquired: " + connection);
}

@Override
public void connectionReleased(Call call, Connection connection) {
logWithTime("connectionReleased");
}

@Override
public void requestHeadersStart(Call call) {
logWithTime("requestHeadersStart");
}

@Override
public void requestHeadersEnd(Call call, Request request) {
logWithTime("requestHeadersEnd");
}

@Override
public void requestBodyStart(Call call) {
logWithTime("requestBodyStart");
}

@Override
public void requestBodyEnd(Call call, long byteCount) {
logWithTime("requestBodyEnd: byteCount=" + byteCount);
}

@Override
public void responseHeadersStart(Call call) {
logWithTime("responseHeadersStart");
}

@Override
public void responseHeadersEnd(Call call, Response response) {
logWithTime("responseHeadersEnd: " + response);
}

@Override
public void responseBodyStart(Call call) {
logWithTime("responseBodyStart");
}

@Override
public void responseBodyEnd(Call call, long byteCount) {
logWithTime("responseBodyEnd: byteCount=" + byteCount);
}

@Override
public void callEnd(Call call) {
logWithTime("callEnd");
}

@Override
public void callFailed(Call call, IOException ioe) {
logWithTime("callFailed: " + ioe);
}

private void logWithTime(String message) {
long timeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
logger.log("[" + timeMs + " ms] " + message);
}

public static class Factory implements EventListener.Factory {
private final HttpLoggingInterceptor.Logger logger;

public Factory() {
this(HttpLoggingInterceptor.Logger.DEFAULT);
}

public Factory(HttpLoggingInterceptor.Logger logger) {
this.logger = logger;
}

@Override
public EventListener create(Call call) {
return new LoggingEventListener(logger);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public final class HttpLoggingInterceptorTest {

@Rule public final MockWebServer server = new MockWebServer();

private HandshakeCertificates handshakeCertificates = localhost();
private HostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
private final HandshakeCertificates handshakeCertificates = localhost();
private final HostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
private OkHttpClient client;
private String host;
private HttpUrl url;
Expand Down Expand Up @@ -650,7 +650,7 @@ private void bodyGetNoBody(int code) throws IOException {
.assertNoMoreLogs();
}

@Test public void isPlaintext() throws IOException {
@Test public void isPlaintext() {
assertTrue(HttpLoggingInterceptor.isPlaintext(new Buffer()));
assertTrue(HttpLoggingInterceptor.isPlaintext(new Buffer().writeUtf8("abc")));
assertTrue(HttpLoggingInterceptor.isPlaintext(new Buffer().writeUtf8("new\r\nlines")));
Expand Down Expand Up @@ -807,7 +807,7 @@ private Request.Builder request() {
return new Request.Builder().url(url);
}

private static class LogRecorder implements HttpLoggingInterceptor.Logger {
static class LogRecorder implements HttpLoggingInterceptor.Logger {
private final List<String> logs = new ArrayList<>();
private int index;

Expand Down

0 comments on commit d0fd9e2

Please sign in to comment.