From 56c89704f300898bbd4bd7e8e76aab42fe53bf7a Mon Sep 17 00:00:00 2001 From: dushani Date: Wed, 8 May 2024 14:59:13 +0530 Subject: [PATCH] fix https://github.com/wso2/api-manager/issues/2875. --- .../transport/WebsocketConnectionFactory.java | 17 ++++++++++++++++- .../websocket/transport/WebsocketConstants.java | 4 ++++ .../endpoint/internal/http/api/Constants.java | 9 ++++++++- .../InboundWebsocketChannelInitializer.java | 14 ++++++++++++-- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/components/carbon-transports/websocket/org.wso2.carbon.websocket.transport/src/main/java/org/wso2/carbon/websocket/transport/WebsocketConnectionFactory.java b/components/carbon-transports/websocket/org.wso2.carbon.websocket.transport/src/main/java/org/wso2/carbon/websocket/transport/WebsocketConnectionFactory.java index 360cb15fed5..c2c93800696 100644 --- a/components/carbon-transports/websocket/org.wso2.carbon.websocket.transport/src/main/java/org/wso2/carbon/websocket/transport/WebsocketConnectionFactory.java +++ b/components/carbon-transports/websocket/org.wso2.carbon.websocket.transport/src/main/java/org/wso2/carbon/websocket/transport/WebsocketConnectionFactory.java @@ -255,6 +255,14 @@ public WebSocketClientHandler cacheNewConnection(final String tenantDomain, handler.setDispatchSequence(responseDispatchSequence); handler.setDispatchErrorSequence(responseErrorSequence); } + int maxInitLength = getMaxValueOrDefault( + transportOut.getParameter(WebsocketConstants.WEBSOCKET_MAX_HTTP_CODEC_INIT_LENGTH), 4096); + int maxHeaderSize = getMaxValueOrDefault( + transportOut.getParameter(WebsocketConstants.WEBSOCKET_MAX_HTTP_CODEC_HEADER_SIZE), 8192); + int maxChunkSize = getMaxValueOrDefault( + transportOut.getParameter(WebsocketConstants.WEBSOCKET_MAX_HTTP_CODEC_CHUNK_SIZE), 8192); + int maxContentLength = getMaxValueOrDefault( + transportOut.getParameter(WebsocketConstants.WEBSOCKET_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH), 8192); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .handler(new ChannelInitializer() { @@ -275,7 +283,8 @@ protected void initChannel(SocketChannel ch) { sslEngine.setSSLParameters(sslParams); } p.addLast(sslHandler); } - p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), + p.addLast(new HttpClientCodec(maxInitLength, maxHeaderSize, maxChunkSize), + new HttpObjectAggregator(maxContentLength), new WebSocketFrameAggregator(Integer.MAX_VALUE), handler); } }); @@ -381,5 +390,11 @@ public void removeChannelHandler(String sourceIdentifier, } } + private int getMaxValueOrDefault(Parameter parameter, int defaultValue) { + if (parameter != null) { + return Integer.parseInt(parameter.getParameterElement().getText()); + } + return defaultValue; + } } \ No newline at end of file diff --git a/components/carbon-transports/websocket/org.wso2.carbon.websocket.transport/src/main/java/org/wso2/carbon/websocket/transport/WebsocketConstants.java b/components/carbon-transports/websocket/org.wso2.carbon.websocket.transport/src/main/java/org/wso2/carbon/websocket/transport/WebsocketConstants.java index 9a7a455a24c..5b371edcd51 100644 --- a/components/carbon-transports/websocket/org.wso2.carbon.websocket.transport/src/main/java/org/wso2/carbon/websocket/transport/WebsocketConstants.java +++ b/components/carbon-transports/websocket/org.wso2.carbon.websocket.transport/src/main/java/org/wso2/carbon/websocket/transport/WebsocketConstants.java @@ -56,6 +56,10 @@ public class WebsocketConstants { public static final String TRUST_STORE_PASSWORD = "ws.trust.store.Password"; public static final String TRUST_STORE_CONFIG_ELEMENT = "ws.trust.store"; public static final String WEBSOCKET_MAX_FRAME_PAYLOAD_LENGTH = "wsMaxFrameLength"; + public static final String WEBSOCKET_MAX_HTTP_CODEC_INIT_LENGTH = "wsMaxHttpCodecInitLength"; + public static final String WEBSOCKET_MAX_HTTP_CODEC_HEADER_SIZE = "wsMaxHttpCodecHeaderSize"; + public static final String WEBSOCKET_MAX_HTTP_CODEC_CHUNK_SIZE = "wsMaxHttpCodecChunkSize"; + public static final String WEBSOCKET_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH = "wsMaxHttpAggregateContentLength"; public static final String SYNAPSE_SUBPROTOCOL_PREFIX = "synapse"; public static final String WEBSOCKET_SUBSCRIBER_PATH = "websocket.subscriber.path"; diff --git a/components/inbound-endpoints/org.wso2.carbon.inbound.endpoint/src/main/java/org/wso2/carbon/inbound/endpoint/internal/http/api/Constants.java b/components/inbound-endpoints/org.wso2.carbon.inbound.endpoint/src/main/java/org/wso2/carbon/inbound/endpoint/internal/http/api/Constants.java index 22588925ccf..16e9b0b1cff 100644 --- a/components/inbound-endpoints/org.wso2.carbon.inbound.endpoint/src/main/java/org/wso2/carbon/inbound/endpoint/internal/http/api/Constants.java +++ b/components/inbound-endpoints/org.wso2.carbon.inbound.endpoint/src/main/java/org/wso2/carbon/inbound/endpoint/internal/http/api/Constants.java @@ -30,5 +30,12 @@ public class Constants { public static final String INTERNAL_APIS_FILE = "internal-apis.xml"; public static final String PREFIX_TO_ENABLE_INTERNAL_APIS = "enable"; public static final String WEBSOCKET_TRANSPORT_MAX_FRAME_PAYLOAD_LENGTH = "ws.transport.max.frame.payload.length"; - + public static final String WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_INIT_LENGTH + = "ws.transport.max.http.codec.init.length"; + public static final String WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_HEADER_SIZE + = "ws.transport.max.http.codec.header.size"; + public static final String WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_CHUNK_SIZE + = "ws.transport.max.http.codec.chunk.size"; + public static final String WEBSOCKET_TRANSPORT_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH + = "ws.transport.max.http.aggregator.content.length"; } diff --git a/components/inbound-endpoints/org.wso2.carbon.inbound.endpoint/src/main/java/org/wso2/carbon/inbound/endpoint/protocol/websocket/InboundWebsocketChannelInitializer.java b/components/inbound-endpoints/org.wso2.carbon.inbound.endpoint/src/main/java/org/wso2/carbon/inbound/endpoint/protocol/websocket/InboundWebsocketChannelInitializer.java index bcd5abad546..8678b934539 100644 --- a/components/inbound-endpoints/org.wso2.carbon.inbound.endpoint/src/main/java/org/wso2/carbon/inbound/endpoint/protocol/websocket/InboundWebsocketChannelInitializer.java +++ b/components/inbound-endpoints/org.wso2.carbon.inbound.endpoint/src/main/java/org/wso2/carbon/inbound/endpoint/protocol/websocket/InboundWebsocketChannelInitializer.java @@ -25,8 +25,10 @@ import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator; import io.netty.handler.ssl.SslHandler; import io.netty.handler.timeout.IdleStateHandler; +import org.apache.synapse.config.SynapsePropertiesLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.wso2.carbon.inbound.endpoint.internal.http.api.Constants; import org.wso2.carbon.inbound.endpoint.protocol.websocket.ssl.InboundWebsocketSSLConfiguration; import org.wso2.carbon.inbound.endpoint.protocol.websocket.ssl.SSLHandlerFactory; @@ -99,8 +101,16 @@ protected void initChannel(SocketChannel websocketChannel) throws Exception { } ChannelPipeline p = websocketChannel.pipeline(); - p.addLast("codec", new HttpServerCodec()); - p.addLast("aggregator", new HttpObjectAggregator(65536)); + int maxInitLength = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue( + Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_INIT_LENGTH, "4096")); + int maxHeaderSize = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue( + Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_HEADER_SIZE, "8192")); + int maxChunkSize = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue( + Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_CHUNK_SIZE, "8192")); + int maxContentLength = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue( + Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH, "65536")); + p.addLast("codec", new HttpServerCodec(maxInitLength, maxHeaderSize, maxChunkSize)); + p.addLast("aggregator", new HttpObjectAggregator(maxContentLength)); p.addLast("frameAggregator", new WebSocketFrameAggregator(Integer.MAX_VALUE)); p.addLast("idleStateHandler", new IdleStateHandler(inflowIdleTime, outflowIdleTime, 0)); InboundWebsocketSourceHandler sourceHandler = new InboundWebsocketSourceHandler();