8216478: Cleanup HttpResponseImpl back reference to HttpConnection

Retain a reference to Exchange and HttpConnection only when necessary, i.e. for WebSocket initial connection.

Reviewed-by: chegar
This commit is contained in:
Daniel Fuchs 2019-01-14 10:46:08 +00:00
parent f00057cb16
commit ad67fe1baf

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -44,16 +44,13 @@ import jdk.internal.net.http.websocket.RawChannel;
class HttpResponseImpl<T> implements HttpResponse<T>, RawChannel.Provider { class HttpResponseImpl<T> implements HttpResponse<T>, RawChannel.Provider {
final int responseCode; final int responseCode;
final Exchange<T> exchange;
final HttpRequest initialRequest; final HttpRequest initialRequest;
final Optional<HttpResponse<T>> previousResponse; final Optional<HttpResponse<T>> previousResponse;
final HttpHeaders headers; final HttpHeaders headers;
final Optional<SSLSession> sslSession; final Optional<SSLSession> sslSession;
final URI uri; final URI uri;
final HttpClient.Version version; final HttpClient.Version version;
RawChannel rawchan; final RawChannelProvider rawChannelProvider;
final HttpConnection connection;
final Stream<T> stream;
final T body; final T body;
public HttpResponseImpl(HttpRequest initialRequest, public HttpResponseImpl(HttpRequest initialRequest,
@ -62,7 +59,6 @@ class HttpResponseImpl<T> implements HttpResponse<T>, RawChannel.Provider {
T body, T body,
Exchange<T> exch) { Exchange<T> exch) {
this.responseCode = response.statusCode(); this.responseCode = response.statusCode();
this.exchange = exch;
this.initialRequest = initialRequest; this.initialRequest = initialRequest;
this.previousResponse = Optional.ofNullable(previousResponse); this.previousResponse = Optional.ofNullable(previousResponse);
this.headers = response.headers(); this.headers = response.headers();
@ -70,23 +66,10 @@ class HttpResponseImpl<T> implements HttpResponse<T>, RawChannel.Provider {
this.sslSession = Optional.ofNullable(response.getSSLSession()); this.sslSession = Optional.ofNullable(response.getSSLSession());
this.uri = response.request().uri(); this.uri = response.request().uri();
this.version = response.version(); this.version = response.version();
this.connection = connection(exch); this.rawChannelProvider = RawChannelProvider.create(response, exch);
this.stream = null;
this.body = body; this.body = body;
} }
private HttpConnection connection(Exchange<?> exch) {
if (exch == null || exch.exchImpl == null) {
assert responseCode == 407;
return null; // case of Proxy 407
}
return exch.exchImpl.connection();
}
private ExchangeImpl<?> exchangeImpl() {
return exchange != null ? exchange.exchImpl : stream;
}
@Override @Override
public int statusCode() { public int statusCode() {
return responseCode; return responseCode;
@ -141,6 +124,52 @@ class HttpResponseImpl<T> implements HttpResponse<T>, RawChannel.Provider {
*/ */
@Override @Override
public synchronized RawChannel rawChannel() throws IOException { public synchronized RawChannel rawChannel() throws IOException {
if (rawChannelProvider == null) {
throw new UnsupportedOperationException(
"RawChannel is only supported for WebSocket creation");
}
return rawChannelProvider.rawChannel();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
String method = request().method();
URI uri = request().uri();
String uristring = uri == null ? "" : uri.toString();
sb.append('(')
.append(method)
.append(" ")
.append(uristring)
.append(") ")
.append(statusCode());
return sb.toString();
}
/**
* An auxiliary class used for RawChannel creation when creating a WebSocket.
* This avoids keeping around references to connection/exchange in the
* regular HttpResponse case. Only those responses corresponding to an
* initial WebSocket request have a RawChannelProvider.
*/
private static final class RawChannelProvider implements RawChannel.Provider {
private final HttpConnection connection;
private final Exchange<?> exchange;
private RawChannel rawchan;
RawChannelProvider(HttpConnection conn, Exchange<?> exch) {
connection = conn;
exchange = exch;
}
static RawChannelProvider create(Response resp, Exchange<?> exch) {
if (resp.request().isWebSocket()) {
return new RawChannelProvider(connection(resp, exch), exch);
}
return null;
}
@Override
public synchronized RawChannel rawChannel() {
if (rawchan == null) { if (rawchan == null) {
ExchangeImpl<?> exchImpl = exchangeImpl(); ExchangeImpl<?> exchImpl = exchangeImpl();
if (!(exchImpl instanceof Http1Exchange)) { if (!(exchImpl instanceof Http1Exchange)) {
@ -160,18 +189,17 @@ class HttpResponseImpl<T> implements HttpResponse<T>, RawChannel.Provider {
return rawchan; return rawchan;
} }
@Override private static HttpConnection connection(Response resp, Exchange<?> exch) {
public String toString() { if (exch == null || exch.exchImpl == null) {
StringBuilder sb = new StringBuilder(); assert resp.statusCode == 407;
String method = request().method(); return null; // case of Proxy 407
URI uri = request().uri(); }
String uristring = uri == null ? "" : uri.toString(); return exch.exchImpl.connection();
sb.append('(') }
.append(method)
.append(" ") private ExchangeImpl<?> exchangeImpl() {
.append(uristring) return exchange != null ? exchange.exchImpl : null;
.append(") ") }
.append(statusCode());
return sb.toString();
} }
} }