Skip to content

Commit

Permalink
Adding support to combine multilple cookie headers into one
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsaldana committed Nov 5, 2024
1 parent 270279f commit 02aef97
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.ibm.ws.http.netty.pipeline.LibertySslHandler;
import com.ibm.ws.http.netty.pipeline.http2.LibertyNettyALPNHandler;
import com.ibm.ws.http.netty.pipeline.http2.LibertyUpgradeCodec;
import com.ibm.ws.http.netty.pipeline.inbound.CombinedCookieHandler;
import com.ibm.ws.http.netty.pipeline.inbound.HttpDispatcherHandler;
import com.ibm.ws.http.netty.pipeline.inbound.LibertyHttpObjectAggregator;
import com.ibm.ws.http.netty.pipeline.inbound.LibertyHttpRequestHandler;
Expand Down Expand Up @@ -251,6 +252,7 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws E
pipeline.addAfter(HTTP_KEEP_ALIVE_HANDLER_NAME, HTTP_AGGREGATOR_HANDLER_NAME,
new LibertyHttpObjectAggregator(httpConfig.getMessageSizeLimit() == -1 ? maxContentLength : httpConfig.getMessageSizeLimit()));
pipeline.addAfter(HTTP_AGGREGATOR_HANDLER_NAME, HTTP_REQUEST_HANDLER_NAME, new LibertyHttpRequestHandler());
pipeline.addAfter(HTTP_AGGREGATOR_HANDLER_NAME, "CombinedCookieHandler", new CombinedCookieHandler());
ctx.pipeline().remove(this);

ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
Expand Down Expand Up @@ -293,6 +295,7 @@ private void addPreDispatcherHandlers(ChannelPipeline pipeline, boolean isHttp2)
pipeline.addAfter(HTTP_KEEP_ALIVE_HANDLER_NAME, HTTP_AGGREGATOR_HANDLER_NAME,
new LibertyHttpObjectAggregator(httpConfig.getMessageSizeLimit() == -1 ? maxContentLength : httpConfig.getMessageSizeLimit()));
pipeline.addAfter(HTTP_AGGREGATOR_HANDLER_NAME, HTTP_REQUEST_HANDLER_NAME, new LibertyHttpRequestHandler());
pipeline.addAfter(HTTP_AGGREGATOR_HANDLER_NAME, "CombinedCookieHandler", new CombinedCookieHandler());
}

pipeline.addBefore(HTTP_DISPATCHER_HANDLER_NAME, "chunkLoggingHandler", new ChunkSizeLoggingHandler());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package com.ibm.ws.http.netty.pipeline.inbound;

import java.util.List;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;

public class CombinedCookieHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;

// Retrieve all Cookie headers
List<String> cookieHeaders = request.headers().getAll(HttpHeaderNames.COOKIE);
if (cookieHeaders.size() > 1) {
// Combine multiple Cookie headers into one
String combinedCookies = String.join("; ", cookieHeaders);
// Remove all existing Cookie headers
request.headers().remove(HttpHeaderNames.COOKIE);
// Set the combined Cookie header
request.headers().set(HttpHeaderNames.COOKIE, combinedCookies);
}
}
// Pass the message along to the next handler
ctx.fireChannelRead(msg);
}
}

0 comments on commit 02aef97

Please sign in to comment.