package jdk.incubator.http;
import java.io.IOException;
import java.net.InetSocketAddress;
import jdk.incubator.http.HttpResponse.BodyHandler;
import jdk.incubator.http.HttpResponse.BodyProcessor;
import java.nio.ByteBuffer;
import java.util.concurrent.CompletableFuture;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Executor;
import jdk.incubator.http.internal.common.Log;
import jdk.incubator.http.internal.common.MinimalFuture;
import jdk.incubator.http.internal.common.Utils;
class Http1Exchange<T> extends ExchangeImpl<T> {
final HttpRequestImpl request;
private final List<CompletableFuture<?>> operations;
final Http1Request requestAction;
private volatile Http1Response<T> response;
private IOException failed;
final HttpConnection connection;
final HttpClientImpl client;
final Executor executor;
volatile ByteBuffer buffer;
@Override
public String toString() {
return request.toString();
}
HttpRequestImpl request() {
return request;
}
Http1Exchange(Exchange<T> exchange, HttpConnection connection)
throws IOException
{
super(exchange);
this.request = exchange.request();
this.client = exchange.client();
this.executor = exchange.executor();
this.operations = new LinkedList<>();
this.buffer = Utils.EMPTY_BYTEBUFFER;
if (connection != null) {
this.connection = connection;
} else {
InetSocketAddress addr = request.getAddress(client);
this.connection = HttpConnection.getConnection(addr, client, request);
}
this.requestAction = new Http1Request(request, client, this.connection);
}
HttpConnection connection() {
return connection;
}
@Override
T readBody(BodyHandler<T> handler, boolean returnConnectionToPool)
throws IOException
{
BodyProcessor<T> processor = handler.apply(response.responseCode(),
response.responseHeaders());
setClientForResponse(processor);
CompletableFuture<T> bodyCF = response.readBody(processor,
returnConnectionToPool,
this::executeInline);
try {
return bodyCF.join();
} catch (CompletionException e) {
throw Utils.getIOException(e);
}
}
private void executeInline(Runnable r) {
r.run();
}
synchronized ByteBuffer getBuffer() {
return buffer;
}
@Override
CompletableFuture<T> readBodyAsync(BodyHandler<T> handler,
boolean returnConnectionToPool,
Executor executor)
{
BodyProcessor<T> processor = handler.apply(response.responseCode(),
response.responseHeaders());
setClientForResponse(processor);
CompletableFuture<T> bodyCF = response.readBody(processor,
returnConnectionToPool,
executor);
return bodyCF;
}
@Override
void () throws IOException, InterruptedException {
try {
if (!connection.connected()) {
connection.connect();
}
requestAction.sendHeadersOnly();
} catch (Throwable e) {
connection.close();
throw e;
}
}
@Override
void sendBody() throws IOException {
try {
requestAction.continueRequest();
} catch (Throwable e) {
connection.close();
throw e;
}
}
@Override
Response getResponse() throws IOException {
try {
response = new Http1Response<>(connection, this);
response.readHeaders();
Response r = response.response();
buffer = response.getBuffer();
return r;
} catch (Throwable t) {
connection.close();
throw t;
}
}
private void closeConnection() {
connection.close();
}
@Override
void cancel() {
cancel(new IOException("Request cancelled"));
}
@Override
synchronized void cancel(IOException cause) {
if (requestAction != null && requestAction.finished()
&& response != null && response.finished()) {
return;
}
connection.close();
int count = 0;
if (operations.isEmpty()) {
failed = cause;
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms] no pending operation."
+ "\n\tCan''t cancel yet with {2}",
request.uri(),
request.duration() == null ? -1 :
(request.duration().getSeconds() * 1000
+ request.duration().getNano() / 1000000),
cause);
} else {
for (CompletableFuture<?> cf : operations) {
cf.completeExceptionally(cause);
count++;
}
}
Log.logError("Http1Exchange.cancel: count=" + count);
}
CompletableFuture<Response> getResponseAsyncImpl(Executor executor) {
return MinimalFuture.supply( () -> {
response = new Http1Response<>(connection, Http1Exchange.this);
response.readHeaders();
Response r = response.response();
buffer = response.getBuffer();
return r;
}, executor);
}
@Override
CompletableFuture<Response> getResponseAsync(Executor executor) {
CompletableFuture<Response> cf =
connection.whenReceivingResponse()
.thenCompose((v) -> getResponseAsyncImpl(executor));
IOException cause;
synchronized(this) {
operations.add(cf);
cause = failed;
failed = null;
}
if (cause != null) {
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms]"
+ "\n\tCompleting exceptionally with {2}\n",
request.uri(),
request.duration() == null ? -1 :
(request.duration().getSeconds() * 1000
+ request.duration().getNano() / 1000000),
cause);
cf.completeExceptionally(cause);
}
return cf;
}
}