Fixes #2420 - Simplify HttpTransportOverHTTP2.

Removed usage of ternary expressions in favor of if/else statements
to improve readability of the logic for the send() method.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2018-04-07 12:25:39 +02:00
parent ea6d18f919
commit 06454f6409
1 changed files with 64 additions and 27 deletions

View File

@ -91,41 +91,61 @@ public class HttpTransportOverHTTP2 implements HttpTransport
if (info != null) if (info != null)
{ {
metaData = info; metaData = info;
int status = info.getStatus(); int status = info.getStatus();
boolean informational = HttpStatus.isInformational(status) && status != HttpStatus.SWITCHING_PROTOCOLS_101; boolean interimResponse = status == HttpStatus.CONTINUE_100 || status == HttpStatus.PROCESSING_102;
if (informational) if (interimResponse)
{ {
if (transportCallback.start(callback, false)) // Must not commit interim responses.
sendHeaders(info, false, transportCallback); if (hasContent)
{
callback.failed(new IllegalStateException("Interim response cannot have content"));
}
else
{
if (transportCallback.start(callback, false))
sendHeadersFrame(info, false, transportCallback);
}
} }
else else
{ {
boolean needsCommit = commit.compareAndSet(false, true); if (commit.compareAndSet(false, true))
if (needsCommit)
{ {
Supplier<HttpFields> trailers = info.getTrailerSupplier();
if (hasContent) if (hasContent)
{ {
Callback nested = trailers == null || !lastContent ? callback : new SendTrailers(callback); Callback commitCallback = new Callback.Nested(callback)
Callback commitCallback = new Callback.Nested(nested)
{ {
@Override @Override
public void succeeded() public void succeeded()
{ {
if (transportCallback.start(nested, false)) if (lastContent)
sendContent(content, lastContent, trailers == null && lastContent, transportCallback); {
Supplier<HttpFields> trailers = info.getTrailerSupplier();
if (transportCallback.start(new SendTrailers(getCallback(), trailers), false))
sendDataFrame(content, true, trailers == null, transportCallback);
}
else
{
if (transportCallback.start(getCallback(), false))
sendDataFrame(content, false, false, transportCallback);
}
} }
}; };
if (transportCallback.start(commitCallback, true)) if (transportCallback.start(commitCallback, true))
sendHeaders(info, false, transportCallback); sendHeadersFrame(info, false, transportCallback);
} }
else else
{ {
Callback nested = trailers == null ? callback : new SendTrailers(callback); if (lastContent)
if (transportCallback.start(nested, true)) {
sendHeaders(info, trailers == null && lastContent, transportCallback); Supplier<HttpFields> trailers = info.getTrailerSupplier();
if (transportCallback.start(new SendTrailers(callback, trailers), true))
sendHeadersFrame(info, trailers == null, transportCallback);
}
else
{
if (transportCallback.start(callback, true))
sendHeadersFrame(info, false, transportCallback);
}
} }
} }
else else
@ -138,10 +158,17 @@ public class HttpTransportOverHTTP2 implements HttpTransport
{ {
if (hasContent || lastContent) if (hasContent || lastContent)
{ {
Supplier<HttpFields> trailers = metaData.getTrailerSupplier(); if (lastContent)
Callback nested = trailers == null ? callback : new SendTrailers(callback); {
if (transportCallback.start(nested, false)) Supplier<HttpFields> trailers = metaData.getTrailerSupplier();
sendContent(content, lastContent, trailers == null && lastContent, transportCallback); if (transportCallback.start(new SendTrailers(callback, trailers), false))
sendDataFrame(content, true, trailers == null, transportCallback);
}
else
{
if (transportCallback.start(callback, false))
sendDataFrame(content, false, false, transportCallback);
}
} }
else else
{ {
@ -186,7 +213,7 @@ public class HttpTransportOverHTTP2 implements HttpTransport
}, new Stream.Listener.Adapter()); // TODO: handle reset from the client ? }, new Stream.Listener.Adapter()); // TODO: handle reset from the client ?
} }
private void sendHeaders(MetaData.Response info, boolean endStream, Callback callback) private void sendHeadersFrame(MetaData.Response info, boolean endStream, Callback callback)
{ {
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
{ {
@ -200,7 +227,7 @@ public class HttpTransportOverHTTP2 implements HttpTransport
stream.headers(frame, callback); stream.headers(frame, callback);
} }
private void sendContent(ByteBuffer content, boolean lastContent, boolean endStream, Callback callback) private void sendDataFrame(ByteBuffer content, boolean lastContent, boolean endStream, Callback callback)
{ {
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
{ {
@ -212,7 +239,7 @@ public class HttpTransportOverHTTP2 implements HttpTransport
stream.data(frame, callback); stream.data(frame, callback);
} }
private void sendTrailers(MetaData metaData, Callback callback) private void sendTrailersFrame(MetaData metaData, Callback callback)
{ {
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
{ {
@ -385,16 +412,26 @@ public class HttpTransportOverHTTP2 implements HttpTransport
private class SendTrailers extends Callback.Nested private class SendTrailers extends Callback.Nested
{ {
private SendTrailers(Callback callback) private final Supplier<HttpFields> trailers;
private SendTrailers(Callback callback, Supplier<HttpFields> trailers)
{ {
super(callback); super(callback);
this.trailers = trailers;
} }
@Override @Override
public void succeeded() public void succeeded()
{ {
if (transportCallback.start(getCallback(), false)) if (trailers != null)
sendTrailers(new MetaData(HttpVersion.HTTP_2, metaData.getTrailerSupplier().get()), transportCallback); {
if (transportCallback.start(getCallback(), false))
sendTrailersFrame(new MetaData(HttpVersion.HTTP_2, trailers.get()), transportCallback);
}
else
{
super.succeeded();
}
} }
} }
} }