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:
parent
ea6d18f919
commit
06454f6409
|
@ -91,41 +91,61 @@ public class HttpTransportOverHTTP2 implements HttpTransport
|
|||
if (info != null)
|
||||
{
|
||||
metaData = info;
|
||||
|
||||
int status = info.getStatus();
|
||||
boolean informational = HttpStatus.isInformational(status) && status != HttpStatus.SWITCHING_PROTOCOLS_101;
|
||||
if (informational)
|
||||
boolean interimResponse = status == HttpStatus.CONTINUE_100 || status == HttpStatus.PROCESSING_102;
|
||||
if (interimResponse)
|
||||
{
|
||||
if (transportCallback.start(callback, false))
|
||||
sendHeaders(info, false, transportCallback);
|
||||
// Must not commit interim responses.
|
||||
if (hasContent)
|
||||
{
|
||||
callback.failed(new IllegalStateException("Interim response cannot have content"));
|
||||
}
|
||||
else
|
||||
{
|
||||
boolean needsCommit = commit.compareAndSet(false, true);
|
||||
if (needsCommit)
|
||||
if (transportCallback.start(callback, false))
|
||||
sendHeadersFrame(info, false, transportCallback);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (commit.compareAndSet(false, true))
|
||||
{
|
||||
Supplier<HttpFields> trailers = info.getTrailerSupplier();
|
||||
|
||||
if (hasContent)
|
||||
{
|
||||
Callback nested = trailers == null || !lastContent ? callback : new SendTrailers(callback);
|
||||
Callback commitCallback = new Callback.Nested(nested)
|
||||
Callback commitCallback = new Callback.Nested(callback)
|
||||
{
|
||||
@Override
|
||||
public void succeeded()
|
||||
{
|
||||
if (transportCallback.start(nested, false))
|
||||
sendContent(content, lastContent, trailers == null && lastContent, transportCallback);
|
||||
}
|
||||
};
|
||||
if (transportCallback.start(commitCallback, true))
|
||||
sendHeaders(info, false, transportCallback);
|
||||
if (lastContent)
|
||||
{
|
||||
Supplier<HttpFields> trailers = info.getTrailerSupplier();
|
||||
if (transportCallback.start(new SendTrailers(getCallback(), trailers), false))
|
||||
sendDataFrame(content, true, trailers == null, transportCallback);
|
||||
}
|
||||
else
|
||||
{
|
||||
Callback nested = trailers == null ? callback : new SendTrailers(callback);
|
||||
if (transportCallback.start(nested, true))
|
||||
sendHeaders(info, trailers == null && lastContent, transportCallback);
|
||||
if (transportCallback.start(getCallback(), false))
|
||||
sendDataFrame(content, false, false, transportCallback);
|
||||
}
|
||||
}
|
||||
};
|
||||
if (transportCallback.start(commitCallback, true))
|
||||
sendHeadersFrame(info, false, transportCallback);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (lastContent)
|
||||
{
|
||||
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
|
||||
|
@ -137,11 +157,18 @@ public class HttpTransportOverHTTP2 implements HttpTransport
|
|||
else
|
||||
{
|
||||
if (hasContent || lastContent)
|
||||
{
|
||||
if (lastContent)
|
||||
{
|
||||
Supplier<HttpFields> trailers = metaData.getTrailerSupplier();
|
||||
Callback nested = trailers == null ? callback : new SendTrailers(callback);
|
||||
if (transportCallback.start(nested, false))
|
||||
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
|
||||
{
|
||||
|
@ -186,7 +213,7 @@ public class HttpTransportOverHTTP2 implements HttpTransport
|
|||
}, 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())
|
||||
{
|
||||
|
@ -200,7 +227,7 @@ public class HttpTransportOverHTTP2 implements HttpTransport
|
|||
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())
|
||||
{
|
||||
|
@ -212,7 +239,7 @@ public class HttpTransportOverHTTP2 implements HttpTransport
|
|||
stream.data(frame, callback);
|
||||
}
|
||||
|
||||
private void sendTrailers(MetaData metaData, Callback callback)
|
||||
private void sendTrailersFrame(MetaData metaData, Callback callback)
|
||||
{
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
|
@ -385,16 +412,26 @@ public class HttpTransportOverHTTP2 implements HttpTransport
|
|||
|
||||
private class SendTrailers extends Callback.Nested
|
||||
{
|
||||
private SendTrailers(Callback callback)
|
||||
private final Supplier<HttpFields> trailers;
|
||||
|
||||
private SendTrailers(Callback callback, Supplier<HttpFields> trailers)
|
||||
{
|
||||
super(callback);
|
||||
this.trailers = trailers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void succeeded()
|
||||
{
|
||||
if (trailers != null)
|
||||
{
|
||||
if (transportCallback.start(getCallback(), false))
|
||||
sendTrailers(new MetaData(HttpVersion.HTTP_2, metaData.getTrailerSupplier().get()), transportCallback);
|
||||
sendTrailersFrame(new MetaData(HttpVersion.HTTP_2, trailers.get()), transportCallback);
|
||||
}
|
||||
else
|
||||
{
|
||||
super.succeeded();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue