Skip to content

Commit

Permalink
Expect:100-continue fixes for Netty
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
  • Loading branch information
senivam committed Oct 3, 2023
1 parent 3339510 commit 38264cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
Expand Down Expand Up @@ -413,27 +412,24 @@ public void operationComplete(io.netty.util.concurrent.Future<? super Void> futu
final Expect100ContinueConnectorExtension expect100ContinueExtension
= new Expect100ContinueConnectorExtension();

final DefaultFullHttpRequest rq = new DefaultFullHttpRequest(nettyRequest.protocolVersion(),
nettyRequest.method(), nettyRequest.uri());
rq.headers().setAll(nettyRequest.headers());
expect100ContinueExtension.invoke(jerseyRequest, rq);

ChannelFutureListener expect100ContinueListener = null;
ChannelFuture expect100ContinueFuture = null;

if (HttpUtil.is100ContinueExpected(rq)) {
expect100ContinueListener =
future -> {
if (future.isSuccess()) {
entityWriter.writeAndFlush(nettyRequest);
} else {
future.channel().pipeline().fireExceptionCaught(future.cause());
}
};
expect100ContinueFuture = ch.pipeline().writeAndFlush(rq).sync().awaitUninterruptibly()
.addListener(expect100ContinueListener);
} else {
// Send the HTTP request.
final DefaultFullHttpRequest nettyRequestHeaders =
new DefaultFullHttpRequest(nettyRequest.protocolVersion(), nettyRequest.method(), nettyRequest.uri());
nettyRequestHeaders.headers().setAll(nettyRequest.headers());
//If Expect:100-continue feature is enabled and client supports it, the nettyRequestHeaders will be
//enriched with the 'Expect:100-continue' header.
expect100ContinueExtension.invoke(jerseyRequest, nettyRequestHeaders);

final ChannelFuture expect100ContinueFuture = (HttpUtil.is100ContinueExpected(nettyRequestHeaders))
// Send only head of the HTTP request enriched with Expect:100-continue header.
? ch.writeAndFlush(nettyRequestHeaders).sync().awaitUninterruptibly()
// Expect:100-Continue either is not supported or is turned off
: null;


if (expect100ContinueFuture == null
|| !expect100ContinueFuture.isSuccess()) {
// Send the HTTP request. In case of Expect:100-continue processing, this is the
// 2nd attempt without Expect:100-continue header.
entityWriter.writeAndFlush(nettyRequest);
}

Expand All @@ -449,9 +445,6 @@ public OutputStream getOutputStream(int contentLength) throws IOException {
} else {
entityWriter.write(entityWriter.getChunkedInput());
}
if (expect100ContinueFuture != null && expect100ContinueListener != null) {
expect100ContinueFuture.removeListener(expect100ContinueListener);
}

executorService.execute(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@
import org.glassfish.jersey.netty.connector.NettyConnectorProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.netty.NettyTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.jupiter.api.Test;

import javax.ws.rs.Consumes;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down

0 comments on commit 38264cc

Please sign in to comment.