Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to combine cookie header #30069

Open
wants to merge 1 commit into
base: netty-transport-http
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,41 @@
/*******************************************************************************
* 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;

/**
* A Netty channel handler that combines multiple Cookie headers into a single header.
*
* According to RFC6265, an HTTP request should contain at most one Cookie header, with
* individual cookies separated by semicolons. This handler checks for multiple Cookie headers,
* and combines them into a single header. This ensures complliance with the HTTP specification.
*/
public class CombinedCookieHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;

List<String> cookieHeaders = request.headers().getAll(HttpHeaderNames.COOKIE);
if (cookieHeaders.size() > 1) {
String combinedCookies = String.join("; ", cookieHeaders);
request.headers().remove(HttpHeaderNames.COOKIE);
request.headers().set(HttpHeaderNames.COOKIE, combinedCookies);
}
}
ctx.fireChannelRead(msg);
}
}