Skip to content

Commit

Permalink
Merge pull request OpenLiberty#2 from isaacrivriv/add-trailers-async-…
Browse files Browse the repository at this point in the history
…headers-fixes

Added fixes for multiple issues found in servlet 4 FAT
  • Loading branch information
mrsaldana authored Oct 24, 2023
2 parents 4414080 + 6e34bc0 commit 882e5f2
Show file tree
Hide file tree
Showing 13 changed files with 653 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public abstract class BNFHeadersImpl implements BNFHeaders, Externalizable {
private static byte[] whitespace = null;

/** Empty object used when a header is not present */
private static final HeaderField NULL_HEADER = new EmptyHeaderField();
public static final HeaderField NULL_HEADER = new EmptyHeaderField();

private static final String FOR = "for";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,19 +531,19 @@ protected void parseConfig(String name, Map<String, Object> config) {
}

if (key.equalsIgnoreCase(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_ADD)) {
props.put(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_ADD, value);
props.put(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_ADD_INTERNAL, value);
}

if (key.equalsIgnoreCase(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_SET)) {
props.put(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_SET, value);
props.put(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_SET_INTERNAL, value);
}

if (key.equalsIgnoreCase(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_SET_IF_MISSING)) {
props.put(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_SET_IF_MISSING, value);
props.put(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_SET_IF_MISSING_INTERNAL, value);
}

if (key.equalsIgnoreCase(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_REMOVE)) {
props.put(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_REMOVE, value);
props.put(HttpConfigConstants.PROPNAME_RESPONSE_HEADERS_REMOVE_INTERNAL, value);
}

props.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpContent;
import io.netty.handler.codec.http.DefaultLastHttpContent;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpUtil;
Expand Down Expand Up @@ -3284,23 +3286,36 @@ final protected void sendFullOutgoing(WsByteBuffer[] wsbb) throws IOException {
// this.nettyContext.channel().write(buffer);
}
}
NettyResponseMessage resp = (NettyResponseMessage) getResponse();
HttpHeaders trailers = resp.getNettyTrailers();
DefaultLastHttpContent lastContent;
if (trailers.isEmpty())
lastContent = new LastStreamSpecificHttpContent(Integer.valueOf(nettyResponse.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
"-1")));
else {
System.out.println("Adding trailers to last http content! " + trailers.toString());
lastContent = new LastStreamSpecificHttpContent(Integer.valueOf(nettyResponse.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
"-1")), trailers);
}
// Sending last http content since all data was written
System.out.println("Sending last http content!");
this.nettyContext.channel().write(new LastStreamSpecificHttpContent(Integer.valueOf(nettyResponse.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
"-1"))));
} else if (!getResponse().isBodyAllowed()) {
// No body so need to write out the last default content
System.out.println("VERIFY THIS!! Writing last http content");
// this.nettyContext.channel().write(new DefaultLastHttpContent());
this.nettyContext.channel().write(new LastStreamSpecificHttpContent(Integer.valueOf(nettyResponse.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
"-1"))));
this.nettyContext.channel().write(lastContent);
} else {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "sendFullOutgoing : No buffers to write ");
}
//this.nettyContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
this.nettyContext.channel().write(new LastStreamSpecificHttpContent(Integer.valueOf(nettyResponse.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
"-1"))));
NettyResponseMessage resp = (NettyResponseMessage) getResponse();
HttpHeaders trailers = resp.getNettyTrailers();
DefaultLastHttpContent lastContent;
if (trailers.isEmpty()) {
lastContent = new LastStreamSpecificHttpContent(Integer.valueOf(nettyResponse.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
"-1")));
} else {
System.out.println("Adding trailers to last http content! " + trailers.toString());
lastContent = new LastStreamSpecificHttpContent(Integer.valueOf(nettyResponse.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
"-1")), trailers);
}
this.nettyContext.channel().write(lastContent);
}
this.nettyContext.channel().flush();
MSP.log("set message sent");
Expand Down Expand Up @@ -3882,6 +3897,7 @@ public String[] introspectSelf() {
* @return InterChannelCallback
*/
final public InterChannelCallback getAppWriteCallback() {
System.out.println("IRR getAppWriteCallback called!!");
return this.appWriteCB;
}

Expand All @@ -3891,6 +3907,7 @@ final public InterChannelCallback getAppWriteCallback() {
* @return InterChannelCallback
*/
final public InterChannelCallback getAppReadCallback() {
System.out.println("IRR getAppReadCallback called!!");
return this.appReadCB;
}

Expand All @@ -3900,6 +3917,7 @@ final public InterChannelCallback getAppReadCallback() {
* @param cb
*/
final protected void setAppWriteCallback(InterChannelCallback cb) {
System.out.println("IRR setAppWriteCallback called!!");
this.appWriteCB = cb;
}

Expand All @@ -3909,6 +3927,7 @@ final protected void setAppWriteCallback(InterChannelCallback cb) {
* @param cb
*/
final protected void setAppReadCallback(InterChannelCallback cb) {
System.out.println("IRR setAppReadCallback called!!");
this.appReadCB = cb;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.ibm.ws.http.dispatcher.internal.HttpDispatcher;
import com.ibm.ws.http.netty.MSP;
import com.ibm.ws.http.netty.NettyHttpConstants;
import com.ibm.ws.http.netty.NettyVirtualConnectionImpl;
import com.ibm.ws.http.netty.message.NettyRequestMessage;
import com.ibm.ws.http.netty.message.NettyResponseMessage;
import com.ibm.wsspi.bytebuffer.WsByteBuffer;
Expand Down Expand Up @@ -1715,6 +1716,13 @@ public VirtualConnection getRequestBodyBuffer(InterChannelCallback callback, boo
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "getRequestBodyBuffer(async) hc: " + this.hashCode());
}

// Netty involved so need to just call it complete
if (Objects.nonNull(this.nettyContext)) {
callback.complete(NettyVirtualConnectionImpl.DUMMY_NETTY_VC);
return null;
}

boolean isError = false;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ public void reset() {
public void setTrailer(String name, String value) {

HttpTrailers trailers = message.createTrailers();
if (trailers == null)// Netty implementation
trailers = message.getTrailers();
HeaderKeys key = HttpHeaderKeys.find(name, false);

if (trailers.containsDeferredTrailer(key)) {
Expand All @@ -361,6 +363,8 @@ public void setTrailer(String name, String value) {
@Override
public void writeTrailers() {
HttpTrailers trailers = message.createTrailers();
if (trailers == null)// Netty implementation
trailers = message.getTrailers();
trailers.computeRemainingTrailers();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
*/
public class NettyVirtualConnectionImpl implements VirtualConnection {

public static final NettyVirtualConnectionImpl DUMMY_NETTY_VC;

static {
DUMMY_NETTY_VC = new NettyVirtualConnectionImpl();
DUMMY_NETTY_VC.init();
}

private Map<Object, Object> stateStore = null;

protected NettyVirtualConnectionImpl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.ibm.wsspi.http.HttpCookie;
import com.ibm.wsspi.http.channel.HttpConstants;
import com.ibm.wsspi.http.channel.HttpRequestMessage;
import com.ibm.wsspi.http.channel.HttpTrailers;
import com.ibm.wsspi.http.channel.inbound.HttpInboundServiceContext;
import com.ibm.wsspi.http.channel.values.HttpHeaderKeys;
import com.ibm.wsspi.http.channel.values.MethodValues;
Expand Down Expand Up @@ -450,6 +451,13 @@ public void setScheme(byte[] scheme) throws UnsupportedSchemeException {

}

@Override
public HttpTrailers getTrailers() {
// if (request.trailingHeaders().isEmpty())
// return null;
return new NettyTrailers(this.request.trailingHeaders());
}

@Override
public HttpRequestMessage duplicate() {
throw new UnsupportedOperationException("The duplicate method is not supported.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.ibm.wsspi.http.channel.values.TransferEncodingValues;
import com.ibm.wsspi.http.channel.values.VersionValues;

import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpHeaders;
Expand All @@ -63,6 +64,7 @@ public class NettyResponseMessage extends NettyBaseMessage implements HttpRespon

HttpResponse nettyResponse;
HttpHeaders headers;
HttpHeaders trailers;
HttpInboundServiceContext context;
HttpChannelConfig config;

Expand All @@ -73,6 +75,7 @@ public NettyResponseMessage(HttpResponse response, HttpInboundServiceContext isc
this.context = isc;
this.nettyResponse = response;
this.headers = nettyResponse.headers();
this.trailers = new DefaultHttpHeaders().clear();

if (request.headers().contains(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text())) {
String streamId = request.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
Expand Down Expand Up @@ -285,8 +288,15 @@ public void setCharset(Charset set) {

@Override
public HttpTrailers getTrailers() {
// TODO Auto-generated method stub
return null;
// TODO Auto-generated method stub)
// if (trailers.isEmpty())
// return null;
return new NettyTrailers(trailers);
}

public HttpHeaders getNettyTrailers() {
// TODO Auto-generated method stub)
return trailers;
}

@Override
Expand Down
Loading

0 comments on commit 882e5f2

Please sign in to comment.