mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 12:04:39 +02:00
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:
parent
f00057cb16
commit
ad67fe1baf
1 changed files with 64 additions and 36 deletions
|
@ -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,23 +124,11 @@ class HttpResponseImpl<T> implements HttpResponse<T>, RawChannel.Provider {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public synchronized RawChannel rawChannel() throws IOException {
|
public synchronized RawChannel rawChannel() throws IOException {
|
||||||
if (rawchan == null) {
|
if (rawChannelProvider == null) {
|
||||||
ExchangeImpl<?> exchImpl = exchangeImpl();
|
throw new UnsupportedOperationException(
|
||||||
if (!(exchImpl instanceof Http1Exchange)) {
|
"RawChannel is only supported for WebSocket creation");
|
||||||
// RawChannel is only used for WebSocket - and WebSocket
|
|
||||||
// is not supported over HTTP/2 yet, so we should not come
|
|
||||||
// here. Getting a RawChannel over HTTP/2 might be supported
|
|
||||||
// in the future, but it would entail retrieving any left over
|
|
||||||
// bytes that might have been read but not consumed by the
|
|
||||||
// HTTP/2 connection.
|
|
||||||
throw new UnsupportedOperationException("RawChannel is not supported over HTTP/2");
|
|
||||||
}
|
|
||||||
// Http1Exchange may have some remaining bytes in its
|
|
||||||
// internal buffer.
|
|
||||||
Supplier<ByteBuffer> initial = ((Http1Exchange<?>)exchImpl)::drainLeftOverBytes;
|
|
||||||
rawchan = new RawChannelTube(connection, initial);
|
|
||||||
}
|
}
|
||||||
return rawchan;
|
return rawChannelProvider.rawChannel();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -174,4 +145,61 @@ class HttpResponseImpl<T> implements HttpResponse<T>, RawChannel.Provider {
|
||||||
.append(statusCode());
|
.append(statusCode());
|
||||||
return sb.toString();
|
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) {
|
||||||
|
ExchangeImpl<?> exchImpl = exchangeImpl();
|
||||||
|
if (!(exchImpl instanceof Http1Exchange)) {
|
||||||
|
// RawChannel is only used for WebSocket - and WebSocket
|
||||||
|
// is not supported over HTTP/2 yet, so we should not come
|
||||||
|
// here. Getting a RawChannel over HTTP/2 might be supported
|
||||||
|
// in the future, but it would entail retrieving any left over
|
||||||
|
// bytes that might have been read but not consumed by the
|
||||||
|
// HTTP/2 connection.
|
||||||
|
throw new UnsupportedOperationException("RawChannel is not supported over HTTP/2");
|
||||||
|
}
|
||||||
|
// Http1Exchange may have some remaining bytes in its
|
||||||
|
// internal buffer.
|
||||||
|
Supplier<ByteBuffer> initial = ((Http1Exchange<?>) exchImpl)::drainLeftOverBytes;
|
||||||
|
rawchan = new RawChannelTube(connection, initial);
|
||||||
|
}
|
||||||
|
return rawchan;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HttpConnection connection(Response resp, Exchange<?> exch) {
|
||||||
|
if (exch == null || exch.exchImpl == null) {
|
||||||
|
assert resp.statusCode == 407;
|
||||||
|
return null; // case of Proxy 407
|
||||||
|
}
|
||||||
|
return exch.exchImpl.connection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ExchangeImpl<?> exchangeImpl() {
|
||||||
|
return exchange != null ? exchange.exchImpl : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue